Browse Source

发送订阅消息 初版

dev_shibei_match
liushaowen 5 years ago
parent
commit
f4a64a8999
  1. 4
      epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpMsgSendRecordDTO.java
  2. 80
      epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java
  3. 10
      epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java
  4. 6
      epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java
  5. 50
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/constant/WxmpMessageConstant.java
  6. 11
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/WxmpMessageController.java
  7. 5
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpMsgSendRecordDao.java
  8. 39
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java
  9. 40
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpWorkUserSubscribeDao.java
  10. 2
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/entity/WxmpMsgSendRecordEntity.java
  11. 32
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/exception/WxSubscribeException.java
  12. 30
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/redis/WxmpMessageRedis.java
  13. 15
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMessageService.java
  14. 12
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMsgSendRecordService.java
  15. 2
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpResiUserSubscribeService.java
  16. 208
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMessageServiceImpl.java
  17. 7
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMsgSendRecordServiceImpl.java
  18. 44
      epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpMsgSendRecordDao.xml
  19. 34
      epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml
  20. 33
      epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml

4
epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpMsgSendRecordDTO.java

@ -81,7 +81,7 @@ public class WxmpMsgSendRecordDTO implements Serializable {
/**
* 消息时间 消息时间
*/
private Date messageTitme;
private Date messageTime;
/**
* 发送结果(成功success 失败error)
@ -123,4 +123,4 @@ public class WxmpMsgSendRecordDTO implements Serializable {
*/
private Date updatedTime;
}
}

80
epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java

@ -0,0 +1,80 @@
package com.epmet.dto.form;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
/**
* @description: 微信订阅消息FormDTO
* @author: liushaowen
* @date: 2020/10/21 14:29
*/
@Data
public class WxSubscribeMessageFormDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 客户id
*/
@NotBlank(message = "客户id不能为空")
private String customerId;
/**
* 客户端类型 居民端:resi 工作端:work
*/
@NotBlank(message = "客户端类型不能为空")
private String clientType;
/**
* 接收者用户 userId
*/
@NotBlank(message = "接收用户id不能为空")
private String userId;
/**
* 行为类型(存title字段的中间值) 入组申请党员认证等
*/
@NotBlank(message = "行为类型不能为空")
private String behaviorType;
/**
* 消息内容
*/
@NotBlank(message = "消息内容不能为空")
private String messageContent;
/**
* 消息时间
*/
@NotNull(message = "消息时间不能为空")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date messageTime;
/**
* 所需下发的订阅模板id
*/
@NotBlank(message = "模板id不能为空")
private String templateId;
/**
* 点击模板卡片后的跳转页面仅限本小程序内的页面支持带参数,示例index?foo=bar该字段不填则模板无跳转
*/
private String page;
/**
* 跳转小程序类型developer为开发版trial为体验版formal为正式版默认为正式版
*/
private String miniprogramState;
/**
* 进入小程序查看的语言类型支持zh_CN(简体中文)en_US(英文)zh_HK(繁体中文)zh_TW(繁体中文)默认为zh_CN
*/
private String lang;
}

10
epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java

