skyouc
5 天以前 40d9cd510741a098bd52cbe22a5f9e5528f45abc
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
70
71
72
73
74
75
76
77
78
79
80
package com.vincent.rsf.server.system.controller;
 
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.system.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
 
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
/**
 * Created by vincent on 1/30/2024
 */
 
public class BaseController {
 
    public User getLoginUser() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null) {
                Object object = authentication.getPrincipal();
                if (object instanceof User) {
                    return (User) object;
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
 
    public Long getLoginUserId() {
        User loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getId();
    }
 
    public Long getTenantId() {
        User loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getTenantId();
    }
 
    public <T extends BaseParam> T buildParam(Map<String, Object> map, Class<T> clz) {
        if (!Objects.isNull(map.get("meta"))) {
            Map<String, Object> meta = (Map<String, Object>) map.get("meta");
            meta.keySet().forEach(key -> {
                map.put(key, meta.get(key));
            });
            map.remove("meta");
        }
        T t  = null;
        try {
            t = clz.getDeclaredConstructor().newInstance();
            t.syncMap(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
 
    public Boolean hasCreateTimeDesc(List<OrderItem> orderItems) {
        if (Cools.isEmpty(orderItems)) {
            return true;
        }
        if (orderItems.size() > 1) {
            return false;
        }
        OrderItem orderItem = orderItems.get(0);
        if (!orderItem.getColumn().equals("create_time")) {
            return false;
        }
        if (orderItem.isAsc()) {
            return false;
        }
        return true;
    }
 
}