ParkingInfoPushJob.java 8.16 KB
package com.zteits.park.job.datahub;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zteits.clouds.api.apibase.constants.DataStatusEnum;
import com.zteits.park.domain.HubEnterpriseInfo;
import com.zteits.park.domain.HubEnterpriseInfoExample;
import com.zteits.park.domain.HubParkingInfoBO;
import com.zteits.park.mapper.HubEnterpriseInfoMapper;
import com.zteits.park.mapper.smapper.HubParkingInfoSMapper;
import com.zteits.park.util.ParkInfoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 停车场数据推送JOB
 * @since 2025-08-17
 */
@Component
public class ParkingInfoPushJob {
    private static final Logger logger = LoggerFactory.getLogger(ParkingInfoPushJob.class);
    @Autowired
    private HubParkingInfoSMapper hubParkingInfoSMapper;
    @Autowired
    private HubEnterpriseInfoMapper hubEnterpriseInfoMapper;
    @Autowired
    private HubConfig hubConfig;
    @Autowired
    private TokenService tokenService;

    @XxlJob("ParkingInfoPushJob")
    public ReturnT<String> execute()
    {
        log("开始推送停车场信息JOB");
        try{
            this.pushData();
        } catch (Exception e){
            logger.error("推送停车场信息JOB异常:"+e.getMessage(),e);
            log("推送停车场信息JOB异常:"+e.getMessage());
        }
        return ReturnT.SUCCESS;
    }

    private void pushData() {
        List<HubParkingInfoBO> hubParkingInfoList = hubParkingInfoSMapper.selectList(null);
        if (CollectionUtils.isEmpty(hubParkingInfoList)) {
            log("没有需要推送的停车场信息");
        }
        // 查询运营单位信息
        HubEnterpriseInfoExample enterpriseInfoExample = new HubEnterpriseInfoExample();
        enterpriseInfoExample.createCriteria().andDataStatusEqualTo(DataStatusEnum.DATA_STATUS_VALID.value());
        List<HubEnterpriseInfo> hubEnterpriseInfos = hubEnterpriseInfoMapper.selectByExample(enterpriseInfoExample);
        if (CollectionUtils.isEmpty(hubEnterpriseInfos)) {
            log("没有需要推送的运营单位信息");
            return;
        }
        HubEnterpriseInfo hubEnterpriseInfo = hubEnterpriseInfos.get(0);
        for (HubParkingInfoBO hubParkingInfo : hubParkingInfoList) {
            //发送停车场信息
            hubParkingInfo.setBizId(hubEnterpriseInfo.getBizId());
            hubParkingInfo.setOpenParkPlaceNum(hubParkingInfo.getBerths());
            // Integer类型字段如果为空则赋值为0
            convert(hubParkingInfo);

            // 发送停车场信息
            int code = ParkInfoUtil.getSingleInstance().sendParkInfo(hubConfig.getUrlPrefix(), tokenService.getToken(false), hubParkingInfo);
            if (code == 401) {
                // 重新刷新token,再次尝试发送
                ParkInfoUtil.getSingleInstance().sendParkInfo(hubConfig.getUrlPrefix(), tokenService.getToken(true), hubParkingInfo);
            }
        }
    }

