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.Channel;
import com.tuoheng.admin.entity.Leg;
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;

/**
 * 根据航道id获取该航道下航段信息列表
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-24
 */
@Slf4j
@Service
public class QueryLegListByChannelIdService {

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private LegMapper legMapper;

    @Autowired
    private ChannelDeptMapper channelDeptMapper;

    @Autowired
    private LegDeptMapper legDeptMapper;

    public JsonResult getList(String channelId) {
//        log.info("根据航道id获取该航道下航段信息列表业务");
        String tenantId = CurrentUserUtil.getTenantId();

        // 校验
        JsonResult result = this.check(tenantId, channelId);
        if (0 != result.getCode()) {
            log.info("根据航道id获取该航道下航段信息列表业务：校验失败：{}", result.getMsg());
            return result;
        }

        // 获取该航道下航段列表
        List<Leg> legList = this.getLegList(tenantId, channelId);
        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) {
        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 (null == channel) {
            return JsonResult.error(QueryLegListByChannelIdCodeEnum.ROAD_IS_NOT_EXIST.getCode(), QueryLegListByChannelIdCodeEnum.ROAD_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;
    }
}
