PublicWeChatPushMessageTemplate.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package com.java110.job.task.wechat;
  2. import com.alibaba.fastjson.JSON;
  3. import com.java110.core.factory.WechatFactory;
  4. import com.java110.dto.community.CommunityDto;
  5. import com.java110.dto.fee.BillOweFeeDto;
  6. import com.java110.dto.owner.OwnerAppUserDto;
  7. import com.java110.dto.smallWeChat.SmallWeChatDto;
  8. import com.java110.dto.smallWechatAttr.SmallWechatAttrDto;
  9. import com.java110.dto.task.TaskDto;
  10. import com.java110.entity.wechat.Content;
  11. import com.java110.entity.wechat.Data;
  12. import com.java110.entity.wechat.Miniprogram;
  13. import com.java110.entity.wechat.PropertyFeeTemplateMessage;
  14. import com.java110.intf.fee.IFeeInnerServiceSMO;
  15. import com.java110.intf.store.ISmallWeChatInnerServiceSMO;
  16. import com.java110.intf.store.ISmallWechatAttrInnerServiceSMO;
  17. import com.java110.intf.user.IOwnerAppUserInnerServiceSMO;
  18. import com.java110.job.quartz.TaskSystemQuartz;
  19. import com.java110.utils.cache.MappingCache;
  20. import com.java110.utils.constant.WechatConstant;
  21. import com.java110.utils.util.DateUtil;
  22. import com.java110.utils.util.StringUtil;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.http.ResponseEntity;
  27. import org.springframework.stereotype.Component;
  28. import org.springframework.web.client.RestTemplate;
  29. import java.text.SimpleDateFormat;
  30. import java.util.ArrayList;
  31. import java.util.Calendar;
  32. import java.util.Date;
  33. import java.util.List;
  34. /**
  35. * @program: MicroCommunity
  36. * @description: 微信公众号主动推送信息
  37. * @author: zcc
  38. * @create: 2020-06-15 13:35
  39. **/
  40. @Component
  41. public class PublicWeChatPushMessageTemplate extends TaskSystemQuartz {
  42. private static Logger logger = LoggerFactory.getLogger(PublicWeChatPushMessageTemplate.class);
  43. @Autowired
  44. private IFeeInnerServiceSMO feeInnerServiceSMOImpl;
  45. @Autowired
  46. private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
  47. @Autowired
  48. private ISmallWechatAttrInnerServiceSMO smallWechatAttrInnerServiceSMOImpl;
  49. @Autowired
  50. private IOwnerAppUserInnerServiceSMO ownerAppUserInnerServiceSMOImpl;
  51. @Autowired
  52. private RestTemplate outRestTemplate;
  53. //模板信息推送地址
  54. private static String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
  55. //模板id
  56. private static String DEFAULT_TEMPLATE_ID = "ZF4j_ug2XW-UGwW1F-Gi4M1-51lpiu-PM89Oa6oZv6w";
  57. @Override
  58. protected void process(TaskDto taskDto) {
  59. logger.debug("开始执行微信模板信息推送" + taskDto.toString());
  60. // 获取小区
  61. List<CommunityDto> communityDtos = getAllCommunity();
  62. for (CommunityDto communityDto : communityDtos) {
  63. try {
  64. publishMsg(taskDto, communityDto);
  65. } catch (Exception e) {
  66. logger.error("推送消息失败", e);
  67. }
  68. }
  69. }
  70. private void publishMsg(TaskDto taskDto, CommunityDto communityDto) throws Exception {
  71. //
  72. // String templateId = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.KEY_PROPERTY_FEE_TEMPLATE_ID);
  73. //
  74. // templateId = StringUtil.isEmpty(templateId) ? DEFAULT_TEMPLATE_ID : templateId;
  75. //查询公众号配置
  76. SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
  77. smallWeChatDto.setWeChatType("1100");
  78. smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY);
  79. smallWeChatDto.setObjId(communityDto.getCommunityId());
  80. List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
  81. if (smallWeChatDto == null || smallWeChatDtos.size() <= 0) {
  82. logger.info("未配置微信公众号信息,定时任务执行结束");
  83. return;
  84. }
  85. SmallWeChatDto weChatDto = smallWeChatDtos.get(0);
  86. SmallWechatAttrDto smallWechatAttrDto = new SmallWechatAttrDto();
  87. smallWechatAttrDto.setCommunityId(communityDto.getCommunityId());
  88. smallWechatAttrDto.setWechatId(weChatDto.getWeChatId());
  89. smallWechatAttrDto.setSpecCd(SmallWechatAttrDto.SPEC_CD_OWE_FEE_TEMPLATE);
  90. List<SmallWechatAttrDto> smallWechatAttrDtos = smallWechatAttrInnerServiceSMOImpl.querySmallWechatAttrs(smallWechatAttrDto);
  91. if (smallWechatAttrDtos == null || smallWechatAttrDtos.size() <= 0) {
  92. logger.info("未配置微信公众号消息模板");
  93. return;
  94. }
  95. String templateId = smallWechatAttrDtos.get(0).getValue();
  96. String accessToken = WechatFactory.getAccessToken(weChatDto.getAppId(), weChatDto.getAppSecret());
  97. if (accessToken == null || accessToken == "") {
  98. logger.info("推送微信模板,获取accessToken失败:{}", accessToken);
  99. return;
  100. }
  101. //根据小区id查询业主与公众号绑定信息
  102. OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
  103. ownerAppUserDto.setCommunityId(weChatDto.getObjId());
  104. ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
  105. List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
  106. if (ownerAppUserDtos.size() <= 0 || ownerAppUserDtos == null) {
  107. logger.info("未查询到业主与微信公众号绑定关系");
  108. return;
  109. }
  110. List<String> memberIdList = new ArrayList<>(ownerAppUserDtos.size());
  111. for (OwnerAppUserDto appUserDto : ownerAppUserDtos) {
  112. memberIdList.add(appUserDto.getMemberId());
  113. }
  114. String[] memberIds = memberIdList.toArray(new String[memberIdList.size()]);
  115. //查询欠费信息
  116. BillOweFeeDto billOweFeeDto = new BillOweFeeDto();
  117. billOweFeeDto.setCommunityId(weChatDto.getObjId());
  118. billOweFeeDto.setOwnerIds(memberIds);
  119. billOweFeeDto.setState("1000");
  120. billOweFeeDto.setCurBill("T");
  121. List<BillOweFeeDto> billOweFeeDtos = feeInnerServiceSMOImpl.queryBillOweFees(billOweFeeDto);
  122. String url = sendMsgUrl + accessToken;
  123. String oweUrl = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.OWE_FEE_PAGE);
  124. Miniprogram miniprogram = null;
  125. if (oweUrl.contains("@@")) {
  126. miniprogram = new Miniprogram();
  127. miniprogram.setAppid(oweUrl.split("@@")[0]);
  128. }
  129. for (BillOweFeeDto fee : billOweFeeDtos) {
  130. for (OwnerAppUserDto appUserDto : ownerAppUserDtos) {
  131. if (fee.getOwnerId().equals(appUserDto.getMemberId())) {
  132. Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(fee.getFeeEndTime());
  133. Calendar now = Calendar.getInstance();
  134. now.setTime(date);
  135. // int year = now.get(Calendar.YEAR);
  136. // int month = now.get(Calendar.MONTH);
  137. Data data = new Data();
  138. PropertyFeeTemplateMessage templateMessage = new PropertyFeeTemplateMessage();
  139. templateMessage.setTemplate_id(templateId);
  140. templateMessage.setTouser(appUserDto.getOpenId());
  141. /*data.setFirst(new Content("物业费缴费提醒"));*/
  142. data.setFirst(new Content(fee.getFeeTypeName() + "提醒"));
  143. data.setKeyword1(new Content(fee.getPayerObjName()));
  144. data.setKeyword2(new Content(DateUtil.dateTimeToDate(fee.getFeeEndTime()) + "-" + DateUtil.dateTimeToDate(fee.getDeadlineTime())));
  145. data.setKeyword3(new Content(fee.getBillAmountOwed()));
  146. data.setRemark(new Content("请您及时缴费,如有疑问请联系相关物业人员"));
  147. if (!StringUtil.isEmpty(oweUrl)) {
  148. if (miniprogram == null) {
  149. templateMessage.setUrl(oweUrl + fee.getPayObjId() + "&wAppId=" + weChatDto.getAppId());
  150. } else {
  151. miniprogram.setPagepath(oweUrl.split("@@")[1] + fee.getPayObjId() + "&wAppId=" + weChatDto.getAppId());
  152. templateMessage.setMiniprogram(miniprogram);
  153. }
  154. }
  155. templateMessage.setData(data);
  156. logger.info("发送模板消息内容:{}", JSON.toJSONString(templateMessage));
  157. ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(url, JSON.toJSONString(templateMessage), String.class);
  158. logger.info("微信模板返回内容:{}", responseEntity);
  159. }
  160. }
  161. }
  162. }
  163. }