    private static void convert(HubParkingInfoBO hubParkingInfo) {
        if (hubParkingInfo.getBerths() == null) hubParkingInfo.setBerths(0);
        if (hubParkingInfo.getOpenParkPlaceNum() == null) hubParkingInfo.setOpenParkPlaceNum(0);
        if (hubParkingInfo.getChargeNum() == null) hubParkingInfo.setChargeNum(0);
        if (hubParkingInfo.getEntranceNum() == null) hubParkingInfo.setEntranceNum(0);
        if (hubParkingInfo.getExitNum() == null) hubParkingInfo.setExitNum(0);
        if (hubParkingInfo.getSharedParkPlaceNum() == null) hubParkingInfo.setSharedParkPlaceNum(0);
        if (hubParkingInfo.getFreeNum() == null) hubParkingInfo.setFreeNum(0);
        if (hubParkingInfo.getLargeVehicleNum() == null) hubParkingInfo.setLargeVehicleNum(0);
        if (hubParkingInfo.getLightVehicleNum() == null) hubParkingInfo.setLightVehicleNum(0);
        if (hubParkingInfo.getParkPlaceNum01() == null) hubParkingInfo.setParkPlaceNum01(0);
        if (hubParkingInfo.getParkPlaceNum02() == null) hubParkingInfo.setParkPlaceNum02(0);
        if (hubParkingInfo.getParkPlaceNum03() == null) hubParkingInfo.setParkPlaceNum03(0);
        if (hubParkingInfo.getParkPlaceNum04() == null) hubParkingInfo.setParkPlaceNum04(0);
        if (hubParkingInfo.getParkPlaceNum05() == null) hubParkingInfo.setParkPlaceNum05(0);
        if (hubParkingInfo.getParkPlaceNum06() == null) hubParkingInfo.setParkPlaceNum06(0);
        if (hubParkingInfo.getWorkerNum() == null) hubParkingInfo.setWorkerNum(0);
        if (hubParkingInfo.getDaySmallCarChrononMinsFirstHour() == null) hubParkingInfo.setDaySmallCarChrononMinsFirstHour(0);
        if (hubParkingInfo.getDaySmallCarChrononMins() == null) hubParkingInfo.setDaySmallCarChrononMins(0);
        if (hubParkingInfo.getNightSmallCarChrononMins() == null) hubParkingInfo.setNightSmallCarChrononMins(0);
        if (hubParkingInfo.getDayBigCarChrononMinsFirstHour() == null) hubParkingInfo.setDayBigCarChrononMinsFirstHour(0);
        if (hubParkingInfo.getDayBigCarChrononMins() == null) hubParkingInfo.setDayBigCarChrononMins(0);
        if (hubParkingInfo.getNightBigCarChrononMins() == null) hubParkingInfo.setNightBigCarChrononMins(0);
        if (hubParkingInfo.getChargingStandardLeaseSmallCar() == null) hubParkingInfo.setChargingStandardLeaseSmallCar(0);
        if (hubParkingInfo.getChargingStandardLeaseBigCar() == null) hubParkingInfo.setChargingStandardLeaseBigCar(0);
        if (hubParkingInfo.getDataStatus() == null) hubParkingInfo.setDataStatus(0);

        // Float类型字段如果为空则赋值为0,不为空则保留2位小数
        if (hubParkingInfo.getChargingStandardTempSmallCarDayFirstHour() == null) {
            hubParkingInfo.setChargingStandardTempSmallCarDayFirstHour(0.0f);
        } else {
            hubParkingInfo.setChargingStandardTempSmallCarDayFirstHour(Math.round(hubParkingInfo.getChargingStandardTempSmallCarDayFirstHour() * 100.0f) / 100.0f);
        }

        if (hubParkingInfo.getChargingStandardTempSmallCarDay() == null) {
            hubParkingInfo.setChargingStandardTempSmallCarDay(0.0f);
        } else {
            hubParkingInfo.setChargingStandardTempSmallCarDay(Math.round(hubParkingInfo.getChargingStandardTempSmallCarDay() * 100.0f) / 100.0f);
        }

        if (hubParkingInfo.getChargingStandardTempSmallCarNight() == null) {
            hubParkingInfo.setChargingStandardTempSmallCarNight(0.0f);
        } else {
            hubParkingInfo.setChargingStandardTempSmallCarNight(Math.round(hubParkingInfo.getChargingStandardTempSmallCarNight() * 100.0f) / 100.0f);
        }

        if (hubParkingInfo.getChargingStandardTempBigCarDayFirstHour() == null) {
            hubParkingInfo.setChargingStandardTempBigCarDayFirstHour(0.0f);
        } else {
            hubParkingInfo.setChargingStandardTempBigCarDayFirstHour(Math.round(hubParkingInfo.getChargingStandardTempBigCarDayFirstHour() * 100.0f) / 100.0f);
        }

        if (hubParkingInfo.getChargingStandardTempBigCarDay() == null) {
            hubParkingInfo.setChargingStandardTempBigCarDay(0.0f);
        } else {
            hubParkingInfo.setChargingStandardTempBigCarDay(Math.round(hubParkingInfo.getChargingStandardTempBigCarDay() * 100.0f) / 100.0f);
        }

        if (hubParkingInfo.getChargingStandardTempBigCarNight() == null) {
            hubParkingInfo.setChargingStandardTempBigCarNight(0.0f);
        } else {
            hubParkingInfo.setChargingStandardTempBigCarNight(Math.round(hubParkingInfo.getChargingStandardTempBigCarNight() * 100.0f) / 100.0f);
        }

        if (hubParkingInfo.getTsaaFee() == null) {
            hubParkingInfo.setTsaaFee(0.0f);
        } else {
            hubParkingInfo.setTsaaFee(Math.round(hubParkingInfo.getTsaaFee() * 100.0f) / 100.0f);
        }

        if (hubParkingInfo.getTbaaFee() == null) {
            hubParkingInfo.setTbaaFee(0.0f);
        } else {
            hubParkingInfo.setTbaaFee(Math.round(hubParkingInfo.getTbaaFee() * 100.0f) / 100.0f);
        }
    }

    private void log(String msg){
        XxlJobHelper.log(msg);
        logger.info(msg);
    }
}