@ -19,25 +19,22 @@ package com.epmet.service.impl;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONObject ;
import com.epmet.commons.tools.constant.AppClientConstant ;
import com.epmet.commons.tools.constant.NumConstant ;
import com.epmet.commons.tools.enums.EnvEnum ;
import com.epmet.commons.tools.exception.RenException ;
import com.epmet.commons.tools.exception.ValidateException ;
import com.epmet.commons.tools.utils.ConvertUtils ;
import com.epmet.commons.tools.utils.HttpClientManager ;
import com.epmet.commons.tools.utils.Result ;
import com.epmet.commons.tools.validator.ValidatorUtil s ;
import com.epmet.constant.WxMsgTemplateSubscribeStatu s ;
import com.epmet.constant.WxmpMessageConstant ;
import com.epmet.dao.WxmpResiUserSubscribeDao ;
import com.epmet.dao.WxmpTemplateMsgSubscribeStatusDao ;
import com.epmet.dao.WxmpUserSubscribeRecordDao ;
import com.epmet.dao.WxmpWorkUserSubscribeDao ;
import com.epmet.dto.form.GetTemplateListFormDTO ;
import com.epmet.dto.form.StaffBasicInfoFormDTO ;
import com.epmet.dto.form.UserBasicInfoFormDTO ;
import com.epmet.dto.form.WxSubscribeMessageFormDTO ;
import com.epmet.dto.result.GetTemplateListResultDTO ;
import com.epmet.entity.WxmpMsgSendRecordEntity ;
import com.epmet.entity.WxmpTemplateMsgSubscribeStatusEntity ;
import com.epmet.dto.form.* ;
import com.epmet.dto.result.* ;
import com.epmet.entity.* ;
import com.epmet.exception.WxSubscribeException ;
import com.epmet.feign.EpmetUserOpenFeignClient ;
import com.epmet.redis.WxmpMessageRedis ;
@ -48,11 +45,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import javax.annotation.Resource ;
import java.text.ParseException ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
@ -85,6 +82,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
@Autowired
private WxmpMsgSendRecordService wxmpMsgSendRecordService ;
@Autowired
private WxmpUserSubscribeRecordDao subscribeRecordDao ;
/ * *
* @return void
* @Description 保存系统授权信息
@ -92,8 +92,18 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
* @date 2020 . 10 . 21 17 : 29
* /
@Override
public void saveSysAuthorizeInfo ( String customerId , String clientType , String alwaysVisit , String subscribeStatus , String userId ) {
WxmpTemplateMsgSubscribeStatusEntity userSubscribeStatusEntity = msgSubscribeStatusDao . getUserSubscribeStatusEntity ( userId , customerId , clientType ) ;
public void saveSysAuthorizeInfo ( String customerId , String clientType , String alwaysVisit , String subscribeStatus , String userId , String templateId ) {
String openId = null ;
switch ( clientType ) {
case AppClientConstant . APP_GOV :
openId = getWorkOpenId ( userId ) ;
break ;
case AppClientConstant . APP_RESI :
openId = getResiOpenId ( userId ) ;
break ;
}
WxmpTemplateMsgSubscribeStatusEntity userSubscribeStatusEntity = msgSubscribeStatusDao . getUserSubscribeStatusEntity ( userId , customerId , clientType , templateId , openId ) ;
if ( userSubscribeStatusEntity ! = null ) {
userSubscribeStatusEntity . setAlwaysVisit ( alwaysVisit ) ;
userSubscribeStatusEntity . setSubscribeStatus ( subscribeStatus ) ;
@ -104,7 +114,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
userSubscribeStatusEntity = new WxmpTemplateMsgSubscribeStatusEntity ( ) ;
userSubscribeStatusEntity . setCustomerId ( customerId ) ;
userSubscribeStatusEntity . setClientType ( clientType ) ;
userSubscribeStatusEntity . setTemplateId ( templateId ) ;
userSubscribeStatusEntity . setAlwaysVisit ( alwaysVisit ) ;
userSubscribeStatusEntity . setWxOpenId ( openId ) ;
userSubscribeStatusEntity . setSubscribeStatus ( subscribeStatus ) ;
userSubscribeStatusEntity . setUserId ( userId ) ;
msgSubscribeStatusDao . insert ( userSubscribeStatusEntity ) ;
@ -120,7 +132,17 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
@Override
public void sendWxSubscribeMessage ( List < WxSubscribeMessageFormDTO > msgList ) {
logger . info ( "待发送订阅消息数量:{}" , msgList . size ( ) ) ;
if ( ! ( msgList . size ( ) > NumConstant . ZERO ) ) {
throw new RenException ( "待发送消息不能为空" ) ;
}
int succecssCount = 0 ;
// 获取templateId
CustomerTemplateListFormDTO customerTemplateListFormDTO = new CustomerTemplateListFormDTO ( ) ;
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 ( ) ;
@ -133,51 +155,54 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
if ( WxmpMessageConstant . RESI . equals ( clientType ) ) {
UserBasicInfoFormDTO userBasicInfoFormDTO = new UserBasicInfoFormDTO ( ) ;
userBasicInfoFormDTO . setUserId ( userId ) ;
openId = epmetUserOpenFeignClient . getUserBasicInfo ( userBasicInfoFormDTO ) . getData ( ) . getOpenId ( ) ;
} else if ( WxmpMessageConstant . WORK . equals ( clientType ) ) {
Result < UserBasicInfo > userBasicInfo = epmetUserOpenFeignClient . getUserBasicInfo ( userBasicInfoFormDTO ) ;
if ( userBasicInfo . getCode ( ) ! = 0 ) {
throw new Exception ( userBasicInfo . getInternalMsg ( ) ) ;
} else {
openId = userBasicInfo . getData ( ) . getOpenId ( ) ;
}
} else if ( WxmpMessageConstant . GOV_DB . equals ( clientType ) ) {
StaffBasicInfoFormDTO staffBasicInfoFormDTO = new StaffBasicInfoFormDTO ( ) ;
staffBasicInfoFormDTO . setStaffId ( userId ) ;
openId = epmetUserOpenFeignClient . getStaffBasicInfo ( staffBasicInfoFormDTO ) . getData ( ) . getOpenId ( ) ;
Result < StaffBasicInfo > staffBasicInfo = epmetUserOpenFeignClient . getStaffBasicInfo ( staffBasicInfoFormDTO ) ; ;
if ( staffBasicInfo . getCode ( ) ! = 0 ) {
throw new Exception ( staffBasicInfo . getInternalMsg ( ) ) ;
} else {
openId = staffBasicInfo . getData ( ) . getOpenId ( ) ;
}
} else {
throw new WxSubscribeException ( "clientType有误" , "" , openId ) ;
}
} catch ( Exception e ) {
throw new WxSubscribeException ( "连接User服务失败" , "" , "" ) ;
e . printStackTrace ( ) ;
throw new WxSubscribeException ( "获取openId失败:" + e . getMessage ( ) , "" , "" ) ;
}
if ( StringUtils . isBlank ( openId ) ) {
throw new WxSubscribeException ( "openId获取失败 " , "" , "" ) ;
throw new WxSubscribeException ( "该userId未查询到关联的 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 ) ;
}
//获取模板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 ( customerTemplateListResultDTOS . size ( ) > NumConstant . ZERO ) {
for ( CustomerTemplateListResultDTO customerTemplateListResultDTO : customerTemplateListResultDTOS ) {
if ( WxmpMessageConstant . GOV_REDIS . equals ( customerTemplateListResultDTO . getClientType ( ) ) ) {
customerTemplateListResultDTO . setClientType ( WxmpMessageConstant . GOV_DB ) ;
}
if ( clientType . equals ( customerTemplateListResultDTO . getClientType ( ) ) ) {
templateId = customerTemplateListResultDTO . getTemplateId ( ) ;
}
}
} else {
throw new WxSubscribeException ( "获取模板id失败" , "" , openId ) ;
}
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 ) ;
count = wxmpResiUserSubscribeDao . getResiSubscribeInfo ( openId , templateId , customerId , userId ) ;
} else if ( WxmpMessageConstant . GOV_DB . equals ( clientType ) ) {
count = wxmpWorkUserSubscribeDao . getWorkSubscribeInfo ( openId , templateId , customerId , userId ) ;
}
if ( count = = null ) {
//用户未订阅
@ -187,6 +212,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 ( ) ;
@ -194,16 +230,30 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
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 ( ) ) ) ) ;
//标题
String title = "您有一条" + msg . getBehaviorType ( ) ;
JSONObject titleJson = new JSONObject ( ) ;
titleJson . put ( "value" , title . length ( ) > WxmpMessageConstant . TITLE_LIMIT ? title . substring ( 0 , WxmpMessageConstant . TITLE_LIMIT ) : title ) ;
data . put ( WxmpMessageConstant . TITLE , titleJson ) ;
//内容
JSONObject contentJson = new JSONObject ( ) ;
contentJson . put ( "value" , msg . getMessageContent ( ) . length ( ) > WxmpMessageConstant . MESSAGE_CONTENT_LIMIT ? msg . getMessageContent ( ) . substring ( 0 , WxmpMessageConstant . MESSAGE_CONTENT_LIMIT ) : msg . getMessageContent ( ) ) ;
data . put ( WxmpMessageConstant . MESSAGE_CONTENT , contentJson ) ;
//时间
JSONObject timeJson = new JSONObject ( ) ;
timeJson . put ( "value" , new SimpleDateFormat ( "yyyy-MM-dd HH:mm" ) . format ( msg . getMessageTime ( ) ) ) ;
data . put ( WxmpMessageConstant . MESSAGE_TIME , timeJson ) ;
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 ) ;
StringBuilder resiPage = new StringBuilder ( WxmpMessageConstant . PAGE_URL ) ;
resiPage . append ( "?customerId=" ) . append ( customerId ) . append ( "&gridId=" ) . append ( msg . getGridId ( ) ) ;
jsonObject . put ( WxmpMessageConstant . PAGE , resiPage . toString ( ) ) ;
} else if ( WxmpMessageConstant . GOV_DB . equals ( clientType ) ) {
StringBuilder govPage = new StringBuilder ( WxmpMessageConstant . PAGE_URL ) ;
govPage . append ( "?customerId=" ) . append ( customerId ) . append ( "&staffId=" ) . append ( userId ) ;
jsonObject . put ( WxmpMessageConstant . PAGE , govPage . toString ( ) ) ;
}
//开发环境
if ( "dev" . equals ( envEnum . getCode ( ) ) ) {
@ -223,9 +273,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
//订阅条数-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 ) ;
decrease = wxmpResiUserSubscribeDao . decreaseResiSubscribeCount ( openId , templateId , customerId , 1 , userId ) ;
} else if ( WxmpMessageConstant . GOV_DB . equals ( clientType ) ) {
decrease = wxmpWorkUserSubscribeDao . decreaseWorkSubscribeCount ( openId , templateId , customerId , 1 , userId ) ;
}
if ( decrease = = 0 ) {
logger . error ( "消息{}发送成功但订阅条数-1失败" , JSON . toJSONString ( msg ) ) ;
@ -241,9 +291,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 ) ;
} else if ( WxmpMessageConstant . WORK . equals ( clientType ) ) {
clear = wxmpWorkUserSubscribeDao . clearWorkSubscribeCount ( openId , templateId , customerId ) ;
clear = wxmpResiUserSubscribeDao . clearResiSubscribeCount ( openId , templateId , customerId , userId ) ;
} else if ( WxmpMessageConstant . GOV_DB . equals ( clientType ) ) {
clear = wxmpWorkUserSubscribeDao . clearWorkSubscribeCount ( openId , templateId , customerId , userId ) ;
}
if ( clear = = 0 ) {
logger . error ( "消息{}发送失败且清空订阅条数失败" , JSON . toJSONString ( msg ) ) ;
@ -251,9 +301,8 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
}
//抛出错误
throw new WxSubscribeException ( String . valueOf ( resultMap . get ( WxmpMessageConstant . ERR_MSG ) ) , templateId , openId ) ;
throw new WxSubscribeException ( String . valueOf ( resultMap . get ( WxmpMessageConstant . ERR_MSG ) ) , templateId , openId ) ;
}
succecssCount + + ;
} catch ( Exception e ) {
String errMsg = e . getMessage ( ) ;
@ -284,39 +333,326 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
wxmpMsgSendRecordEntity . setCustomerId ( msg . getCustomerId ( ) ) ;
wxmpMsgSendRecordEntity . setClientType ( msg . getClientType ( ) ) ;
wxmpMsgSendRecordEntity . setTemplateId ( templateId ) ;
wxmpMsgSendRecordEntity . setGridId ( msg . getGridId ( ) = = null ? "" : msg . getGridId ( ) ) ;
wxmpMsgSendRecordEntity . setUserId ( msg . getUserId ( ) ) ;
wxmpMsgSendRecordEntity . setWxOpenId ( openId ) ;
wxmpMsgSendRecordEntity . setBehaviorType ( msg . getBehaviorType ( ) ) ;
wxmpMsgSendRecordEntity . setTitle ( "您有一条" + msg . getBehaviorType ( ) ) ;
wxmpMsgSendRecordEntity . setMessageContent ( msg . getMessageContent ( ) ) ;
wxmpMsgSendRecordEntity . setMessageTime ( new Dat e( ) ) ;
wxmpMsgSendRecordEntity . setMessageTime ( msg . getMessageTim e( ) ) ;
wxmpMsgSendRecordEntity . setResult ( status ) ;
wxmpMsgSendRecordEntity . setReferer ( msg . getReferer ( ) ) ;
return wxmpMsgSendRecordEntity ;
}
/ * *
* @return
* @Description 居民端 、 工作端 - 获取客户小程序模板列表
* @author sun
* /
@Override
public List < GetTemplateListResultDTO > templateList ( GetTemplateListFormDTO formDTO ) {
GetTemplateListFormDTO dto = ConvertUtils . sourceToTarget ( formDTO , GetTemplateListFormDTO . class ) ;
String url = "https://epmet-cloud.elinkservice.cn/api/third/personaltemplate/templatelist" ;
//String url = "http://localhost:8080/api/third/personaltemplate/templatelist";
String data = HttpClientManager . getInstance ( ) . sendPostByJSON ( url , JSON . toJSONString ( dto ) ) . getData ( ) ;
logger . info ( "ThirdLoginServiceImpl.getUserWeChat:httpclient->url:" + url + ",结果->" + data ) ;
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 ( ) ) {
logger . error ( "调用epmet_third服务获取小程序消息订阅模板数据失败" ) ;
throw new RenException ( mapToResult . getCode ( ) ) ;
}
List < GetTemplateListResultDTO > resultList = ( List < GetTemplateListResultDTO > ) mapToResult . getData ( ) ;
return resultList ;
}
//获取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 居民端 、 工作端 - 获取客户小程序模板列表
* @author sun
* /
@Override
public List < GetTemplateListResultDTO > templateList ( GetTemplateListFormDTO formDTO ) {
GetTemplateListFormDTO dto = ConvertUtils . sourceToTarget ( formDTO , GetTemplateListFormDTO . class ) ;
String url = "https://epmet-cloud.elinkservice.cn/api/third/personaltemplate/templatelist" ;
//String url = "http://localhost:8080/api/third/personaltemplate/templatelist";
String data = HttpClientManager . getInstance ( ) . sendPostByJSON ( url , JSON . toJSONString ( dto ) ) . getData ( ) ;
logger . info ( "ThirdLoginServiceImpl.getUserWeChat:httpclient->url:" + url + ",结果->" + data ) ;
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 ( ) ) {
logger . error ( "调用epmet_third服务获取小程序消息订阅模板数据失败" ) ;
throw new RenException ( mapToResult . getCode ( ) ) ;
}
List < GetTemplateListResultDTO > resultList = ( List < GetTemplateListResultDTO > ) mapToResult . getData ( ) ;
return resultList ;
/ * List < Object > resultList = ( List < Object > ) mapToResult . getData ( ) ;
List < CustomerTemplateListResultDTO > list = new ArrayList < > ( ) ;
//必须转换
for ( Object f : resultList ) {
JSONObject json = JSON . parseObject ( f . toString ( ) ) ;
CustomerTemplateListResultDTO ff = ConvertUtils . mapToEntity ( json , CustomerTemplateListResultDTO . class ) ;
list . add ( ff ) ;
}
return list ; * /
}
/ * *
* @return
* @Description 查询客户两个端小程序对应的站内信模板Id
* @author sun
* /
public List < CustomerTemplateListResultDTO > customerTemplateList ( CustomerTemplateListFormDTO formDTO ) {
String url = "https://epmet-cloud.elinkservice.cn/api/third/personaltemplate/customertemplatelist" ;
String data = HttpClientManager . getInstance ( ) . sendPostByJSON ( url , JSON . toJSONString ( formDTO ) ) . getData ( ) ;
logger . info ( "ThirdLoginServiceImpl.customerTemplateList:httpclient->url:" + url + ",结果->" + data ) ;
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 ( ) ) {
logger . error ( "调用epmet_third服务获取客户小程序消息订阅模板数据失败" ) ;
throw new RenException ( mapToResult . getCode ( ) ) ;
}
List < Object > resultList = ( List < Object > ) mapToResult . getData ( ) ;
List < CustomerTemplateListResultDTO > list = new ArrayList < > ( ) ;
//必须转换
for ( Object f : resultList ) {
JSONObject json = JSON . parseObject ( f . toString ( ) ) ;
CustomerTemplateListResultDTO ff = ConvertUtils . mapToEntity ( json , CustomerTemplateListResultDTO . class ) ;
list . add ( ff ) ;
}
return list ;
}
@Override
public WxMsgAuthInfoResultDTO getAuthorizationInfo ( String customerId , String clientType , String loginUserId , String templateId ) {
String openId = null ;
switch ( clientType ) {
case AppClientConstant . APP_RESI :
openId = getResiOpenId ( loginUserId ) ;
break ;
case AppClientConstant . APP_GOV :
openId = getWorkOpenId ( loginUserId ) ;
break ;
}
return msgSubscribeStatusDao . getUserSubscribeStatusDTO ( loginUserId , customerId , clientType , templateId , openId ) ;
}
@Transactional
@Override
public void saveWxSubscribe ( String customerId , String clientType , String templateId , String wxAlwaysVisit ,
String wxSubscribeStatus , String behaviorType , String userId ) {
String openId ;
// 1. 用户订阅信息表新增或者更新
if ( AppClientConstant . APP_GOV . equals ( clientType ) ) {
// 工作端
openId = getWorkOpenId ( userId ) ;
saveWxWorkSubscribe ( customerId , templateId , wxSubscribeStatus , userId , openId , wxAlwaysVisit ) ;
} else {
//居民端
openId = getResiOpenId ( userId ) ;
saveWxResiSubscribe ( customerId , templateId , wxSubscribeStatus , userId , openId , wxAlwaysVisit ) ;
}
// 2.保存行为记录
WxmpUserSubscribeRecordEntity record = new WxmpUserSubscribeRecordEntity ( ) ;
record . setBehaviorType ( behaviorType ) ;
record . setCustomerId ( customerId ) ;
record . setTemplateId ( templateId ) ;
record . setUserId ( userId ) ;
record . setWxAlwaysVisit ( wxAlwaysVisit ) ;
record . setWxOpenId ( openId ) ;
record . setWxSubscribeStatus ( wxSubscribeStatus ) ;
subscribeRecordDao . insert ( record ) ;
// 3.更新用户模板订阅状态
WxmpTemplateMsgSubscribeStatusEntity subscribeStatusEntity = msgSubscribeStatusDao . getUserSubscribeStatusEntity ( userId , customerId , clientType , templateId , openId ) ;
if ( subscribeStatusEntity = = null ) {
subscribeStatusEntity = new WxmpTemplateMsgSubscribeStatusEntity ( ) ;
subscribeStatusEntity . setUserId ( userId ) ;
subscribeStatusEntity . setWxSubscribeStatus ( wxSubscribeStatus ) ;
subscribeStatusEntity . setTemplateId ( templateId ) ;
subscribeStatusEntity . setWxAlwaysVisit ( wxAlwaysVisit ) ;
subscribeStatusEntity . setCustomerId ( customerId ) ;
subscribeStatusEntity . setClientType ( clientType ) ;
subscribeStatusEntity . setWxOpenId ( openId ) ;
msgSubscribeStatusDao . insert ( subscribeStatusEntity ) ;
return ;
}
subscribeStatusEntity . setWxAlwaysVisit ( wxAlwaysVisit ) ;
subscribeStatusEntity . setWxSubscribeStatus ( wxSubscribeStatus ) ;
msgSubscribeStatusDao . updateById ( subscribeStatusEntity ) ;
}
/ * *
* @return void
* @Description 保存居民端微信订阅信息
* @author wxz
* @date 2020 . 10 . 22 09 : 56
* /
private void saveWxResiSubscribe ( String customerId , String templateId , String wxSubscribeStatus , String userId ,
String openId , String wxAlwaysVisit ) {
WxmpResiUserSubscribeEntity wxResiSubscribe = wxmpResiUserSubscribeDao . getWxResiSubscribe ( userId , customerId , templateId , openId ) ;
if ( wxResiSubscribe = = null ) {
WxmpResiUserSubscribeEntity insert = new WxmpResiUserSubscribeEntity ( ) ;
insert . setCount ( WxMsgTemplateSubscribeStatus . SUBSCRIBE . equals ( wxSubscribeStatus ) ? 1 : 0 ) ;
insert . setCustomerId ( customerId ) ;
insert . setTemplateId ( templateId ) ;
insert . setUserId ( userId ) ;
insert . setWxOpenId ( openId ) ;
insert . setWxSubscribeStatus ( wxSubscribeStatus ) ;
wxmpResiUserSubscribeDao . insert ( insert ) ;
return ;
}
if ( WxMsgTemplateSubscribeStatus . SUBSCRIBE . equals ( wxSubscribeStatus ) ) {
wxmpResiUserSubscribeDao . incrSubscribeCount ( 1 , userId , customerId , templateId , openId ) ;
} else if ( WxMsgTemplateSubscribeStatus . UNSUBSCRIBE . equals ( wxSubscribeStatus ) & & "yes" . equals ( wxAlwaysVisit ) ) {
// 总是+取消
wxmpResiUserSubscribeDao . unSubscribe ( userId , customerId , templateId , openId ) ;
wxmpResiUserSubscribeDao . clearResiSubscribeCount ( openId , templateId , customerId , userId ) ;
} else {
// 取消
wxmpResiUserSubscribeDao . unSubscribe ( userId , customerId , templateId , openId ) ;
}
}
/ * *
* @return void
* @Description 保存工作端订阅信息
* @author wxz
* @date 2020 . 10 . 22 14 : 21
* /
private void saveWxWorkSubscribe ( String customerId , String templateId , String wxSubscribeStatus , String userId ,
String openId , String wxAlwaysVisit ) {
WxmpWorkUserSubscribeEntity wxWorkSubscribe = wxmpWorkUserSubscribeDao . getWxWorkSubscribe ( userId , customerId , templateId , openId ) ;
if ( wxWorkSubscribe = = null ) {
WxmpWorkUserSubscribeEntity insert = new WxmpWorkUserSubscribeEntity ( ) ;
insert . setCount ( WxMsgTemplateSubscribeStatus . SUBSCRIBE . equals ( wxSubscribeStatus ) ? 1 : 0 ) ;
insert . setCustomerId ( customerId ) ;
insert . setTemplateId ( templateId ) ;
insert . setUserId ( userId ) ;
insert . setWxOpenId ( openId ) ;
insert . setWxSubscribeStatus ( wxSubscribeStatus ) ;
wxmpWorkUserSubscribeDao . insert ( insert ) ;
return ;
}
if ( WxMsgTemplateSubscribeStatus . SUBSCRIBE . equals ( wxSubscribeStatus ) ) {
wxmpWorkUserSubscribeDao . incrSubscribeCount ( 1 , userId , customerId , templateId , openId ) ;
} else if ( WxMsgTemplateSubscribeStatus . UNSUBSCRIBE . equals ( wxSubscribeStatus ) & & "yes" . equals ( wxAlwaysVisit ) ) {
// 取消订阅+总是
wxmpWorkUserSubscribeDao . unSubscribe ( userId , customerId , templateId , openId ) ;
wxmpWorkUserSubscribeDao . clearWorkSubscribeCount ( openId , templateId , customerId , userId ) ;
} else {
// 取消订阅+不总是
wxmpWorkUserSubscribeDao . unSubscribe ( userId , customerId , templateId , openId ) ;
}
}
/ * *
* @return string
* @Description 根据UserId查询居民OpenId
* @author wxz
* @date 2020 . 10 . 22 13 : 35
* /
public String getResiOpenId ( String userId ) {
UserBasicInfoFormDTO bif = new UserBasicInfoFormDTO ( ) ;
bif . setUserId ( userId ) ;
Result < UserBasicInfo > result = epmetUserOpenFeignClient . getUserBasicInfo ( bif ) ;
if ( ! result . success ( ) ) {
throw new RenException ( String . format ( "查询居民OpenId失败,userId:%s,错误信息:%s" , userId , result . getInternalMsg ( ) ) ) ;
}
UserBasicInfo userBasicInfo = result . getData ( ) ;
if ( userBasicInfo = = null ) {
throw new RenException ( String . format ( "根据userId:%s,没有查询到居民OpenId" , userId ) ) ;
}
return userBasicInfo . getOpenId ( ) ;
}
/ * *
* @return java . lang . String
* @Description 查询工作人员openId
* @author wxz
* @date 2020 . 10 . 22 14 : 46
* /
public String getWorkOpenId ( String userId ) {
StaffBasicInfoFormDTO form = new StaffBasicInfoFormDTO ( ) ;
form . setStaffId ( userId ) ;
Result < StaffBasicInfo > result = epmetUserOpenFeignClient . getStaffBasicInfo ( form ) ;
if ( ! result . success ( ) ) {
throw new RenException ( String . format ( "查询工作人员OpenId失败,userId:%s,错误信息:%s" , userId , result . getInternalMsg ( ) ) ) ;
}
StaffBasicInfo staffBasicInfo = result . getData ( ) ;
if ( staffBasicInfo = = null ) {
throw new RenException ( String . format ( "根据userId:%s,没有查询到工作人员OpenId" , userId ) ) ;
}
return staffBasicInfo . getOpenId ( ) ;
}
/ * *
* @return
* @Description 居民端 、 工作端 - 重新激活授权页
* @author sun
* /
@Override
public void activationSubscribe ( ActivationSubscribeFormDTO formDTO ) {
//1.根据所属端和用户Id查询对应的openId
String openId = "" ;
if ( AppClientConstant . APP_GOV . equals ( formDTO . getClientType ( ) ) ) {
// 工作端
openId = getWorkOpenId ( formDTO . getUserId ( ) ) ;
} else {
//居民端
openId = getResiOpenId ( formDTO . getUserId ( ) ) ;
}
formDTO . setOpenId ( openId ) ;
//2.更新我们自己授权页用户授权状态
if ( msgSubscribeStatusDao . updateUserSubscribe ( formDTO ) < NumConstant . ONE ) {
throw new RenException ( "重新激活订阅操作失败" ) ;
}
}
}