Browse Source

发送订阅 改为获取templateId

dev_shibei_match
liushaowen 5 years ago
parent
commit
db380a1ef4
  1. 5
      epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java
  2. 10
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java
  3. 11
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpWorkUserSubscribeDao.java
  4. 13
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/exception/WxSubscribeException.java
  5. 55
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMessageServiceImpl.java
  6. 13
      epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml
  7. 12
      epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml

5
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 = "消息内容不能为空") @NotBlank(message = "消息内容不能为空")
private String messageContent; private String messageContent;
/**
* 所需下发的订阅模板id
*/
@NotBlank(message = "模板id不能为空")
private String templateId;
} }

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

@ -64,4 +64,14 @@ public interface WxmpResiUserSubscribeDao extends BaseDao<WxmpResiUserSubscribeE
* @Date 2020/10/22 13:23 * @Date 2020/10/22 13:23
*/ */
int clearResiSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId); int clearResiSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
/**
* @Description 获取模板id
* @param openId
* @param templateId
* @param customerId
* @return java.lang.Integer
* @Author liushaowen
* @Date 2020/10/23 10:53
*/
String getResiSubscribeTemplateId(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
} }

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

@ -64,4 +64,15 @@ public interface WxmpWorkUserSubscribeDao extends BaseDao<WxmpWorkUserSubscribeE
* @Date 2020/10/22 13:22 * @Date 2020/10/22 13:22
*/ */
int clearWorkSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId); int clearWorkSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
/**
* @Description 获取模板id
* @param openId
* @param templateId
* @param customerId
* @return java.lang.String
* @Author liushaowen
* @Date 2020/10/23 10:54
*/
String getWorkSubscribeTemplateId(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
} }

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