@ -5,6 +5,7 @@ import com.epmet.commons.tools.utils.Result;
import com.epmet.dto.SysSmsDTO;
import com.epmet.dto.form.SendVerificationCodeFormDTO;
import com.epmet.dto.form.UserMessageFormDTO;
import com.epmet.dto.form.WxSubscribeMessageFormDTO;
import com.epmet.dto.result.SendVerificationCodeResultDTO;
import com.epmet.feign.fallback.EpmetMessageOpenFeignClientFallback;
import org.springframework.cloud.openfeign.FeignClient;
@ -67,4 +68,13 @@ public interface EpmetMessageOpenFeignClient {
**/
@PostMapping(value = "message/usermessage/saveusermessagelist", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
Result saveUserMessageList(List<UserMessageFormDTO> msgList);
/**
* @param msgList
* @return com.epmet.commons.tools.utils.Result
* @Author liushaowen
* @Description 发送微信订阅消息
**/
@PostMapping(value = "message/wxmpmessage/sendwxsubscribemessage", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
Result sendWxSubscribeMessage(List<WxSubscribeMessageFormDTO> msgList);
}

6
epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java

@ -6,6 +6,7 @@ import com.epmet.commons.tools.utils.Result;
import com.epmet.dto.SysSmsDTO;
import com.epmet.dto.form.SendVerificationCodeFormDTO;
import com.epmet.dto.form.UserMessageFormDTO;
import com.epmet.dto.form.WxSubscribeMessageFormDTO;
import com.epmet.dto.result.SendVerificationCodeResultDTO;
import com.epmet.feign.EpmetMessageOpenFeignClient;
import org.springframework.stereotype.Component;
@ -45,4 +46,9 @@ public class EpmetMessageOpenFeignClientFallback implements EpmetMessageOpenFeig
public Result saveUserMessageList(List<UserMessageFormDTO> msgList) {
return ModuleUtils.feignConError(ServiceConstant.EPMET_MESSAGE_SERVER, "saveUserMessageList", msgList);
}
@Override
public Result sendWxSubscribeMessage(List<WxSubscribeMessageFormDTO> msgList) {
return ModuleUtils.feignConError(ServiceConstant.EPMET_MESSAGE_SERVER, "sendWxSubscribeMessage", msgList);
}
}

50
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/constant/WxmpMessageConstant.java

@ -0,0 +1,50 @@
package com.epmet.constant;
import java.util.HashMap;
import java.util.Map;
/**
* @description: 微信订阅消息常量
* @author: liushaowen
* @date: 2020/10/21 17:45
*/
public interface WxmpMessageConstant {
String SEND_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";
String ERR_CODE = "errcode";
String ERR_MSG = "errmsg";
int USER_REFUSED = 43101;
String AUTHORIZER_ACCESS_TOKEN = "authorizer_access_token";
String RESI = "resi";
String WORK = "work";
String ACCESS_TOKEN = "access_token";
String TOUSER = "touser";
String TEMPLATE_ID = "template_id";
String PAGE = "page";
String TITLE = "title";
String MESSAGE_CONTENT = "message_content";
String MESSAGE_TIME = "message_time";
String DATA = "data";
String MINIPROGRAM_STATE = "miniprogram_state";
String LANG = "lang";
String SUCCESS = "success";
String ERROR = "error";
}

11
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/WxmpMessageController.java

@ -21,10 +21,13 @@ import com.epmet.commons.tools.security.user.LoginUserUtil;
import com.epmet.commons.tools.utils.Result;
import com.epmet.commons.tools.validator.ValidatorUtils;
import com.epmet.dto.form.WxMsgAuthInfoFormDTO;
import com.epmet.dto.form.WxSubscribeMessageFormDTO;
import com.epmet.service.WxmpMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 微信消息订阅controller
@ -61,5 +64,9 @@ public class WxmpMessageController {
return new Result();
}
}
@PostMapping("sendwxsubscribemessage")
public Result sendWxSubscribeMessage(@RequestBody List<WxSubscribeMessageFormDTO> msgList){
wxmpMessageService.sendWxSubscribeMessage(msgList);
return new Result();
}
}

5
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpMsgSendRecordDao.java

@ -29,5 +29,6 @@ import org.apache.ibatis.annotations.Mapper;
*/
@Mapper
public interface WxmpMsgSendRecordDao extends BaseDao<WxmpMsgSendRecordEntity> {
}
int saveRecord(WxmpMsgSendRecordEntity entity);
}

39
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java

@ -18,8 +18,10 @@
package com.epmet.dao;
import com.epmet.commons.mybatis.dao.BaseDao;
import com.epmet.dto.WxmpResiUserSubscribeDTO;
import com.epmet.entity.WxmpResiUserSubscribeEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 居民端用户订阅模板消息次数记录表
@ -29,5 +31,38 @@ import org.apache.ibatis.annotations.Mapper;
*/
@Mapper
public interface WxmpResiUserSubscribeDao extends BaseDao<WxmpResiUserSubscribeEntity> {
}
/**
* @Description 根据openId获取剩余订阅条数
* @param openId
* @param templateId
* @param customerId
* @return java.lang.Integer
* @Author liushaowen
* @Date 2020/10/22 9:30
*/
Integer getResiSubscribeInfo(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
/**
* @Description 减少订阅条数
* @param openId
* @param customerId
* @param templateId
* @param i 减少的数量
* @return int
* @Author liushaowen
* @Date 2020/10/22 9:38
*/
int decreaseResiSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId,@Param("num") int i);
/**
* @Description 清空订阅条数修改订阅状态
* @param openId
* @param customerId
* @param templateId
* @return int
* @Author liushaowen
* @Date 2020/10/22 13:23
*/
int clearResiSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
}

40
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpWorkUserSubscribeDao.java

@ -20,14 +20,48 @@ package com.epmet.dao;
import com.epmet.commons.mybatis.dao.BaseDao;
import com.epmet.entity.WxmpWorkUserSubscribeEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 工作端用户订阅模板消息有效次数记录表
* 工作端用户订阅模板消息有效次数记录表
*
* @author generator generator@elink-cn.com
* @since v1.0.0 2020-10-21
*/
@Mapper
public interface WxmpWorkUserSubscribeDao extends BaseDao<WxmpWorkUserSubscribeEntity> {
}
/**
* @Description 根据openId获取剩余订阅条数
* @param openId
* @param templateId
* @param customerId
* @return java.lang.Integer
* @Author liushaowen
* @Date 2020/10/22 9:31
*/
Integer getWorkSubscribeInfo(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
/**
* @Description 减少订阅条数
* @param openId
* @param templateId
* @param customerId
* @param i 减少的数量
* @return int
* @Author liushaowen
* @Date 2020/10/22 9:38
*/
int decreaseWorkSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId, @Param("num") int i);
/**
* @Description 清空订阅数修改订阅状态
* @param openId
* @param customerId
* @param templateId
* @return int
* @Author liushaowen
* @Date 2020/10/22 13:22
*/
int clearWorkSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
}

2
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/entity/WxmpMsgSendRecordEntity.java

@ -81,7 +81,7 @@ public class WxmpMsgSendRecordEntity extends BaseEpmetEntity {
/**
* 消息时间 消息时间
*/
private Date messageTitme;
private Date messageTime;
/**
* 发送结果(成功success 失败error)

32
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/exception/WxSubscribeException.java

@ -0,0 +1,32 @@
package com.epmet.exception;
/**
* @description:
* @author: liushaowen
* @date: 2020/10/22 11:07
*/
public class WxSubscribeException extends Exception {
private String openId;
/**
* Constructs a new exception with the specified detail message. The
* cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public WxSubscribeException(String message, String openId) {
super(message);
this.openId = openId;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
}

30
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/redis/WxmpMessageRedis.java

@ -0,0 +1,30 @@
package com.epmet.redis;
import com.epmet.commons.tools.redis.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @description: 微信订阅Redis
* @author: liushaowen
* @date: 2020/10/21 15:28
*/
@Slf4j
@Component
public class WxmpMessageRedis {
@Autowired
private RedisUtils redisUtils;
/**
* @Description 获取刷新
* @param key = epmet:wechartthird:authorizerrefreshtoken:customerId:clientType 前缀+客户ID+客户端类型
* @author zxc
*/
public Map<String,Object> getAuthorizerRefreshToken(String key){
Map<String, Object> result = redisUtils.hGetAll("epmet:wechartthird:authorizerrefreshtoken:" + key);
return result;
}
}

15
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMessageService.java

@ -17,6 +17,10 @@
package com.epmet.service;
import com.epmet.dto.form.WxSubscribeMessageFormDTO;
import java.util.List;
/**
* 微信消息订阅Service
*
@ -26,4 +30,13 @@ package com.epmet.service;
public interface WxmpMessageService {
void saveSysAuthorizeInfo(String customerId, String clientType, String alwaysVisit, String subscribeStatus, String userId);
}
/**
* @Description 发送订阅消息
* @param msgList
* @return void
* @Author liushaowen
* @Date 2020/10/21 15:34
*/
void sendWxSubscribeMessage(List<WxSubscribeMessageFormDTO> msgList);
}

12
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMsgSendRecordService.java

@ -92,4 +92,14 @@ public interface WxmpMsgSendRecordService extends BaseService<WxmpMsgSendRecordE
* @date 2020-10-21
*/
void delete(String[] ids);
}
/**
* 保存
*
* @param entity
* @return int
* @author liushaowen
* @date 2020-10-22
*/
int saveRecord(WxmpMsgSendRecordEntity entity);
}

