package com.hotwj.platform.resourceManagement.vehicleMaintenance.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.resourceManagement.vehicleMaintenance.domain.HotVehicleMaintenance;
import com.hotwj.platform.resourceManagement.vehicleMaintenance.domain.bo.HotVehicleMaintenanceBo;
import com.hotwj.platform.resourceManagement.vehicleMaintenance.domain.vo.HotVehicleMaintenanceVo;
import com.hotwj.platform.resourceManagement.vehicleMaintenance.mapper.HotVehicleMaintenanceMapper;
import com.hotwj.platform.resourceManagement.vehicleMaintenance.service.IHotVehicleMaintenanceService;
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 2025-12-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotVehicleMaintenanceServiceImpl implements IHotVehicleMaintenanceService {

    private final HotVehicleMaintenanceMapper baseMapper;

    /**
     * 查询车辆维护记录
     *
     * @param id 主键
     * @return 车辆维护记录
     */
    @Override
    public HotVehicleMaintenanceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询车辆维护记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 车辆维护记录分页列表
     */
    @Override
    public TableDataInfo<HotVehicleMaintenanceVo> queryPageList(HotVehicleMaintenanceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotVehicleMaintenance> lqw = buildQueryWrapper(bo);
        Page<HotVehicleMaintenanceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的车辆维护记录列表
     *
     * @param bo 查询条件
     * @return 车辆维护记录列表
     */
    @Override
    public List<HotVehicleMaintenanceVo> queryList(HotVehicleMaintenanceBo bo) {
        LambdaQueryWrapper<HotVehicleMaintenance> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据车辆ID查询最新的一条维护记录
     *
     * @param vehicleId 车辆ID
     * @return 最新维护记录
     */
    @Override
    public HotVehicleMaintenanceVo queryLatestByVehicleId(String vehicleId) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<HotVehicleMaintenance>()
            .eq(HotVehicleMaintenance::getVehicleId, vehicleId)
                .eq(HotVehicleMaintenance::getIsDeleted, 0L)
            .orderByDesc(HotVehicleMaintenance::getFinishTime)
            .last("LIMIT 1"));
    }

    private LambdaQueryWrapper<HotVehicleMaintenance> buildQueryWrapper(HotVehicleMaintenanceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotVehicleMaintenance> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotVehicleMaintenance::getId);
        lqw.eq(bo.getCompanyId() != null, HotVehicleMaintenance::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getVehicleId() != null, HotVehicleMaintenance::getVehicleId, bo.getVehicleId());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNumber()), HotVehicleMaintenance::getPlateNumber, bo.getPlateNumber());
        lqw.eq(bo.getMaintainDate() != null, HotVehicleMaintenance::getMaintainDate, bo.getMaintainDate());
        lqw.eq(bo.getFinishTime() != null, HotVehicleMaintenance::getFinishTime, bo.getFinishTime());
        lqw.eq(StringUtils.isNotBlank(bo.getTransferor()), HotVehicleMaintenance::getTransferor, bo.getTransferor());
        lqw.eq(StringUtils.isNotBlank(bo.getEntryInspectImageUrls()), HotVehicleMaintenance::getEntryInspectImageUrls, bo.getEntryInspectImageUrls());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessInspectImageUrls()), HotVehicleMaintenance::getProcessInspectImageUrls, bo.getProcessInspectImageUrls());
        lqw.eq(StringUtils.isNotBlank(bo.getFinalInspectImageUrls()), HotVehicleMaintenance::getFinalInspectImageUrls, bo.getFinalInspectImageUrls());
        lqw.eq(StringUtils.isNotBlank(bo.getFactoryCertImageUrls()), HotVehicleMaintenance::getFactoryCertImageUrls, bo.getFactoryCertImageUrls());
        lqw.eq(bo.getIsDeleted() != null, HotVehicleMaintenance::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增车辆维护记录
     *
     * @param bo 车辆维护记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotVehicleMaintenanceBo bo) {
        HotVehicleMaintenance add = MapstructUtils.convert(bo, HotVehicleMaintenance.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改车辆维护记录
     *
     * @param bo 车辆维护记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotVehicleMaintenanceBo bo) {
        HotVehicleMaintenance update = MapstructUtils.convert(bo, HotVehicleMaintenance.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotVehicleMaintenance entity){
        if (entity.getMaintainDate() != null && entity.getFinishTime() != null) {
            if (!entity.getFinishTime().after(entity.getMaintainDate())) {
                throw new IllegalArgumentException("维护完成时间必须大于维护日期");
            }
        }
    }

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