WeChatPushMessageTemplate.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.RoomDto;
  5. import com.java110.dto.community.CommunityDto;
  6. import com.java110.dto.notice.NoticeDto;
  7. import com.java110.dto.owner.OwnerAppUserDto;
  8. import com.java110.dto.owner.OwnerRoomRelDto;
  9. import com.java110.dto.smallWeChat.SmallWeChatDto;
  10. import com.java110.dto.smallWechatAttr.SmallWechatAttrDto;
  11. import com.java110.dto.task.TaskDto;
  12. import com.java110.entity.wechat.Content;
  13. import com.java110.entity.wechat.Data;
  14. import com.java110.entity.wechat.PropertyFeeTemplateMessage;
  15. import com.java110.intf.community.INoticeInnerServiceSMO;
  16. import com.java110.intf.community.IRoomInnerServiceSMO;
  17. import com.java110.intf.store.ISmallWeChatInnerServiceSMO;
  18. import com.java110.intf.store.ISmallWechatAttrInnerServiceSMO;
  19. import com.java110.intf.user.IOwnerAppUserInnerServiceSMO;
  20. import com.java110.intf.user.IOwnerRoomRelInnerServiceSMO;
  21. import com.java110.job.quartz.TaskSystemQuartz;
  22. import com.java110.utils.cache.MappingCache;
  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.util.List;
  30. /**
  31. * @program: MicroCommunity
  32. * @description: 微信公众号主动推送信息
  33. * @author: wuxw
  34. * @create: 2020-06-15 13:35
  35. **/
  36. @Component
  37. public class WeChatPushMessageTemplate extends TaskSystemQuartz {
  38. private static Logger logger = LoggerFactory.getLogger(WeChatPushMessageTemplate.class);
  39. @Autowired
  40. private INoticeInnerServiceSMO noticeInnerServiceSMOImpl;
  41. @Autowired
  42. private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
  43. @Autowired
  44. private ISmallWechatAttrInnerServiceSMO smallWechatAttrInnerServiceSMOImpl;
  45. @Autowired
  46. private IOwnerAppUserInnerServiceSMO ownerAppUserInnerServiceSMOImpl;
  47. @Autowired
  48. private IRoomInnerServiceSMO roomInnerServiceSMOImpl;
  49. @Autowired
  50. private IOwnerRoomRelInnerServiceSMO ownerRoomRelInnerServiceSMOImpl;
  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. @Override
  56. protected void process(TaskDto taskDto) {
  57. logger.debug("开始执行微信模板信息推送" + taskDto.toString());
  58. // 获取小区
  59. List<CommunityDto> communityDtos = getAllCommunity();
  60. for (CommunityDto communityDto : communityDtos) {
  61. try {
  62. publishMsg(taskDto, communityDto);
  63. } catch (Exception e) {
  64. logger.error("推送消息失败", e);
  65. }
  66. }
  67. }
  68. private void publishMsg(TaskDto taskDto, CommunityDto communityDto) throws Exception {
  69. //查询公众号配置
  70. SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
  71. smallWeChatDto.setWeChatType("1100");
  72. smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY);
  73. smallWeChatDto.setObjId(communityDto.getCommunityId());
  74. List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
  75. if (smallWeChatDto == null || smallWeChatDtos.size() <= 0) {
  76. logger.info("未配置微信公众号信息,定时任务执行结束");
  77. return;
  78. }
  79. SmallWeChatDto weChatDto = smallWeChatDtos.get(0);
  80. SmallWechatAttrDto smallWechatAttrDto = new SmallWechatAttrDto();
  81. smallWechatAttrDto.setCommunityId(communityDto.getCommunityId());
  82. smallWechatAttrDto.setWechatId(weChatDto.getWeChatId());
  83. smallWechatAttrDto.setSpecCd(SmallWechatAttrDto.SPEC_CD_WECHAT_TEMPLATE);
  84. List<SmallWechatAttrDto> smallWechatAttrDtos = smallWechatAttrInnerServiceSMOImpl.querySmallWechatAttrs(smallWechatAttrDto);
  85. if (smallWechatAttrDtos == null || smallWechatAttrDtos.size() <= 0) {
  86. logger.info("未配置微信公众号消息模板");
  87. return;
  88. }
  89. String templateId = smallWechatAttrDtos.get(0).getValue();
  90. String accessToken = WechatFactory.getAccessToken(weChatDto.getAppId(), weChatDto.getAppSecret());
  91. if (accessToken == null || accessToken == "") {
  92. logger.info("推送微信模板,获取accessToken失败:{}", accessToken);
  93. return;
  94. }
  95. //
  96. NoticeDto noticeDto = new NoticeDto();
  97. noticeDto.setCommunityId(communityDto.getCommunityId());
  98. noticeDto.setPage(1);
  99. noticeDto.setRow(50);
  100. noticeDto.setState(NoticeDto.STATE_WAIT);
  101. noticeDto.setNoticeTypeCd(NoticeDto.NOTICE_TYPE_OWNER_WECHAT);
  102. List<NoticeDto> noticeDtos = noticeInnerServiceSMOImpl.queryNotices(noticeDto);
  103. if (noticeDtos == null || noticeDtos.size() < 1) {
  104. return;
  105. }
  106. for (NoticeDto tmpNotice : noticeDtos) {
  107. doSentWechat(tmpNotice, templateId, accessToken);
  108. }
  109. }
  110. private void doSentWechat(NoticeDto noticeDto, String templateId, String accessToken) {
  111. String objType = noticeDto.getObjType();
  112. switch (objType) {
  113. case NoticeDto.OBJ_TYPE_COMMUNITY:
  114. sendAllOwner(noticeDto, templateId, accessToken);
  115. break;
  116. case NoticeDto.OBJ_TYPE_FLOOR:
  117. sendFloorOwner(noticeDto, templateId, accessToken);
  118. break;
  119. case NoticeDto.OBJ_TYPE_UNIT:
  120. sendUnitOwner(noticeDto, templateId, accessToken);
  121. break;
  122. case NoticeDto.OBJ_TYPE_ROOM:
  123. sendRoomOwner(noticeDto, templateId, accessToken);
  124. break;
  125. }
  126. }
  127. private void sendFloorOwner(NoticeDto noticeDto, String templateId, String accessToken) {
  128. RoomDto roomDto = new RoomDto();
  129. roomDto.setCommunityId(noticeDto.getCommunityId());
  130. roomDto.setFloorId(noticeDto.getObjId());
  131. List<RoomDto> roomDtos = roomInnerServiceSMOImpl.queryRooms(roomDto);
  132. for (RoomDto tmpRoomDto : roomDtos) {
  133. if (!RoomDto.STATE_SELL.equals(tmpRoomDto.getState())) {
  134. continue;
  135. }
  136. OwnerRoomRelDto ownerRoomRelDto = new OwnerRoomRelDto();
  137. ownerRoomRelDto.setRoomId(tmpRoomDto.getRoomId());
  138. List<OwnerRoomRelDto> ownerRoomRelDtos = ownerRoomRelInnerServiceSMOImpl.queryOwnerRoomRels(ownerRoomRelDto);
  139. if (ownerRoomRelDtos == null || ownerRoomRelDtos.size() < 1) {
  140. continue;
  141. }
  142. OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
  143. ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
  144. ownerAppUserDto.setMemberId(ownerRoomRelDtos.get(0).getOwnerId());
  145. List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
  146. doSend(ownerAppUserDtos, noticeDto, templateId, accessToken);
  147. }
  148. }
  149. private void sendUnitOwner(NoticeDto noticeDto, String templateId, String accessToken) {
  150. RoomDto roomDto = new RoomDto();
  151. roomDto.setCommunityId(noticeDto.getCommunityId());
  152. roomDto.setUnitId(noticeDto.getObjId());
  153. List<RoomDto> roomDtos = roomInnerServiceSMOImpl.queryRooms(roomDto);
  154. for (RoomDto tmpRoomDto : roomDtos) {
  155. if (!RoomDto.STATE_SELL.equals(tmpRoomDto.getState())) {
  156. continue;
  157. }
  158. OwnerRoomRelDto ownerRoomRelDto = new OwnerRoomRelDto();
  159. ownerRoomRelDto.setRoomId(tmpRoomDto.getRoomId());
  160. List<OwnerRoomRelDto> ownerRoomRelDtos = ownerRoomRelInnerServiceSMOImpl.queryOwnerRoomRels(ownerRoomRelDto);
  161. if (ownerRoomRelDtos == null || ownerRoomRelDtos.size() < 1) {
  162. continue;
  163. }
  164. OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
  165. ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
  166. ownerAppUserDto.setMemberId(ownerRoomRelDtos.get(0).getOwnerId());
  167. List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
  168. doSend(ownerAppUserDtos, noticeDto, templateId, accessToken);
  169. }
  170. }
  171. private void sendRoomOwner(NoticeDto noticeDto, String templateId, String accessToken) {
  172. RoomDto roomDto = new RoomDto();
  173. roomDto.setCommunityId(noticeDto.getCommunityId());
  174. roomDto.setRoomId(noticeDto.getObjId());
  175. List<RoomDto> roomDtos = roomInnerServiceSMOImpl.queryRooms(roomDto);
  176. for (RoomDto tmpRoomDto : roomDtos) {
  177. if (!RoomDto.STATE_SELL.equals(tmpRoomDto.getState())) {
  178. continue;
  179. }
  180. OwnerRoomRelDto ownerRoomRelDto = new OwnerRoomRelDto();
  181. ownerRoomRelDto.setRoomId(tmpRoomDto.getRoomId());
  182. List<OwnerRoomRelDto> ownerRoomRelDtos = ownerRoomRelInnerServiceSMOImpl.queryOwnerRoomRels(ownerRoomRelDto);
  183. if (ownerRoomRelDtos == null || ownerRoomRelDtos.size() < 1) {
  184. continue;
  185. }
  186. OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
  187. ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
  188. ownerAppUserDto.setMemberId(ownerRoomRelDtos.get(0).getOwnerId());
  189. List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
  190. doSend(ownerAppUserDtos, noticeDto, templateId, accessToken);
  191. }
  192. }
  193. private void sendAllOwner(NoticeDto noticeDto, String templateId, String accessToken) {
  194. OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
  195. ownerAppUserDto.setAppType(OwnerAppUserDto.APP_TYPE_WECHAT);
  196. List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
  197. doSend(ownerAppUserDtos, noticeDto, templateId, accessToken);
  198. }
  199. private void doSend(List<OwnerAppUserDto> ownerAppUserDtos, NoticeDto noticeDto, String templateId, String accessToken) {
  200. String wechatUrl = MappingCache.getValue("OWNER_WECHAT_URL") + "/#/pages/notice/detail/detail?noticeId=";
  201. for (OwnerAppUserDto appUserDto : ownerAppUserDtos) {
  202. Data data = new Data();
  203. PropertyFeeTemplateMessage templateMessage = new PropertyFeeTemplateMessage();
  204. templateMessage.setTemplate_id(templateId);
  205. templateMessage.setTouser(appUserDto.getOpenId());
  206. data.setFirst(new Content(noticeDto.getTitle()));
  207. data.setKeyword1(new Content(noticeDto.getTitle()));
  208. data.setKeyword2(new Content(noticeDto.getStartTime()));
  209. data.setKeyword3(new Content(noticeDto.getContext()));
  210. data.setRemark(new Content("如有疑问请联系相关物业人员"));
  211. templateMessage.setData(data);
  212. templateMessage.setUrl(wechatUrl + noticeDto.getNoticeId());
  213. logger.info("发送模板消息内容:{}", JSON.toJSONString(templateMessage));
  214. ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(sendMsgUrl + accessToken, JSON.toJSONString(templateMessage), String.class);
  215. logger.info("微信模板返回内容:{}", responseEntity);
  216. }
  217. }
  218. }