Browse Source

Merge branch 'dev_wx_msg' of http://git.elinkit.com.cn:7070/r/epmet-cloud into dev_wx_msg

master
sunyuchao 5 years ago
parent
commit
8c3fb72df4
  1. 13
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java
  2. 14
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpWorkUserSubscribeDao.java
  3. 109
      epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMessageServiceImpl.java
  4. 14
      epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml
  5. 13
      epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml

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

@ -53,7 +53,7 @@ public interface WxmpResiUserSubscribeDao extends BaseDao<WxmpResiUserSubscribeE
* @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);
int decreaseResiSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId,@Param("num") int i,@Param("userId") String userId);
/**
* @Description 清空订阅条数修改订阅状态
@ -64,16 +64,7 @@ public interface WxmpResiUserSubscribeDao extends BaseDao<WxmpResiUserSubscribeE
* @Author liushaowen
* @Date 2020/10/22 13:23
*/
int clearResiSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
/**
* @Description 获取模板id
* @param openId
* @param customerId
* @return java.lang.Integer
* @Author liushaowen
* @Date 2020/10/23 10:53
*/
String getResiSubscribeTemplateId(@Param("openId") String openId, @Param("customerId") String customerId);
int clearResiSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId,@Param("userId") String userId);
WxmpResiUserSubscribeEntity getWxResiSubscribe(@Param("userId") String userId, @Param("customerId") String customerId,
@Param("templateId") String templateId);

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

