package com.bonus.sgzb.material.service.impl;

import com.bonus.sgzb.common.core.utils.GlobalConstants;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.material.domain.AgreementVo;
import com.bonus.sgzb.material.domain.SelectDto;
import com.bonus.sgzb.material.domain.SelectVo;
import com.bonus.sgzb.material.domain.TreeNode;
import com.bonus.sgzb.material.mapper.SelectMapper;
import com.bonus.sgzb.material.service.SelectService;
import com.bonus.sgzb.material.utils.TreeBuild;
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 10488
 * 统一下拉选
 */
@Service("SelectService")
@Slf4j
public class SelectServiceImpl implements SelectService {

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

    @Override
    public AjaxResult getUnitData(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getUnitData(dto);
        } catch (Exception e) {
            log.error("往来单位-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getProData(SelectDto dto) {
        try {
            List<SelectVo> list = mapper.getProData(dto);
            list.removeIf(item -> item == null);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error("工程-查询失败", e);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getMaTypeData(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getMaTypeData(dto);
        } catch (Exception e) {
            log.error("机具类型-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getDictByPidCbx(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getDictByPidCbx(dto);
        } catch (Exception e) {
            log.error("数据字典-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getDeptTree(SelectDto dto) {
        List<TreeNode> groupList = new ArrayList<>();
        List<TreeNode> list = new ArrayList<>();
        try {
            list = mapper.getDeptTree(dto);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                TreeBuild treeBuild = new TreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("单位树/归属部门/所属上级-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    @Override
    public AjaxResult getPostCbx(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getPostCbx(dto);
        } catch (Exception e) {
            log.error("岗位下拉选-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getRoleCbx(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getRoleCbx(dto);
        } catch (Exception e) {
            log.error("角色下拉选-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getUnitTypeCbx(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getUnitTypeCbx(dto);
        } catch (Exception e) {
            log.error("单位类型下拉选-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getDeviceTypeTree(SelectDto dto) {
        List<TreeNode> groupList = new ArrayList<>();
        List<TreeNode> list = new ArrayList<>();
        try {
            list = mapper.getDeviceTypeTree(dto);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                TreeBuild treeBuild = new TreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("单位树/归属部门/所属上级-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    @Override
    public AjaxResult getAssetAttributesCbx(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getAssetAttributesCbx(dto);
        } catch (Exception e) {
            log.error("资产属性-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getDeviceFactoryCbx(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getDeviceFactoryCbx(dto);
        } catch (Exception e) {
            log.error("机具厂家-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getProCbx(SelectDto dto) {
        List<SelectVo> list = new ArrayList<>();
        try {
            list = mapper.getProCbx(dto);
        } catch (Exception e) {
            log.error("工程项目-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getAccessoryTree() {
        List<TreeNode> groupList = new ArrayList<>();
        List<TreeNode> list = new ArrayList<>();
        try {
            list = mapper.getAccessoryTree();
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                TreeBuild treeBuild = new TreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("配件所属上级树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    @Override
    public AjaxResult getPartTree(SelectDto dto) {
        List<TreeNode> groupList = new ArrayList<>();
        List<TreeNode> list = new ArrayList<>();
        try {
            list = mapper.getPartTree(dto);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                TreeBuild treeBuild = new TreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("配件所属上级树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    @Override
    public AjaxResult getGoodsShelvesCbx(SelectDto dto) {
        List<TreeNode> groupList = new ArrayList<>();
        List<TreeNode> list = new ArrayList<>();
        try {
            list = mapper.getGoodsShelvesCbx(dto);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                TreeBuild treeBuild = new TreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("货架-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    @Override
    public AjaxResult getUserByRoleIdCbx(SelectDto dto) {
        try {
            if (Objects.equals(GlobalConstants.STRING_1, dto.getType())) {
                // 用户/维修员/库管员/采购员-下拉选
                List<SelectVo> list = new ArrayList<>();
                list = mapper.getUserByRoleIdCbxSelect(dto);
                return AjaxResult.success(list);
            } else if (Objects.equals(GlobalConstants.STRING_2, dto.getType())) {
                List<TreeNode> groupList = new ArrayList<>();
                List<TreeNode> list = new ArrayList<>();
                // 用户/维修员/库管员/采购员-树
                list = mapper.getUserByRoleIdCbxTree(dto);
                if (CollectionUtils.isNotEmpty(list)) {
                    // 创建树形结构（数据集合作为参数）
                    TreeBuild treeBuild = new TreeBuild(list);
                    // 原查询结果转换树形结构
                    groupList = treeBuild.buildTree();
                }
                return AjaxResult.success(groupList);
            }
        } catch (Exception e) {
            log.error("用户/维修员/库管员/采购员-查询失败", e);
        }
        return AjaxResult.success(null);
    }

    @Override
    public AjaxResult getAgreementInfoById(SelectDto dto) {
        AgreementVo vo = new AgreementVo();
        try {
            List<AgreementVo> list = mapper.getAgreementInfoById(dto);
            if (CollectionUtils.isNotEmpty(list)) {
                vo = list.get(0);
            }
        } catch (Exception e) {
            log.error("往来单位id和标段工程id获取协议信息", e);
        }
        return AjaxResult.success(vo);
    }


}
