package cn.iocoder.yudao.module.pms.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.pms.controller.admin.iotcarzhbd.vo.IotCarZhbdPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotCarDevicePlateVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcarzhbd.IotCarZhbdDO;
import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.iotcarzhbd.IotCarZhbdMapper;
import cn.iocoder.yudao.module.pms.util.ZHBDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 根据设备表中的 车牌号 对应的 中航北斗的 vehicle_id carId
 */
@Component
@Slf4j
public class IotAllZhbdJob implements JobHandler {

    @Autowired
    private IotDeviceMapper iotDeviceMapper;
    @Autowired
    private ZHBDUtil zhbdUtil;

    @Autowired
    private IotCarZhbdMapper iotCarZhbdMapper;

    @Override
    @TenantIgnore
    public String execute(String param) throws Exception {
        // 查询设备表中 所有设备名称包含车牌号的 车辆
        List<IotCarDevicePlateVO> carContainsPlates = iotDeviceMapper.allCarsContainsPlates();
        Set<String> uniquePlates = new HashSet<>();
        // key车牌号   value车辆对象(设备编码、设备名称、设备id)
        Map<String, IotCarDevicePlateVO> plateDevicePair = new HashMap<>();
        if (CollUtil.isNotEmpty(carContainsPlates)) {
            carContainsPlates.forEach(plate -> {
                uniquePlates.add(plate.getLicensePlate());
                plateDevicePair.put(plate.getLicensePlate(), plate);
            });
            // 将集合中的车辆牌号拼接成逗号分隔的字符串
            if (CollUtil.isNotEmpty(uniquePlates)) {
                // 查询出 rq_iot_car_zhbd 表中已经存在的所有 carId
                IotCarZhbdPageReqVO reqVO = new IotCarZhbdPageReqVO();
                List<IotCarZhbdDO> existCars = iotCarZhbdMapper.selectList(reqVO);
                Set<String> existCarIds = new HashSet<>();
                if (CollUtil.isNotEmpty(existCars)) {
                    existCars.forEach(car -> {
                        existCarIds.add(car.getCarId());
                    });
                }

                String uniquePlatesStr = StrUtil.join(",", uniquePlates);
                log.info("逗号分隔的车辆号：{}", uniquePlatesStr);
                // key车牌号   value中航北斗carId
                Map<String, Integer> carLocationPair = zhbdUtil.carLocations(uniquePlatesStr);
                // 将接口返回的 在本地表不存在的车辆信息保存
                List<IotCarZhbdDO> tobeAddedCars = new ArrayList<>();
                // 筛选出 中航北斗 carId 不在表 rq_iot_car_zhbd 中存在的记录
                if (CollUtil.isNotEmpty(carLocationPair)) {
                    carLocationPair.forEach((plateNum, carId) -> {
                        if (!existCarIds.contains(String.valueOf(carId))) {
                            IotCarZhbdDO car = new IotCarZhbdDO();
                            car.setCarId(String.valueOf(carId));
                            if (plateDevicePair.containsKey(plateNum)) {
                                IotCarDevicePlateVO carDevice = plateDevicePair.get(plateNum);
                                if (ObjUtil.isNotEmpty(carDevice)) {
                                    car.setDeviceCode(carDevice.getDeviceCode());
                                    car.setDeviceName(carDevice.getDeviceName());
                                }
                            }
                            tobeAddedCars.add(car);
                        }
                    });
                    if (CollUtil.isNotEmpty(tobeAddedCars)) {
                        iotCarZhbdMapper.insertBatch(tobeAddedCars);
                    }
                }
            }
        }
        return "success";
    }
}
