Your Name
2023-02-05 b3064052c54fa095ea0b597871f003a97c3f4c99
优化代码
2个文件已修改
2个文件已添加
353 ■■■■■ 已修改文件
java110-bean/src/main/java/com/java110/dto/smallWeChat/TemplateDataDto.java 45 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-utils/src/main/java/com/java110/utils/util/DateUtil.java 56 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service-job/src/main/java/com/java110/job/adapt/DatabusAdaptImpl.java 51 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service-job/src/main/java/com/java110/job/adapt/car/CarOutParkingAreaAdapt.java 201 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-bean/src/main/java/com/java110/dto/smallWeChat/TemplateDataDto.java
New file
@@ -0,0 +1,45 @@
package com.java110.dto.smallWeChat;
import java.io.Serializable;
public class TemplateDataDto implements Serializable {
    private String appId;
    private String templateId;
    private String accessToken;
    public TemplateDataDto() {
    }
    public TemplateDataDto(String templateId, String accessToken,String appId) {
        this.templateId = templateId;
        this.accessToken = accessToken;
        this.appId = appId;
    }
    public String getTemplateId() {
        return templateId;
    }
    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }
    public String getAccessToken() {
        return accessToken;
    }
    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }
    public String getAppId() {
        return appId;
    }
    public void setAppId(String appId) {
        this.appId = appId;
    }
}
java110-utils/src/main/java/com/java110/utils/util/DateUtil.java
@@ -1,5 +1,6 @@
package com.java110.utils.util;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
@@ -610,4 +611,59 @@
        return Integer.parseInt(String.valueOf(between_days));
    }
    public static double dayCompare(Date fromDate, Date toDate) {
        double resMonth = 0.0;
        Calendar from = Calendar.getInstance();
        from.setTime(fromDate);
        Calendar to = Calendar.getInstance();
        to.setTime(toDate);
        //比较月份差 可能有整数 也会负数
        int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
        //比较年差
        int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
        //真实 相差月份
        result = result + month;
        //开始时间  2021-06-01  2021-08-05   result = 2    2021-08-01
        Calendar newFrom = Calendar.getInstance();
        newFrom.setTime(fromDate);
        newFrom.add(Calendar.MONTH, result);
        //如果加月份后 大于了当前时间 默认加 月份 -1 情况 12-19  21-01-10
        //这个是神的逻辑一定好好理解
        if (newFrom.getTime().getTime() > toDate.getTime()) {
            newFrom.setTime(fromDate);
            result = result - 1;
            newFrom.add(Calendar.MONTH, result);
        }
        // t1 2021-08-01   t2 2021-08-05
        long t1 = newFrom.getTime().getTime();
        long t2 = to.getTime().getTime();
        //相差毫秒
        double days = (t2 - t1) * 1.00 / (24 * 60 * 60 * 1000);
        BigDecimal tmpDays = new BigDecimal(days); //相差天数
        BigDecimal monthDay = null;
        Calendar newFromMaxDay = Calendar.getInstance();
        newFromMaxDay.set(newFrom.get(Calendar.YEAR), newFrom.get(Calendar.MONTH), 1, 0, 0, 0);
        newFromMaxDay.add(Calendar.MONTH, 1); //下个月1号
        //在当前月中 这块有问题
        if (toDate.getTime() < newFromMaxDay.getTime().getTime()) {
            monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
            return tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).doubleValue();
        }
        // 上月天数
        days = (newFromMaxDay.getTimeInMillis() - t1) * 1.00 / (24 * 60 * 60 * 1000);
        tmpDays = new BigDecimal(days);
        monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
        BigDecimal preRresMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP);
        //下月天数
        days = (t2 - newFromMaxDay.getTimeInMillis()) * 1.00 / (24 * 60 * 60 * 1000);
        tmpDays = new BigDecimal(days);
        monthDay = new BigDecimal(newFromMaxDay.getActualMaximum(Calendar.DAY_OF_MONTH));
        resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).add(preRresMonth).doubleValue();
        return resMonth;
    }
}
service-job/src/main/java/com/java110/job/adapt/DatabusAdaptImpl.java
@@ -17,13 +17,24 @@
import com.alibaba.fastjson.JSONObject;
import com.java110.core.client.RestTemplate;
import com.java110.core.factory.WechatFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.businessDatabus.CustomBusinessDatabusDto;
import com.java110.dto.machine.CarInoutDto;
import com.java110.dto.machine.MachineDto;
import com.java110.dto.smallWeChat.SmallWeChatDto;
import com.java110.dto.smallWeChat.TemplateDataDto;
import com.java110.dto.smallWechatAttr.SmallWechatAttrDto;
import com.java110.dto.tempCarFeeConfig.TempCarPayOrderDto;
import com.java110.entity.order.Business;
import com.java110.intf.store.ISmallWeChatInnerServiceSMO;
import com.java110.intf.store.ISmallWechatAttrInnerServiceSMO;
import com.java110.job.adapt.hcIot.GetToken;
import com.java110.job.adapt.payment.notice.MachinePaymentNoticeAdapt;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import java.util.List;
@@ -32,6 +43,14 @@
 * @desc add by 吴学文 15:21
 */
