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

import cn.hutool.core.collection.CollectionUtil;
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.RoadDept;
import com.tuoheng.admin.entity.RoadInformation;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.road.QueryRoadListByDeptIdCodeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.RoadDeptMapper;
import com.tuoheng.admin.mapper.RoadInformationMapper;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 根据部门Id查询公路列表业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-22
 */
@Slf4j
@Service
public class QueryRoadListByDeptIdService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private RoadInformationMapper roadInformationMapper;

    @Autowired
    private RoadDeptMapper roadDeptMapper;

    public JsonResult getRoadList(String deptId) {
        log.info("进入根据部门Id查询公路列表业务");
        String tenantId = CurrentUserUtil.getTenantId();

        // 校验
        JsonResult result = this.check(tenantId, deptId);
        if (0 != result.getCode()) {
            log.info("根据部门Id查询公路列表业务：校验失败：{}", result.getMsg());
            return result;
        }

        // 在校验中，判断部门是否存在时，若部门存在，直接将部门信息返回，减少数据库查询次数
        Dept dept = (Dept) result.getData();

        // 获取公路列表
//        List<RoadInformation> roadInformationList;
//        if (SystemConstant.ROOT_DEPT_PID.equals(dept.getPid())) {
//            // 顶级部门获取所有的公路列表
//            roadInformationList = this.getAllRoadInformationList(tenantId);
//        } else {
//            // 非顶级部门获取该部门下公路列表
//            roadInformationList = this.getRoadInformationListByDeptId(tenantId, deptId);
//        }
        // 获取公路列表
        List<RoadInformation> roadInformationList = this.getRoadInformationListByDeptId(tenantId, deptId);
        if (CollectionUtil.isEmpty(roadInformationList)) {
            log.info("根据部门Id查询公路列表业务：公路信息为空");
            return JsonResult.error(QueryRoadListByDeptIdCodeEnum.ROAD_LIST_IS_NULL.getCode(), QueryRoadListByDeptIdCodeEnum.ROAD_LIST_IS_NULL.getMsg());
        }
        return JsonResult.success(roadInformationList);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param id
     * @return
     */
    private JsonResult check(String tenantId, String id) {
        if (StringUtils.isEmpty(id)) {
            return JsonResult.error(QueryRoadListByDeptIdCodeEnum.DEPT_ID_IS_NULL.getCode(), QueryRoadListByDeptIdCodeEnum.DEPT_ID_IS_NULL.getMsg());
        }

        // 判断部门是否存在
        Dept dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
                .eq(Dept::getTenantId, tenantId)
                .eq(Dept::getId, id)
                .eq(Dept::getMark, MarkEnum.VALID.getCode()));
        if (null == dept) {
            return JsonResult.error(QueryRoadListByDeptIdCodeEnum.DEPT_IS_NOT_EXIST.getCode(), QueryRoadListByDeptIdCodeEnum.DEPT_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(dept);
    }

    /**
     * 如果是顶级部门，则查询所有公路列表
     *
     * @param tenantId
     * @return
     */
    private List<RoadInformation> getAllRoadInformationList(String tenantId) {
        List<RoadInformation> roadList = roadInformationMapper.selectList(new LambdaQueryWrapper<RoadInformation>()
                .eq(RoadInformation::getTenantId, tenantId)
                .eq(RoadInformation::getMark, MarkEnum.VALID.getCode()));
        if (CollectionUtil.isEmpty(roadList)) {
            return null;
        }
        return roadList;
    }

    /**
     * 如果是非顶级部门，则查询该部门下公路信息列表
     *
     * @param tenantId
     * @param deptId
     * @return
     */
    private List<RoadInformation> getRoadInformationListByDeptId(String tenantId, String deptId) {
        List<RoadDept> roadDeptList = roadDeptMapper.selectList(new LambdaQueryWrapper<RoadDept>()
                .eq(RoadDept::getTenantId, tenantId)
                .eq(RoadDept::getDeptId, deptId));
        if (CollectionUtil.isEmpty(roadDeptList)) {
            return null;
        }
        List<String> roadIdList = roadDeptList.stream().map(o -> o.getRoadId()).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", tenantId);
        map.put("roadIdList", roadIdList);
        map.put("mark",MarkEnum.VALID.getCode());
        List<RoadInformation> roadInformationList = roadInformationMapper.getListByMap(map);
        return roadInformationList;
    }

}
