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

import com.bonus.common.biz.domain.*;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.back.domain.BackApplyInfo;
import com.bonus.material.basic.domain.BmProject;
import com.bonus.material.basic.domain.BmUnit;
import com.bonus.material.common.domain.dto.SelectDto;
import com.bonus.material.common.domain.vo.AgreementVo;
import com.bonus.material.common.domain.vo.SelectVo;
import com.bonus.material.common.mapper.SelectMapper;
import com.bonus.material.common.service.SelectService;
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;
import java.util.stream.Collectors;

/**
 * @author 10488
 * 统一下拉选
 */
@Service("SelectService")
@Slf4j
public class SelectServiceImpl implements SelectService {

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

    /**
     * 单位下拉类型树
     * @param bmUnit
     * @return
     */
    @Override
    public AjaxResult getUnitList(BmUnit bmUnit) {
        if (bmUnit.getIsApp() != null && bmUnit.getIsApp()) {
            List<BmUnit> list = mapper.getUnitListApp(bmUnit);
            return AjaxResult.success(list);
        }
        List<ProjectTreeNode> groupList = new ArrayList<>();
        List<ProjectTreeNode> list = new ArrayList<>();
        try {
            list = mapper.getUnitList(bmUnit);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                ProjectTreeBuild treeBuild = new ProjectTreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("单位类型树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    /**
     * 工程类型树
     * @param bmProject
     * @return
     */
    @Override
    public AjaxResult getProjectList(BmProject bmProject) {
        if (bmProject.getIsApp() != null && bmProject.getIsApp()) {
            List<BmProject> list = mapper.getProjectListApp(bmProject);
            list.removeIf(Objects::isNull);
            return AjaxResult.success(list);
        }
        List<ProjectTreeNode> groupList = new ArrayList<>();
        List<ProjectTreeNode> list = new ArrayList<>();
        try {
            list = mapper.getProjectList(bmProject);
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                ProjectTreeBuild treeBuild = new ProjectTreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            log.error("工程类型树-查询失败", e);
        }
        return AjaxResult.success(groupList);
    }

    @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);
    }

    /**
     * 在用设备类型树
     * @param bean
     * @return
     */
    @Override
    public AjaxResult getUseTypeTree(BackApplyInfo bean) {
        List<TypeTreeNode> groupList = new ArrayList<>();
        List<TypeTreeNode> list = new ArrayList<>();
        List<TypeTreeNode> listL4 = new ArrayList<>();
        List<TypeTreeNode> listL3 = new ArrayList<>();
        List<TypeTreeNode> listL21 = new ArrayList<>();
        try {
            // 先查第四层类型
            listL4 = mapper.getUseTypeTreeL4(bean);
            if (CollectionUtils.isNotEmpty(listL4)) {
                List<Long> list4ParentIds = listL4.stream().map(o -> o.getParentId()).collect(Collectors.toList());
                // 根据第四层parentId 查第三层类型
                listL3 = mapper.getUseTypeTreeL3(list4ParentIds);
                List<Long> list3ParentIds = listL3.stream().map(o -> o.getParentId()).collect(Collectors.toList());
                // 根据第三层parentId 查第1.2层类型
                listL21 = mapper.getUseTypeTreeL21(list3ParentIds);
                list.addAll(listL4);
                list.addAll(listL3);
                list.addAll(listL21);
            }
            if (CollectionUtils.isNotEmpty(list)) {
                // 创建树形结构（数据集合作为参数）
                TypeTreeBuild treeBuild = new TypeTreeBuild(list);
                // 原查询结果转换树形结构
                groupList = treeBuild.buildTree();
            }
        } catch (Exception e) {
            AjaxResult.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);
            } else {
                return AjaxResult.error("未找到匹配的协议信息");
            }
        } catch (Exception e) {
            log.error("往来单位id和标段工程id获取协议信息", e);
        }
        return AjaxResult.success(vo);
    }

}