2
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpResiUserSubscribeService.java

@ -92,4 +92,4 @@ public interface WxmpResiUserSubscribeService extends BaseService<WxmpResiUserSu
* @date 2020-10-21
*/
void delete(String[] ids);
}
}

208
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMessageServiceImpl.java

@ -17,14 +17,37 @@
package com.epmet.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.exception.ValidateException;
import com.epmet.commons.tools.utils.HttpClientManager;
import com.epmet.commons.tools.validator.ValidatorUtils;
import com.epmet.constant.WxmpMessageConstant;
import com.epmet.dao.WxmpResiUserSubscribeDao;
import com.epmet.dao.WxmpTemplateMsgSubscribeStatusDao;
import com.epmet.dao.WxmpWorkUserSubscribeDao;
import com.epmet.dto.form.StaffBasicInfoFormDTO;
import com.epmet.dto.form.UserBasicInfoFormDTO;
import com.epmet.dto.form.WxSubscribeMessageFormDTO;
import com.epmet.entity.WxmpMsgSendRecordEntity;
import com.epmet.entity.WxmpTemplateMsgSubscribeStatusEntity;
import com.epmet.exception.WxSubscribeException;
import com.epmet.feign.EpmetUserOpenFeignClient;
import com.epmet.redis.WxmpMessageRedis;
import com.epmet.service.WxmpMessageService;
import com.epmet.service.WxmpMsgSendRecordService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 微信消息订阅Service
*
@ -38,6 +61,21 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
@Autowired
private WxmpTemplateMsgSubscribeStatusDao msgSubscribeStatusDao;
@Autowired
private EpmetUserOpenFeignClient epmetUserOpenFeignClient;
@Autowired
private WxmpMessageRedis wxmpMessageRedis;
@Resource
private WxmpResiUserSubscribeDao wxmpResiUserSubscribeDao;
@Resource
private WxmpWorkUserSubscribeDao wxmpWorkUserSubscribeDao;
@Autowired
private WxmpMsgSendRecordService wxmpMsgSendRecordService;
/**
* @Description 保存系统授权信息
* @return void
@ -62,4 +100,172 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
userSubscribeStatusEntity.setUserId(userId);
msgSubscribeStatusDao.insert(userSubscribeStatusEntity);
}
}
/**
* @param msgList
* @return void
* @Description 发送订阅消息
* @Author liushaowen
* @Date 2020/10/21 15:34
*/
@Override
public void sendWxSubscribeMessage(List<WxSubscribeMessageFormDTO> msgList) {
logger.info("待发送订阅消息数量:{}", msgList.size());
int succecssCount = 0;
for (WxSubscribeMessageFormDTO msg : msgList) {
try {
ValidatorUtils.validateEntity(msg);
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服务失败","");
}
if (StringUtils.isBlank(openId)){
throw new WxSubscribeException("openId获取失败","");
}
//获取accessToken
StringBuilder key = new StringBuilder(msg.getCustomerId()).append(":").append(msg.getClientType());
Map<String, Object> 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);
}
//判断用户是否有次数
Integer count = null;
if (WxmpMessageConstant.RESI.equals(clientType)) {
count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, customerId, templateId);
} else if (WxmpMessageConstant.WORK.equals(clientType)) {
count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, customerId, templateId);
}
if (count == null) {
//用户未订阅
throw new WxSubscribeException("用户未订阅",openId);
}
if (count == 0) {
throw new WxSubscribeException("用户可用额度不足",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()));
data.put(WxmpMessageConstant.MESSAGE_CONTENT, new JSONObject().put("value", msg.getMessageContent()));
data.put(WxmpMessageConstant.MESSAGE_TIME, new JSONObject().put("value", msg.getMessageTime()));
jsonObject.put(WxmpMessageConstant.DATA, data);
//选填项
if (StringUtils.isNotBlank(msg.getPage())) {
jsonObject.put(WxmpMessageConstant.PAGE, msg.getPage());
}
if (StringUtils.isNotBlank(msg.getMiniprogramState())) {
jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, msg.getMiniprogramState());
}
if (StringUtils.isNotBlank(msg.getLang())) {
jsonObject.put(WxmpMessageConstant.LANG, msg.getLang());
}
String resultStr = HttpClientManager.getInstance().sendPostByJSON(WxmpMessageConstant.SEND_MESSAGE + accessToken, JSON.toJSONString(jsonObject)).getData();
Map<String, Object> 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, customerId, templateId, 1);
} else if (WxmpMessageConstant.WORK.equals(clientType)) {
decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, customerId, templateId, 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, customerId, templateId);
} else if (WxmpMessageConstant.WORK.equals(clientType)) {
clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, customerId, templateId);
}
if (clear == 0) {
logger.error("消息{}发送失败且清空订阅条数失败", JSON.toJSONString(msg));
}
}
//抛出错误
throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),openId);
}
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));
}
}
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(msg.getMessageTime());
wxmpMsgSendRecordEntity.setResult(status);
return wxmpMsgSendRecordEntity;
}
}

