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.TblDzbcPathBo;
import org.dromara.system.domain.vo.TblDzbcPathVo;
import org.dromara.system.domain.TblDzbcPath;
import org.dromara.system.mapper.TblDzbcPathMapper;
import org.dromara.system.service.ITblDzbcPathService;

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

/**
 * 定制班车-路线Service业务层处理
 *
 * @author Lion Li
 * @date 2025-02-12
 */
@RequiredArgsConstructor
@Service
public class TblDzbcPathServiceImpl implements ITblDzbcPathService {

    private final TblDzbcPathMapper baseMapper;

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

    @Override
    public List<TblDzbcPathVo> queryByIds(Collection<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return List.of();
        }
        return baseMapper.selectVoBatchIds(ids);
    }

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

    /**
     * 查询定制班车-路线列表
     */
    @Override
    public List<TblDzbcPathVo> queryList(TblDzbcPathBo bo) {
        LambdaQueryWrapper<TblDzbcPath> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblDzbcPath> buildQueryWrapper(TblDzbcPathBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblDzbcPath> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblDzbcPath::getName, bo.getName());
        lqw.eq(bo.getStartArea() != null, TblDzbcPath::getStartArea, bo.getStartArea());
        lqw.eq(bo.getEndArea() != null, TblDzbcPath::getEndArea, bo.getEndArea());
        lqw.eq(StringUtils.isNotBlank(bo.getMtime()), TblDzbcPath::getMtime, bo.getMtime());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblDzbcPath::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblDzbcPath::getExt2, bo.getExt2());
        // hasSchedul: 1-只返回有未过期排班的路线 0或不传-返回全部
        if (bo.getHasSchedul() != null && bo.getHasSchedul() == 1) {
            lqw.apply("id IN (SELECT DISTINCT path_id FROM tbl_dzbc_path_scheduling WHERE path_id IS NOT NULL AND mdate >= CURDATE())");
        }
        if(params != null && !params.isEmpty()){
            if(params.containsKey("recommend")){
                lqw.apply(StringUtils.format(" ext1->'$.recommend' = {}", params.get("recommend").toString()));
            }
            if( params.containsKey("mdate")){
                lqw.apply(StrUtil.format("id in (select path_id from tbl_dzbc_path_scheduling where mdate >= '{}' )",params.get("mdate")));
            }
        }
        return lqw;
    }

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

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

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

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