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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.code.section.QuerySectionListByDeptIdCodeEnum;
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.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 QuerySectionListByDeptIdService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private RoadInformationMapper roadInformationMapper;

    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private RoadDeptMapper roadDeptMapper;

    @Autowired
    private SectionDeptMapper sectionDeptMapper;

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

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

        // 获取该部门下路段列表
        List<Section> sectionList = this.getSectionList(tenantId, id);
        if (CollectionUtil.isEmpty(sectionList)) {
            log.info("根据部门Id查询路段列表业务：路段信息为空");
            return JsonResult.error(QuerySectionListByDeptIdCodeEnum.SECTION_LIST_IS_NULL.getCode(), QuerySectionListByDeptIdCodeEnum.SECTION_LIST_IS_NULL.getMsg());
        }

        return JsonResult.success(sectionList);
    }

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

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

    /**
     * 查询该部门下路段信息列表
     *
     * @param tenantId
     * @param id
     * @return
     */
    private List<Section> getSectionList(String tenantId, String id) {
        List<SectionDept> sectionDeptList = sectionDeptMapper.selectList(new LambdaQueryWrapper<SectionDept>()
                .eq(SectionDept::getTenantId, tenantId)
                .eq(SectionDept::getDeptId, id));
        if (CollectionUtil.isEmpty(sectionDeptList)) {
            return null;
        }
        List<String> sectionIdList = sectionDeptList.stream().map(o -> o.getSectionId()).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", tenantId);
        map.put("sectionIdList", sectionIdList);
        List<Section> sectionList = sectionMapper.getListByMap(map);
        return sectionList;
    }
}
