package cn.iocoder.yudao.module.asset.service.vehicle;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.asset.controller.admin.vehicle.vo.VehiclePageReqVO;
import cn.iocoder.yudao.module.asset.controller.admin.vehicle.vo.VehicleRespVO;
import cn.iocoder.yudao.module.asset.controller.admin.vehicle.vo.VehicleSimpleRespVO;
import cn.iocoder.yudao.module.asset.convert.vehicle.VehicleConvert;
import cn.iocoder.yudao.module.asset.dal.dataobject.vehicle.VehicleBaseDO;
import cn.iocoder.yudao.module.asset.dal.dataobject.vehicle.VehicleBusinessDO;
import cn.iocoder.yudao.module.asset.dal.dataobject.vehicle.VehicleLocationDO;
import cn.iocoder.yudao.module.asset.dal.dataobject.vehicle.VehicleStatusDO;
import cn.iocoder.yudao.module.asset.dal.mysql.vehicle.VehicleBaseMapper;
import cn.iocoder.yudao.module.asset.dal.mysql.vehicle.VehicleBusinessMapper;
import cn.iocoder.yudao.module.asset.dal.mysql.vehicle.VehicleLocationMapper;
import cn.iocoder.yudao.module.asset.dal.mysql.vehicle.VehicleStatusMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 车辆 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    private VehicleBaseMapper vehicleBaseMapper;
    @Autowired
    private VehicleLocationMapper vehicleLocationMapper;
    @Autowired
    private VehicleBusinessMapper vehicleBusinessMapper;
    @Autowired
    private VehicleStatusMapper vehicleStatusMapper;

    @Override
    public PageResult<VehicleRespVO> getVehiclePage(VehiclePageReqVO pageReqVO) {
        // 1. 查询基础信息（带分页）
        PageResult<VehicleBaseDO> pageResult = vehicleBaseMapper.selectPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return PageResult.empty();
        }

        // 2. 提取车辆ID列表
        List<Long> vehicleIds = pageResult.getList().stream()
                .map(VehicleBaseDO::getId)
                .collect(Collectors.toList());

        // 3. 批量查询关联表
        List<VehicleLocationDO> locations = vehicleLocationMapper.selectByVehicleIds(vehicleIds);
        List<VehicleBusinessDO> businesses = vehicleBusinessMapper.selectByVehicleIds(vehicleIds);
        List<VehicleStatusDO> statuses = vehicleStatusMapper.selectByVehicleIds(vehicleIds);

        // 4. 组装数据（使用 vehicleId 作为 key）
        Map<Long, VehicleLocationDO> locationMap = CollectionUtils.convertMap(locations, VehicleLocationDO::getVehicleId);
        Map<Long, VehicleBusinessDO> businessMap = CollectionUtils.convertMap(businesses, VehicleBusinessDO::getVehicleId);
        Map<Long, VehicleStatusDO> statusMap = CollectionUtils.convertMap(statuses, VehicleStatusDO::getVehicleId);

        // 5. 转换为 VO
        List<VehicleRespVO> list = pageResult.getList().stream()
                .map(base -> VehicleConvert.INSTANCE.convert(
                        base,
                        locationMap.get(base.getId()),
                        businessMap.get(base.getId()),
                        statusMap.get(base.getId())
                ))
                .collect(Collectors.toList());

        return new PageResult<>(list, pageResult.getTotal());
    }

    @Override
    public List<VehicleSimpleRespVO> getVehicleSimpleList(VehiclePageReqVO reqVO) {
        // 只查询基础信息表，不分页
        List<VehicleBaseDO> list = vehicleBaseMapper.selectList(reqVO);
        
        // 转换为精简 VO（只包含 id、vin、plateNo）
        return VehicleConvert.INSTANCE.convertSimpleList(list);
    }

}
