package org.dromara.system.service.impl;

import cn.hutool.core.util.StrUtil;
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.TblDzbcCarBo;
import org.dromara.system.domain.vo.TblDzbcCarVo;
import org.dromara.system.domain.TblDzbcCar;
import org.dromara.system.mapper.TblDzbcCarMapper;
import org.dromara.system.service.ITblDzbcCarService;

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

/**
 * 定制班车-车辆Service业务层处理
 *
 * @author Lion Li
 * @date 2025-02-12
 */
@RequiredArgsConstructor
@Service
public class TblDzbcCarServiceImpl implements ITblDzbcCarService {

    private final TblDzbcCarMapper baseMapper;

    /**
     * 查询定制班车-车辆
     */
    @Override
    public TblDzbcCarVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询定制班车-车辆列表
     */
    @Override
    public TableDataInfo<TblDzbcCarVo> queryPageList(TblDzbcCarBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblDzbcCar> lqw = buildQueryWrapper(bo);
        Page<TblDzbcCarVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询定制班车-车辆列表
     */
    @Override
    public List<TblDzbcCarVo> queryList(TblDzbcCarBo bo) {
        LambdaQueryWrapper<TblDzbcCar> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblDzbcCar> buildQueryWrapper(TblDzbcCarBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblDzbcCar> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCarNum()), TblDzbcCar::getCarNum, bo.getCarNum());
        lqw.eq(bo.getCarZw() != null, TblDzbcCar::getCarZw, bo.getCarZw());
        lqw.eq(bo.getKyzId() != null, TblDzbcCar::getKyzId, bo.getKyzId());
        lqw.eq(StringUtils.isNotBlank(bo.getGps()), TblDzbcCar::getGps, bo.getGps());
        lqw.eq(bo.getDriverId() != null, TblDzbcCar::getDriverId, bo.getDriverId());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblDzbcCar::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblDzbcCar::getExt2, bo.getExt2());
        if(params!=null && params.containsKey("mdate")){
            lqw.apply(StrUtil.format("id in (select car_id from tbl_dzbc_path_scheduling where mdate >= '{}' )",params.get("mdate")));
        }
        return lqw;
    }

    /**
     * 新增定制班车-车辆
     */
    @Override
    public Boolean insertByBo(TblDzbcCarBo bo) {
        TblDzbcCar add = MapstructUtils.convert(bo, TblDzbcCar.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改定制班车-车辆
     */
    @Override
    public Boolean updateByBo(TblDzbcCarBo bo) {
        TblDzbcCar update = MapstructUtils.convert(bo, TblDzbcCar.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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