ParkingEquipmentPushJob.java 4.63 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.HubParkingEquipment;
import com.zteits.park.domain.HubParkingEquipmentExample;
import com.zteits.park.mapper.HubEnterpriseInfoMapper;
import com.zteits.park.mapper.HubParkingEquipmentMapper;
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.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 停车场设备信息推送JOB
 * @since 2025-08-17
 */
@Component
public class ParkingEquipmentPushJob {
    private static final Logger logger = LoggerFactory.getLogger(ParkingEquipmentPushJob.class);
    @Autowired
    private HubParkingEquipmentMapper hubParkingEquipmentMapper;
    @Autowired
    private HubConfig hubConfig;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private HubEnterpriseInfoMapper hubEnterpriseInfoMapper;

    @XxlJob("ParkingEquipmentPushJob")
    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() {
        // 查询所有设备信息
        HubParkingEquipmentExample example = new HubParkingEquipmentExample();
        example.createCriteria().andDataStatusEqualTo(DataStatusEnum.DATA_STATUS_VALID.value()); // 只查询生效的数据
        List<HubParkingEquipment> equipmentList = hubParkingEquipmentMapper.selectByExample(example);
        
        if (CollectionUtils.isEmpty(equipmentList)) {
            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);
        
        // 按停车场分组
        Map<String, List<HubParkingEquipment>> equipmentByPark = equipmentList.stream()
                .collect(Collectors.groupingBy(HubParkingEquipment::getParkCode));
        
        // 遍历每个停车场的设备信息
        for (Map.Entry<String, List<HubParkingEquipment>> entry : equipmentByPark.entrySet()) {
            String parkCode = entry.getKey();
            List<HubParkingEquipment> equipments = entry.getValue();
            
            // 构造CollectEquipmentVO列表
            List<com.jtjt.upload.client.vo.CollectEquipmentVO> collectEquipmentVOs = new ArrayList<>();
            for (HubParkingEquipment equipment : equipments) {
                com.jtjt.upload.client.vo.CollectEquipmentVO vo = new com.jtjt.upload.client.vo.CollectEquipmentVO();
                vo.setBizId(hubEnterpriseInfo.getBizId());
                vo.setParkCode(parkCode);
                vo.setName(equipment.getName());
                vo.setCode(equipment.getCode());
                vo.setType(equipment.getType());
                vo.setSentTime(LocalDateTime.now());
                collectEquipmentVOs.add(vo);
            }
            
            // 发送设备信息
            int code = ParkInfoUtil.getSingleInstance().sendCollectEqiupment(hubConfig.getUrlPrefix(), tokenService.getToken(false), collectEquipmentVOs);
            if (code == 401) {
                // 重新刷新token,再次尝试发送
                ParkInfoUtil.getSingleInstance().sendCollectEqiupment(hubConfig.getUrlPrefix(), tokenService.getToken(true), collectEquipmentVOs);
            }
        }
    }

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