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

import com.glsx.usedcar.gsc.ReceiveClient;
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>
 * 从收车端同步存量车辆的过户资料(electronicContractField)数据
 * </p>
 */
@Slf4j
@Component
public class TransferMaterialsSyncSchedule {

    @Resource
    private VehicleDao vehicleDao;

    @Resource
    private VehicleService vehicleService;

    @Resource
    private ReceiveClient receiveClient;

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

    /**
     * 定时同步过户资料
     * <p>
     * 每小时执行一次,同步收车端来源的车辆过户资料
     * </p>
     */
    @Scheduled(fixedDelay = 1000 * 60 * 60, initialDelay = 1000 * 60 * 5)
    public void syncTransferMaterials() {
        log.info("开始执行过户资料同步定时任务");
        int totalSynced = 0;
        int totalFailed = 0;

        // 分页查询收车端来源的车辆
        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, String> electronicContractFieldMap = receiveClient.batchQueryElectronicContractField(sourceNos);

                        // 更新车辆过户资料
                        for (Vehicle vehicle : vehicles) {
                            String sourceNo = vehicle.getBaseInfo().getSourceNo();
                            String electronicContractField = electronicContractFieldMap.get(sourceNo);

                            if (electronicContractField != null && !electronicContractField.isEmpty()) {
                                try {
                                    vehicleService.updateTransferMaterials(vehicle.getVehicleId(), electronicContractField);
                                    totalSynced++;
                                } catch (Exception e) {
                                    log.error("更新车辆过户资料失败, vehicleId: {}", vehicle.getVehicleId(), e);
                                    totalFailed++;
                                }
                            }
                        }
                    } catch (Exception e) {
                        log.error("批次 {} 查询过户资料失败", page, e);
                        totalFailed += vehicles.size();
                    }
                }
            }

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

        log.info("过户资料同步定时任务完成,成功: {}, 失败: {}", totalSynced, totalFailed);
    }
}