自动化立体仓库 - WMS系统
#
lty
15 小时以前 22e19afee228c3ecbb41ac8115163063f572e51f
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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.asrs.entity.Node;
import com.zy.asrs.entity.Tag;
import com.zy.asrs.service.NodeService;
import com.zy.asrs.service.TagService;
import com.zy.system.entity.User;
import com.zy.system.entity.UserLogin;
import com.zy.system.service.UserLoginService;
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.math.BigDecimal;
import java.math.BigInteger;
import java.time.temporal.Temporal;
import java.util.*;
 
/**
 * Created by vincent on 2019-09-09
 */
public class BaseController extends AbstractBaseController {
 
    protected static final String RANGE_TIME_LINK = " - ";
 
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    private UserService userService;
    @Autowired
    private TagService tagService;
    @Autowired
    private NodeService nodeService;
    @Autowired
    private UserLoginService userLoginService;
 
    protected Node getOriginNode(){
        Node node = nodeService.getTop();
        if (node == null) {
            throw new CoolException("库区数据错误");
        }
        return node;
    }
 
    protected Long getHostId(){
        if (getUserId() == 9527) {
            return null;
        }
        User user = getUser();
        if (user.getRoleId() == 1) {
            String hostId = String.valueOf(request.getAttribute("hostId"));
            if (Cools.isEmpty(hostId)) {
                UserLogin userLogin = userLoginService.selectOne(new EntityWrapper<UserLogin>().eq("user_id", user.getId()));
                if (userLogin != null) {
                    return userLogin.getHostId();
                }
            }
            return Long.parseLong(hostId);
        } else {
            return user.getHostId();
        }
    }
 
    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 Tag getOriginTag(){
        return tagService.getTop();
    }
 
    protected <T> void hostEq(EntityWrapper<T> wrapper){
        Long hostId = getHostId();
        if (hostId != null) {
            wrapper.eq("host_id", hostId);
        }
    }
 
    /**
     * 分页组装
     * @param pageNumber
     * @param pageSize
     * @param map
     * @param cls
     * @param <T>
     * @return
     */
    protected <T> Page<T> toPage(Integer pageNumber, Integer pageSize, Map<String, Object> map, Class<T> cls){
        // 分页索引和单页数量组装
        pageNumber = Optional.ofNullable(pageNumber).orElse(1);
        pageSize = Optional.ofNullable(pageSize).orElse(10);
        Page<T> page = new Page<>(pageNumber, pageSize);
        map.put("pageNumber", pageNumber);
        map.put("pageSize", pageSize);
 
        // 全字段模糊搜索 todo
        if (!Cools.isEmpty(map.get("condition"))) {
            Set<String> columns = new HashSet<>();
            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)) {
                    TableField tableField = field.getAnnotation(TableField.class);
                    if (!tableField.exist()) {
                        continue;
                    }
                    column = tableField.value();
                }
                if (Cools.isEmpty(column)) {
                    column = field.getName();
                }
                if (!map.keySet().contains(column)) {
                    columns.add(column);
                }
            }
            columns.forEach(col->map.put(col, map.get("condition")));
        }
        page.setCondition(map);
        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;
        }
        String keyword = String.valueOf(condition).trim();
        if (keyword.isEmpty()) {
            return;
        }
        boolean appended = false;
        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)) {
                TableField tableField = field.getAnnotation(TableField.class);
                if (!tableField.exist()) {
                    continue;
                }
                column = tableField.value();
            }
            if (Cools.isEmpty(column)) {
                column = field.getName();
            }
            if (!set.contains(column)) {
                if (supportsLikeSearch(field.getType())) {
                    if (!appended) {
                        wrapper.andNew();
                    } else {
                        wrapper.or();
                    }
                    wrapper.like(column, keyword);
                    appended = true;
                    continue;
                }
                Object exactValue = parseExactSearchValue(field.getType(), keyword);
                if (exactValue != null) {
                    if (!appended) {
                        wrapper.andNew();
                    } else {
                        wrapper.or();
                    }
                    wrapper.eq(column, exactValue);
                    appended = true;
                }
            }
        }
    }
 
    private boolean supportsLikeSearch(Class<?> fieldType) {
        return CharSequence.class.isAssignableFrom(fieldType)
                || fieldType == Character.class
                || fieldType == char.class
                || Date.class.isAssignableFrom(fieldType)
                || Temporal.class.isAssignableFrom(fieldType);
    }
 
    private Object parseExactSearchValue(Class<?> fieldType, String keyword) {
        if (!isNumericField(fieldType)) {
            return null;
        }
        try {
            BigDecimal numericValue = new BigDecimal(keyword);
            if (fieldType == Integer.class || fieldType == int.class) {
                return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.intValueExact() : null;
            }
            if (fieldType == Long.class || fieldType == long.class) {
                return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.longValueExact() : null;
            }
            if (fieldType == Short.class || fieldType == short.class) {
                return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.shortValueExact() : null;
            }
            if (fieldType == Byte.class || fieldType == byte.class) {
                return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.byteValueExact() : null;
            }
            if (fieldType == BigInteger.class) {
                return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.toBigIntegerExact() : null;
            }
            if (fieldType == Float.class || fieldType == float.class) {
                float value = numericValue.floatValue();
                return Float.isFinite(value) ? value : null;
            }
            if (fieldType == Double.class || fieldType == double.class) {
                double value = numericValue.doubleValue();
                return Double.isFinite(value) ? value : null;
            }
            if (fieldType == BigDecimal.class) {
                return numericValue;
            }
        } catch (Exception ignored) {
            return null;
        }
        return null;
    }
 
    private boolean isNumericField(Class<?> fieldType) {
        return Number.class.isAssignableFrom(fieldType)
                || fieldType == byte.class
                || fieldType == short.class
                || fieldType == int.class
                || fieldType == long.class
                || fieldType == float.class
                || fieldType == double.class;
    }
}