package com.tuoheng.admin.service.section.query;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.entity.Dept;
import com.tuoheng.admin.entity.RoadInformation;
import com.tuoheng.admin.entity.Section;
import com.tuoheng.admin.entity.SectionDept;
import com.tuoheng.admin.enums.code.section.QuerySectionListByRoadIdCodeEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 根据公路id和部门id获取该公路下路段信息列表
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-01-10
 */
@Slf4j
@Service
public class QuerySectionListByRoadIdAndDeptIdService {

    @Autowired
    private RoadInformationMapper roadInformationMapper;

    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private RoadDeptMapper roadDeptMapper;

    @Autowired
    private SectionDeptMapper sectionDeptMapper;

    @Autowired
    private DeptMapper deptMapper;

    public JsonResult getList(String roadId, String deptId) {
//        log.info("根据公路id获取该公路下路段信息列表业务");
        String tenantId = CurrentUserUtil.getTenantId();
        // 校验
        JsonResult result = this.check(tenantId, roadId, deptId);
        if (0 != result.getCode()) {
            log.info("根据公路id获取该公路下路段信息列表业务：校验失败：{}", result.getMsg());
            return result;
        }
        // 获取该部门公路下路段列表
        List<Section> sectionList;
        if (SystemConstant.ROOT_DEPT_PID.equals(deptId)) {
            sectionList = this.getSectionList(tenantId, roadId);
        } else {
            sectionList = this.getSectionList(tenantId, roadId, deptId);
        }
        if (CollectionUtil.isEmpty(sectionList)) {
            log.info("根据公路id获取该公路下路段信息列表业务：路段信息为空");
            return JsonResult.error(QuerySectionListByRoadIdCodeEnum.SECTION_LIST_IS_NULL.getCode(), QuerySectionListByRoadIdCodeEnum.SECTION_LIST_IS_NULL.getMsg());
        }
        return JsonResult.success(sectionList);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param roadId
     * @return
     */
    private JsonResult check(String tenantId, String roadId, String deptId) {
        if (StringUtils.isEmpty(roadId)) {
            return JsonResult.error(QuerySectionListByRoadIdCodeEnum.ROAD_ID_IS_NULL.getCode(), QuerySectionListByRoadIdCodeEnum.ROAD_ID_IS_NULL.getMsg());
        }
        // 判断公路是否存在
        RoadInformation roadInformation = roadInformationMapper.selectOne(new LambdaQueryWrapper<RoadInformation>()
                .eq(RoadInformation::getTenantId, tenantId)
                .eq(RoadInformation::getId, roadId)
                .eq(RoadInformation::getMark, 1));
        if (ObjectUtil.isNull(roadInformation)) {
            return JsonResult.error(QuerySectionListByRoadIdCodeEnum.ROAD_IS_NOT_EXIST.getCode(), QuerySectionListByRoadIdCodeEnum.ROAD_IS_NOT_EXIST.getMsg());
        }
        if (StringUtils.isEmpty(deptId)) {
            return JsonResult.error(QuerySectionListByRoadIdCodeEnum.DEPT_ID_IS_NULL.getCode(), QuerySectionListByRoadIdCodeEnum.DEPT_ID_IS_NULL.getMsg());
        }
        // 判断部门是否存在
        Dept dept;
        if (SystemConstant.ROOT_DEPT_PID.equals(deptId)) {
            dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
                    .eq(Dept::getTenantId, tenantId)
                    .eq(Dept::getPid, SystemConstant.ROOT_DEPT_PID)
                    .eq(Dept::getMark, 1));
        } else {
            dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
                    .eq(Dept::getTenantId, tenantId)
                    .eq(Dept::getId, deptId)
                    .eq(Dept::getMark, 1));
        }
        if (ObjectUtil.isNull(dept)) {
            return JsonResult.error(QuerySectionListByRoadIdCodeEnum.DEPT_IS_NOT_EXIST.getCode(), QuerySectionListByRoadIdCodeEnum.DEPT_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(roadInformation);
    }

    /**
     * 查询该公路下路段信息列表
     *
     * @param tenantId
     * @param roadId
     * @return
     */
    private List<Section> getSectionList(String tenantId, String roadId) {
        List<Section> sectionList = sectionMapper.selectList(new LambdaQueryWrapper<Section>()
                .eq(Section::getTenantId, tenantId)
                .eq(Section::getRoadId, roadId)
                .eq(Section::getMark, 1));
        return sectionList;
    }

    /**
     * 查询该部门公路下路段信息列表
     *
     * @param tenantId
     * @param roadId
     * @param deptId
     * @return
     */
    private List<Section> getSectionList(String tenantId, String roadId, String deptId) {
        List<SectionDept> sectionDeptList = sectionDeptMapper.selectList(new LambdaQueryWrapper<SectionDept>()
                .eq(SectionDept::getTenantId, tenantId)
                .eq(SectionDept::getDeptId, deptId));
        if (CollectionUtil.isEmpty(sectionDeptList)) {
            return null;
        }
        List<String> sectionIdList = sectionDeptList.stream().map(o -> o.getSectionId()).collect(Collectors.toList());
        List<Section> sectionList = sectionMapper.selectList(new LambdaQueryWrapper<Section>()
                .eq(Section::getTenantId, tenantId)
                .eq(Section::getRoadId, roadId)
                .in(Section::getId, sectionIdList)
                .eq(Section::getMark, 1));
        return sectionList;
    }
}
