package com.hotwj.platform.config.vehicleBrandModel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.config.vehicleBrandModel.domain.HotVehicleBrandModel;
import com.hotwj.platform.config.vehicleBrandModel.domain.bo.HotVehicleBrandModelBo;
import com.hotwj.platform.config.vehicleBrandModel.domain.vo.HotVehicleBrandModelVo;
import com.hotwj.platform.config.vehicleBrandModel.mapper.HotVehicleBrandModelMapper;
import com.hotwj.platform.config.vehicleBrandModel.service.IHotVehicleBrandModelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 车辆品牌型号库Service业务层处理
 *
 * @author shihongwei
 * @date 2026-02-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotVehicleBrandModelServiceImpl implements IHotVehicleBrandModelService {

    private final HotVehicleBrandModelMapper baseMapper;

    /**
     * 查询车辆品牌型号库
     *
     * @param id 主键
     * @return 车辆品牌型号库
     */
    @Override
    public HotVehicleBrandModelVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询车辆品牌型号库列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 车辆品牌型号库分页列表
     */
    @Override
    public TableDataInfo<HotVehicleBrandModelVo> queryPageList(HotVehicleBrandModelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotVehicleBrandModel> lqw = buildQueryWrapper(bo);
        Page<HotVehicleBrandModelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的车辆品牌型号库列表
     *
     * @param bo 查询条件
     * @return 车辆品牌型号库列表
     */
    @Override
    public List<HotVehicleBrandModelVo> queryList(HotVehicleBrandModelBo bo) {
        LambdaQueryWrapper<HotVehicleBrandModel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotVehicleBrandModel> buildQueryWrapper(HotVehicleBrandModelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotVehicleBrandModel> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotVehicleBrandModel::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleType()), HotVehicleBrandModel::getVehicleType, bo.getVehicleType());
        lqw.eq(StringUtils.isNotBlank(bo.getBrandModel()), HotVehicleBrandModel::getBrandModel, bo.getBrandModel());
        lqw.eq(StringUtils.isNotBlank(bo.getBodyColor()), HotVehicleBrandModel::getBodyColor, bo.getBodyColor());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateColor()), HotVehicleBrandModel::getPlateColor, bo.getPlateColor());
        lqw.eq(StringUtils.isNotBlank(bo.getFuelType()), HotVehicleBrandModel::getFuelType, bo.getFuelType());
        lqw.like(StringUtils.isNotBlank(bo.getManufacturerName()), HotVehicleBrandModel::getManufacturerName, bo.getManufacturerName());
        lqw.eq(bo.getEngineDisplacement() != null, HotVehicleBrandModel::getEngineDisplacement, bo.getEngineDisplacement());
        lqw.eq(bo.getEnginePowerKw() != null, HotVehicleBrandModel::getEnginePowerKw, bo.getEnginePowerKw());
        lqw.eq(bo.getMotorPowerKw() != null, HotVehicleBrandModel::getMotorPowerKw, bo.getMotorPowerKw());
        lqw.eq(StringUtils.isNotBlank(bo.getDriveMotorModel()), HotVehicleBrandModel::getDriveMotorModel, bo.getDriveMotorModel());
        lqw.eq(bo.getWheelbaseMm() != null, HotVehicleBrandModel::getWheelbaseMm, bo.getWheelbaseMm());
        lqw.eq(bo.getTireCount() != null, HotVehicleBrandModel::getTireCount, bo.getTireCount());
        lqw.eq(StringUtils.isNotBlank(bo.getTireSpec()), HotVehicleBrandModel::getTireSpec, bo.getTireSpec());
        lqw.eq(bo.getAxleCount() != null, HotVehicleBrandModel::getAxleCount, bo.getAxleCount());
        lqw.like(StringUtils.isNotBlank(bo.getVehicleModel()), HotVehicleBrandModel::getVehicleModel, bo.getVehicleModel());
        lqw.eq(bo.getCargoLengthMm() != null, HotVehicleBrandModel::getCargoLengthMm, bo.getCargoLengthMm());
        lqw.eq(bo.getCargoWidthMm() != null, HotVehicleBrandModel::getCargoWidthMm, bo.getCargoWidthMm());
        lqw.eq(bo.getCargoHeightMm() != null, HotVehicleBrandModel::getCargoHeightMm, bo.getCargoHeightMm());
        lqw.eq(bo.getCargoVolume() != null, HotVehicleBrandModel::getCargoVolume, bo.getCargoVolume());
        lqw.eq(StringUtils.isNotBlank(bo.getTransmission()), HotVehicleBrandModel::getTransmission, bo.getTransmission());
        lqw.eq(StringUtils.isNotBlank(bo.getRetarder()), HotVehicleBrandModel::getRetarder, bo.getRetarder());
        lqw.eq(StringUtils.isNotBlank(bo.getEmissionStandard()), HotVehicleBrandModel::getEmissionStandard, bo.getEmissionStandard());
        lqw.eq(StringUtils.isNotBlank(bo.getBatteryType()), HotVehicleBrandModel::getBatteryType, bo.getBatteryType());
        lqw.eq(bo.getInsuredSeatCount() != null, HotVehicleBrandModel::getInsuredSeatCount, bo.getInsuredSeatCount());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceBrakeType()), HotVehicleBrandModel::getServiceBrakeType, bo.getServiceBrakeType());
        lqw.eq(StringUtils.isNotBlank(bo.getFrontBrakeType()), HotVehicleBrandModel::getFrontBrakeType, bo.getFrontBrakeType());
        lqw.eq(StringUtils.isNotBlank(bo.getAbsSystem()), HotVehicleBrandModel::getAbsSystem, bo.getAbsSystem());
        lqw.eq(StringUtils.isNotBlank(bo.getRearBrakeType()), HotVehicleBrandModel::getRearBrakeType, bo.getRearBrakeType());
        lqw.eq(StringUtils.isNotBlank(bo.getParkingBrakeType()), HotVehicleBrandModel::getParkingBrakeType, bo.getParkingBrakeType());
        lqw.eq(StringUtils.isNotBlank(bo.getAirConditioning()), HotVehicleBrandModel::getAirConditioning, bo.getAirConditioning());
        lqw.eq(StringUtils.isNotBlank(bo.getChassisModel()), HotVehicleBrandModel::getChassisModel, bo.getChassisModel());
        lqw.eq(StringUtils.isNotBlank(bo.getDomesticFlag()), HotVehicleBrandModel::getDomesticFlag, bo.getDomesticFlag());
        lqw.eq(bo.getDataMissingCount() != null, HotVehicleBrandModel::getDataMissingCount, bo.getDataMissingCount());
        return lqw;
    }

    /**
     * 新增车辆品牌型号库
     *
     * @param bo 车辆品牌型号库
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotVehicleBrandModelBo bo) {
        HotVehicleBrandModel add = MapstructUtils.convert(bo, HotVehicleBrandModel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改车辆品牌型号库
     *
     * @param bo 车辆品牌型号库
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotVehicleBrandModelBo bo) {
        HotVehicleBrandModel update = MapstructUtils.convert(bo, HotVehicleBrandModel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotVehicleBrandModel entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除车辆品牌型号库信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
