PublicWeChatPushMessageTemplate.java 7.5 KB

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