package com.securitycontrol.system.base.service.impl;

import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.entity.system.TreeBuild;
import com.securitycontrol.entity.system.dto.MenuDto;
import com.securitycontrol.entity.system.dto.SelectDto;
import com.securitycontrol.entity.system.vo.SelectVo;
import com.securitycontrol.entity.system.vo.TreeNode;
import com.securitycontrol.system.base.mapper.ISelectMapper;
import com.securitycontrol.system.base.service.ISelectService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author：cwchen
 * @date：2024-02-22-9:37
 * @version：1.0
 * @description：公共下拉选/树-业务层逻辑层
 */
@Service(value = "ISelectService")
@Slf4j
public class SelectServiceImpl implements ISelectService {

    @Resource(name = "ISelectMapper")
    private ISelectMapper mapper;

    @Override
    public AjaxResult getOrgTree() {
        List<TreeNode> groupList = new ArrayList<>();
        try {
            List<TreeNode> list = mapper.getOrgTree();
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                groupList = TreeBuild.transTreeList(list);
            }
        } catch (Exception e) {
            log.error("组织机构树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    @Override
    public AjaxResult getRoleLists() {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getRoleLists();
        } catch (Exception e) {
            log.error("角色下拉选",e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getMenuTree(MenuDto dto) {
        List<TreeNode> groupList = new ArrayList<>();
        try {
            List<TreeNode> list = mapper.getMenuTree();
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                if (dto.getMenuId() != null) {
                    list.forEach(item -> {
                        if (Objects.equals(dto.getMenuId() + "", item.getId())) {
                            item.setDisabled(true);
                        }
                    });
                }
                groupList = TreeBuild.transTreeList(list);
            }
        } catch (Exception e) {
            log.error("菜单树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }


    /**
     * 字典下拉选
     *
     * @return
     */
    @Override
    public AjaxResult getDictList(String code) {
        List<TreeNode> groupList = new ArrayList<>();
        try {

            List<TreeNode> list = mapper.getDictList(code);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                groupList = TreeBuild.transTreeList(list);
            }
        } catch (Exception e) {
            log.error("组织机构树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    @Override
    public AjaxResult getBuildLists() {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getBuildLists();
        } catch (Exception e) {
            log.error("建管单位下拉选",e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getDictLists(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getDictLists(dto);
        } catch (Exception e) {
            log.error("字典表下拉选",e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getTowerLists(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getTowerLists(dto);
        } catch (Exception e) {
            log.error("杆塔下拉选",e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getTeamLists(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getTeamLists(dto);
        } catch (Exception e) {
            log.error("班组下拉选",e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getProLists(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getProLists(dto);
        } catch (Exception e) {
            log.error("工程下拉选",e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getAreaLists(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getAreaLists(dto);
        } catch (Exception e) {
            log.error("区域下拉选");
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getDictSelect(String code) {
        List<TreeNode> groupList = new ArrayList<>();
        try {
            groupList = mapper.getDictList(code);
        } catch (Exception e) {
            log.error("组织机构树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    /**
     *  工程下拉选
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getProList(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try{
            list=mapper.getProList(dto);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getSelectLists() {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getSelectLists();
        } catch (Exception e) {
            log.error("告警管理下拉选",e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getGtLists(String bidCode) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getGtLists(bidCode);
        } catch (Exception e) {
            log.error("杆塔表下拉选",e);
        }
        return AjaxResult.success(list);
    }


    @Override
    public AjaxResult getBdLists(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getBdLists(dto);
        } catch (Exception e) {
            log.error("边带下拉选");
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getProjectList(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getProjectList(dto);
        } catch (Exception e) {
            log.error("项目下拉选");
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getSignProList(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getSignProList(dto);
        } catch (Exception e) {
            log.error("单项工程下拉选");
        }
        return AjaxResult.success(list);
    }
}
