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.TblCarPathBo;
import org.dromara.system.domain.vo.TblCarPathVo;
import org.dromara.system.domain.TblCarPath;
import org.dromara.system.mapper.TblCarPathMapper;
import org.dromara.system.service.ITblCarPathService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 车辆路线Service业务层处理
 *
 * @author Lion Li
 * @date 2024-02-22
 */
@RequiredArgsConstructor
@Service
public class TblCarPathServiceImpl implements ITblCarPathService {

    private final TblCarPathMapper baseMapper;

    /**
     * 查询车辆路线
     */
    @Override
    public TblCarPathVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询车辆路线列表
     */
    @Override
    public TableDataInfo<TblCarPathVo> queryPageList(TblCarPathBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblCarPath> lqw = buildQueryWrapper(bo);
        Page<TblCarPathVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询车辆路线列表
     */
    @Override
    public List<TblCarPathVo> queryList(TblCarPathBo bo) {
        LambdaQueryWrapper<TblCarPath> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblCarPath> buildQueryWrapper(TblCarPathBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblCarPath> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblCarPath::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStationList()), TblCarPath::getStationList, bo.getStationList());
        lqw.eq(bo.getDriverInfo() != null, TblCarPath::getDriverInfo, bo.getDriverInfo());
        lqw.eq(bo.getAttendantInfo() != null, TblCarPath::getAttendantInfo, bo.getAttendantInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblCarPath::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblCarPath::getExt2, bo.getExt2());
        if(bo.getParams()!=null && bo.getParams().get("station")!=null){
            lqw.like(TblCarPath::getStationList, bo.getParams().get("station"));
        }



        return lqw;
    }

    /**
     * 新增车辆路线
     */
    @Override
    public Boolean insertByBo(TblCarPathBo bo) {
        TblCarPath add = MapstructUtils.convert(bo, TblCarPath.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改车辆路线
     */
    @Override
    public Boolean updateByBo(TblCarPathBo bo) {
        TblCarPath update = MapstructUtils.convert(bo, TblCarPath.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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