TransferMaterialsSyncSchedule.java 6.78 KB
package com.glsx.usedcar.task;

import com.glsx.usedcar.gsc.GscInvoker;
import com.glsx.usedcar.gsc.dto.VehicleSyncDataDTO;
import com.glsx.usedcar.module.system.enums.SystemConfigKeyEnum;
import com.glsx.usedcar.module.system.service.SystemConfigService;
import com.glsx.usedcar.module.vehicle.dao.VehicleDao;
import com.glsx.usedcar.module.vehicle.entity.Vehicle;
import com.glsx.usedcar.module.vehicle.entity.Vehicle_;
import com.glsx.usedcar.module.vehicle.service.VehicleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 车辆同步数据定时任务
 * <p>
 * 从收车端同步存量车辆的过户资料和评估人员信息
 * </p>
 * <p>
 * 同步状态说明(通过 system_config.transfer_materials_synced 控制):
 * <ul>
 *   <li>0: 未执行,开始执行</li>
 *   <li>1: 执行中,跳过</li>
 *   <li>2: 已完成,跳过</li>
 * </ul>
 * </p>
 */
@Slf4j
@Component
public class TransferMaterialsSyncSchedule {

    @Resource
    private VehicleDao vehicleDao;

    @Resource
    private VehicleService vehicleService;

    @Resource
    private GscInvoker gscInvoker;

    @Resource
    private SystemConfigService systemConfigService;

    /**
     * 每批次处理数量
     */
    private static final int BATCH_SIZE = 100;

    /**
     * 同步状态常量
     */
    private static final String STATUS_NOT_SYNCED = "0";
    private static final String STATUS_SYNCING = "1";
    private static final String STATUS_SYNCED = "2";

    /**
     * 定时同步车辆数据
     * <p>
     * 每30分钟执行一次,检查同步状态,仅当状态为0时执行
     * </p>
     */
    @Scheduled(fixedDelay = 1000 * 60 * 30)
    public void syncTransferMaterials() {
        String configKey = SystemConfigKeyEnum.TRANSFER_MATERIALS_SYNCED.getKey();
        String status = systemConfigService.getConfigValueAsString(configKey, STATUS_NOT_SYNCED);

        // 状态为2(已完成)或1(执行中),跳过
        if (STATUS_SYNCED.equals(status)) {
            log.debug("过户资料同步任务已完成,跳过");
            return;
        }
        if (STATUS_SYNCING.equals(status)) {
            log.info("过户资料同步任务执行中,跳过");
            return;
        }

        // 状态为0(未执行),开始同步
        log.info("开始执行过户资料同步任务");
        int totalSynced = 0;
        int totalFailed = 0;

        try {
            // 更新状态为执行中
            systemConfigService.updateConfigValue(configKey, STATUS_SYNCING, "system");

            // 分页查询收车端来源的车辆
            int page = 0;
            Page<Vehicle> vehiclePage;

            do {
                Specification<Vehicle> spec = (root, query, cb) ->
                        cb.equal(root.get(Vehicle_.vehicleSource), Vehicle.VehicleSource.RECEIVE);

                vehiclePage = vehicleDao.findAll(spec, PageRequest.of(page, BATCH_SIZE));

                if (vehiclePage.hasContent()) {
                    List<Vehicle> vehicles = vehiclePage.getContent();
                    List<String> sourceNos = new ArrayList<>();

                    // 收集车源号
                    for (Vehicle vehicle : vehicles) {
                        if (vehicle.getBaseInfo() != null && vehicle.getBaseInfo().getSourceNo() != null) {
                            sourceNos.add(vehicle.getBaseInfo().getSourceNo());
                        }
                    }

                    if (!sourceNos.isEmpty()) {
                        try {
                            // 批量查询车辆同步数据(过户资料 + 评估人员信息)
                            Map<String, VehicleSyncDataDTO> syncDataMap = gscInvoker.batchQueryVehicleSyncData(sourceNos);

                            // 更新车辆数据
                            for (Vehicle vehicle : vehicles) {
                                String sourceNo = vehicle.getBaseInfo().getSourceNo();
                                VehicleSyncDataDTO syncData = syncDataMap.get(sourceNo);

                                if (syncData != null && syncData.hasData()) {
                                    boolean updated = false;

                                    // 更新过户资料
                                    if (syncData.hasElectronicContractField()) {
                                        try {
                                            vehicleService.updateTransferMaterials(vehicle.getVehicleId(), syncData.getElectronicContractField());
                                            updated = true;
                                        } catch (Exception e) {
                                            log.error("更新车辆过户资料失败, vehicleId: {}", vehicle.getVehicleId(), e);
                                        }
                                    }

                                    // 更新评估人员信息
                                    if (syncData.hasEvalOperatorInfo()) {
                                        try {
                                            vehicleService.updateEvalOperatorInfo(
                                                    vehicle.getVehicleId(),
                                                    syncData.getEvalOperatorName(),
                                                    syncData.getEvalOperatorPhone()
                                            );
                                            updated = true;
                                        } catch (Exception e) {
                                            log.error("更新车辆评估人员信息失败, vehicleId: {}", vehicle.getVehicleId(), e);
                                        }
                                    }

                                    if (updated) {
                                        totalSynced++;
                                    }
                                }
                            }
                        } catch (Exception e) {
                            log.error("批次 {} 查询同步数据失败", page, e);
                            totalFailed += vehicles.size();
                        }
                    }
                }

                page++;
            } while (vehiclePage.hasNext());

            log.info("车辆同步数据任务完成,成功: {}, 失败: {}", totalSynced, totalFailed);

        } finally {
            // 更新状态为已完成
            systemConfigService.updateConfigValue(configKey, STATUS_SYNCED, "system");
        }
    }
}