ParkingChargingPilePushJob.java 4.27 KB
package com.zteits.park.job.datahub;

import java.time.LocalDateTime;
import java.util.List;

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 com.jtjt.upload.client.vo.ChargePileVO;
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.HubParkingChargingPile;
import com.zteits.park.domain.HubParkingChargingPileExample;
import com.zteits.park.mapper.HubEnterpriseInfoMapper;
import com.zteits.park.mapper.HubParkingChargingPileMapper;
import com.zteits.park.util.ParkInfoUtil;

/**
 * 停车场充电桩信息数据推送JOB
 * @since 2025-08-17
 */
@Component
public class ParkingChargingPilePushJob {
    private static final Logger logger = LoggerFactory.getLogger(ParkingChargingPilePushJob.class);
    @Autowired
    private HubParkingChargingPileMapper hubParkingChargingPileMapper;
    @Autowired
    private HubConfig hubConfig;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private HubEnterpriseInfoMapper hubEnterpriseInfoMapper;

    @XxlJob("ParkingChargingPilePushJob")
    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() {
        HubParkingChargingPileExample example = new HubParkingChargingPileExample();
        example.createCriteria().andDataStatusEqualTo(DataStatusEnum.DATA_STATUS_VALID.value()); // 1-生效
        List<HubParkingChargingPile> hubParkingChargingPileList = hubParkingChargingPileMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(hubParkingChargingPileList)) {
            log("没有需要推送的停车场充电桩信息");
            return;
        }

        // 查询运营单位信息
        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 (HubParkingChargingPile hubParkingChargingPile : hubParkingChargingPileList) {
            ChargePileVO chargePileVO = new ChargePileVO();
            chargePileVO.setBizId(hubEnterpriseInfo.getBizId());
            chargePileVO.setParkCode(hubParkingChargingPile.getParkCode());
            chargePileVO.setChargePileNum(hubParkingChargingPile.getChargePileNum());
            chargePileVO.setDcChargePileNum(hubParkingChargingPile.getDcChargePileNum());
            chargePileVO.setAcChargePileNum(hubParkingChargingPile.getAcChargePileNum());
            chargePileVO.setChargeStationOperationName(hubParkingChargingPile.getChargeStationOperationName());
            java.util.Date sentTime = hubParkingChargingPile.getSentTime();
            chargePileVO.setSentTime(sentTime != null ? LocalDateTime.ofInstant(sentTime.toInstant(), java.time.ZoneId.systemDefault()) : null);

            // 发送充电桩信息
            int code = ParkInfoUtil.getSingleInstance().sendChargePile(hubConfig.getUrlPrefix(), tokenService.getToken(false), chargePileVO);
            if (code == 401) {
                // 重新刷新token,再次尝试发送
                ParkInfoUtil.getSingleInstance().sendChargePile(hubConfig.getUrlPrefix(), tokenService.getToken(true), chargePileVO);
            }
        }
    }

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