public abstract class DatabusAdaptImpl implements IDatabusAdapt {
    private static Logger logger = LoggerFactory.getLogger(DatabusAdaptImpl.class);
    @Autowired
    private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
    @Autowired
    private ISmallWechatAttrInnerServiceSMO smallWechatAttrInnerServiceSMOImpl;
    /**
     * 封装头信息
@@ -174,4 +193,36 @@
    public void customExchange(CustomBusinessDatabusDto customBusinessDatabusDto) {
    }
    /**
     * 查询模板信息
     * @param communityId
     * @param specCd
     * @return
     */
    protected TemplateDataDto getOwnerTemplateId(String communityId, String specCd){
        SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
        smallWeChatDto.setWeChatType("1100");
        smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY);
        smallWeChatDto.setObjId(communityId);
        List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
        if (smallWeChatDto == null || smallWeChatDtos.size() <= 0) {
            logger.info("未配置微信公众号信息,定时任务执行结束");
            return null;
        }
        SmallWeChatDto weChatDto = smallWeChatDtos.get(0);
        SmallWechatAttrDto smallWechatAttrDto = new SmallWechatAttrDto();
        smallWechatAttrDto.setCommunityId(communityId);
        smallWechatAttrDto.setWechatId(weChatDto.getWeChatId());
        smallWechatAttrDto.setSpecCd(specCd);
        List<SmallWechatAttrDto> smallWechatAttrDtos = smallWechatAttrInnerServiceSMOImpl.querySmallWechatAttrs(smallWechatAttrDto);
        if (smallWechatAttrDtos == null || smallWechatAttrDtos.size() <= 0) {
            logger.info("未配置微信公众号消息模板");
            return null;
        }
        String templateId = smallWechatAttrDtos.get(0).getValue();
        String accessToken = WechatFactory.getAccessToken(weChatDto.getAppId(), weChatDto.getAppSecret());
        return new TemplateDataDto(templateId,accessToken,smallWeChatDtos.get(0).getAppId());
    }
}
service-job/src/main/java/com/java110/job/adapt/car/CarOutParkingAreaAdapt.java
New file
@@ -0,0 +1,201 @@
package com.java110.job.adapt.car;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.factory.WechatFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.machine.CarInoutDetailDto;
import com.java110.dto.machine.CarInoutDto;
import com.java110.dto.marketRuleCommunity.MarketRuleCommunityDto;
import com.java110.dto.marketRuleObj.MarketRuleObjDto;
import com.java110.dto.marketRuleWay.MarketRuleWayDto;
import com.java110.dto.marketText.MarketTextDto;
import com.java110.dto.owner.OwnerAppUserDto;
import com.java110.dto.owner.OwnerCarDto;
import com.java110.dto.owner.OwnerDto;
import com.java110.dto.smallWeChat.TemplateDataDto;
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.common.ICarInoutInnerServiceSMO;
import com.java110.intf.report.IReportOweFeeInnerServiceSMO;
import com.java110.intf.user.IOwnerAppUserInnerServiceSMO;
import com.java110.intf.user.IOwnerCarV1InnerServiceSMO;
import com.java110.job.adapt.DatabusAdaptImpl;
import com.java110.job.adapt.market.ISendExecutor;
import com.java110.job.adapt.payment.notice.MachinePaymentNoticeAdapt;
import com.java110.po.car.CarInoutDetailPo;
import com.java110.utils.cache.UrlCache;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.*;
@Component(value = "carOutParkingAreaAdapt")
public class CarOutParkingAreaAdapt extends DatabusAdaptImpl {
    private static Logger logger = LoggerFactory.getLogger(CarOutParkingAreaAdapt.class);
    @Autowired
    private IOwnerCarV1InnerServiceSMO ownerCarV1InnerServiceSMOImpl;
    @Autowired
    private IOwnerAppUserInnerServiceSMO ownerAppUserInnerServiceSMOImpl;
    @Autowired
    private ICarInoutInnerServiceSMO carInoutInnerServiceSMOImpl;
    @Autowired
    private IReportOweFeeInnerServiceSMO reportOweFeeInnerServiceSMOImpl;
    @Autowired
    private RestTemplate outRestTemplate;
    //模板信息推送地址
    private static String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
    @Override
    public void execute(Business business, List<Business> businesses) {
        JSONObject data = business.getData();
        JSONArray businessCarInoutDetailPos = new JSONArray();
        if (data instanceof JSONObject) {
            businessCarInoutDetailPos.add(data);
        }
        //JSONObject businessCarBlackWhite = data.getJSONObject("businessCarBlackWhite");
        for (int bCarInoutDetailPoIndex = 0; bCarInoutDetailPoIndex < businessCarInoutDetailPos.size(); bCarInoutDetailPoIndex++) {
            JSONObject businessCarInoutDetailPo = businessCarInoutDetailPos.getJSONObject(bCarInoutDetailPoIndex);
            doSendCarOutParkingAreaToWechat(business, businessCarInoutDetailPo);
        }
    }
    /**
     * 发送出厂通知
     *
     * @param business
     * @param businessCarInoutDetailPo 开门记录信息
     */
    private void doSendCarOutParkingAreaToWechat(Business business, JSONObject businessCarInoutDetailPo) {
        CarInoutDetailPo carInoutDetailPo = BeanConvertUtil.covertBean(businessCarInoutDetailPo, CarInoutDetailPo.class);
        //没有手机号 说明  没法发送营销信息 所以 放弃 不推送
        if (StringUtil.isEmpty(carInoutDetailPo.getCarNum())) {
            return;
        }
        //不是出厂不发信息
        if (!CarInoutDetailDto.CAR_INOUT_OUT.equals(carInoutDetailPo.getCarInout())) {
            return;
        }
        String communityId = carInoutDetailPo.getCommunityId();
        OwnerCarDto ownerCarDto = new OwnerCarDto();
        ownerCarDto.setCarNum(carInoutDetailPo.getCarNum());
        ownerCarDto.setCommunityId(communityId);
        ownerCarDto.setLeaseTypes(new String[]{OwnerCarDto.LEASE_TYPE_INNER,
                OwnerCarDto.LEASE_TYPE_MONTH,
                OwnerCarDto.LEASE_TYPE_NO_MONEY,
                OwnerCarDto.LEASE_TYPE_SALE});
        List<OwnerCarDto> ownerCarDtos = ownerCarV1InnerServiceSMOImpl.queryOwnerCars(ownerCarDto);
        if (ownerCarDtos == null || ownerCarDtos.size() < 1) {
            return;
        }
        if (StringUtil.isEmpty(ownerCarDtos.get(0).getOwnerId())) {
            return;
        }
        if (ownerCarDtos.get(0).getOwnerId().startsWith("-")) {
            return;
        }
        TemplateDataDto templateDataDto = getOwnerTemplateId(communityId, "");
        OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
        ownerAppUserDto.setMemberId(ownerCarDtos.get(0).getOwnerId());
        ownerAppUserDto.setAppType("WECHAT");
        List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
        if (ownerAppUserDtos == null || ownerAppUserDtos.size() < 1) {
            return;
        }
        String openId = ownerAppUserDtos.get(0).getOpenId();
        String url = sendMsgUrl + templateDataDto.getAccessToken();
        Data data = new Data();
        PropertyFeeTemplateMessage templateMessage = new PropertyFeeTemplateMessage();
        templateMessage.setTemplate_id(templateDataDto.getTemplateId());
        templateMessage.setTouser(openId);
        data.setFirst(new Content("亲爱的月租车用户,一路平安"));
        data.setKeyword1(new Content(carInoutDetailPo.getCarNum()));
        data.setKeyword2(new Content(ownerCarDtos.get(0).getAreaNum()));
        CarInoutDto carInoutDto = new CarInoutDto();
        carInoutDto.setInoutId(carInoutDetailPo.getInoutId());
        List<CarInoutDto> carInoutDtos = carInoutInnerServiceSMOImpl.queryCarInouts(carInoutDto);
        if (carInoutDtos == null || carInoutDtos.size() < 1) {
            return;
        }
        data.setKeyword3(new Content(carInoutDtos.get(0).getInTime()));
        data.setKeyword4(new Content(DateUtil.getFormatTimeString(new Date(), DateUtil.DATE_FORMATE_STRING_A)));
        double day = DateUtil.dayCompare(DateUtil.getCurrentDate(), ownerCarDtos.get(0).getEndTime());
        data.setKeyword5(new Content(day + ""));
        String remark = getRemark(communityId, ownerCarDtos.get(0).getOwnerId());
        data.setRemark(new Content(remark));
        templateMessage.setData(data);
        //获取业主公众号地址
        String wechatUrl = UrlCache.getOwnerUrl();
        wechatUrl += "/#/pages/fee/oweFee";
        if (wechatUrl.contains("?")) {
            wechatUrl += ("&wAppId=" + templateDataDto.getAppId());
        } else {
            wechatUrl += ("?wAppId=" + templateDataDto.getAppId());
        }
        templateMessage.setUrl(wechatUrl);
        logger.info("发送模板消息内容:{}", JSON.toJSONString(templateMessage));
        ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(url, JSON.toJSONString(templateMessage), String.class);
        logger.info("微信模板返回内容:{}", responseEntity);
    }
    private String getRemark(String communityId, String ownerId) {
        Map info = new HashMap();
        info.put("communityId", communityId);
        info.put("ownerIds", new String[]{ownerId});
        List<Map> oweFees = reportOweFeeInnerServiceSMOImpl.queryOweFeesByOwnerIds(info);
        String remark = "感谢您的使用,如有疑问请联系相关物业人员";
        if (oweFees == null || oweFees.size() < 1) {
            remark = "感谢您的使用,如有疑问请联系相关物业人员";
            return remark;
        }
        double oweFee = Double.parseDouble(oweFees.get(0).get("oweFee").toString());
        if (oweFee < 0) {
            remark = "感谢您的使用,如有疑问请联系相关物业人员";
            return remark;
        }
        remark = "物业费欠费"+oweFee+"元,请点击详情交费!";
        return remark;
    }
}