From 58020f3d711d4baa16ef092c2ea826071c7d23b9 Mon Sep 17 00:00:00 2001
From: cuilei <ray_tsu1@163.com>
Date: 星期三, 16 七月 2025 18:26:35 +0800
Subject: [PATCH] 消息推送定时任务逻辑调整

---
 lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamWeekMaintenanceOrderServiceImpl.java |  521 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 493 insertions(+), 28 deletions(-)

diff --git a/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamWeekMaintenanceOrderServiceImpl.java b/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamWeekMaintenanceOrderServiceImpl.java
index c17c420..4bc6c16 100644
--- a/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamWeekMaintenanceOrderServiceImpl.java
+++ b/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamWeekMaintenanceOrderServiceImpl.java
@@ -2,43 +2,64 @@
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSON;
+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.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shiro.SecurityUtils;
+import org.flowable.engine.TaskService;
+import org.flowable.task.api.Task;
+import org.jeecg.common.api.vo.FileUploadResult;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.constant.CommonConstant;
 import org.jeecg.common.constant.DataBaseConstant;
 import org.jeecg.common.exception.JeecgBootException;
 import org.jeecg.common.system.vo.LoginUser;
 import org.jeecg.common.util.oConvertUtils;
-import org.jeecg.modules.eam.constant.WeekMaintenanceStatusEnum;
-import org.jeecg.modules.eam.constant.OrderCreationMethodEnum;
+import org.jeecg.modules.eam.aspect.annotation.EquipmentHistoryLog;
+import org.jeecg.modules.eam.constant.*;
+import org.jeecg.modules.eam.entity.EamEquipment;
 import org.jeecg.modules.eam.entity.EamWeekMaintenanceOrder;
 import org.jeecg.modules.eam.entity.EamWeekMaintenanceOrderDetail;
 import org.jeecg.modules.eam.mapper.EamWeekMaintenanceOrderMapper;
 import org.jeecg.modules.eam.request.EamWeekMaintenanceQuery;
 import org.jeecg.modules.eam.request.EamWeekMaintenanceRequest;
-import org.jeecg.modules.eam.service.IEamWeekMaintenanceOrderDetailService;
-import org.jeecg.modules.eam.service.IEamWeekMaintenanceOrderService;
+import org.jeecg.modules.eam.service.*;
+import org.jeecg.modules.eam.vo.EquipmentInspectionStatistics;
+import org.jeecg.modules.eam.vo.EquipmentMaintenanceStatistics;
 import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness;
+import org.jeecg.modules.flowable.apithird.business.service.IFlowMyBusinessService;
 import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI;
 import org.jeecg.modules.flowable.apithird.service.FlowCommonService;
+import org.jeecg.modules.flowable.domain.vo.WorkTaskDataVo;
 import org.jeecg.modules.flowable.service.IFlowDefinitionService;
+import org.jeecg.modules.flowable.service.IFlowTaskService;
+import org.jeecg.modules.qywx.message.vo.TemplateCard;
+import org.jeecg.modules.qywx.message.vo.TemplateCardEntity;
+import org.jeecg.modules.system.entity.SysUser;
+import org.jeecg.modules.system.service.IMdcProductionService;
+import org.jeecg.modules.system.service.ISysUserService;
+import org.jeecg.modules.system.service.impl.ThirdAppWechatEnterpriseServiceImpl;
+import org.jeecg.modules.system.vo.UserSelector;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import java.time.LocalDate;
 import java.util.*;
 import java.util.stream.Collectors;
 
 /**
  * @Description: 鍛ㄤ繚宸ュ崟
  * @Author: jeecg-boot
- * @Date:   2025-04-02
+ * @Date: 2025-04-02
  * @Version: V1.0
  */
 @Service("IEamWeekMaintenanceOrderService")
@@ -50,14 +71,40 @@
     @Autowired
     private IEamWeekMaintenanceOrderDetailService eamWeekMaintenanceOrderDetailService;
 
-    @Resource
+    @Autowired
     private FlowCommonService flowCommonService;
-    @Resource
+    @Autowired
     private IFlowDefinitionService flowDefinitionService;
