自动化立体仓库 - WMS系统
luxiaotao1123
2020-06-22 abf97299ede3798c35377d0ea8b559f91c586b89
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.zy.common.web;
 
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.controller.AbstractBaseController;
import com.core.exception.CoolException;
import com.zy.system.entity.User;
import com.zy.system.service.UserService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
 
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
/**
 * Created by vincent on 2019-09-09
 */
public class BaseController extends AbstractBaseController {
 
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    private UserService userService;
 
    protected Long getUserId(){
        return Long.parseLong(String.valueOf(request.getAttribute("userId")));
    }
 
    protected User getUser(){
        User user = userService.selectById(getUserId());
        if (null == user) {
            throw new CoolException(BaseRes.DENIED);
        }
        return user;
    }
 
    protected String getComment(Class<?> cls, String fieldName){
        Field[] fields = Cools.getAllFields(cls);
        for (Field field : fields){
            if (fieldName.equals(field.getName())){
                return field.getAnnotation(ApiModelProperty.class).value();
            }
        }
        return "";
    }
 
    protected <T> Page<T> toPage(Integer curr, Integer limit, Map<String, Object> param){
        Page<T> page = new Page<T>(curr, limit);
 
        param.entrySet().removeIf(next -> next.getKey().equals("curr")
                || next.getKey().equals("limit")
                || next.getKey().equals("orderByField")
                || next.getKey().equals("orderByType")
                || Cools.isEmpty(next.getValue()));
        return page;
    }
 
    /**
     * 全字段模糊搜索
     * @param cls 模型类
     * @param set 排除字段集合
     * @param condition 搜索内容
     */
    protected <T> void allLike(Class<T> cls, Set<String> set, EntityWrapper<T> wrapper, String condition){
        if (Cools.isEmpty(condition)) {
            return;
        }
        List<String> columns = new ArrayList<>();
        for (Field field :Cools.getAllFields(cls)){
            if (Modifier.isFinal(field.getModifiers())
                    || Modifier.isStatic(field.getModifiers())
                    || Modifier.isTransient(field.getModifiers())){
                continue;
            }
            String column = null;
            if (field.isAnnotationPresent(TableField.class)) {
                column = field.getAnnotation(TableField.class).value();
            }
            if (Cools.isEmpty(column)) {
                column = field.getName();
            }
            if (!set.contains(column)) {
                columns.add(column);
            }
        }
        if (columns.isEmpty()) {
            return;
        }
        for (int i=0;i<columns.size();i++){
            if (i==0){
                wrapper.andNew();
            } else {
                wrapper.or();
            }
            wrapper.like(columns.get(i), condition);
        }
    }
}