| | |
| | | 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.*; |
| | | |
| | | /** |
| | |
| | | */ |
| | | 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"))); |
| | |
| | | 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 |
| | |
| | | // 全字段模糊搜索 todo |
| | | if (!Cools.isEmpty(map.get("condition"))) { |
| | | Set<String> columns = new HashSet<>(); |
| | | for (Field field :Cools.getAllFields(cls)){ |
| | | for (Field field : Cools.getAllFields(cls)){ |
| | | if (Modifier.isFinal(field.getModifiers()) |
| | | || Modifier.isStatic(field.getModifiers()) |
| | | || Modifier.isTransient(field.getModifiers())){ |
| | |
| | | } |
| | | String column = null; |
| | | if (field.isAnnotationPresent(TableField.class)) { |
| | | column = field.getAnnotation(TableField.class).value(); |
| | | TableField tableField = field.getAnnotation(TableField.class); |
| | | if (!tableField.exist()) { |
| | | continue; |
| | | } |
| | | column = tableField.value(); |
| | | } |
| | | if (Cools.isEmpty(column)) { |
| | | column = field.getName(); |
| | |
| | | if (Cools.isEmpty(condition)) { |
| | | return; |
| | | } |
| | | List<String> columns = new ArrayList<>(); |
| | | 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()) |
| | |
| | | } |
| | | String column = null; |
| | | if (field.isAnnotationPresent(TableField.class)) { |
| | | column = field.getAnnotation(TableField.class).value(); |
| | | TableField tableField = field.getAnnotation(TableField.class); |
| | | if (!tableField.exist()) { |
| | | continue; |
| | | } |
| | | column = tableField.value(); |
| | | } |
| | | if (Cools.isEmpty(column)) { |
| | | column = field.getName(); |
| | | } |
| | | if (!set.contains(column)) { |
| | | columns.add(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; |
| | | } |
| | | } |
| | | } |
| | | if (columns.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | for (int i=0;i<columns.size();i++){ |
| | | if (i==0){ |
| | | wrapper.andNew(); |
| | | } else { |
| | | wrapper.or(); |
| | | try { |
| | | BigDecimal numericValue = new BigDecimal(keyword); |
| | | if (fieldType == Integer.class || fieldType == int.class) { |
| | | return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.intValueExact() : null; |
| | | } |
| | | wrapper.like(columns.get(i), condition); |
| | | 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; |
| | | } |
| | | } |