package com.tuoheng.admin.service.leg.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.leg.QueryLegListByDeptIdCodeEnum;
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 QueryLegListByDeptIdService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private LegMapper legMapper;

    @Autowired
    private ChannelDeptMapper channelDeptMapper;

    @Autowired
    private LegDeptMapper legDeptMapper;

    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<Leg> legList = this.getLegList(tenantId, id);
        if (CollectionUtil.isEmpty(legList)) {
            log.info("根据部门Id查询航段列表业务：航段信息为空");
            return JsonResult.error(QueryLegListByDeptIdCodeEnum.SECTION_LIST_IS_NULL.getCode(), QueryLegListByDeptIdCodeEnum.SECTION_LIST_IS_NULL.getMsg());
        }

        return JsonResult.success(legList);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param id
     * @return
     */
    private JsonResult check(String tenantId, String id) {
        if (StringUtils.isEmpty(id)) {
            return JsonResult.error(QueryLegListByDeptIdCodeEnum.DEPT_ID_IS_NULL.getCode(), QueryLegListByDeptIdCodeEnum.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(QueryLegListByDeptIdCodeEnum.DEPT_IS_NOT_EXIST.getCode(), QueryLegListByDeptIdCodeEnum.DEPT_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(dept);
    }

    /**
     * 查询该部门下航段信息列表
     *
     * @param tenantId
     * @param id
     * @return
     */
    private List<Leg> getLegList(String tenantId, String id) {
        List<LegDept> legDeptList = legDeptMapper.selectList(new LambdaQueryWrapper<LegDept>()
                .eq(LegDept::getTenantId, tenantId)
                .eq(LegDept::getDeptId, id));
        if (CollectionUtil.isEmpty(legDeptList)) {
            return null;
        }
        List<String> legIdList = legDeptList.stream().map(o -> o.getLegId()).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", tenantId);
        map.put("legIdList", legIdList);
        List<Leg> legList = legMapper.getListByMap(map);
        return legList;
    }
}