@ -53,7 +53,7 @@ public interface WxmpWorkUserSubscribeDao extends BaseDao<WxmpWorkUserSubscribeE
* @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);
int decreaseWorkSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId, @Param("num") int i,@Param("userId") String userId);
/**
* @Description 清空订阅数修改订阅状态
@ -64,17 +64,7 @@ public interface WxmpWorkUserSubscribeDao extends BaseDao<WxmpWorkUserSubscribeE
* @Author liushaowen
* @Date 2020/10/22 13:22
*/
int clearWorkSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId);
/**
* @Description 获取模板id
* @param openId
* @param customerId
* @return java.lang.String
* @Author liushaowen
* @Date 2020/10/23 10:54
*/
String getWorkSubscribeTemplateId(@Param("openId") String openId, @Param("customerId") String customerId);
int clearWorkSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId,@Param("userId") String userId);
WxmpWorkUserSubscribeEntity getWxWorkSubscribe(@Param("userId") String userId,
@Param("customerId") String customerId,

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

@ -32,6 +32,7 @@ import com.epmet.dao.WxmpResiUserSubscribeDao;
import com.epmet.dao.WxmpTemplateMsgSubscribeStatusDao;
import com.epmet.dao.WxmpUserSubscribeRecordDao;
import com.epmet.dao.WxmpWorkUserSubscribeDao;
import com.epmet.dto.UserWechatDTO;
import com.epmet.dto.form.*;
import com.epmet.dto.result.*;
import com.epmet.entity.*;
@ -120,7 +121,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
@Override
public void sendWxSubscribeMessage(List<WxSubscribeMessageFormDTO> msgList) {
logger.info("待发送订阅消息数量:{}", msgList.size());
if (!(msgList.size()>NumConstant.ZERO)){
if (!(msgList.size() > NumConstant.ZERO)) {
throw new RenException("待发送消息不能为空");
}
int succecssCount = 0;
@ -129,7 +130,8 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
customerTemplateListFormDTO.setCustomerId(msgList.get(0).getCustomerId());
customerTemplateListFormDTO.setPublicId(WxmpMessageConstant.TEMPLATE_TYPE);
List<CustomerTemplateListResultDTO> customerTemplateListResultDTOS = customerTemplateList(customerTemplateListFormDTO);
// 获取AccessToken
Map<String,String> accessTokenMap = getAccessToken(msgList.get(0).getCustomerId());
for (WxSubscribeMessageFormDTO msg : msgList) {
try {
String userId = msg.getUserId();
@ -151,37 +153,16 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
throw new WxSubscribeException("clientType有误", "", openId);
}
} catch (Exception e) {
throw new WxSubscribeException("连接User服务失败"+e.getMessage(), "", "");
throw new WxSubscribeException("连接User服务失败" + e.getMessage(), "", "");
}
if (StringUtils.isBlank(openId)) {
throw new WxSubscribeException("openId获取失败", "", "");
}
//获取accessToken
String clientTypeRedis = null;
if (WxmpMessageConstant.RESI.equals(msg.getClientType())) {
clientTypeRedis = WxmpMessageConstant.RESI;
}
if (WxmpMessageConstant.GOV_DB.equals(msg.getClientType())) {
clientTypeRedis = WxmpMessageConstant.GOV_REDIS;
}
StringBuilder key = new StringBuilder(msg.getCustomerId()).append(":").append(clientTypeRedis);
Map<String, Object> authorizerRefreshToken = new HashMap<>();
try {
authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(key.toString());
} catch (Exception e) {
throw new WxSubscribeException("连接缓存服务器失败"+e.getMessage(), "", openId);
}
String accessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN);
if (StringUtils.isBlank(accessToken)) {
throw new WxSubscribeException("accessToken获取失败", "", openId);
}
//获取模板id
if (customerTemplateListResultDTOS.size() > NumConstant.ZERO) {
for (CustomerTemplateListResultDTO customerTemplateListResultDTO : customerTemplateListResultDTOS) {
if (WxmpMessageConstant.GOV_REDIS.equals(customerTemplateListResultDTO.getClientType())){
if (WxmpMessageConstant.GOV_REDIS.equals(customerTemplateListResultDTO.getClientType())) {
customerTemplateListResultDTO.setClientType(WxmpMessageConstant.GOV_DB);
}
if (clientType.equals(customerTemplateListResultDTO.getClientType())) {
@ -197,9 +178,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
//判断用户是否有次数
Integer count = null;
if (WxmpMessageConstant.RESI.equals(clientType)) {
count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, templateId, customerId,userId);
count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, templateId, customerId, userId);
} else if (WxmpMessageConstant.GOV_DB.equals(clientType)) {
count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, templateId, customerId,userId);
count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, templateId, customerId, userId);
}
if (count == null) {
//用户未订阅
@ -209,6 +190,17 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
throw new WxSubscribeException("用户可用额度不足", templateId, openId);
}
//获取accessToken
String accessToken = null;
if (WxmpMessageConstant.RESI.equals(clientType)) {
accessToken = accessTokenMap.get("resiToken");
} else if (WxmpMessageConstant.GOV_DB.equals(clientType)) {
accessToken = accessTokenMap.get("govToken");
}
if (StringUtils.isBlank(accessToken)) {
throw new WxSubscribeException("accessToken获取失败"+accessTokenMap.get("errorMsg"), "", openId);
}
//发送消息
JSONObject jsonObject = new JSONObject();
JSONObject data = new JSONObject();
@ -259,9 +251,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
//订阅条数-1
int decrease = 0;
if (WxmpMessageConstant.RESI.equals(clientType)) {
decrease = wxmpResiUserSubscribeDao.decreaseResiSubscribeCount(openId, templateId, customerId, 1);
decrease = wxmpResiUserSubscribeDao.decreaseResiSubscribeCount(openId, templateId, customerId, 1,userId);
} else if (WxmpMessageConstant.GOV_DB.equals(clientType)) {
decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, templateId, customerId, 1);
decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, templateId, customerId, 1,userId);
}
if (decrease == 0) {
logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg));
@ -277,9 +269,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
if (errcode.equals(WxmpMessageConstant.USER_REFUSED)) {
int clear = 0;
if (WxmpMessageConstant.RESI.equals(clientType)) {
clear = wxmpResiUserSubscribeDao.clearResiSubscribeCount(openId, templateId, customerId);
clear = wxmpResiUserSubscribeDao.clearResiSubscribeCount(openId, templateId, customerId,userId);
} else if (WxmpMessageConstant.GOV_DB.equals(clientType)) {
clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, templateId, customerId);
clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, templateId, customerId,userId);
}
if (clear == 0) {
logger.error("消息{}发送失败且清空订阅条数失败", JSON.toJSONString(msg));
@ -319,7 +311,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId());
wxmpMsgSendRecordEntity.setClientType(msg.getClientType());
wxmpMsgSendRecordEntity.setTemplateId(templateId);
wxmpMsgSendRecordEntity.setGridId(msg.getGridId()==null?"":msg.getGridId());
wxmpMsgSendRecordEntity.setGridId(msg.getGridId() == null ? "" : msg.getGridId());
wxmpMsgSendRecordEntity.setUserId(msg.getUserId());
wxmpMsgSendRecordEntity.setWxOpenId(openId);
wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType());
@ -330,6 +322,57 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
return wxmpMsgSendRecordEntity;
}
//获取AccessToken
private Map<String,String> getAccessToken(String customerId) {
EnvEnum envEnum = EnvEnum.getCurrentEnv();
String resiAccessToken = null;
String govAccessToken = null;
String errorMsg = null;
if (EnvEnum.PROD.getCode().equals(envEnum.getCode())) {
//居民端
StringBuilder resiKey = new StringBuilder(customerId).append(":").append(WxmpMessageConstant.RESI);
Map<String, Object> authorizerRefreshToken = new HashMap<>();
try {
authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(resiKey.toString());
} catch (Exception e) {
errorMsg = e.getMessage();
}
resiAccessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN);
//政府端
StringBuilder govKey = new StringBuilder(customerId).append(":").append(WxmpMessageConstant.GOV_REDIS);
authorizerRefreshToken = new HashMap<>();
try {
authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(resiKey.toString());
} catch (Exception e) {
errorMsg = e.getMessage();
}
govAccessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN);
} else {
String url = "https://epmet-cloud.elinkservice.cn/api/third/pacustomer/tokenlist";
JSONObject postData = new JSONObject();
postData.put("customerId", customerId);
String data = HttpClientManager.getInstance().sendPostByJSON(url, JSON.toJSONString(postData)).getData();
JSONObject toResult = JSON.parseObject(data);
Result mapToResult = ConvertUtils.mapToEntity(toResult, Result.class);
if (null != toResult.get("code")) {
mapToResult.setCode(((Integer) toResult.get("code")).intValue());
}
if (!mapToResult.success()) {
errorMsg = StringUtils.isBlank(mapToResult.getMsg()) ? mapToResult.getInternalMsg() : mapToResult.getMsg();
}
Object CustomerTokensResultDTO = mapToResult.getData();
JSONObject json = JSON.parseObject(CustomerTokensResultDTO.toString());
CustomerTokensResultDTO customerTokensResultDTO = ConvertUtils.mapToEntity(json, CustomerTokensResultDTO.class);
resiAccessToken = customerTokensResultDTO.getResiAuthorizerToken();
govAccessToken = customerTokensResultDTO.getWorkAuthorizerToken();
}
Map resultMap = new HashMap();
resultMap.put("resiToken",resiAccessToken);
resultMap.put("govToken",govAccessToken);
resultMap.put("errorMsg",errorMsg);
return resultMap;
}
/**
* @return
* @Description 居民端工作端-获取客户小程序模板列表
@ -385,7 +428,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
List<Object> resultList = (List<Object>) mapToResult.getData();
List<CustomerTemplateListResultDTO> list = new ArrayList<>();
//必须转换
for(Object f:resultList){
for (Object f : resultList) {
JSONObject json = JSON.parseObject(f.toString());
CustomerTemplateListResultDTO ff = ConvertUtils.mapToEntity(json, CustomerTemplateListResultDTO.class);
list.add(ff);

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

@ -24,6 +24,7 @@
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
and user_id = #{userId}
and wx_subscribe_status = 'subscribe'
</update>
@ -36,20 +37,9 @@
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
and user_id = #{userId}
</update>
<select id="getResiSubscribeTemplateId" resultType="String">
select
template_id
from Wxmp_Resi_User_Subscribe
where
del_flag = 0
and customer_id = #{customerId}
and wx_open_id = #{openId}
and wx_subscribe_status = 'subscribe'
limit 1
</select>
<!--增加订阅次数-->
<update id="incrSubscribeCount">
update wxmp_resi_user_subscribe

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

@ -33,6 +33,7 @@
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
and user_id = #{userId}
and wx_subscribe_status = 'subscribe'
</update>
<update id="clearWorkSubscribeCount" parameterType="String">
@ -44,18 +45,8 @@
and customer_id = #{customerId}
and template_id = #{templateId}
and wx_open_id = #{openId}
and user_id = #{userId}
</update>
<select id="getWorkSubscribeTemplateId" resultType="String">
select
template_id
from Wxmp_Work_User_Subscribe
where
del_flag = 0
and customer_id = #{customerId}
and wx_open_id = #{openId}
and wx_subscribe_status = 'subscribe'
limit 1
</select>
<update id="unSubscribe">
update wxmp_work_user_subscribe

Loading…
Cancel
Save