+    @Autowired
+    private IFlowMyBusinessService flowMyBusinessService;
+    @Autowired
+    private TaskService taskService;
+    @Autowired
+    private IFlowTaskService flowTaskService;
+
+    @Autowired
+    private ISysUserService sysUserService;
+    @Autowired
+    private IEamEquipmentService eamEquipmentService;
+    @Autowired
+    private IEamReportRepairService eamReportRepairService;
+    @Autowired
+    private IEamEquipmentExtendService eamEquipmentExtendService;
+    @Autowired
+    private IMdcProductionService mdcProductionService;
+    @Autowired
+    private ThirdAppWechatEnterpriseServiceImpl wechatEnterpriseService;
+
+    @Value("${wechatEnterprise.cardActionUrl}")
+    private String cardActionUrl;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public boolean addWeekMaintenance(EamWeekMaintenanceRequest request) {
+        EamEquipment equipment = eamEquipmentService.getById(request.getEquipmentId());
+        if (equipment == null) {
+            throw new JeecgBootException("璁惧涓嶅瓨鍦紝娣诲姞澶辫触锛�");
+        }
         EamWeekMaintenanceOrder order = new EamWeekMaintenanceOrder();
         order.setOrderNum(request.getOrderNum());
         order.setEquipmentId(request.getEquipmentId());
@@ -67,12 +114,12 @@
         order.setRemark(request.getRemark());
         //鐘舵�佸垵濮嬪寲
         order.setMaintenanceStatus(WeekMaintenanceStatusEnum.WAIT_MAINTENANCE.name());
-        order.setCreationMethod(OrderCreationMethodEnum.MANUAL.name());
+        order.setCreationMethod(request.getCreationMethod());
         //鍒犻櫎鏍囪
         order.setDelFlag(CommonConstant.DEL_FLAG_0);
         eamWeekMaintenanceOrderMapper.insert(order);
         //澶勭悊鏄庣粏鏁版嵁
-        if(CollectionUtil.isNotEmpty(request.getTableDetailList())) {
+        if (CollectionUtil.isNotEmpty(request.getTableDetailList())) {
             request.getTableDetailList().forEach(tableDetail -> {
                 tableDetail.setId(null);
                 tableDetail.setOrderId(order.getId());
@@ -80,9 +127,9 @@
             eamWeekMaintenanceOrderDetailService.saveBatch(request.getTableDetailList());
         }
         //鍒ゆ柇鏄惁瀛樺湪淇濆吇浜� 濡傛灉瀛樺湪鍒欏惎鍔ㄦ祦绋�
-        if(StringUtils.isNotBlank(order.getOperator())) {
+        if (StringUtils.isNotBlank(order.getOperator())) {
             //鍚姩娴佺▼
-            flowCommonService.initActBusiness("宸ュ崟鍙�:" + order.getOrderNum() + ";寮�濮嬭繘琛岃澶囧懆淇�",
+            flowCommonService.initActBusiness("宸ュ崟鍙�: " + order.getOrderNum() + ";璁惧缂栧彿: " + equipment.getEquipmentCode() + ";瀹夎浣嶇疆: " + equipment.getInstallationPosition(),
                     order.getId(), "IEamWeekMaintenanceOrderService", "week_maintenance_process", null);
             Map<String, Object> variables = new HashMap<>();
             variables.put("dataId", order.getId());
@@ -98,11 +145,15 @@
             usernames.add(order.getOperator());
             variables.put("NextAssignee", usernames);
             Result result = flowDefinitionService.startProcessInstanceByKey("week_maintenance_process", variables);
-            if(result != null){
+            if (result != null) {
                 //鏇存柊鍛ㄤ繚鐘舵��
                 order.setMaintenanceStatus(WeekMaintenanceStatusEnum.UNDER_MAINTENANCE.name());
                 order.setActualStartTime(new Date());
                 eamWeekMaintenanceOrderMapper.updateById(order);
+                //鏇存柊璁惧淇濆吇鐘舵��
+                eamEquipmentExtendService.updateEquipmentMaintenanceStatus(order.getEquipmentId(), EquipmentMaintenanceStatus.UNDER_MAINTENANCE.name());
+                //鎺ㄩ�佷紒涓氬井淇℃秷鎭�
+                sendQywxTemplateCardMessage(order, null);
                 return result.isSuccess();
             }
         }
@@ -140,6 +191,20 @@
             if (query.getMaintenanceDateBegin() != null && query.getMaintenanceDateEnd() != null) {
                 queryWrapper.between("wmo.maintenance_date", query.getMaintenanceDateBegin(), query.getMaintenanceDateEnd());
             }
+            if(StringUtils.isNotBlank(query.getRandomInspectionFlag())) {
+                if(CommonConstant.DEFAULT_1.equals(query.getRandomInspectionFlag())) {
+                    queryWrapper.eq("wmo.random_inspection_flag", CommonConstant.DEFAULT_1);
+                }else {
+                    queryWrapper.and(q -> q.eq("wmo.random_inspection_flag", CommonConstant.DEFAULT_0).or().isNull("wmo.random_inspection_flag"));
+                }
+            }
+            if(StringUtils.isNotBlank(query.getFinalRandomInspectionFlag())) {
+                if(CommonConstant.DEFAULT_1.equals(query.getFinalRandomInspectionFlag())) {
+                    queryWrapper.eq("wmo.final_random_inspection_flag", CommonConstant.DEFAULT_1);
+                }else {
+                    queryWrapper.and(q -> q.eq("wmo.final_random_inspection_flag", CommonConstant.DEFAULT_0).or().isNull("wmo.final_random_inspection_flag"));
+                }
+            }
             //鎺掑簭
             if (StringUtils.isNotBlank(query.getColumn()) && StringUtils.isNotBlank(query.getOrder())) {
                 String column = query.getColumn();
@@ -164,11 +229,15 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public boolean editWeekMaintenance(EamWeekMaintenanceRequest request) {
+        EamEquipment equipment = eamEquipmentService.getById(request.getEquipmentId());
+        if (equipment == null) {
+            throw new JeecgBootException("璁惧涓嶅瓨鍦紝娣诲姞澶辫触锛�");
+        }
         EamWeekMaintenanceOrder entity = eamWeekMaintenanceOrderMapper.selectById(request.getId());
-        if(entity == null){
+        if (entity == null) {
             throw new JeecgBootException("缂栬緫鐨勬暟鎹凡鍒犻櫎锛岃鍒锋柊閲嶈瘯锛�");
         }
-        if(!WeekMaintenanceStatusEnum.WAIT_MAINTENANCE.name().equals(entity.getMaintenanceStatus())){
+        if (!WeekMaintenanceStatusEnum.WAIT_MAINTENANCE.name().equals(entity.getMaintenanceStatus())) {
             throw new JeecgBootException("鍙湁寰呬繚鍏荤姸鎬佺殑鏁版嵁鎵嶅彲缂栬緫锛�");
         }
         entity.setMaintenanceDate(request.getMaintenanceDate());
@@ -177,32 +246,32 @@
 
         eamWeekMaintenanceOrderMapper.updateById(entity);
         //澶勭悊璇︽儏
-        if(CollectionUtil.isNotEmpty(request.getTableDetailList())) {
+        if (CollectionUtil.isNotEmpty(request.getTableDetailList())) {
             List<EamWeekMaintenanceOrderDetail> addList = new ArrayList<>();
             List<EamWeekMaintenanceOrderDetail> updateList = new ArrayList<>();
             request.getTableDetailList().forEach(tableDetail -> {
                 tableDetail.setOrderId(entity.getId());
-                if(tableDetail.getId() == null){
+                if (tableDetail.getId() == null) {
                     addList.add(tableDetail);
-                }else {
+                } else {
                     updateList.add(tableDetail);
                 }
             });
-            if(CollectionUtil.isNotEmpty(addList)){
+            if (CollectionUtil.isNotEmpty(addList)) {
                 eamWeekMaintenanceOrderDetailService.saveBatch(addList);
             }
-            if(CollectionUtil.isNotEmpty(updateList)){
+            if (CollectionUtil.isNotEmpty(updateList)) {
                 eamWeekMaintenanceOrderDetailService.updateBatchById(updateList);
             }
         }
-        if(CollectionUtil.isNotEmpty(request.getRemoveDetailList())) {
+        if (CollectionUtil.isNotEmpty(request.getRemoveDetailList())) {
             List<String> ids = request.getRemoveDetailList().stream().map(EamWeekMaintenanceOrderDetail::getId).collect(Collectors.toList());
             eamWeekMaintenanceOrderDetailService.removeBatchByIds(ids);
         }
         //鍒ゆ柇鏄惁瀛樺湪淇濆吇浜� 濡傛灉瀛樺湪鍒欏惎鍔ㄦ祦绋�
-        if(StringUtils.isNotBlank(entity.getOperator())) {
+        if (StringUtils.isNotBlank(entity.getOperator())) {
             //鍚姩娴佺▼
-            flowCommonService.initActBusiness("宸ュ崟鍙�:" + entity.getOrderNum() + ";寮�濮嬭繘琛岃澶囧懆淇�",
+            flowCommonService.initActBusiness("宸ュ崟鍙�: " + entity.getOrderNum() + ";璁惧缂栧彿: " + equipment.getEquipmentCode() + ";瀹夎浣嶇疆: " + equipment.getInstallationPosition(),
                     entity.getId(), "IEamWeekMaintenanceOrderService", "week_maintenance_process", null);
             Map<String, Object> variables = new HashMap<>();
             variables.put("dataId", entity.getId());
@@ -218,13 +287,16 @@
             usernames.add(entity.getOperator());
             variables.put("NextAssignee", usernames);
             Result result = flowDefinitionService.startProcessInstanceByKey("week_maintenance_process", variables);
-            if(result != null){
+            if (result != null) {
                 //鏇存柊鍛ㄤ繚鐘舵��
                 entity.setMaintenanceStatus(WeekMaintenanceStatusEnum.UNDER_MAINTENANCE.name());
                 entity.setActualStartTime(new Date());
                 eamWeekMaintenanceOrderMapper.updateById(entity);
+                //鏇存柊璁惧淇濆吇鐘舵��
+                eamEquipmentExtendService.updateEquipmentMaintenanceStatus(entity.getEquipmentId(), EquipmentMaintenanceStatus.UNDER_MAINTENANCE.name());
                 return result.isSuccess();
             }
+
         }
         return true;
     }
@@ -233,20 +305,27 @@
     @Transactional(rollbackFor = Exception.class)
     public boolean collect(String id) {
         EamWeekMaintenanceOrder entity = eamWeekMaintenanceOrderMapper.selectById(id);
-        if(entity == null) {
+        if (entity == null) {
             throw new JeecgBootException("瑕侀鍙栫殑宸ュ崟涓嶅瓨鍦紝璇峰埛鏂伴噸璇曪紒");
         }
-        if(!WeekMaintenanceStatusEnum.WAIT_MAINTENANCE.name().equals(entity.getMaintenanceStatus())) {
+        if (!WeekMaintenanceStatusEnum.WAIT_MAINTENANCE.name().equals(entity.getMaintenanceStatus())) {
             throw new JeecgBootException("璇ュ伐鍗曞凡杩涜杩囬鍙栵紒");
         }
+        EamEquipment equipment = eamEquipmentService.getById(entity.getEquipmentId());
+        if (equipment == null) {
+            throw new JeecgBootException("璁惧涓嶅瓨鍦紝娣诲姞澶辫触锛�");
+        }
         LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
+        if(sysUser == null || !BusinessCodeConst.PCR0001.equals(sysUser.getPost())) {
+            throw new JeecgBootException("涓嶆槸鎿嶄綔宸ワ紝鏃犳硶棰嗗彇姝ゅ伐鍗曪紒");
+        }
         entity.setOperator(sysUser.getUsername());
         entity.setMaintenanceStatus(WeekMaintenanceStatusEnum.UNDER_MAINTENANCE.name());
         entity.setActualStartTime(new Date());
         eamWeekMaintenanceOrderMapper.updateById(entity);
 
         //鍚姩娴佺▼
-        flowCommonService.initActBusiness("宸ュ崟鍙�:" + entity.getOrderNum() + ";寮�濮嬭繘琛岃澶囧懆淇�",
+        flowCommonService.initActBusiness("宸ュ崟鍙�: " + entity.getOrderNum() + ";璁惧缂栧彿: " + equipment.getEquipmentCode() + ";瀹夎浣嶇疆: " + equipment.getInstallationPosition(),
                 entity.getId(), "IEamWeekMaintenanceOrderService", "week_maintenance_process", null);
         Map<String, Object> variables = new HashMap<>();
         variables.put("dataId", entity.getId());
@@ -262,9 +341,395 @@
         usernames.add(entity.getOperator());
         variables.put("NextAssignee", usernames);
         Result result = flowDefinitionService.startProcessInstanceByKey("week_maintenance_process", variables);
-        if(result != null){
+        if (result != null) {
+            //鏇存柊璁惧淇濆吇鐘舵��
+            eamEquipmentExtendService.updateEquipmentMaintenanceStatus(entity.getEquipmentId(), EquipmentMaintenanceStatus.UNDER_MAINTENANCE.name());
+            //鎺ㄩ�佷紒涓氬井淇℃秷鎭�
+            sendQywxTemplateCardMessage(entity, null);
             return result.isSuccess();
         }
+        return true;
+    }
+
+    private void sendQywxTemplateCardMessage(EamWeekMaintenanceOrder order, EamWeekMaintenanceRequest request) {
+        TemplateCard templateCard = new TemplateCard();
+        EamEquipment equipment = eamEquipmentService.getById(order.getEquipmentId());
+        order.setEquipmentCode(equipment.getEquipmentCode());
+        String maintenanceStatus = order.getMaintenanceStatus();
+        if (WeekMaintenanceStatusEnum.UNDER_MAINTENANCE.name().equals(maintenanceStatus)) {
+            //宸ュ崟淇濆吇涓紝鎺ㄩ�佹秷鎭粰淇濆吇浜猴紙鏂板宸ュ崟鏃舵寚瀹氥�佷繚鍏讳汉棰嗗彇锛�
+            String operator = order.getOperator();
+            String touser = wechatEnterpriseService.getTouser(operator, false);
+            templateCard.setTouser(touser);
+        }
+        if (WeekMaintenanceStatusEnum.WAIT_CONFIRM.name().equals(maintenanceStatus)
+                || WeekMaintenanceStatusEnum.WAIT_INITIAL_ACCEPTANCE.name().equals(maintenanceStatus)
+                || WeekMaintenanceStatusEnum.WAIT_FINAL_ACCEPTANCE.name().equals(maintenanceStatus)) {
+            //宸ュ崟宸蹭繚鍏诲畬鎴愬緟纭锛屾帹閫佹秷鎭粰鐝粍闀�
+            //鐝粍闀垮凡纭锛屾帹閫佹秷鎭粰璁惧绠$悊鍛樼‘璁�
+            //璁惧绠$悊鍛樺凡纭锛屾帹閫佺粰璁捐兘閮ㄧ鐞嗗憳纭
+            List<String> nextAssignee = (List<String>) request.getValues().get("NextAssignee");
+            String nextAssigneeStr = nextAssignee.stream()
+                    .filter(Objects::nonNull) // 鍙�夛細杩囨护 null 鍊�
+                    .collect(Collectors.joining(","));
+            String touser = wechatEnterpriseService.getTouser(nextAssigneeStr, false);
+            templateCard.setTouser(touser);
+        }
+
+        TemplateCardEntity templateCardEntity = new TemplateCardEntity();
+        templateCard.setTemplate_card(templateCardEntity);
+        templateCardEntity.setTask_id(order.getId());
+        TemplateCardEntity.MainTitle mainTitle = new TemplateCardEntity.MainTitle();
+        mainTitle.setTitle("璁惧鍛ㄤ繚");
+        templateCardEntity.setMain_title(mainTitle);
+
+        if (WeekMaintenanceStatusEnum.UNDER_MAINTENANCE.name().equals(maintenanceStatus)) {
+            if (Objects.nonNull(request) && StrUtil.isNotBlank(request.getConfirmDealType())) {
+                templateCardEntity.setSub_title_text("宸ュ崟: " + order.getOrderNum() + " 琚┏鍥烇紝璇烽噸鏂版墽琛屼繚鍏诲悗鎻愪氦");
+            } else {
+                templateCardEntity.setSub_title_text("宸查鍙栧懆淇濆伐鍗�: " + order.getOrderNum() + "锛屽紑濮嬭繘琛屼繚鍏�");
+            }
+        }
+        if (WeekMaintenanceStatusEnum.WAIT_CONFIRM.name().equals(maintenanceStatus)) {
+            templateCardEntity.setSub_title_text("宸ュ崟: " + order.getOrderNum() + " 宸蹭繚鍏诲畬鎴愶紝璇疯繘鍏ョ郴缁熺‘璁�");
+        }
+        if (WeekMaintenanceStatusEnum.WAIT_INITIAL_ACCEPTANCE.name().equals(maintenanceStatus)) {
+            templateCardEntity.setSub_title_text("宸ュ崟: " + order.getOrderNum() + " 宸蹭繚鍏诲畬鎴愶紝璇疯繘鍏ョ郴缁熻繘琛屽垵楠屾敹");
+        }
+        if (WeekMaintenanceStatusEnum.WAIT_FINAL_ACCEPTANCE.name().equals(maintenanceStatus)) {
+            templateCardEntity.setSub_title_text("宸ュ崟: " + order.getOrderNum() + " 宸插畬鎴愬垵楠屾敹锛岃杩涘叆绯荤粺杩涜缁堥獙鏀�");
+        }
+
+        List<TemplateCardEntity.HorizontalContent> horizontalContentList = CollectionUtil.newArrayList();
+        TemplateCardEntity.HorizontalContent content1 = new TemplateCardEntity.HorizontalContent();
+        content1.setKeyname("宸ュ崟鍙�");
+        content1.setValue(order.getOrderNum());
+        horizontalContentList.add(content1);
+        TemplateCardEntity.HorizontalContent content2 = new TemplateCardEntity.HorizontalContent();
+        content2.setKeyname("璁惧缂栧彿");
+        content2.setValue(order.getEquipmentCode());
+        horizontalContentList.add(content2);
+
+        //濡傛灉宸ュ崟鐘舵�佷负寰呬繚鍏伙紙娌℃寚瀹氫繚鍏讳汉鐨勬湭棰嗗彇鐘舵�侊級锛屾祦绋嬫槸娌℃湁鍚姩鐨勶紝鏃犳硶鑾峰彇鑺傜偣淇℃伅
+        if (!WeekMaintenanceStatusEnum.WAIT_MAINTENANCE.name().equals(maintenanceStatus)) {
+            WorkTaskDataVo previousFlowInfo = flowTaskService.getPreviousFlowInfo(order.getId());
+            TemplateCardEntity.HorizontalContent content3 = new TemplateCardEntity.HorizontalContent();
+            content3.setKeyname("鍓嶉┍鑺傜偣");
+            content3.setValue(previousFlowInfo.getPreNode());
+            horizontalContentList.add(content3);
+            TemplateCardEntity.HorizontalContent content4 = new TemplateCardEntity.HorizontalContent();
+            content4.setKeyname("褰撳墠鑺傜偣");
+            if (WeekMaintenanceStatusEnum.COMPLETE.name().equals(maintenanceStatus)) {
+                content4.setValue("宸插畬鎴�");
+            } else {
+                content4.setValue(previousFlowInfo.getName());
+            }
+            horizontalContentList.add(content4);
+        }
+
+        if (WeekMaintenanceStatusEnum.UNDER_MAINTENANCE.name().equals(maintenanceStatus)) {
+            if (Objects.isNull(request)) {
+                //鏂板鐨勫伐鍗曪紙鎸囧畾浜嗕繚鍏讳汉锛夈�佹柊棰嗗彇鐨勫伐鍗�
+                TemplateCardEntity.HorizontalContent content = new TemplateCardEntity.HorizontalContent();
+                content.setKeyname("鍙戣捣浜�");
+                SysUser sysUser = sysUserService.getUserByName(order.getCreateBy());
+                content.setValue(sysUser.getRealname());
+                horizontalContentList.add(content);
+            } else {
+                //琚┏鍥炵殑宸ュ崟
+                TemplateCardEntity.HorizontalContent content3 = new TemplateCardEntity.HorizontalContent();
+                TemplateCardEntity.HorizontalContent content4 = new TemplateCardEntity.HorizontalContent();
+                String confirmAssign = request.getAssignee();
+                SysUser confirmUser = sysUserService.getUserByName(confirmAssign);
+                content3.setKeyname("纭浜�");
+                content3.setValue(confirmUser.getRealname());
+                horizontalContentList.add(content3);
+                content4.setKeyname("纭鎰忚");
+                content4.setValue("椹冲洖");
+                horizontalContentList.add(content4);
+            }
+        }
+        if (WeekMaintenanceStatusEnum.WAIT_CONFIRM.name().equals(maintenanceStatus)) {
+            TemplateCardEntity.HorizontalContent content = new TemplateCardEntity.HorizontalContent();
+            content.setKeyname("淇濆吇浜�");
+            SysUser sysUser = sysUserService.getUserByName(order.getOperator());
+            content.setValue(sysUser.getRealname());
+            horizontalContentList.add(content);
+            //TemplateCardEntity.HorizontalContent content = new TemplateCardEntity.HorizontalContent();
+            //List<String> nextAssignee = (List<String>) request.getValues().get("NextAssignee");
+            //String realNameStr = sysUserService.getRealNameStrByUserNameList(nextAssignee);
+            //content.setKeyname("纭浜�");
+            //content.setValue(realNameStr);
+            //horizontalContentList.add(content);
+        }
+        if (WeekMaintenanceStatusEnum.WAIT_INITIAL_ACCEPTANCE.name().equals(maintenanceStatus)) {
+            TemplateCardEntity.HorizontalContent content3 = new TemplateCardEntity.HorizontalContent();
+            //TemplateCardEntity.HorizontalContent content4 = new TemplateCardEntity.HorizontalContent();
+            String confirmAssignee = request.getAssignee();
+            SysUser confirmUser = sysUserService.getUserByName(confirmAssignee);
+            content3.setKeyname("纭浜�");
+            content3.setValue(confirmUser.getRealname());
+            horizontalContentList.add(content3);
+            //List<String> nextAssignee = (List<String>) request.getValues().get("NextAssignee");
+            //String realNameStr = sysUserService.getRealNameStrByUserNameList(nextAssignee);
+            //content4.setKeyname("鍒濋獙浜�");
+            //content4.setValue(realNameStr);
+            //horizontalContentList.add(content4);
+        }
+        if (WeekMaintenanceStatusEnum.WAIT_FINAL_ACCEPTANCE.name().equals(maintenanceStatus)) {
+            TemplateCardEntity.HorizontalContent content3 = new TemplateCardEntity.HorizontalContent();
+            //TemplateCardEntity.HorizontalContent content4 = new TemplateCardEntity.HorizontalContent();
+            String initialAssignee = request.getAssignee();
+            SysUser initialUser = sysUserService.getUserByName(initialAssignee);
+            content3.setKeyname("鍒濋獙浜�");
+            content3.setValue(initialUser.getRealname());
+            horizontalContentList.add(content3);
+            //List<String> nextAssignee = (List<String>) request.getValues().get("NextAssignee");
+            //String realNameStr = sysUserService.getRealNameStrByUserNameList(nextAssignee);
+            //content4.setKeyname("缁堥獙浜�");
+            //content4.setValue(realNameStr);
+            //horizontalContentList.add(content4);
+        }
+
+        templateCardEntity.setHorizontal_content_list(horizontalContentList);
+
+        TemplateCardEntity.CardAction cardAction = new TemplateCardEntity.CardAction();
+        cardAction.setType(1);
+        cardAction.setUrl(cardActionUrl);
+        templateCardEntity.setCard_action(cardAction);
+
+        wechatEnterpriseService.sendTemplateCardMsg(templateCard, true);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    @EquipmentHistoryLog(operationTag = EquipmentOperationTagEnum.WEEK_MAINTENANCE, businessTable = "eam_week_maintenance_order")
+    public EamWeekMaintenanceOrder approval(EamWeekMaintenanceRequest request) {
+        EamWeekMaintenanceOrder entity = eamWeekMaintenanceOrderMapper.selectById(request.getId());
+        if (entity == null) {
+            throw new JeecgBootException("瀹℃壒鐨勬暟鎹凡鍒犻櫎锛岃鍒锋柊閲嶈瘯锛�");
+        }
+        // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛
+        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
+        if (user == null || StrUtil.isBlank(user.getId())) {
+            throw new JeecgBootException("鏈幏鍙栧埌鐧诲綍鐢ㄦ埛锛岃閲嶆柊鐧诲綍鍚庡啀璇曪紒");
+        }
+        request.setAssignee(user.getUsername());
+        // 鑾峰彇娴佺▼涓氬姟璁板綍
+        FlowMyBusiness flowMyBusiness = flowMyBusinessService.getFlowMyBusiness(request.getInstanceId());
+        if (flowMyBusiness == null) {
+            throw new JeecgBootException("娴佺▼瀹炰緥涓嶅瓨鍦紝璇峰埛鏂板悗閲嶈瘯锛�");
+        }
+
+        boolean userAuthorized = isUserAuthorized(flowMyBusiness, user);
+        if (!userAuthorized) {
+            throw new JeecgBootException("鐢ㄦ埛鏃犳潈鎿嶄綔姝や换鍔★紝璇峰埛鏂板悗閲嶈瘯锛�");
+        }
+        // 璁ら浠诲姟
+        if (!claimTask(flowMyBusiness.getTaskId(), user)) {
+            throw new JeecgBootException("浠诲姟涓嶅瓨鍦ㄣ�佸凡瀹屾垚鎴栧凡琚粬浜鸿棰嗭紒");
+        }
+
+        EamEquipment equipment = eamEquipmentService.getById(entity.getEquipmentId());
+        if (equipment == null) {
+            throw new JeecgBootException("璁惧涓嶅瓨鍦紝璇锋鏌ワ紒");
+        }
+
+        WeekMaintenanceStatusEnum status = WeekMaintenanceStatusEnum.getInstance(entity.getMaintenanceStatus());
+        if (status == null) {
+            return null;
+        }
+        //娴佺▼鍙橀噺
+        Map<String, Object> values = new HashMap<>();
+        switch (status) {
+            case UNDER_MAINTENANCE:
+                //鎵ц瀹屾垚
+                List<UserSelector> userSelectors = sysUserService.selectOperatorList(equipment.getEquipmentCode(), equipment.getOrgId(), BusinessCodeConst.PCR0003);
+                if (CollectionUtil.isEmpty(userSelectors)) {
+                    throw new JeecgBootException("璁惧鏈垎閰嶇粰鐝粍闀匡紝鏃犳硶杩涘叆涓嬬骇瀹℃壒锛�");
+                }
+                List<String> userApprovalList = userSelectors.stream().map(UserSelector::getUsername).collect(Collectors.toList());
+                values.put("dataId", entity.getId());
+                values.put("organization", "鍛ㄤ繚鎵ц缁撴潫");
+                values.put("comment", "鍛ㄤ繚鎵ц缁撴潫");
+                values.put("NextAssignee", userApprovalList);
+                request.setComment("鍛ㄤ繚鎵ц缁撴潫");
+                //璁剧疆entity
+                entity.setMaintenanceStatus(WeekMaintenanceStatusEnum.WAIT_CONFIRM.name());
+                entity.setActualEndTime(new Date());
+                //澶勭悊闄勪欢
+                if (CollectionUtil.isNotEmpty(request.getImageFilesResult())) {
+                    List<FileUploadResult> fileUploadResultList = request.getImageFilesResult();
+                    ObjectMapper mapper = new ObjectMapper();
+                    try {
+                        String referenceFile = mapper.writeValueAsString(fileUploadResultList);
+                        entity.setImageFiles(referenceFile);
+                    } catch (JsonProcessingException e) {
+                        log.error("JSON杞崲澶辫触锛�" + e.getMessage(), e);
+                    }
+                }
+                //澶勭悊璇︽儏
+                if (CollectionUtil.isNotEmpty(request.getTableDetailList())) {
+                    eamWeekMaintenanceOrderDetailService.updateBatchById(request.getTableDetailList());
+                }
+                //鏇存柊璁惧淇濆吇鐘舵��
+                eamEquipmentExtendService.updateEquipmentMaintenanceStatus(entity.getEquipmentId(), EquipmentMaintenanceStatus.WAIT_CONFIRM.name());
+                break;
+            case WAIT_CONFIRM:
+                if (StringUtils.isBlank(equipment.getEquipmentManager())) {
+                    throw new JeecgBootException("璁惧鏈垎閰嶈澶囩鐞嗗憳锛屾棤娉曡繘鍏ヤ笅绾у鎵癸紒");
+                }
+                //鐝粍闀跨‘璁�
+                userApprovalList = new ArrayList<>();
+                userApprovalList.add(equipment.getEquipmentManager());
+                values.put("dataId", entity.getId());
+                values.put("organization", request.getConfirmComment());
+                values.put("comment", request.getConfirmComment());
+                values.put("NextAssignee", userApprovalList);
+                values.put("confirmation", request.getConfirmDealType());
+                request.setComment(request.getConfirmComment());
+                //璁剧疆entity
+                entity.setMaintenanceStatus(WeekMaintenanceStatusEnum.WAIT_INITIAL_ACCEPTANCE.name());
+                entity.setConfirmUser(user.getUsername());
+                entity.setConfirmComment(request.getConfirmComment());
+                entity.setConfirmTime(new Date());
+                entity.setConfirmDealType(request.getConfirmDealType());
+                if (CommonConstant.HAS_CANCLE.equals(request.getConfirmDealType())) {
+                    //椹冲洖
+                    userApprovalList = new ArrayList<>();
+                    userApprovalList.add(entity.getOperator());
+                    values.put("NextAssignee", userApprovalList);
+                    entity.setMaintenanceStatus(WeekMaintenanceStatusEnum.UNDER_MAINTENANCE.name());
+                }
+                //澶勭悊鎶ヤ慨
+                List<EamWeekMaintenanceOrderDetail> collect = request.getTableDetailList().stream().filter((detail) -> CommonConstant.DEFAULT_1.equals(detail.getReportFlag())).collect(Collectors.toList());
+                if (CollectionUtil.isNotEmpty(collect)) {
+                    eamReportRepairService.reportRepairFromMaintenance(equipment.getId(), entity.getOperator(), collect);
+                }
+                break;
+            case WAIT_INITIAL_ACCEPTANCE:
+                userSelectors = sysUserService.selectOperatorList(equipment.getEquipmentCode(), equipment.getOrgId(), BusinessCodeConst.PCR0005);
+                if (CollectionUtil.isEmpty(userSelectors)) {
+                    throw new JeecgBootException("璁惧鏈垎閰嶇粰璁捐兘閮ㄧ鐞嗗憳锛屾棤娉曡繘鍏ヤ笅绾у鎵癸紒");
+                }
+                userApprovalList = userSelectors.stream().map(UserSelector::getUsername).collect(Collectors.toList());
+                values.put("dataId", entity.getId());
+                values.put("organization", request.getInitialAcceptanceComment());
+                values.put("comment", request.getInitialAcceptanceComment());
+                values.put("NextAssignee", userApprovalList);
+                request.setComment(request.getInitialAcceptanceComment());
+                //璁剧疆entity
+                entity.setMaintenanceStatus(WeekMaintenanceStatusEnum.WAIT_FINAL_ACCEPTANCE.name());
+                entity.setInitialAcceptanceUser(user.getUsername());
+                entity.setInitialAcceptanceComment(request.getInitialAcceptanceComment());
+                entity.setInitialAcceptanceTime(new Date());
+                entity.setRandomInspectionFlag(request.getRandomInspectionFlag());
+                //澶勭悊闄勪欢
+                if (CollectionUtil.isNotEmpty(request.getInitialAcceptanceFilesResult())) {
+                    List<FileUploadResult> fileUploadResultList = request.getInitialAcceptanceFilesResult();
+                    ObjectMapper mapper = new ObjectMapper();
+                    try {
+                        String referenceFile = mapper.writeValueAsString(fileUploadResultList);
+                        entity.setInitialAcceptanceFiles(referenceFile);
+                    } catch (JsonProcessingException e) {
+                        log.error("JSON杞崲澶辫触锛�" + e.getMessage(), e);
+                    }
+                }
+                break;
+            case WAIT_FINAL_ACCEPTANCE:
+                values.put("dataId", entity.getId());
+                values.put("organization", request.getFinalAcceptanceComment());
+                values.put("comment", request.getFinalAcceptanceComment());
+                request.setComment(request.getFinalAcceptanceComment());
+                //璁剧疆entity
+                entity.setMaintenanceStatus(WeekMaintenanceStatusEnum.COMPLETE.name());
+                entity.setFinalAcceptanceUser(user.getUsername());
+                entity.setFinalAcceptanceComment(request.getFinalAcceptanceComment());
+                entity.setFinalAcceptanceTime(new Date());
+                entity.setFinalRandomInspectionFlag(request.getFinalRandomInspectionFlag());
+                //澶勭悊闄勪欢
+                if (CollectionUtil.isNotEmpty(request.getFinalAcceptanceFilesResult())) {
+                    List<FileUploadResult> fileUploadResultList = request.getFinalAcceptanceFilesResult();
+                    ObjectMapper mapper = new ObjectMapper();
+                    try {
+                        String referenceFile = mapper.writeValueAsString(fileUploadResultList);
+                        entity.setFinalAcceptanceFiles(referenceFile);
+                    } catch (JsonProcessingException e) {
+                        log.error("JSON杞崲澶辫触锛�" + e.getMessage(), e);
+                    }
+                }
+                //鏇存柊璁惧淇濆吇鐘舵��
+                eamEquipmentExtendService.updateEquipmentMaintenanceStatus(entity.getEquipmentId(), EquipmentMaintenanceStatus.NORMAL.name());
+                break;
+        }
+        request.setValues(values);
+
+        // 瀹屾垚娴佺▼浠诲姟
+        Result result = flowTaskService.complete(request);
+        if (!result.isSuccess()) {
+            throw new JeecgBootException("瀹℃壒澶辫触锛岃鍒锋柊鏌ョ湅锛�");
+        }
+        //淇濆瓨宸ュ崟
+        eamWeekMaintenanceOrderMapper.updateById(entity);
+        //鎺ㄩ�佷紒涓氬井淇℃秷鎭紝宸ュ崟瀹岀粨涓嶅彂閫�
+        if (!WeekMaintenanceStatusEnum.COMPLETE.name().equals(entity.getMaintenanceStatus())) {
+            sendQywxTemplateCardMessage(entity, request);
+        }
+        return entity;
+    }
+
+    @Override
+    public List<EamWeekMaintenanceOrder> selectUnCompleteOrder(String expiredDate) {
+        LambdaQueryWrapper<EamWeekMaintenanceOrder> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.lt(EamWeekMaintenanceOrder::getMaintenanceDate, expiredDate);
+        List<String> unCompleteStatus = new ArrayList<>();
+        unCompleteStatus.add(WeekMaintenanceStatusEnum.WAIT_MAINTENANCE.name());
+        unCompleteStatus.add(WeekMaintenanceStatusEnum.UNDER_MAINTENANCE.name());
+        queryWrapper.in(EamWeekMaintenanceOrder::getMaintenanceStatus, unCompleteStatus);
+        queryWrapper.orderByAsc(EamWeekMaintenanceOrder::getMaintenanceDate);
+        return eamWeekMaintenanceOrderMapper.selectList(queryWrapper);
+    }
+
+    @Override
+    public List<EquipmentMaintenanceStatistics> equipmentMaintenanceStatistics(String productionId, LocalDate firstOfMonth, LocalDate today) {
+        if (StringUtils.isNotBlank(productionId)) {
+            //杞﹂棿缂栫爜涓嶄负绌�
+            List<String> productIds = mdcProductionService.recursionChildren(productionId);
+            if (CollectionUtil.isEmpty(productIds)) {
+                return Collections.emptyList();
+            }
+            List<EquipmentMaintenanceStatistics> list = this.baseMapper.equipmentMaintenanceStatistics(productIds, firstOfMonth.toString(), today.plusDays(1).toString());
+            if (CollectionUtil.isEmpty(list)) {
+                return Collections.emptyList();
+            }
+            return list;
+        }
+        List<EquipmentMaintenanceStatistics> list = this.baseMapper.equipmentMaintenanceStatistics(null, firstOfMonth.toString(), today.plusDays(1).toString());
+        if (CollectionUtil.isEmpty(list)) {
+            return Collections.emptyList();
+        }
+        return list;
+    }
+
+    @Override
+    public List<EamWeekMaintenanceOrder> selectUnReceivedMaintenanceOrder() {
+        return this.baseMapper.selectUnReceivedMaintenanceOrder();
+    }
+
+    private boolean isUserAuthorized(FlowMyBusiness flowMyBusiness, LoginUser user) {
+        List<String> todoUsers = JSON.parseArray(flowMyBusiness.getTodoUsers(), String.class);
+        return todoUsers != null && todoUsers.contains(user.getUsername());
+    }
+
+    private boolean claimTask(String taskId, LoginUser user) {
+        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
+        if (task == null) {
+            return false;
+        }
+        if (task.getAssignee() != null && !task.getAssignee().equals(user.getUsername())) {
+            return false;
+        }
+        taskService.claim(taskId, user.getUsername());
         return true;
     }
 
@@ -289,7 +754,7 @@
     public List<String> flowCandidateUsernamesOfTask(String taskNameId, Map<String, Object> values) {
         //涓氬姟鏄惁骞查娴佺▼锛屼笟鍔″共棰勶紝娴佺▼骞查锛屾寚瀹氫汉鍛樿繘琛屽鐞�
         //鑾峰彇涓嬩竴姝ュ鐞嗕汉
-        Object object=values.get("NextAssignee");
+        Object object = values.get("NextAssignee");
         return (List<String>) object;
     }
 }

--
Gitblit v1.9.3