7
epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMsgSendRecordServiceImpl.java

@ -101,4 +101,9 @@ public class WxmpMsgSendRecordServiceImpl extends BaseServiceImpl<WxmpMsgSendRec
baseDao.deleteBatchIds(Arrays.asList(ids));
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public int saveRecord(WxmpMsgSendRecordEntity entity) {
return baseDao.saveRecord(entity);
}
}

44
epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpMsgSendRecordDao.xml

@ -3,6 +3,46 @@
<mapper namespace="com.epmet.dao.WxmpMsgSendRecordDao">
<insert id="saveRecord" parameterType="com.epmet.entity.WxmpMsgSendRecordEntity">
insert into wxmp_msg_send_record
(id,
customer_id,
client_type,
template_id,
user_id,
wx_open_id,
behavior_type,
title,
message_content,
message_time,
result,
reason,
del_flag,
revision,
created_by,
created_time,
updated_by,
updated_time)
values
(MD5(replace(UUID(),'-','')),
#{customerId},
#{clientType},
#{templateId},
#{userId},
#{wxOpenId},
#{behaviorType},
#{title},
#{messageContent},
#{messageTime},
#{result},
#{reason},
0,
0,
'sys',
now(),
'sys',
now()
)
</insert>
</mapper>
</mapper>

34
epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml

@ -3,6 +3,38 @@
<mapper namespace="com.epmet.dao.WxmpResiUserSubscribeDao">
<select id="getResiSubscribeInfo" parameterType="String" resultType="Integer">
select
`count`
from WxmpResiUserSubscribe
where
del_flag =0
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
and wx_subscribe_status = 'subscribe'
</select>
<update id="decreaseResiSubscribeCount">
update WxmpResiUserSubscribe
set count = if(count &lt; 1, 0, count - ${num} )
where
del_flag =0
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
and wx_subscribe_status = 'subscribe'
</update>
</mapper>
<update id="clearResiSubscribeCount" parameterType="String">
update WxmpResiUserSubscribe
set count = 0,
wx_subscribe_status = 'unsubscribe'
where
del_flag =0
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
</update>
</mapper>

33
epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml

@ -3,6 +3,35 @@
<mapper namespace="com.epmet.dao.WxmpWorkUserSubscribeDao">
<select id="getWorkSubscribeInfo" parameterType="String" resultType="Integer">
select
`count`
from WxmpWorkUserSubscribe
where
del_flag =0
and template_id = #{templateId}
and wx_open_id = #{openId}
and wx_subscribe_status = 'subscribe'
</select>
</mapper>
<update id="decreaseWorkSubscribeCount">
update WxmpWorkUserSubscribe
set count = if(count &lt; 1, 0, count - ${num} )
where
del_flag =0
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
and wx_subscribe_status = 'subscribe'
</update>
<update id="clearWorkSubscribeCount" parameterType="String">
update WxmpWorkUserSubscribe
set count = 0,
wx_subscribe_status = 'unsubscribe'
where
del_flag =0
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
</update>
</mapper>

Loading…
Cancel
Save