From 4f36b1a680f6243fc2ddaff18e6fb349ab8134b0 Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Sun, 4 Feb 2024 19:15:40 +0800 Subject: [PATCH] =?UTF-8?q?#=20=E9=A3=8E=E9=99=A9=E7=82=B9=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=AF=BC=E5=87=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hnac/hzims/common/support/constants/Order.java | 5 +++ .../hnac/hzims/common/support/utils/Condition.java | 49 ++++++++++++++++++++++ .../safeproduct/entity/DangerSourceEntity.java | 2 + .../hazard/service/IDangerSourceService.java | 11 +++++ .../service/impl/DangerSourceServiceImpl.java | 28 +++---------- 5 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 hzims-biz-common/src/main/java/com/hnac/hzims/common/support/constants/Order.java create mode 100644 hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java diff --git a/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/constants/Order.java b/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/constants/Order.java new file mode 100644 index 0000000..67e68c4 --- /dev/null +++ b/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/constants/Order.java @@ -0,0 +1,5 @@ +package com.hnac.hzims.common.support.constants; + +public enum Order { + ASC,DESC; +} diff --git a/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java b/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java new file mode 100644 index 0000000..c593bb0 --- /dev/null +++ b/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java @@ -0,0 +1,49 @@ +package com.hnac.hzims.common.support.utils; + + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.google.common.collect.Lists; +import com.hnac.hzims.common.support.constants.Order; +import org.springblade.core.tool.utils.BeanUtil; + +import java.lang.reflect.Field; +import java.util.*; +import java.util.function.Function; + +public class Condition extends org.springblade.core.mp.support.Condition { + + public static List getFieldList(Object object) { + Class clazz = object.getClass(); + List fields = Lists.newArrayList(); + while (null != clazz){ + fields.addAll(Arrays.asList(clazz.getDeclaredFields())); + clazz = clazz.getSuperclass(); + } + return fields; + } + + public static LambdaQueryWrapper getQueryWrapper(Object query, Class clazz, Map> orderFields) { + QueryWrapper queryWrapper = getQueryWrapper(BeanUtil.copy(query,clazz)); + List fields = getFieldList(query); + // 填充排序字段 + orderFields.forEach((fieldName,order) -> { + Optional existField = fields.stream().filter(field -> camel2under(fieldName).equals(camel2under(field.getName()))).findAny(); + if(existField.isPresent()) { + if(Order.ASC.equals(order)) { + queryWrapper.orderByAsc(fieldName); + } + else if(Order.DESC.equals(order)) { + queryWrapper.orderByDesc(fieldName); + } + } + }); + return queryWrapper.lambda(); + } + + private static String camel2under(String c) { + String separator = "_"; + c = c.replaceAll("([a-z])([A-Z])", "$1" + separator + "$2").toUpperCase(); + return c; + } +} diff --git a/hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/entity/DangerSourceEntity.java b/hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/entity/DangerSourceEntity.java index 7d56405..f40f1a0 100644 --- a/hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/entity/DangerSourceEntity.java +++ b/hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/entity/DangerSourceEntity.java @@ -49,6 +49,7 @@ public class DangerSourceEntity extends TenantEntity { */ @ApiModelProperty(value = "设备ID") @JsonSerialize(nullsUsing = NullSerializer.class) + @QueryField(condition = SqlCondition.EQUAL) private Long eqId; @ApiModelProperty("设备编号") @@ -74,6 +75,7 @@ public class DangerSourceEntity extends TenantEntity { * 应用类型 */ @ApiModelProperty(value = "应用类型") + @QueryField(condition = SqlCondition.EQUAL) private String applyType; /** * 可能导致的事故 diff --git a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/hazard/service/IDangerSourceService.java b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/hazard/service/IDangerSourceService.java index eb26ebf..53b565a 100644 --- a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/hazard/service/IDangerSourceService.java +++ b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/hazard/service/IDangerSourceService.java @@ -1,6 +1,9 @@ package com.hnac.hzims.safeproduct.hazard.service; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.hnac.hzims.common.support.constants.Order; +import com.hnac.hzims.common.support.utils.Condition; import com.hnac.hzims.safeproduct.hazard.dto.DangerSourceDTO; import com.hnac.hzims.safeproduct.entity.DangerSourceEntity; import com.hnac.hzims.safeproduct.hazard.dto.HazardInstanceDTO; @@ -14,7 +17,9 @@ import org.springblade.system.vo.CreateTableVO; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author xiashandong @@ -52,4 +57,10 @@ public interface IDangerSourceService extends BaseService { * @param response 请求响应 */ void exportDangerSource(DangerSourceEntity req, HttpServletResponse response); + + default LambdaQueryWrapper getQueryWrapper(Object req) { + Map param = new HashMap<>(); + param.put("create_time",Order.DESC); + return Condition.getQueryWrapper(req,DangerSourceEntity.class,param); + } } diff --git a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/hazard/service/impl/DangerSourceServiceImpl.java b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/hazard/service/impl/DangerSourceServiceImpl.java index 4a8e35e..8e0418a 100644 --- a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/hazard/service/impl/DangerSourceServiceImpl.java +++ b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/hazard/service/impl/DangerSourceServiceImpl.java @@ -10,6 +10,7 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.hnac.hzims.common.support.constants.Order; import com.hnac.hzims.equipment.entity.EmInfoEntity; import com.hnac.hzims.equipment.feign.IEmInfoClient; import com.hnac.hzims.operational.maintenance.vo.MaintenanceTaskQueryVO; @@ -183,28 +184,7 @@ public class DangerSourceServiceImpl extends BaseServiceImpl> pageCondition(DangerSourceEntity req, Query query) { - Wrapper queryWrapper = new LambdaQueryWrapper() {{ - orderByDesc(DangerSourceEntity::getCreateTime); - if (req.getType() != null) { - eq(DangerSourceEntity::getType, req.getType()); - } - if (null != req.getEqId()) { - eq(DangerSourceEntity::getEqId, req.getEqId()); - } - if (null != req.getLevel()) { - eq(DangerSourceEntity::getLevel, req.getLevel()); - } - if (StringUtil.isNotBlank(req.getName())) { - like(DangerSourceEntity::getName, req.getName()); - } - if (StringUtil.isNotBlank(req.getDangerEle())) { - like(DangerSourceEntity::getDangerEle, req.getDangerEle()); - } - if (StringUtil.isNotBlank(req.getApplyType())) { - eq(DangerSourceEntity::getApplyType, req.getApplyType()); - } - }}; - IPage page = super.page(Condition.getPage(query), queryWrapper); + IPage page = super.page(Condition.getPage(query), getQueryWrapper(req)); page.setRecords(DangerSourceWrapper.build().listVO(page.getRecords())); return R.data(page); } @@ -336,6 +316,8 @@ public class DangerSourceServiceImpl extends BaseServiceImpl sourceList = req.getHazardList().stream().map(h -> { DangerSourceEntity result = JSONObject.parseObject(JSON.toJSONString(h), DangerSourceEntity.class); result.setId(null); + result.setEmCode(req.getEmCode()); + result.setEmName(req.getEmName()); result.setLibraryId(h.getId()); result.setRefDept(req.getDeptId()); result.setRefDeptName(req.getDeptName()); @@ -352,7 +334,7 @@ public class DangerSourceServiceImpl extends BaseServiceImpl dangerSourceList = this.list(Condition.getQueryWrapper(req)); + List dangerSourceList = this.list(getQueryWrapper(req)); List voList = DangerSourceWrapper.build().listVO(dangerSourceList); List exportVOList = BeanUtil.copy(voList, DangerSourceExportVO.class); // 设置响应头