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.TblCarDriveWarningBo;
import org.dromara.system.domain.vo.TblCarDriveWarningVo;
import org.dromara.system.domain.TblCarDriveWarning;
import org.dromara.system.mapper.TblCarDriveWarningMapper;
import org.dromara.system.service.ITblCarDriveWarningService;

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

/**
 * 驾驶监管Service业务层处理
 *
 * @author Lion Li
 * @date 2024-02-22
 */
@RequiredArgsConstructor
@Service
public class TblCarDriveWarningServiceImpl implements ITblCarDriveWarningService {

    private final TblCarDriveWarningMapper baseMapper;

    /**
     * 查询驾驶监管
     */
    @Override
    public TblCarDriveWarningVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询驾驶监管列表
     */
    @Override
    public TableDataInfo<TblCarDriveWarningVo> queryPageList(TblCarDriveWarningBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblCarDriveWarning> lqw = buildQueryWrapper(bo);
        Page<TblCarDriveWarningVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询驾驶监管列表
     */
    @Override
    public List<TblCarDriveWarningVo> queryList(TblCarDriveWarningBo bo) {
        LambdaQueryWrapper<TblCarDriveWarning> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblCarDriveWarning> buildQueryWrapper(TblCarDriveWarningBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblCarDriveWarning> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCTime() != null, TblCarDriveWarning::getCTime, bo.getCTime());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), TblCarDriveWarning::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblCarDriveWarning::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblCarDriveWarning::getExt2, bo.getExt2());
        return lqw;
    }

    /**
     * 新增驾驶监管
     */
    @Override
    public Boolean insertByBo(TblCarDriveWarningBo bo) {
        TblCarDriveWarning add = MapstructUtils.convert(bo, TblCarDriveWarning.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改驾驶监管
     */
    @Override
    public Boolean updateByBo(TblCarDriveWarningBo bo) {
        TblCarDriveWarning update = MapstructUtils.convert(bo, TblCarDriveWarning.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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