Browse Source

给所有售后发送转售后的故障公众号消息

tanbin 1 year ago
parent
commit
d466c18c46

+ 65 - 5
device-api-service/src/main/java/com/xy/service/DeviceMqttConsumerImpl.java

@@ -5,6 +5,7 @@ import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DatePattern;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.map.MapUtil;
+import cn.hutool.core.util.BooleanUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
@@ -232,7 +233,7 @@ public class DeviceMqttConsumerImpl implements DeviceMqttConsumer {
                 Long minuteNum = config.getMinuteNum();
                 Integer faultLevel = config.getFaultLevel();
                 Long monitorId = config.getMonitorId();
-                Boolean toAfterSale = config.getToAfterSale();
+                Boolean toAfterSale = BooleanUtil.isTrue(config.getToAfterSale());
                 // 获取当前时间
                 Date now = DateUtil.date();
 
@@ -266,10 +267,9 @@ public class DeviceMqttConsumerImpl implements DeviceMqttConsumer {
                         }
                         //累加次数
                         deviceFaultInfo.setEventNum(deviceFaultInfo.getEventNum() + 1).setMonitorId(monitorId);
-
                         deviceFaultInfoService.updateById(deviceFaultInfo);
                     } else {
-                        deviceFaultInfoService.save(new DeviceFaultInfo()
+                        deviceFaultInfo = new DeviceFaultInfo()
                                 .setMercId(mercId)
                                 .setDeviceId(deviceId)
                                 .setDescribes(msg)
@@ -277,10 +277,14 @@ public class DeviceMqttConsumerImpl implements DeviceMqttConsumer {
                                 .setEventCode(code)
                                 .setEventNum(count.intValue())
                                 .setMonitorId(monitorId)
-                                .setToAfterSale(toAfterSale)
-                        );
+                                .setToAfterSale(toAfterSale);
+                        deviceFaultInfoService.save(deviceFaultInfo);
                         deviceInfoService.updateById(new DeviceInfo().setDeviceId(deviceId).setFaultLevel(faultLevel));
                     }
+                    //给售后推送故障信息
+                    if (toAfterSale.booleanValue()) {
+                        deviceFaultPushMsg(deviceEventMsg);
+                    }
                     //已按最大故障级别排序,所以满足条件就退出循环
                     break;
                 }
@@ -290,6 +294,62 @@ public class DeviceMqttConsumerImpl implements DeviceMqttConsumer {
         }
     }
 
+    /**
+     * 故障推送给所有售后
+     *
+     * @param deviceEventMsg
+     */
+    public void deviceFaultPushMsg(DeviceEventMsgDto.Vo deviceEventMsg) {
+        if (deviceEventMsg != null) {
+            Long deviceId = deviceEventMsg.getDeviceId();
+            Long mercId = deviceEventMsg.getMercId();
+            LocalDateTime createTime = deviceEventMsg.getCreateTime();
+            String msg = deviceEventMsg.getMsg();
+            Long configId = MsgConfigId.DEVICE_EXCEPTION.getId();
+            MsgConfigDto.Vo msgConfig = R.feignCheckData(msgSendApiService.getMsgConfig(new MsgConfigDto.Vo().setId(configId)));
+            List<MsgConfigTestDto.BizParam> bizParams = R.feignCheckData(msgSendApiService.getBizParamByMsgConfig(new MsgConfigTestDto.MsgConfig().setConfigId(configId)));
+            if (CollUtil.isNotEmpty(bizParams) && msgConfig != null) {
+                List<MsgConfigTestDto.BizData> bizDataList = BeanUtil.copyToList(bizParams, MsgConfigTestDto.BizData.class);
+                List<MsgConfigTestDto.BizData> sendList = new ArrayList<>();
+                DeviceInfo deviceInfo = deviceInfoService.getById(deviceId);
+                String deviceName = deviceInfo.getDeviceName();
+                if (StrUtil.isEmpty(deviceName)) {
+                    deviceName = deviceId + "";
+                } else {
+                    deviceName = deviceName + "(" + deviceId + ")";
+                }
+                //TODO 获取售后及售后创建的角色用户
+                UserInfoDto.SaleBackUsersVO saleBackUsersVO = R.feignCheckData(userInfoService.getSaleBackUsers());
+                List<String> mpOpenIds = saleBackUsersVO.getMpOpenIds();
+                if (CollUtil.isNotEmpty(mpOpenIds)) {
+                    Set<String> mpIds = new HashSet<>(mpOpenIds);
+                    for (MsgConfigTestDto.BizData b : bizDataList) {
+                        String channelType = b.getChannelType();
+                        if (Integer.valueOf(channelType).intValue() == ChannelType.OFFICIAL_ACCOUNT.getCode().intValue()) {
+                            //微信公众号
+                            //扩展参数
+                            MsgConfigTestDto.BizExtraParam bizExtraParam = new MsgConfigTestDto.BizExtraParam();
+                            Map<String, Object> params = MapUtil.newHashMap();
+                            params.put("keyword1", deviceName);
+                            params.put("keyword2", msg);
+                            params.put("keyword3", DateUtil.format(createTime, DatePattern.NORM_DATETIME_PATTERN));
+
+                            //商户管理员发
+                            b.setReceivers(mpIds);
+                            b.setTemplateParams(params);
+                            b.setExtraParam(bizExtraParam);
+                            sendList.add(b);
+                        }
+                    }
+                }
+                //是否可推送条件
+                if (CollUtil.isNotEmpty(sendList)) {
+                    msgSendApiService.sendByMsgConfig(new MsgConfigTestDto.SendByMsgConfig().setConfigId(configId).setBizDataList(sendList));
+                }
+            }
+        }
+    }
+
 
     /**
      * 设备联网状态