package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.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 org.dromara.system.domain.bo.TblCarServiceBo;
import org.dromara.system.domain.vo.TblCarServiceVo;
import org.dromara.system.domain.TblCarService;
import org.dromara.system.mapper.TblCarServiceMapper;
import org.dromara.system.service.ITblCarServiceService;

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

/**
 * 车务信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-02-22
 */
@RequiredArgsConstructor
@Service
public class TblCarServiceServiceImpl implements ITblCarServiceService {

    private final TblCarServiceMapper baseMapper;

    /**
     * 查询车务信息
     */
    @Override
    public TblCarServiceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询车务信息列表
     */
    @Override
    public TableDataInfo<TblCarServiceVo> queryPageList(TblCarServiceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblCarService> lqw = buildQueryWrapper(bo);
        Page<TblCarServiceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询车务信息列表
     */
    @Override
    public List<TblCarServiceVo> queryList(TblCarServiceBo bo) {
        LambdaQueryWrapper<TblCarService> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblCarService> buildQueryWrapper(TblCarServiceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblCarService> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCarId() != null, TblCarService::getCarId, bo.getCarId());
        lqw.eq(StringUtils.isNotBlank(bo.getMileageInfo()), TblCarService::getMileageInfo, bo.getMileageInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getOilInfo()), TblCarService::getOilInfo, bo.getOilInfo());
        lqw.eq(bo.getRepairInfo() != null, TblCarService::getRepairInfo, bo.getRepairInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckInfo()), TblCarService::getCheckInfo, bo.getCheckInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblCarService::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblCarService::getExt2, bo.getExt2());
        return lqw;
    }

    /**
     * 新增车务信息
     */
    @Override
    public Boolean insertByBo(TblCarServiceBo bo) {
        TblCarService add = MapstructUtils.convert(bo, TblCarService.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改车务信息
     */
    @Override
    public Boolean updateByBo(TblCarServiceBo bo) {
        TblCarService update = MapstructUtils.convert(bo, TblCarService.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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