package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.maintain.common.utils.StringUtils;
import com.maintain.common.core.page.TableDataInfo;
import com.maintain.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.maintain.business.domain.bo.ErpExamineVehicleModelBo;
import com.maintain.business.domain.vo.ErpExamineVehicleModelVo;
import com.maintain.business.domain.ErpExamineVehicleModel;
import com.maintain.business.mapper.ErpExamineVehicleModelMapper;
import com.maintain.business.service.IErpExamineVehicleModelService;

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

/**
 * 检验与车型关联Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class ErpExamineVehicleModelServiceImpl implements IErpExamineVehicleModelService {

    private final ErpExamineVehicleModelMapper baseMapper;

    /**
     * 查询检验与车型关联
     */
    @Override
    public ErpExamineVehicleModelVo queryById(Long examineId){
        return baseMapper.selectVoById(examineId);
    }

    /**
     * 查询检验与车型关联列表
     */
    @Override
    public TableDataInfo<ErpExamineVehicleModelVo> queryPageList(ErpExamineVehicleModelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpExamineVehicleModel> lqw = buildQueryWrapper(bo);
        Page<ErpExamineVehicleModelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询检验与车型关联列表
     */
    @Override
    public List<ErpExamineVehicleModelVo> queryList(ErpExamineVehicleModelBo bo) {
        LambdaQueryWrapper<ErpExamineVehicleModel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpExamineVehicleModel> buildQueryWrapper(ErpExamineVehicleModelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpExamineVehicleModel> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExamineId() != null, ErpExamineVehicleModel::getExamineId, bo.getExamineId());
        lqw.like(StringUtils.isNotBlank(bo.getExamineName()), ErpExamineVehicleModel::getExamineName, bo.getExamineName());
        lqw.eq(bo.getVehicleModelId() != null, ErpExamineVehicleModel::getVehicleModelId, bo.getVehicleModelId());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleModelCode()), ErpExamineVehicleModel::getVehicleModelCode, bo.getVehicleModelCode());
        return lqw;
    }

    /**
     * 新增检验与车型关联
     */
    @Override
    public Boolean insertByBo(ErpExamineVehicleModelBo bo) {
        ErpExamineVehicleModel add = BeanUtil.toBean(bo, ErpExamineVehicleModel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExamineId(add.getExamineId());
        }
        return flag;
    }

    /**
     * 修改检验与车型关联
     */
    @Override
    public Boolean updateByBo(ErpExamineVehicleModelBo bo) {
        ErpExamineVehicleModel update = BeanUtil.toBean(bo, ErpExamineVehicleModel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除检验与车型关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
