|  |  | 
 |  |  |  | 
 |  |  | 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.Tag; | 
 |  |  | import com.zy.asrs.service.TagService; | 
 |  |  | import com.zy.system.entity.User; | 
 |  |  | import com.zy.system.service.UserService; | 
 |  |  | import io.swagger.annotations.ApiModelProperty; | 
 |  |  | 
 |  |  | 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.Set; | 
 |  |  | 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; | 
 |  |  |  | 
 |  |  |     protected Long getUserId(){ | 
 |  |  |         return Long.parseLong(String.valueOf(request.getAttribute("userId"))); | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return ""; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     protected Tag getOriginTag(){ | 
 |  |  |         return tagService.getTop(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 分页组装 | 
 |  |  |      * @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)) { | 
 |  |  |                     column = field.getAnnotation(TableField.class).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; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |         } | 
 |  |  |         for (int i=0;i<columns.size();i++){ | 
 |  |  |             if (i==0){ | 
 |  |  |                 wrapper.and(); | 
 |  |  |                 wrapper.andNew(); | 
 |  |  |             } else { | 
 |  |  |                 wrapper.or(); | 
 |  |  |             } | 
 |  |  |             wrapper.like(columns.get(i), condition); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static boolean isJSON(String str) { | 
 |  |  |         if (Cools.isEmpty(str)) { | 
 |  |  |             return false; | 
 |  |  |         } else { | 
 |  |  |             str = str.trim(); | 
 |  |  |             if (str.startsWith("{") && str.endsWith("}")) { | 
 |  |  |                 return true; | 
 |  |  |             } else if (str.startsWith("[") && str.endsWith("]")) { | 
 |  |  |                 return true; | 
 |  |  |             } else { | 
 |  |  |                 return false; | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | } |