package com.bs.cm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.cm.domain.CmVehicle;
import com.bs.cm.domain.CmVehicleRfidDate;
import com.bs.cm.mapper.CmVehicleRfidMapper;
import com.bs.cm.domain.CmVehicleRfid;
import com.bs.cm.service.ICmVehicleRfidDateService;
import com.bs.cm.service.ICmVehicleRfidService;
import com.bs.cm.service.ICmVehicleService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆RFID信息Service业务层处理
 *
 * @author bs
 * @date 2025-01-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CmVehicleRfidServiceImpl  extends MPJBaseServiceImpl<CmVehicleRfidMapper, CmVehicleRfid> implements ICmVehicleRfidService {
    @Resource
    private ICmVehicleService cmVehicleService;
    @Resource
    private ICmVehicleRfidDateService cmVehicleRfidDateService;
    @Override
    public List<CmVehicleRfid> getVehicleInfo(List<CmVehicleRfid> lsData) {
        if (null != lsData && lsData.size() > 0) {
            Map<Long, CmVehicle> vehicleMap = new HashMap<>();
            List<CmVehicle> lsVehicle = cmVehicleService.list();
            if (null != lsVehicle &&lsVehicle.size() > 0) {
                for (CmVehicle vehicle : lsVehicle) {
                    vehicleMap.put(vehicle.getId(), vehicle);
                }
            }
            for (CmVehicleRfid rfid : lsData) {
                if (null != rfid.getVehicleId()) {
                    CmVehicle vehicle = vehicleMap.get(rfid.getVehicleId());
                    rfid.setVehicle(vehicle);
                    List<CmVehicleRfidDate> lsDate = cmVehicleRfidDateService.list(new LambdaQueryWrapper<CmVehicleRfidDate>().eq(CmVehicleRfidDate::getRfidId, rfid.getId()));
                    if (null != lsDate && lsDate.size() > 0) {
                        rfid.setRfidDate(lsDate.get(0));
                    }
                }
            }
        }
        return lsData;
    }
}