@ -8,6 +8,7 @@ package com.epmet.exception;
public class WxSubscribeException extends Exception { public class WxSubscribeException extends Exception {
private String openId; private String openId;
private String templateId;
/** /**
* Constructs a new exception with the specified detail message. The * Constructs a new exception with the specified detail message. The
@ -17,9 +18,18 @@ public class WxSubscribeException extends Exception {
* @param message the detail message. The detail message is saved for * @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method. * later retrieval by the {@link #getMessage()} method.
*/ */
public WxSubscribeException(String message, String openId) { public WxSubscribeException(String message, String templateId, String openId) {
super(message); super(message);
this.openId = openId; this.openId = openId;
this.templateId = templateId;
}
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
} }
public String getOpenId() { public String getOpenId() {
@ -29,4 +39,5 @@ public class WxSubscribeException extends Exception {
public void setOpenId(String openId) { public void setOpenId(String openId) {
this.openId = openId; this.openId = openId;
} }
} }

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

@ -81,8 +81,8 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
private WxmpMsgSendRecordService wxmpMsgSendRecordService; private WxmpMsgSendRecordService wxmpMsgSendRecordService;
/** /**
* @Description 保存系统授权信息
* @return void * @return void
* @Description 保存系统授权信息
* @author wxz * @author wxz
* @date 2020.10.21 17:29 * @date 2020.10.21 17:29
*/ */
@ -121,7 +121,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
String userId = msg.getUserId(); String userId = msg.getUserId();
String clientType = msg.getClientType(); String clientType = msg.getClientType();
String customerId = msg.getCustomerId(); String customerId = msg.getCustomerId();
String templateId = msg.getTemplateId(); String templateId = null;
String openId = null; String openId = null;
//通过userId获取openId //通过userId获取openId
try { try {
@ -134,14 +134,14 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
staffBasicInfoFormDTO.setStaffId(userId); staffBasicInfoFormDTO.setStaffId(userId);
openId = epmetUserOpenFeignClient.getStaffBasicInfo(staffBasicInfoFormDTO).getData().getOpenId(); openId = epmetUserOpenFeignClient.getStaffBasicInfo(staffBasicInfoFormDTO).getData().getOpenId();
} else { } else {
throw new WxSubscribeException("clientType有误",openId); throw new WxSubscribeException("clientType有误", "", openId);
} }
}catch (Exception e){ } catch (Exception e) {
throw new WxSubscribeException("连接User服务失败",""); throw new WxSubscribeException("连接User服务失败", "", "");
} }
if (StringUtils.isBlank(openId)){ if (StringUtils.isBlank(openId)) {
throw new WxSubscribeException("openId获取失败",""); throw new WxSubscribeException("openId获取失败", "", "");
} }
//获取accessToken //获取accessToken
@ -149,12 +149,22 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
Map<String, Object> authorizerRefreshToken = new HashMap<>(); Map<String, Object> authorizerRefreshToken = new HashMap<>();
try { try {
authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(key.toString()); authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(key.toString());
}catch (Exception e){ } catch (Exception e) {
throw new WxSubscribeException("连接缓存服务器失败",openId); throw new WxSubscribeException("连接缓存服务器失败", "", openId);
} }
String accessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN); String accessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN);
if (StringUtils.isBlank(accessToken)) { if (StringUtils.isBlank(accessToken)) {
throw new WxSubscribeException("accessToken获取失败",openId); throw new WxSubscribeException("accessToken获取失败", "", 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);
} }
//判断用户是否有次数 //判断用户是否有次数
@ -166,10 +176,10 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
} }
if (count == null) { if (count == null) {
//用户未订阅 //用户未订阅
throw new WxSubscribeException("用户未订阅",openId); throw new WxSubscribeException("用户未订阅", templateId, openId);
} }
if (count == 0) { if (count == 0) {
throw new WxSubscribeException("用户可用额度不足",openId); throw new WxSubscribeException("用户可用额度不足", templateId, openId);
} }
//发送消息 //发送消息
@ -179,8 +189,8 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
jsonObject.put(WxmpMessageConstant.ACCESS_TOKEN, accessToken); jsonObject.put(WxmpMessageConstant.ACCESS_TOKEN, accessToken);
jsonObject.put(WxmpMessageConstant.TOUSER, openId); jsonObject.put(WxmpMessageConstant.TOUSER, openId);
jsonObject.put(WxmpMessageConstant.TEMPLATE_ID, templateId); jsonObject.put(WxmpMessageConstant.TEMPLATE_ID, templateId);
data.put(WxmpMessageConstant.TITLE, new JSONObject().put("value", ("您有一条" + msg.getBehaviorType()).substring(0,WxmpMessageConstant.TITLE_LIMIT))); 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_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()))); data.put(WxmpMessageConstant.MESSAGE_TIME, new JSONObject().put("value", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date())));
jsonObject.put(WxmpMessageConstant.DATA, data); jsonObject.put(WxmpMessageConstant.DATA, data);
EnvEnum envEnum = EnvEnum.getCurrentEnv(); EnvEnum envEnum = EnvEnum.getCurrentEnv();
@ -191,11 +201,11 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_WORK); jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_WORK);
} }
//开发环境 //开发环境
if ("dev".equals(envEnum.getCode())){ if ("dev".equals(envEnum.getCode())) {
jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_DEV); jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_DEV);
} }
//测试环境 //测试环境
if ("test".equals(envEnum.getCode())){ if ("test".equals(envEnum.getCode())) {
jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_TEST); jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_TEST);
} }
@ -216,7 +226,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg)); logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg));
} }
//存表 //存表
int saveRes = wxmpMsgSendRecordService.saveRecord(initRecord(msg,openId,WxmpMessageConstant.SUCCESS)); int saveRes = wxmpMsgSendRecordService.saveRecord(initRecord(msg, templateId, openId, WxmpMessageConstant.SUCCESS));
if (saveRes == 0) { if (saveRes == 0) {
logger.error("消息{}发送成功但存入记录表失败", JSON.toJSONString(msg)); logger.error("消息{}发送成功但存入记录表失败", JSON.toJSONString(msg));
} }
@ -236,7 +246,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
} }
//抛出错误 //抛出错误
throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),openId); throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),templateId, openId);
} }
succecssCount++; succecssCount++;
@ -246,9 +256,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
// if (StringUtils.isBlank(errMsg) && e instanceof ValidateException) { // if (StringUtils.isBlank(errMsg) && e instanceof ValidateException) {
// errMsg = ((ValidateException) e).getMsg(); // errMsg = ((ValidateException) e).getMsg();
// } // }
if (e instanceof WxSubscribeException){ if (e instanceof WxSubscribeException) {
//存表 //存表
WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = initRecord(msg, ((WxSubscribeException) e).getOpenId(), WxmpMessageConstant.ERROR); WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = initRecord(msg, ((WxSubscribeException) e).getTemplateId(), ((WxSubscribeException) e).getOpenId(), WxmpMessageConstant.ERROR);
wxmpMsgSendRecordEntity.setReason(errMsg); wxmpMsgSendRecordEntity.setReason(errMsg);
int saveRes = wxmpMsgSendRecordService.saveRecord(wxmpMsgSendRecordEntity); int saveRes = wxmpMsgSendRecordService.saveRecord(wxmpMsgSendRecordEntity);
if (saveRes == 0) { if (saveRes == 0) {
@ -262,12 +272,13 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
} }
logger.info("{}条消息中的{}条发送成功", msgList.size(), succecssCount); logger.info("{}条消息中的{}条发送成功", msgList.size(), succecssCount);
} }
//初始化记录对象 //初始化记录对象
private WxmpMsgSendRecordEntity initRecord(WxSubscribeMessageFormDTO msg, String openId, String status){ private WxmpMsgSendRecordEntity initRecord(WxSubscribeMessageFormDTO msg, String templateId, String openId, String status) {
WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = new WxmpMsgSendRecordEntity(); WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = new WxmpMsgSendRecordEntity();
wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId()); wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId());
wxmpMsgSendRecordEntity.setClientType(msg.getClientType()); wxmpMsgSendRecordEntity.setClientType(msg.getClientType());
wxmpMsgSendRecordEntity.setTemplateId(msg.getTemplateId()); wxmpMsgSendRecordEntity.setTemplateId(templateId);
wxmpMsgSendRecordEntity.setUserId(msg.getUserId()); wxmpMsgSendRecordEntity.setUserId(msg.getUserId());
wxmpMsgSendRecordEntity.setWxOpenId(openId); wxmpMsgSendRecordEntity.setWxOpenId(openId);
wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType()); wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType());

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

@ -37,4 +37,17 @@
and wx_open_id = #{openId} and wx_open_id = #{openId}
</update> </update>
<select id="getResiSubscribeTemplateId" resultType="String">
select
template_id
from Wxmp_Resi_User_Subscribe
where
del_flag = 0
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
and wx_subscribe_status = 'subscribe'
limit 1
</select>
</mapper> </mapper>

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

@ -34,4 +34,16 @@
and template_id = #{templateId} and template_id = #{templateId}
and wx_open_id = #{openId} and wx_open_id = #{openId}
</update> </update>
<select id="getWorkSubscribeTemplateId" resultType="String">
select
template_id
from Wxmp_Work_User_Subscribe
where
del_flag = 0
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
and wx_subscribe_status = 'subscribe'
limit 1
</select>
</mapper> </mapper>

Loading…
Cancel
Save