|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.annotation.TableField; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.zy.asrs.common.sys.entity.User; | 
|---|
|  |  |  | import com.zy.asrs.common.sys.entity.UserLogin; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private TagService tagService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected Long getHostId(){ | 
|---|
|  |  |  | protected Long getHostId() { | 
|---|
|  |  |  | if (getUserId() == 9527) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected Long getUserId(){ | 
|---|
|  |  |  | protected Long getUserId() { | 
|---|
|  |  |  | return Long.parseLong(String.valueOf(request.getAttribute("userId"))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected User getUser(){ | 
|---|
|  |  |  | protected User getUser() { | 
|---|
|  |  |  | User user = userService.getById(getUserId()); | 
|---|
|  |  |  | if (null == user) { | 
|---|
|  |  |  | throw new CoolException(BaseRes.DENIED); | 
|---|
|  |  |  | 
|---|
|  |  |  | return user; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected String getComment(Class<?> cls, String fieldName){ | 
|---|
|  |  |  | protected String getComment(Class<?> cls, String fieldName) { | 
|---|
|  |  |  | Field[] fields = Cools.getAllFields(cls); | 
|---|
|  |  |  | for (Field field : fields){ | 
|---|
|  |  |  | if (fieldName.equals(field.getName())){ | 
|---|
|  |  |  | for (Field field : fields) { | 
|---|
|  |  |  | if (fieldName.equals(field.getName())) { | 
|---|
|  |  |  | return field.getAnnotation(ApiModelProperty.class).value(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 分页组装 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param pageNumber | 
|---|
|  |  |  | * @param pageSize | 
|---|
|  |  |  | * @param map | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param <T> | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected <T> Page<T> toPage(Integer pageNumber, Integer pageSize, Map<String, Object> map, Class<T> cls){ | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | 
|---|
|  |  |  | // 全字段模糊搜索 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())){ | 
|---|
|  |  |  | for (Field field : Cools.getAllFields(cls)) { | 
|---|
|  |  |  | if (Modifier.isFinal(field.getModifiers()) || Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String column = null; | 
|---|
|  |  |  | 
|---|
|  |  |  | columns.add(column); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | columns.forEach(col->map.put(col, map.get("condition"))); | 
|---|
|  |  |  | columns.forEach(col -> map.put(col, map.get("condition"))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //        page.setCondition(map); | 
|---|
|  |  |  | return page; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected Tag getOriginTag(){ | 
|---|
|  |  |  | protected Tag getOriginTag() { | 
|---|
|  |  |  | return tagService.getTop(getHostId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 全字段模糊搜索 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param cls       模型类 | 
|---|
|  |  |  | * @param set       排除字段集合 | 
|---|
|  |  |  | * @param condition 搜索内容 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | protected <T> void allLike(Class<T> cls, Set<String> set, QueryWrapper<T> wrapper, String condition) { | 
|---|
|  |  |  | if (Cools.isEmpty(condition)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | final 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 = camelToUnderscore(field.getAnnotation(TableField.class).value()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (Cools.isEmpty(column)) { | 
|---|
|  |  |  | column = camelToUnderscore(field.getName()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //if (!set.contains(column)) { | 
|---|
|  |  |  | columns.add(column); | 
|---|
|  |  |  | //} | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (columns.isEmpty()) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | wrapper.and(w -> { | 
|---|
|  |  |  | for (int i = 0; i < columns.size(); i++) { | 
|---|
|  |  |  | String colums = columns.get(i); | 
|---|
|  |  |  | if (i == 0) { | 
|---|
|  |  |  | w.like(colums, condition); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | w.or().like(colums, condition); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        for (int i=0;i<columns.size();i++){ | 
|---|
|  |  |  | //            String colums = columns.get(i); | 
|---|
|  |  |  | //            if (i==0){ | 
|---|
|  |  |  | //                wrapper.and(w -> w.like(colums, condition)); | 
|---|
|  |  |  | //            } else { | 
|---|
|  |  |  | //                wrapper.or(w -> w.like(colums, condition)); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | ////            wrapper.like(columns.get(i), condition); | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static String camelToUnderscore(String camelCase) { | 
|---|
|  |  |  | return camelCase.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    /** | 
|---|
|  |  |  | //     * 全字段模糊搜索 | 
|---|
|  |  |  | //     * | 
|---|
|  |  |  | //     * @param cls       模型类 | 
|---|
|  |  |  | //     * @param set       排除字段集合 | 
|---|
|  |  |  | //     * @param condition 搜索内容 | 
|---|
|  |  |  | //     */ | 
|---|
|  |  |  | //    protected <T> void allLike(Class<T> cls, Set<String> set, LambdaQueryWrapper<T> wrapper, String condition) { | 
|---|
|  |  |  | //        if (Cools.isEmpty(condition)) { | 
|---|
|  |  |  | //            return; | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        final 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 = camelToUnderscore(field.getAnnotation(TableField.class).value()); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //            if (Cools.isEmpty(column)) { | 
|---|
|  |  |  | //                column = camelToUnderscore(field.getName()); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //            //if (!set.contains(column)) { | 
|---|
|  |  |  | //            columns.add(column); | 
|---|
|  |  |  | //            //} | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        if (columns.isEmpty()) { | 
|---|
|  |  |  | //            return; | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        SFunction<cls, String> columnFunc = Entity::columnName; | 
|---|
|  |  |  | //        wrapper.and(w -> { | 
|---|
|  |  |  | //            for (int i = 0; i < columns.size(); i++) { | 
|---|
|  |  |  | //                String colums = columns.get(i); | 
|---|
|  |  |  | //                if (i == 0) { | 
|---|
|  |  |  | //                    w.like(colums, condition); | 
|---|
|  |  |  | //                } else { | 
|---|
|  |  |  | //                    w.or().like(colums, condition); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        }); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|