package com.java110.job.adapt.payment.notice; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.java110.core.factory.WechatFactory; import com.java110.dto.basePrivilege.BasePrivilegeDto; import com.java110.dto.community.CommunityDto; import com.java110.dto.smallWeChat.SmallWeChatDto; import com.java110.dto.smallWechatAttr.SmallWechatAttrDto; import com.java110.dto.staffAppAuth.StaffAppAuthDto; import com.java110.dto.user.UserDto; import com.java110.entity.order.Business; import com.java110.entity.wechat.Content; import com.java110.entity.wechat.Data; import com.java110.entity.wechat.PropertyFeeTemplateMessage; import com.java110.intf.community.ICommunityInnerServiceSMO; import com.java110.intf.order.IPrivilegeInnerServiceSMO; import com.java110.intf.store.ISmallWeChatInnerServiceSMO; import com.java110.intf.store.ISmallWechatAttrInnerServiceSMO; import com.java110.intf.user.IStaffAppAuthInnerServiceSMO; import com.java110.intf.user.IUserInnerServiceSMO; import com.java110.job.adapt.DatabusAdaptImpl; import com.java110.po.fee.PayFeeDetailPo; import com.java110.utils.cache.MappingCache; import com.java110.utils.util.BeanConvertUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.util.List; import java.util.Map; /** * 缴费通知适配器 * * @author fqz * @date 2020-12-11 18:54 */ @Component(value = "machinePaymentNoticeAdapt") public class MachinePaymentNoticeAdapt extends DatabusAdaptImpl { private static Logger logger = LoggerFactory.getLogger(MachinePaymentNoticeAdapt.class); @Autowired private ICommunityInnerServiceSMO communityInnerServiceSMO; @Autowired private IUserInnerServiceSMO userInnerServiceSMO; @Autowired private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl; @Autowired private ISmallWechatAttrInnerServiceSMO smallWechatAttrInnerServiceSMOImpl; @Autowired private IPrivilegeInnerServiceSMO privilegeInnerServiceSMO; @Autowired private RestTemplate outRestTemplate; @Autowired private IStaffAppAuthInnerServiceSMO staffAppAuthInnerServiceSMO; //模板信息推送地址 private static String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="; @Override public void execute(Business business, List businesses) { JSONObject data = business.getData(); if (data.containsKey(PayFeeDetailPo.class.getSimpleName())) { Object bObj = data.get(PayFeeDetailPo.class.getSimpleName()); JSONArray businessMachines = null; if (bObj instanceof JSONObject) { businessMachines = new JSONArray(); businessMachines.add(bObj); } else if (bObj instanceof Map) { businessMachines = new JSONArray(); businessMachines.add(JSONObject.parseObject(JSONObject.toJSONString(bObj))); } else if (bObj instanceof List) { businessMachines = JSONArray.parseArray(JSONObject.toJSONString(bObj)); } else { businessMachines = (JSONArray) bObj; } //JSONObject businessMachine = data.getJSONObject("businessMachine"); for (int bMachineIndex = 0; bMachineIndex < businessMachines.size(); bMachineIndex++) { JSONObject businessMachine = businessMachines.getJSONObject(bMachineIndex); doSendPayFeeDetail(business, businessMachine); } } } private void doSendPayFeeDetail(Business business, JSONObject businessMachine) { //查询缴费明细 PayFeeDetailPo payFeeDetailPo = BeanConvertUtil.covertBean(businessMachine, PayFeeDetailPo.class); //拿到员工信息 UserDto userDto = new UserDto(); List users = userInnerServiceSMO.getUsers(userDto); //查询小区信息 CommunityDto communityDto = new CommunityDto(); communityDto.setCommunityId(payFeeDetailPo.getCommunityId()); List communityDtos = communityInnerServiceSMO.queryCommunitys(communityDto); //获取费用类型 String feeTypeCdName = businessMachine.get("feeTypeCdName").toString(); //获取缴费用户楼栋单元房间号 String payerObjName = businessMachine.get("payerObjName").toString(); //获得用户缴费开始时间 String startTime = payFeeDetailPo.getStartTime(); //获取用户缴费到期时间 String endTime = payFeeDetailPo.getEndTime(); //获取用户缴费金额 String receivedAmount = payFeeDetailPo.getReceivedAmount(); //获取社区名称 String name = communityDtos.get(0).getName(); JSONObject paramIn = new JSONObject(); paramIn.put("payFeeRoom", name + payerObjName); paramIn.put("feeTypeCdName", feeTypeCdName); paramIn.put("payFeeTime", startTime + "至" + endTime); paramIn.put("receivedAmount", receivedAmount); paramIn.put("endTime", endTime); publishMsg(paramIn, communityDtos.get(0)); } private void publishMsg(JSONObject paramIn, CommunityDto communityDto) { //查询公众号配置 SmallWeChatDto smallWeChatDto = new SmallWeChatDto(); smallWeChatDto.setWeChatType("1100"); smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY); smallWeChatDto.setObjId(communityDto.getCommunityId()); List smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto); if (smallWeChatDto == null || smallWeChatDtos.size() <= 0) { logger.info("未配置微信公众号信息,定时任务执行结束"); return; } SmallWeChatDto weChatDto = smallWeChatDtos.get(0); SmallWechatAttrDto smallWechatAttrDto = new SmallWechatAttrDto(); smallWechatAttrDto.setCommunityId(communityDto.getCommunityId()); smallWechatAttrDto.setWechatId(weChatDto.getWeChatId()); smallWechatAttrDto.setSpecCd(SmallWechatAttrDto.SPEC_CD_WECHAT_SUCCESS_TEMPLATE); List smallWechatAttrDtos = smallWechatAttrInnerServiceSMOImpl.querySmallWechatAttrs(smallWechatAttrDto); if (smallWechatAttrDtos == null || smallWechatAttrDtos.size() <= 0) { logger.info("未配置微信公众号消息模板"); return; } String templateId = smallWechatAttrDtos.get(0).getValue(); String accessToken = WechatFactory.getAccessToken(weChatDto.getAppId(), weChatDto.getAppSecret()); if (accessToken == null || accessToken == "") { logger.info("推送微信模板,获取accessToken失败:{}", accessToken); return; } // 根据特定权限查询 有该权限的 员工 BasePrivilegeDto basePrivilegeDto = new BasePrivilegeDto(); basePrivilegeDto.setPId("502020121454780004"); basePrivilegeDto.setStoreId("402020112366290004"); List userDtos = privilegeInnerServiceSMO.queryPrivilegeUsers(basePrivilegeDto); String url = sendMsgUrl + accessToken; for (UserDto userDto : userDtos) { //根据 userId 查询到openId StaffAppAuthDto staffAppAuthDto = new StaffAppAuthDto(); staffAppAuthDto.setStaffId(userDto.getUserId()); staffAppAuthDto.setAppType("WECHAT"); List staffAppAuthDtos = staffAppAuthInnerServiceSMO.queryStaffAppAuths(staffAppAuthDto); String openId = staffAppAuthDtos.get(0).getOpenId(); Data data = new Data(); PropertyFeeTemplateMessage templateMessage = new PropertyFeeTemplateMessage(); templateMessage.setTemplate_id(templateId); templateMessage.setTouser(openId); data.setFirst(new Content("本次缴费已到账")); data.setKeyword1(new Content(paramIn.getString("payFeeRoom"))); data.setKeyword2(new Content(paramIn.getString("feeTypeCdName"))); data.setKeyword3(new Content(paramIn.getString("payFeeTime"))); data.setKeyword4(new Content(paramIn.getString("receivedAmount") + "元")); data.setRemark(new Content("感谢您的使用,如有疑问请联系相关物业人员")); templateMessage.setData(data); String wechatUrl = MappingCache.getValue("OWNER_WECHAT_URL"); templateMessage.setUrl(wechatUrl); logger.info("发送模板消息内容:{}", JSON.toJSONString(templateMessage)); ResponseEntity responseEntity = outRestTemplate.postForEntity(url, JSON.toJSONString(templateMessage), String.class); logger.info("微信模板返回内容:{}", responseEntity); } } }