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.TblDzbcSiteBo;
import org.dromara.system.domain.vo.TblDzbcSiteVo;
import org.dromara.system.domain.TblDzbcSite;
import org.dromara.system.mapper.TblDzbcSiteMapper;
import org.dromara.system.service.ITblDzbcSiteService;

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

/**
 * 定制班车-站点Service业务层处理
 *
 * @author Lion Li
 * @date 2025-02-12
 */
@RequiredArgsConstructor
@Service
public class TblDzbcSiteServiceImpl implements ITblDzbcSiteService {

    private final TblDzbcSiteMapper baseMapper;

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

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

    /**
     * 查询定制班车-站点列表
     */
    @Override
    public List<TblDzbcSiteVo> queryList(TblDzbcSiteBo bo) {
        LambdaQueryWrapper<TblDzbcSite> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblDzbcSite> buildQueryWrapper(TblDzbcSiteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblDzbcSite> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblDzbcSite::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAddr()), TblDzbcSite::getAddr, bo.getAddr());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), TblDzbcSite::getLocation, bo.getLocation());
        lqw.eq(bo.getAreaId() != null, TblDzbcSite::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblDzbcSite::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblDzbcSite::getExt2, bo.getExt2());
        // hasSchedul: 1-只返回有未过期排班的站点 0或不传-返回全部
        if (bo.getHasSchedul() != null && bo.getHasSchedul() == 1) {
            lqw.apply("id IN (" +
                // 普通路线关联的站点：站点所属区域为路线起点或终点
                "SELECT DISTINCT s.id FROM tbl_dzbc_site s " +
                "INNER JOIN tbl_dzbc_path p ON (s.area_id = p.start_area OR s.area_id = p.end_area) " +
                "WHERE p.id IN (SELECT DISTINCT path_id FROM tbl_dzbc_path_scheduling WHERE mdate >= CURDATE() AND path_id IS NOT NULL AND (ext2 IS NULL OR ext2->'$.type' != 2))" +
                ") OR id IN (" +
                // 多站点路线直接关联的站点
                "SELECT DISTINCT site_id FROM tbl_dzbc_path_site " +
                "WHERE path_id IN (SELECT DISTINCT path_id FROM tbl_dzbc_path_scheduling WHERE mdate >= CURDATE() AND path_id IS NOT NULL AND ext2->'$.type' = 2)" +
                ")");
        }
        return lqw;
    }

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

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

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

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