|
@@ -0,0 +1,135 @@
|
|
|
+package com.rongwei.zhsw.system.controller;
|
|
|
+
|
|
|
+import com.rongwei.rwcommon.base.R;
|
|
|
+import com.rongwei.zhsw.system.dao.SwEnterpriseConfigInfoDao;
|
|
|
+import com.rongwei.zhsw.system.service.impl.SwEnterpriseConfigInfoServiceImpl;
|
|
|
+import com.rongwei.zhsw.system.utils.WxMessageUtils;
|
|
|
+import com.rongwe.zhsw.system.domain.SwEnterpriseConfigInfoDo;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/officialAccountReminder")
|
|
|
+public class WeChatOfficialController {
|
|
|
+ @Autowired
|
|
|
+ private SwEnterpriseConfigInfoServiceImpl swEnterpriseConfigInfoService;
|
|
|
+
|
|
|
+ @PostMapping("/sendRepairMessage")
|
|
|
+ public R sendRepairMessage(@RequestBody Map<String, Object> dataInfo) {
|
|
|
+ try {
|
|
|
+ log.info("开始处理报修消息提醒,消息内容:{}", dataInfo);
|
|
|
+
|
|
|
+ // 参数验证
|
|
|
+ if (dataInfo == null || !dataInfo.containsKey("mbtype") || !dataInfo.containsKey("receiverrole")) {
|
|
|
+ return R.error("请求参数不完整");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> mbtypeMap = (Map<String, String>) dataInfo.get("mbtype");
|
|
|
+ Map<String, String> receiverRoleMap = (Map<String, String>) dataInfo.get("receiverrole");
|
|
|
+
|
|
|
+ if (mbtypeMap == null || !mbtypeMap.containsKey("value") ||
|
|
|
+ receiverRoleMap == null || !receiverRoleMap.containsKey("value")) {
|
|
|
+ return R.error("请求参数格式不正确");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取企业配置信息
|
|
|
+ SwEnterpriseConfigInfoDo config = swEnterpriseConfigInfoService.getBaseMapper().getEnterpriseConfig();
|
|
|
+ if (config == null) {
|
|
|
+ return R.error("未找到企业配置信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取模板ID
|
|
|
+ String mbtype = mbtypeMap.get("value");
|
|
|
+ JSONObject templateConfig = JSON.parseObject(config.getTemplateconfiguration());
|
|
|
+ String templateId = templateConfig.getString(mbtype);
|
|
|
+ if (templateId == null) {
|
|
|
+ return R.error("未找到对应的模板ID");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取接收者角色
|
|
|
+ String receiverRole = receiverRoleMap.get("value");
|
|
|
+ List<String> roleCodes = Arrays.asList(receiverRole.split(","));
|
|
|
+
|
|
|
+ // 获取用户OpenID
|
|
|
+ String openIds = swEnterpriseConfigInfoService.getBaseMapper().getUserOpenIdsByRoles(roleCodes);
|
|
|
+ if (openIds == null || openIds.isEmpty()) {
|
|
|
+ return R.error("未找到符合条件的接收者");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建模板消息数据
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+
|
|
|
+ Map<String, String> firstMap = new HashMap<>();
|
|
|
+ firstMap.put("value", ((Map<String, String>) dataInfo.get("first")).get("value"));
|
|
|
+ data.put("first", firstMap);
|
|
|
+ Map<String, String> keyword1Map = new HashMap<>();
|
|
|
+ keyword1Map.put("value", ((Map<String, String>) dataInfo.get("keyword1")).get("value"));
|
|
|
+ data.put("keyword1", keyword1Map);
|
|
|
+ Map<String, String> keyword2Map = new HashMap<>();
|
|
|
+ keyword2Map.put("value", ((Map<String, String>) dataInfo.get("keyword2")).get("value"));
|
|
|
+ data.put("keyword2", keyword2Map);
|
|
|
+ Map<String, String> keyword3Map = new HashMap<>();
|
|
|
+ keyword3Map.put("value", ((Map<String, String>) dataInfo.get("keyword3")).get("value"));
|
|
|
+ data.put("keyword3", keyword3Map);
|
|
|
+ Map<String, String> keyword4Map = new HashMap<>();
|
|
|
+ keyword4Map.put("value", ((Map<String, String>) dataInfo.get("keyword4")).get("value"));
|
|
|
+ data.put("keyword4", keyword4Map);
|
|
|
+ Map<String, String> remarkMap = new HashMap<>();
|
|
|
+ remarkMap.put("value", "请尽快处理!");
|
|
|
+ data.put("remark", remarkMap);
|
|
|
+ // 发送消息给所有接收者
|
|
|
+ String[] openIdArray = openIds.split(",");
|
|
|
+ boolean allSuccess = true;
|
|
|
+ int successCount = 0;
|
|
|
+ int failCount = 0;
|
|
|
+
|
|
|
+ for (String openId : openIdArray) {
|
|
|
+ try {
|
|
|
+ boolean result = WxMessageUtils.sendTemplateMessage(
|
|
|
+ config.getGzhapppid(),
|
|
|
+ config.getGzappsecret(),
|
|
|
+ templateId,
|
|
|
+ openId,
|
|
|
+ data
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!result) {
|
|
|
+ allSuccess = false;
|
|
|
+ failCount++;
|
|
|
+ log.error("发送消息给用户 {} 失败", openId);
|
|
|
+ } else {
|
|
|
+ successCount++;
|
|
|
+ log.info("成功发送消息给用户: {}", openId);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ allSuccess = false;
|
|
|
+ failCount++;
|
|
|
+ log.error("发送消息给用户 {} 时发生异常: {}", openId, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (allSuccess) {
|
|
|
+ log.info("所有消息发送成功,共发送{}条", successCount);
|
|
|
+ return R.ok("所有消息发送成功,共发送" + successCount + "条");
|
|
|
+ } else {
|
|
|
+ String message = String.format("部分消息发送失败,成功:%d条,失败:%d条,请检查日志", successCount, failCount);
|
|
|
+ log.warn(message);
|
|
|
+ return R.error(message);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发送报修消息提醒失败", e);
|
|
|
+ return R.error("发送报修消息提醒失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|