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.RoadInformation;
import com.tuoheng.admin.entity.Section;
import com.tuoheng.admin.enums.code.section.QuerySectionListByRoadIdCodeEnum;
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 QuerySectionListByRoadIdService {

    @Autowired
    private RoadInformationMapper roadInformationMapper;

    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private RoadDeptMapper roadDeptMapper;

    @Autowired
    private SectionDeptMapper sectionDeptMapper;

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

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

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

        return JsonResult.success(sectionList);
    }

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

        // 判断部门是否存在
        RoadInformation roadInformation = roadInformationMapper.selectOne(new LambdaQueryWrapper<RoadInformation>()
                .eq(RoadInformation::getTenantId, tenantId)
                .eq(RoadInformation::getId, roadId)
                .eq(RoadInformation::getMark, 1));
        if (null == roadInformation) {
            return JsonResult.error(QuerySectionListByRoadIdCodeEnum.ROAD_IS_NOT_EXIST.getCode(), QuerySectionListByRoadIdCodeEnum.ROAD_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(roadInformation);
    }

    /**
     * 查询该公路下路段信息列表
     *
     * @param tenantId
     * @param roadId
     * @return
     */
    private List<Section> getSectionList(String tenantId, String roadId) {
        List<Section> sectionList = sectionMapper.selectList(new LambdaQueryWrapper<Section>()
                .eq(Section::getTenantId, tenantId)
                .eq(Section::getRoadId, roadId)
                .eq(Section::getMark, 1));
        return sectionList;
    }
}
