#
Junjie
2024-07-04 7a41565df8f9daf81889b504062d25a3b00d8400
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.zy.asrs.wms.system.controller;
 
import com.zy.asrs.wms.common.domain.BaseParam;
import com.zy.asrs.wms.system.entity.User;
import com.zy.asrs.wms.system.entity.UserLogin;
import com.zy.asrs.wms.system.service.UserLoginService;
import com.zy.asrs.wms.system.service.UserRoleService;
import com.zy.asrs.wms.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.*;
 
/**
 * Created by vincent on 1/30/2024
 */
public class BaseController {
 
    protected static final String RANGE_TIME_LINK = " - ";
 
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private UserService userService;
 
    public User getLoginUser() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null) {
                Object object = authentication.getPrincipal();
                if (object instanceof User) {
                    return (User) object;
                }
                if(object instanceof UserLogin) {
                    UserLogin userLogin = (UserLogin) object;
                    User user = userService.superGetById(userLogin.getUserId());
                    return user;
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
 
    public Long getLoginUserId() {
        User loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getId();
    }
 
    public Long getHostId() {
        User loginUser = getLoginUser();
        return loginUser.getHostId();
    }
 
    public <T extends BaseParam> T buildParam(Map<String, Object> map, Class<T> clz) {
        T t  = null;
        try {
            t = clz.getDeclaredConstructor().newInstance();
            t.syncMap(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
 
}