package com.tuoheng.admin.service.leg.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.Channel;
import com.tuoheng.admin.entity.Leg;
import com.tuoheng.admin.entity.LegDept;
import com.tuoheng.admin.enums.code.leg.QueryLegListByChannelIdCodeEnum;
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 QueryLegListByChannelIdAndDeptIdService {

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private LegMapper legMapper;

    @Autowired
    private ChannelDeptMapper channelDeptMapper;

    @Autowired
    private LegDeptMapper legDeptMapper;

    @Autowired
    private DeptMapper deptMapper;

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

    /**
     * 检查参数
     *
     * @param tenantId
     * @param channelId
     * @return
     */
    private JsonResult check(String tenantId, String channelId, String deptId) {
        if (StringUtils.isEmpty(channelId)) {
            return JsonResult.error(QueryLegListByChannelIdCodeEnum.ROAD_ID_IS_NULL.getCode(), QueryLegListByChannelIdCodeEnum.ROAD_ID_IS_NULL.getMsg());
        }
        // 判断航道是否存在
        Channel channel = channelMapper.selectOne(new LambdaQueryWrapper<Channel>()
                .eq(Channel::getTenantId, tenantId)
                .eq(Channel::getId, channelId)
                .eq(Channel::getMark, 1));
        if (ObjectUtil.isNull(channel)) {
            return JsonResult.error(QueryLegListByChannelIdCodeEnum.ROAD_IS_NOT_EXIST.getCode(), QueryLegListByChannelIdCodeEnum.ROAD_IS_NOT_EXIST.getMsg());
        }
        if (StringUtils.isEmpty(deptId)) {
            return JsonResult.error(QueryLegListByChannelIdCodeEnum.DEPT_ID_IS_NULL.getCode(), QueryLegListByChannelIdCodeEnum.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(QueryLegListByChannelIdCodeEnum.DEPT_IS_NOT_EXIST.getCode(), QueryLegListByChannelIdCodeEnum.DEPT_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(channel);
    }

    /**
     * 查询该航道下航段信息列表
     *
     * @param tenantId
     * @param channelId
     * @return
     */
    private List<Leg> getLegList(String tenantId, String channelId) {
        List<Leg> legList = legMapper.selectList(new LambdaQueryWrapper<Leg>()
                .eq(Leg::getTenantId, tenantId)
                .eq(Leg::getChannelId, channelId)
                .eq(Leg::getMark, 1));
        return legList;
    }

    /**
     * 查询该部门航道下航段信息列表
     *
     * @param tenantId
     * @param channelId
     * @param deptId
     * @return
     */
    private List<Leg> getLegList(String tenantId, String channelId, String deptId) {
        List<LegDept> legDeptList = legDeptMapper.selectList(new LambdaQueryWrapper<LegDept>()
                .eq(LegDept::getTenantId, tenantId)
                .eq(LegDept::getDeptId, deptId));
        if (CollectionUtil.isEmpty(legDeptList)) {
            return null;
        }
        List<String> legIdList = legDeptList.stream().map(o -> o.getLegId()).collect(Collectors.toList());
        List<Leg> legList = legMapper.selectList(new LambdaQueryWrapper<Leg>()
                .eq(Leg::getTenantId, tenantId)
                .eq(Leg::getChannelId, channelId)
                .in(Leg::getId, legIdList)
                .eq(Leg::getMark, 1));
        return legList;
    }
}
