From db380a1ef48fd0a9eabd1615581304bd712c29dc Mon Sep 17 00:00:00 2001 From: liushaowen <565850092@qq.com> Date: Fri, 23 Oct 2020 11:01:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E8=AE=A2=E9=98=85=20?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E8=8E=B7=E5=8F=96templateId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/form/WxSubscribeMessageFormDTO.java | 5 - .../epmet/dao/WxmpResiUserSubscribeDao.java | 10 + .../epmet/dao/WxmpWorkUserSubscribeDao.java | 11 + .../epmet/exception/WxSubscribeException.java | 13 +- .../service/impl/WxmpMessageServiceImpl.java | 399 +++++++++--------- .../mapper/WxmpResiUserSubscribeDao.xml | 13 + .../mapper/WxmpWorkUserSubscribeDao.xml | 12 + 7 files changed, 263 insertions(+), 200 deletions(-) diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java index f712c2106f..1dc30e2f8c 100644 --- a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java @@ -48,9 +48,4 @@ public class WxSubscribeMessageFormDTO implements Serializable { @NotBlank(message = "消息内容不能为空") private String messageContent; - /** - * 所需下发的订阅模板id - */ - @NotBlank(message = "模板id不能为空") - private String templateId; } diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java index d2ec0c8443..c1a6e83d2f 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java @@ -64,4 +64,14 @@ public interface WxmpResiUserSubscribeDao extends BaseDao msgList) { - logger.info("待发送订阅消息数量:{}", msgList.size()); - int succecssCount = 0; - for (WxSubscribeMessageFormDTO msg : msgList) { - try { - String userId = msg.getUserId(); - String clientType = msg.getClientType(); - String customerId = msg.getCustomerId(); - String templateId = msg.getTemplateId(); - String openId = null; - //通过userId获取openId - try { - if (WxmpMessageConstant.RESI.equals(clientType)) { - UserBasicInfoFormDTO userBasicInfoFormDTO = new UserBasicInfoFormDTO(); - userBasicInfoFormDTO.setUserId(userId); - openId = epmetUserOpenFeignClient.getUserBasicInfo(userBasicInfoFormDTO).getData().getOpenId(); - } else if (WxmpMessageConstant.WORK.equals(clientType)) { - StaffBasicInfoFormDTO staffBasicInfoFormDTO = new StaffBasicInfoFormDTO(); - staffBasicInfoFormDTO.setStaffId(userId); - openId = epmetUserOpenFeignClient.getStaffBasicInfo(staffBasicInfoFormDTO).getData().getOpenId(); - } else { - throw new WxSubscribeException("clientType有误",openId); - } - }catch (Exception e){ - throw new WxSubscribeException("连接User服务失败",""); - } + /** + * @param msgList + * @return void + * @Description 发送订阅消息 + * @Author liushaowen + * @Date 2020/10/21 15:34 + */ + @Override + public void sendWxSubscribeMessage(List msgList) { + logger.info("待发送订阅消息数量:{}", msgList.size()); + int succecssCount = 0; + for (WxSubscribeMessageFormDTO msg : msgList) { + try { + String userId = msg.getUserId(); + String clientType = msg.getClientType(); + String customerId = msg.getCustomerId(); + String templateId = null; + String openId = null; + //通过userId获取openId + try { + if (WxmpMessageConstant.RESI.equals(clientType)) { + UserBasicInfoFormDTO userBasicInfoFormDTO = new UserBasicInfoFormDTO(); + userBasicInfoFormDTO.setUserId(userId); + openId = epmetUserOpenFeignClient.getUserBasicInfo(userBasicInfoFormDTO).getData().getOpenId(); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + StaffBasicInfoFormDTO staffBasicInfoFormDTO = new StaffBasicInfoFormDTO(); + staffBasicInfoFormDTO.setStaffId(userId); + openId = epmetUserOpenFeignClient.getStaffBasicInfo(staffBasicInfoFormDTO).getData().getOpenId(); + } else { + throw new WxSubscribeException("clientType有误", "", openId); + } + } catch (Exception e) { + throw new WxSubscribeException("连接User服务失败", "", ""); + } - if (StringUtils.isBlank(openId)){ - throw new WxSubscribeException("openId获取失败",""); + if (StringUtils.isBlank(openId)) { + throw new WxSubscribeException("openId获取失败", "", ""); } - //获取accessToken - StringBuilder key = new StringBuilder(msg.getCustomerId()).append(":").append(msg.getClientType()); - Map authorizerRefreshToken = new HashMap<>(); + //获取accessToken + StringBuilder key = new StringBuilder(msg.getCustomerId()).append(":").append(msg.getClientType()); + Map authorizerRefreshToken = new HashMap<>(); try { - authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(key.toString()); - }catch (Exception e){ - throw new WxSubscribeException("连接缓存服务器失败",openId); - } - String accessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN); - if (StringUtils.isBlank(accessToken)) { - throw new WxSubscribeException("accessToken获取失败",openId); - } + authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(key.toString()); + } catch (Exception e) { + throw new WxSubscribeException("连接缓存服务器失败", "", openId); + } + String accessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN); + if (StringUtils.isBlank(accessToken)) { + throw new WxSubscribeException("accessToken获取失败", "", openId); + } - //判断用户是否有次数 - Integer count = null; - if (WxmpMessageConstant.RESI.equals(clientType)) { - count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, templateId, customerId); - } else if (WxmpMessageConstant.WORK.equals(clientType)) { - count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, templateId, customerId); - } - if (count == null) { - //用户未订阅 - throw new WxSubscribeException("用户未订阅",openId); - } - if (count == 0) { - throw new WxSubscribeException("用户可用额度不足",openId); - } + //获取模板id + if (WxmpMessageConstant.RESI.equals(clientType)) { + templateId = wxmpResiUserSubscribeDao.getResiSubscribeTemplateId(openId, templateId, customerId); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + templateId = wxmpWorkUserSubscribeDao.getWorkSubscribeTemplateId(openId, templateId, customerId); + } + if (StringUtils.isBlank(templateId)) { + throw new WxSubscribeException("获取模板id失败", "", openId); + } + + //判断用户是否有次数 + Integer count = null; + if (WxmpMessageConstant.RESI.equals(clientType)) { + count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, templateId, customerId); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, templateId, customerId); + } + if (count == null) { + //用户未订阅 + throw new WxSubscribeException("用户未订阅", templateId, openId); + } + if (count == 0) { + throw new WxSubscribeException("用户可用额度不足", templateId, openId); + } - //发送消息 - JSONObject jsonObject = new JSONObject(); - JSONObject data = new JSONObject(); - //必填项 - jsonObject.put(WxmpMessageConstant.ACCESS_TOKEN, accessToken); - jsonObject.put(WxmpMessageConstant.TOUSER, openId); - jsonObject.put(WxmpMessageConstant.TEMPLATE_ID, templateId); - data.put(WxmpMessageConstant.TITLE, new JSONObject().put("value", ("您有一条" + msg.getBehaviorType()).substring(0,WxmpMessageConstant.TITLE_LIMIT))); - data.put(WxmpMessageConstant.MESSAGE_CONTENT, new JSONObject().put("value", msg.getMessageContent().substring(0,WxmpMessageConstant.MESSAGE_CONTENT_LIMIT))); - data.put(WxmpMessageConstant.MESSAGE_TIME, new JSONObject().put("value", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()))); - jsonObject.put(WxmpMessageConstant.DATA, data); - EnvEnum envEnum = EnvEnum.getCurrentEnv(); - //选填项 - if (WxmpMessageConstant.RESI.equals(clientType)) { - jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_RESI); - } else if (WxmpMessageConstant.WORK.equals(clientType)) { - jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_WORK); - } - //开发环境 - if ("dev".equals(envEnum.getCode())){ - jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_DEV); - } - //测试环境 - if ("test".equals(envEnum.getCode())){ - jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_TEST); - } + //发送消息 + JSONObject jsonObject = new JSONObject(); + JSONObject data = new JSONObject(); + //必填项 + jsonObject.put(WxmpMessageConstant.ACCESS_TOKEN, accessToken); + jsonObject.put(WxmpMessageConstant.TOUSER, openId); + jsonObject.put(WxmpMessageConstant.TEMPLATE_ID, templateId); + data.put(WxmpMessageConstant.TITLE, new JSONObject().put("value", ("您有一条" + msg.getBehaviorType()).substring(0, WxmpMessageConstant.TITLE_LIMIT))); + data.put(WxmpMessageConstant.MESSAGE_CONTENT, new JSONObject().put("value", msg.getMessageContent().substring(0, WxmpMessageConstant.MESSAGE_CONTENT_LIMIT))); + data.put(WxmpMessageConstant.MESSAGE_TIME, new JSONObject().put("value", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()))); + jsonObject.put(WxmpMessageConstant.DATA, data); + EnvEnum envEnum = EnvEnum.getCurrentEnv(); + //选填项 + if (WxmpMessageConstant.RESI.equals(clientType)) { + jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_RESI); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_WORK); + } + //开发环境 + if ("dev".equals(envEnum.getCode())) { + jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_DEV); + } + //测试环境 + if ("test".equals(envEnum.getCode())) { + jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_TEST); + } - String resultStr = HttpClientManager.getInstance().sendPostByJSON(WxmpMessageConstant.SEND_MESSAGE + accessToken, JSON.toJSONString(jsonObject)).getData(); - Map resultMap = JSON.parseObject(resultStr, Map.class); - Object errcode = resultMap.get(WxmpMessageConstant.ERR_CODE); - if (errcode.equals(NumConstant.ZERO)) { - //发送成功 + String resultStr = HttpClientManager.getInstance().sendPostByJSON(WxmpMessageConstant.SEND_MESSAGE + accessToken, JSON.toJSONString(jsonObject)).getData(); + Map resultMap = JSON.parseObject(resultStr, Map.class); + Object errcode = resultMap.get(WxmpMessageConstant.ERR_CODE); + if (errcode.equals(NumConstant.ZERO)) { + //发送成功 - //订阅条数-1 - int decrease = 0; - if (WxmpMessageConstant.RESI.equals(clientType)) { - decrease = wxmpResiUserSubscribeDao.decreaseResiSubscribeCount(openId, templateId, customerId, 1); - } else if (WxmpMessageConstant.WORK.equals(clientType)) { - decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, templateId, customerId, 1); - } - if (decrease == 0) { - logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg)); - } - //存表 - int saveRes = wxmpMsgSendRecordService.saveRecord(initRecord(msg,openId,WxmpMessageConstant.SUCCESS)); - if (saveRes == 0) { - logger.error("消息{}发送成功但存入记录表失败", JSON.toJSONString(msg)); - } - } else { - //发送失败 - //用户拒绝,需清空订阅表条数,修改订阅状态 - if (errcode.equals(WxmpMessageConstant.USER_REFUSED)) { - int clear = 0; - if (WxmpMessageConstant.RESI.equals(clientType)) { - clear = wxmpResiUserSubscribeDao.clearResiSubscribeCount(openId, templateId, customerId); - } else if (WxmpMessageConstant.WORK.equals(clientType)) { - clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, templateId, customerId); - } - if (clear == 0) { - logger.error("消息{}发送失败且清空订阅条数失败", JSON.toJSONString(msg)); - } - } + //订阅条数-1 + int decrease = 0; + if (WxmpMessageConstant.RESI.equals(clientType)) { + decrease = wxmpResiUserSubscribeDao.decreaseResiSubscribeCount(openId, templateId, customerId, 1); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, templateId, customerId, 1); + } + if (decrease == 0) { + logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg)); + } + //存表 + int saveRes = wxmpMsgSendRecordService.saveRecord(initRecord(msg, templateId, openId, WxmpMessageConstant.SUCCESS)); + if (saveRes == 0) { + logger.error("消息{}发送成功但存入记录表失败", JSON.toJSONString(msg)); + } + } else { + //发送失败 + //用户拒绝,需清空订阅表条数,修改订阅状态 + if (errcode.equals(WxmpMessageConstant.USER_REFUSED)) { + int clear = 0; + if (WxmpMessageConstant.RESI.equals(clientType)) { + clear = wxmpResiUserSubscribeDao.clearResiSubscribeCount(openId, templateId, customerId); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, templateId, customerId); + } + if (clear == 0) { + logger.error("消息{}发送失败且清空订阅条数失败", JSON.toJSONString(msg)); + } + } - //抛出错误 - throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),openId); - } + //抛出错误 + throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),templateId, openId); + } - succecssCount++; - } catch (Exception e) { - String errMsg = e.getMessage(); - //ValidateException错误信息为getMsg + succecssCount++; + } catch (Exception e) { + String errMsg = e.getMessage(); + //ValidateException错误信息为getMsg // if (StringUtils.isBlank(errMsg) && e instanceof ValidateException) { // errMsg = ((ValidateException) e).getMsg(); // } - if (e instanceof WxSubscribeException){ - //存表 - WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = initRecord(msg, ((WxSubscribeException) e).getOpenId(), WxmpMessageConstant.ERROR); - wxmpMsgSendRecordEntity.setReason(errMsg); - int saveRes = wxmpMsgSendRecordService.saveRecord(wxmpMsgSendRecordEntity); - if (saveRes == 0) { - logger.error("消息{}发送失败且存入记录表失败", JSON.toJSONString(msg)); - } - } + if (e instanceof WxSubscribeException) { + //存表 + WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = initRecord(msg, ((WxSubscribeException) e).getTemplateId(), ((WxSubscribeException) e).getOpenId(), WxmpMessageConstant.ERROR); + wxmpMsgSendRecordEntity.setReason(errMsg); + int saveRes = wxmpMsgSendRecordService.saveRecord(wxmpMsgSendRecordEntity); + if (saveRes == 0) { + logger.error("消息{}发送失败且存入记录表失败", JSON.toJSONString(msg)); + } + } + + logger.error("消息:{}发送失败,原因是:{}", JSON.toJSONString(msg), errMsg); + continue; + } + } + logger.info("{}条消息中的{}条发送成功", msgList.size(), succecssCount); + } - logger.error("消息:{}发送失败,原因是:{}", JSON.toJSONString(msg), errMsg); - continue; - } - } - logger.info("{}条消息中的{}条发送成功", msgList.size(), succecssCount); - } - //初始化记录对象 - private WxmpMsgSendRecordEntity initRecord(WxSubscribeMessageFormDTO msg, String openId, String status){ - WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = new WxmpMsgSendRecordEntity(); - wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId()); - wxmpMsgSendRecordEntity.setClientType(msg.getClientType()); - wxmpMsgSendRecordEntity.setTemplateId(msg.getTemplateId()); - wxmpMsgSendRecordEntity.setUserId(msg.getUserId()); - wxmpMsgSendRecordEntity.setWxOpenId(openId); - wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType()); - wxmpMsgSendRecordEntity.setTitle("您有一条" + msg.getBehaviorType()); - wxmpMsgSendRecordEntity.setMessageContent(msg.getMessageContent()); - wxmpMsgSendRecordEntity.setMessageTime(new Date()); - wxmpMsgSendRecordEntity.setResult(status); - return wxmpMsgSendRecordEntity; - } + //初始化记录对象 + private WxmpMsgSendRecordEntity initRecord(WxSubscribeMessageFormDTO msg, String templateId, String openId, String status) { + WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = new WxmpMsgSendRecordEntity(); + wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId()); + wxmpMsgSendRecordEntity.setClientType(msg.getClientType()); + wxmpMsgSendRecordEntity.setTemplateId(templateId); + wxmpMsgSendRecordEntity.setUserId(msg.getUserId()); + wxmpMsgSendRecordEntity.setWxOpenId(openId); + wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType()); + wxmpMsgSendRecordEntity.setTitle("您有一条" + msg.getBehaviorType()); + wxmpMsgSendRecordEntity.setMessageContent(msg.getMessageContent()); + wxmpMsgSendRecordEntity.setMessageTime(new Date()); + wxmpMsgSendRecordEntity.setResult(status); + return wxmpMsgSendRecordEntity; + } } diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml index 2b97a22eec..2a1df8a057 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml +++ b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml @@ -37,4 +37,17 @@ and wx_open_id = #{openId} + + diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml index 4673179dee..efa31174f9 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml +++ b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml @@ -34,4 +34,16 @@ and template_id = #{templateId} and wx_open_id = #{openId} +