src/main/java/com/moral/common/util/ExampleUtil.java
@@ -1,10 +1,12 @@ package com.moral.common.util; import com.moral.common.bean.Constants; import com.moral.common.bean.PageBean; import lombok.extern.log4j.Log4j; import tk.mybatis.mapper.entity.Example; import java.beans.PropertyDescriptor; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URLDecoder; @@ -12,13 +14,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @Log4j public class ExampleUtil { private final static String OR_SPLIT = "or\\|"; private final static String CRITERIA_SPLIT = "\\|\\|\\|"; private final static String CONDITION_SPLIT = "\\|\\|"; private static Map<String, Method> criteriaMethodMap = null; private static Map<String, Method> criteriaMethodMap = new ConcurrentHashMap<>(); private static void setOrderByClause(Example example,String orderByClause) throws UnsupportedEncodingException, NoSuchFieldException { orderByClause = URLDecoder.decode(orderByClause,"UTF-8"); String[] orderBys = orderByClause.split(CRITERIA_SPLIT); @@ -38,16 +41,19 @@ } } } public static Example generateExample(Class clzz, PageBean page) { public static Example generateExample(Class clazz, PageBean page){ return generateExample(clazz,false,page); } public static Example generateExample(Class clazz, boolean isDelete, PageBean page) { Example example = null; try { String params = page.getQueryParams(); String orderByClause = page.getOrderByClause(); example = new Example(clzz); example = new Example(clazz); if(!StringUtils.isNullOrEmpty(orderByClause)){ setOrderByClause(example,orderByClause); } if(StringUtils.isNullOrEmpty(params)){ return example;} if(!StringUtils.isNullOrEmpty(params)){ params = URLDecoder.decode(params, "UTF-8"); if (!StringUtils.isNullOrEmpty(params) && params.startsWith("or|")) { String[] criteria = params.trim().split(OR_SPLIT); @@ -65,14 +71,15 @@ String methodName = conditionItems[0]; //属性名 String propertyName = conditionItems[1]; Method method = getMethod(methodName); if (method != null && isPropertyOfClass(clzz, propertyName)) { //参数列表 List values = new ArrayList<Object>(); if (conditionItems.length > 2) { for (int index = 2; index < conditionItems.length; index++) { values.add(conditionItems[index]); } } Method method = getExampleMethod(methodName,values.size()+1); if (method != null && (methodName.equals("andCondition")||isPropertyOfClass(clazz, propertyName))) { invokeMethod(criteriaOfExample,method,propertyName,values); } @@ -81,6 +88,10 @@ } } } } } if(!isDelete) { addDeletesToExample(example,clazz); } } catch (Exception ex) { log.error(ex.getMessage()); @@ -119,23 +130,43 @@ } ; private static boolean isPropertyOfClass(Class clzz, String propertyName) throws NoSuchFieldException { return clzz.getDeclaredField(propertyName) != null; /** * 增加删除条件过滤 * @param example */ private static void addDeletesToExample(Example example,Class clzz){ if(isPropertyOfClass(clzz,"isDelete")) { List<Example.Criteria> criteriaList = example.getOredCriteria(); if(criteriaList!=null&&criteriaList.size()>0){ for(Example.Criteria cri : criteriaList){ cri.andNotEqualTo("isDelete", Constants.IS_DELETE_TRUE); } }else { example.or().andNotEqualTo("isDelete",Constants.IS_DELETE_TRUE); } } } private static boolean isPropertyOfClass(Class clzz, String propertyName){ for(Field field : clzz.getDeclaredFields()) { if(field.getName().equals(propertyName)) { return true; } } return false; } private static Method getMethod(String methodName) { if (criteriaMethodMap == null) { criteriaMethodMap = new HashMap<>(); private static Method getExampleMethod(String methodName, int paramCount) { String criteriaMethodKey = methodName+"_"+String.valueOf(paramCount); Method methodTemp = criteriaMethodMap.get(criteriaMethodKey); if(methodTemp==null) { Method[] methods = Example.Criteria.class.getMethods(); for (Method m : methods) { Method method = criteriaMethodMap.get(m.getName()); //重载函数存储参数最长的 if (method==null||m.getParameterTypes().length>method.getParameterTypes().length){ criteriaMethodMap.put(m.getName(),m); for (Method method : methods) { if(method.getName().equals(methodName)&&method.getParameterCount() == paramCount) { criteriaMethodMap.put(criteriaMethodKey,method); return method; } } } return criteriaMethodMap.get(methodName); return methodTemp; } } src/main/java/com/moral/controller/DeviceVersionController.java
@@ -37,6 +37,14 @@ public List<Integer> getSensorIds(Integer deviceVersionId){ return deviceVersionService.getSensorIds(deviceVersionId); } @GetMapping("get-maxverno") public ResultBean<Integer> getMaxVersionNo(){ return new ResultBean<Integer>(deviceVersionService.queryMaxVersionNo()); } @GetMapping("get-byversion") public ResultBean<DeviceVersion> getByVersion(Integer version){ return new ResultBean<DeviceVersion>(deviceVersionService.queryByVersionNo(version)); } @PostMapping("version-sensor-config/{id}") public ResultBean versionSensorConfig(@PathVariable("id") Integer deviceVersionId,@RequestBody Integer [] sensorIds){ ResultBean resultBean = new ResultBean(); src/main/java/com/moral/entity/DeviceVersion.java
@@ -1,8 +1,11 @@ package com.moral.entity; import lombok.Data; import javax.persistence.Id; import java.util.Date; @Data public class DeviceVersion { @Id private Integer id; @@ -13,45 +16,9 @@ private Date createTime; private Date updateTime; private Boolean isDelete; private String description; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description == null ? null : description.trim(); } } src/main/java/com/moral/service/DeviceVersionService.java
@@ -14,6 +14,10 @@ void versionSensorConfig(Integer deviceVersionId, Integer[] sensorIds); Integer queryMaxVersionNo(); DeviceVersion queryByVersionNo(int versionNo); List<Integer> getSensorIds(int deviceVersionId); List<DeviceVersion> queryByOrgId(Integer organizationId); src/main/java/com/moral/service/impl/DeviceServiceImpl.java
@@ -350,8 +350,8 @@ if(pageBean.getPageSize()>0){ PageHelper.startPage(pageBean.getPageIndex(),pageBean.getPageSize()); } List<Device> organizationList = deviceMapper.selectWithRelationData(example); return new PageBean(organizationList); List<Device> deviceList = deviceMapper.selectWithRelationData(example); return new PageBean(deviceList); } @Override src/main/java/com/moral/service/impl/DeviceVersionServiceImpl.java
@@ -13,6 +13,7 @@ import javax.annotation.Resource; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; @Service @@ -28,8 +29,10 @@ public void addOrModify(DeviceVersion deviceVersion){ try{ if(deviceVersion.getId()==null){ deviceVersion.setUpdateTime(new Date()); deviceVersionMapper.insertSelective(deviceVersion); }else{ deviceVersion.setUpdateTime(new Date()); deviceVersionMapper.updateByPrimaryKeySelective(deviceVersion); } } @@ -42,11 +45,18 @@ public void deleteByIds(Integer... ids) { if(ids!=null&&ids.length>0){ if(ids.length==1){ deviceVersionMapper.deleteByPrimaryKey(ids[0]); DeviceVersion deviceVersion = new DeviceVersion(); deviceVersion.setId(ids[0]); deviceVersion.setIsDelete(true); deviceVersion.setUpdateTime(new Date()); deviceVersionMapper.updateByPrimaryKeySelective(deviceVersion); }else{ DeviceVersion deviceVersion = new DeviceVersion(); deviceVersion.setIsDelete(true); deviceVersion.setUpdateTime(new Date()); Example example = new Example(ENTITY_CLASS); example.or().andIn("id", Arrays.asList(ids)); deviceVersionMapper.deleteByExample(example); deviceVersionMapper.updateByExampleSelective(deviceVersion,example); } } @@ -70,6 +80,23 @@ public List<DeviceVersion> getVersionsByOrgId(int organizationId){ return null; } @Override public Integer queryMaxVersionNo() { Example example = new Example(DeviceVersion.class); example.orderBy("version").desc(); List<DeviceVersion> deviceVersionList = deviceVersionMapper.selectByExample(example); if(deviceVersionList.size()>0){ return deviceVersionList.get(0).getVersion(); } return null; } @Override public DeviceVersion queryByVersionNo(int versionNo) { DeviceVersion query = new DeviceVersion(); query.setVersion(versionNo); return deviceVersionMapper.selectOne(query); } @Override public List<Integer> getSensorIds(int deviceVersionId){ DeviceVersionSensor deviceVersionSensor = new DeviceVersionSensor();