package com.xindazhou.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xindazhou.framework.common.exception.util.ServiceExceptionUtil;
import com.xindazhou.vehicle.api.dto.VehicleRespDTO;
import com.xindazhou.vehicle.convert.vehicle.VehicleConvert;
import com.xindazhou.vehicle.dal.dataobject.binding.UserVehicleBindingDO;
import com.xindazhou.vehicle.dal.dataobject.vehicle.VehicleDO;
import com.xindazhou.vehicle.dal.mysql.binding.UserVehicleBindingMapper;
import com.xindazhou.vehicle.dal.mysql.vehicle.VehicleMapper;
import com.xindazhou.vehicle.service.VehicleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

import static com.xindazhou.vehicle.enums.ErrorCodeConstants.VEHICLE_NOT_EXISTS;

/**
 * 车辆信息 Service 实现类
 *
 * @author xindazhou
 */
@Service
@Slf4j
public class VehicleServiceImpl implements VehicleService {

    @Resource
    private VehicleMapper vehicleMapper;

    @Resource
    private UserVehicleBindingMapper userVehicleBindingMapper;

    @Override
    public VehicleRespDTO getVehicle(Long id) {
        VehicleDO vehicle = vehicleMapper.selectById(id);
        if (vehicle == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_NOT_EXISTS);
        }
        return VehicleConvert.INSTANCE.convert(vehicle);
    }

    @Override
    public VehicleRespDTO getVehicleByVin(String vin) {
        VehicleDO vehicle = vehicleMapper.selectByVin(vin);
        if (vehicle == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_NOT_EXISTS);
        }
        return VehicleConvert.INSTANCE.convert(vehicle);
    }

    @Override
    public List<VehicleRespDTO> listVehicleByUserId(Long userId, Integer userType) {
        // 通过user_vehicle_binding表查询用户绑定的车辆
        List<UserVehicleBindingDO> bindings = userVehicleBindingMapper.selectListByUserId(userId);
        List<Long> vehicleIds = bindings.stream()
                .map(UserVehicleBindingDO::getVehicleId)
                .collect(Collectors.toList());
        
        if (vehicleIds.isEmpty()) {
            return List.of();
        }
        
        // 批量查询车辆信息
        List<VehicleDO> vehicles = vehicleMapper.selectList(
                new LambdaQueryWrapper<VehicleDO>()
                        .in(VehicleDO::getId, vehicleIds)
                        .eq(VehicleDO::getDeleted, false)
        );
        return vehicles.stream()
                .map(VehicleConvert.INSTANCE::convert)
                .collect(Collectors.toList());
    }

    @Override
    public void updateVehicle(Long id, String name, String imageUrl) {
        VehicleDO vehicle = vehicleMapper.selectById(id);
        if (vehicle == null) {
            throw ServiceExceptionUtil.exception(VEHICLE_NOT_EXISTS);
        }
        
        // 更新车辆信息
        if (name != null) {
            vehicle.setNickname(name);
        }
        // TODO: imageUrl需要存储到vehicle表或vehicle_image表
        // 暂时先更新nickname
        
        vehicleMapper.updateById(vehicle);
    }

}

