|  | @ -0,0 +1,238 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.base.util;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.org.BaseOrgDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.wx.BasePatientWechatDo;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.wx.WxAccessTokenDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.wx.WxTemplateConfigDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.course.RecruitStudentsRecordDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.message.dao.SystemMessageDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.org.dao.BaseOrgDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.http.HttpClientUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.wechat.WeixinMessagePushUtils;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.dao.BasePatientWechatDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.dao.WxAccessTokenDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.dao.WxTemplateConfigDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.service.WxAccessTokenService;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.scheduling.annotation.Async;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by liub on 2020/12/28.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Component
 | 
	
		
			
				|  |  | public class WxMessageUtil {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static Logger logger = LoggerFactory.getLogger(MessageUtil.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private WxAccessTokenService wxAccessTokenService;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private WeixinMessagePushUtils weixinMessagePushUtils;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private WxTemplateConfigDao wxTemplateConfigDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private SystemMessageDao systemMessageDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private WxAccessTokenDao wxAccessTokenDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorDao baseDoctorDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BasePatientDao basePatientDao;
 | 
	
		
			
				|  |  |     @Value("${wechat.id}")
 | 
	
		
			
				|  |  |     private String wechatId;
 | 
	
		
			
				|  |  |     @Value("${wechat.url}")
 | 
	
		
			
				|  |  |     private String serviceUrl;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BasePatientWechatDao basePatientWechatDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseOrgDao orgDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 批量异步发送微信模板消息
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     public void sengdWxTemplates(List<RecruitStudentsRecordDO> list){
 | 
	
		
			
				|  |  |         String orgMobile = "";
 | 
	
		
			
				|  |  |         String orgId = "";
 | 
	
		
			
				|  |  |         for (RecruitStudentsRecordDO recordDO:list){
 | 
	
		
			
				|  |  |             if("".equals(orgId)){
 | 
	
		
			
				|  |  |                 BaseOrgDO orgDO = orgDao.findByCode(recordDO.getOrgCode());
 | 
	
		
			
				|  |  |                 orgMobile = orgDO.getMobile();
 | 
	
		
			
				|  |  |                 orgId = orgDO.getId();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             /**
 | 
	
		
			
				|  |  |              * 学生被录取的时候推送一个录取成功通知微信模板消息
 | 
	
		
			
				|  |  |              dc--ecFzT5vrnUFTNdVB6Sg4wXBnv14IX5sFP3jCFfk
 | 
	
		
			
				|  |  |              您好,恭喜您已被【机构名】成功录取
 | 
	
		
			
				|  |  |              姓名:【报名人姓名】
 | 
	
		
			
				|  |  |              手机号:【报名人手机号】
 | 
	
		
			
				|  |  |              请及时前往托育机构完成报名,如有疑问请联系招生热线【机构联系方式】
 | 
	
		
			
				|  |  |              (点击进入对应报名的机构详情页)
 | 
	
		
			
				|  |  |              */
 | 
	
		
			
				|  |  |             String first = "您好,恭喜您已被"+recordDO.getOrgName()+"成功录取";
 | 
	
		
			
				|  |  |             String remark = "请及时前往托育机构完成报名,如有疑问请联系招生热线"+orgMobile;
 | 
	
		
			
				|  |  |             String url = serviceUrl+"/medical-care-patient/onlineRegistration/nurseriesdetail?id="+orgId;
 | 
	
		
			
				|  |  |             List<BasePatientWechatDo> basePatientWechatDos = basePatientWechatDao.findByWechatIdAndPatientId(wechatId,recordDO.getPatient());
 | 
	
		
			
				|  |  |             if(basePatientWechatDos.size()>0){
 | 
	
		
			
				|  |  |                 String openId = basePatientWechatDos.get(0).getOpenid();
 | 
	
		
			
				|  |  |                 putTemplateWxMessage(wechatId,"template_success_notice","cgrxtz",openId,first,url,remark,2,null,
 | 
	
		
			
				|  |  |                         recordDO.getPatientName(),recordDO.getMobile());
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param wechatId
 | 
	
		
			
				|  |  |      * @param templateName
 | 
	
		
			
				|  |  |      * @param scene
 | 
	
		
			
				|  |  |      * @param openId
 | 
	
		
			
				|  |  |      * @param first
 | 
	
		
			
				|  |  |      * @param url 跳转链接
 | 
	
		
			
				|  |  |      * @param remark
 | 
	
		
			
				|  |  |      * @param type  模板通知类型
 | 
	
		
			
				|  |  |      * @param json 用于对跳转连接的修改,
 | 
	
		
			
				|  |  |      * @param keywords
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void putTemplateWxMessage(String wechatId, String templateName, String scene, String openId, String first
 | 
	
		
			
				|  |  |             ,String url, String remark, Integer type,JSONObject json,String ...keywords) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             System.out.println(wechatId);
 | 
	
		
			
				|  |  |             WxAccessTokenDO wxAccessTokenDO = wxAccessTokenService.getWxAccessTokenById(wechatId);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             if (wxAccessTokenDO == null) {
 | 
	
		
			
				|  |  |                 logger.info("wx_access_token表获取为空,wechatId" + wechatId);
 | 
	
		
			
				|  |  |                 return;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             WxTemplateConfigDO templateConfig = wxTemplateConfigDao.findByWechatIdAndTemplateNameAndSceneAndStatus(wechatId, templateName, scene, 1);
 | 
	
		
			
				|  |  |             WxTemplateConfigDO newConfig = new WxTemplateConfigDO();
 | 
	
		
			
				|  |  |             BeanUtils.copyProperties(templateConfig,newConfig);
 | 
	
		
			
				|  |  |             if (newConfig == null) {
 | 
	
		
			
				|  |  |                 logger.info("微信模板不存在!请确认wechatId:" + wechatId + ",templateName:" + templateName + ",scene:" + scene);
 | 
	
		
			
				|  |  |                 return;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             logger.info("微信模板推送前");
 | 
	
		
			
				|  |  |             if (StringUtils.isNoneBlank(first)){
 | 
	
		
			
				|  |  |                 newConfig.setFirst(first);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (StringUtils.isNoneBlank(url)){
 | 
	
		
			
				|  |  |                newConfig.setUrl(url);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (StringUtils.isNoneBlank(remark)){
 | 
	
		
			
				|  |  |                 newConfig.setRemark(remark);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             newConfig = setTemPlateUrl(newConfig,type,openId,json);
 | 
	
		
			
				|  |  |             int keyLength = keywords.length;
 | 
	
		
			
				|  |  |             if (keyLength >= 1) {
 | 
	
		
			
				|  |  |                if(StringUtils.isNoneBlank(keywords[0])){
 | 
	
		
			
				|  |  |                    newConfig.setKeyword1(keywords[0]);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (keyLength >= 2) {
 | 
	
		
			
				|  |  |                 if(StringUtils.isNoneBlank(keywords[1])){
 | 
	
		
			
				|  |  |                     newConfig.setKeyword2(keywords[1]);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (keyLength >= 3) {
 | 
	
		
			
				|  |  |                 if(StringUtils.isNoneBlank(keywords[2])){
 | 
	
		
			
				|  |  |                     newConfig.setKeyword3(keywords[2]);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (keyLength >= 4) {
 | 
	
		
			
				|  |  |                 if(StringUtils.isNoneBlank(keywords[3])){
 | 
	
		
			
				|  |  |                     newConfig.setKeyword4(keywords[3]);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (keyLength >= 5) {
 | 
	
		
			
				|  |  |                 if(StringUtils.isNoneBlank(keywords[4])){
 | 
	
		
			
				|  |  |                     newConfig.setKeyword5(keywords[4]);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (keyLength >= 6) {
 | 
	
		
			
				|  |  |                 if(StringUtils.isNoneBlank(keywords[5])){
 | 
	
		
			
				|  |  |                     newConfig.setKeyword6(keywords[5]);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (keyLength >= 7) {
 | 
	
		
			
				|  |  |                 if(StringUtils.isNoneBlank(keywords[6])){
 | 
	
		
			
				|  |  |                     newConfig.setKeyword7(keywords[6]);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             //发起微信消息模板推送
 | 
	
		
			
				|  |  |             weixinMessagePushUtils.putWxMsg(wxAccessTokenDO.getAccessToken(), openId, newConfig);
 | 
	
		
			
				|  |  |             logger.info("微信模板消息推送后");
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             logger.info("微信模板推送异常");
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public WxTemplateConfigDO setTemPlateUrl(WxTemplateConfigDO wxTemplateConfigDO,Integer type,String openid,JSONObject json){
 | 
	
		
			
				|  |  |         String url =  "/";
 | 
	
		
			
				|  |  |         if (json==null){
 | 
	
		
			
				|  |  |             return wxTemplateConfigDO;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         switch (type){
 | 
	
		
			
				|  |  |             case 1:
 | 
	
		
			
				|  |  |                 //咨询回复
 | 
	
		
			
				|  |  |                 wxTemplateConfigDO.setUrl(wxTemplateConfigDO.getUrl() + "?sessionId=" + json.getString("sessionId")
 | 
	
		
			
				|  |  |                         +"&type="+json.getString("consultType"));
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 19:
 | 
	
		
			
				|  |  |                 String urlStr= wxTemplateConfigDO.getUrl();
 | 
	
		
			
				|  |  |                 boolean status = urlStr.contains("openid=");
 | 
	
		
			
				|  |  |                 if(!status){
 | 
	
		
			
				|  |  |                     urlStr=json.getString("url")+"?openid=" + openid;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 wxTemplateConfigDO.setUrl(url + urlStr );
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 30:
 | 
	
		
			
				|  |  |                 //反馈通知 测试 TPbq9m0SAiVfRhXtXq17SDmYIfrJ8Whp2NpSrq9wlfI
 | 
	
		
			
				|  |  |                 if(json.containsKey("consult")) {
 | 
	
		
			
				|  |  |                     wxTemplateConfigDO.setUrl(url + wxTemplateConfigDO.getUrl() + "?openid=" + openid + "&consult=" + json.getString("consult"));
 | 
	
		
			
				|  |  |                 }else if (json.containsKey("id")){
 | 
	
		
			
				|  |  |                     wxTemplateConfigDO.setUrl(url + wxTemplateConfigDO.getUrl() + "?openid=" + openid + "&id=" + json.getString("id"));
 | 
	
		
			
				|  |  |                 }else if(json.containsKey("relationCode")){
 | 
	
		
			
				|  |  |                     wxTemplateConfigDO.setUrl(url + wxTemplateConfigDO.getUrl() + "?openid=" + openid );
 | 
	
		
			
				|  |  |                 }else if(json.containsKey("resultCode")){
 | 
	
		
			
				|  |  |                     wxTemplateConfigDO.setUrl(url + wxTemplateConfigDO.getUrl() + "?resultCode=" + json.getString("resultCode"));
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 31:
 | 
	
		
			
				|  |  |                 //上门服务医生评分
 | 
	
		
			
				|  |  |                 wxTemplateConfigDO.setUrl(url + wxTemplateConfigDO.getUrl()+"?openid=" + openid + "&id=" + json.getString("id")+ "&finish=" + json.getString("finish"));
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 32:
 | 
	
		
			
				|  |  |                 String url1 = "wx/common/";
 | 
	
		
			
				|  |  |                 wxTemplateConfigDO.setUrl(url1 + wxTemplateConfigDO.getUrl()+"?openid=" + openid + "&consult=" + json.getString("consult") + "&status=" + json.getInteger("status"));
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 34:
 | 
	
		
			
				|  |  |                 wxTemplateConfigDO.setUrl(url + wxTemplateConfigDO.getUrl() + "?openid=" + openid + "&orderId=" + json.getString("orderId")+ "&authorizeImage=" + json.getString("authorizeImage"));
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 35:
 | 
	
		
			
				|  |  |                 wxTemplateConfigDO.setUrl(url + wxTemplateConfigDO.getUrl()+json.getString("orderId")+"&isMask=1");
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return wxTemplateConfigDO;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |