package cn.iocoder.yudao.module.pms.controller.admin.stat;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
public class DeptUtil {
    @Resource
    private DeptApi deptApi;
    @Resource
    private DeptService deptService;

    public String getCompanyCode(Long deptId) {
        DeptRespDTO dept = deptApi.getDeptNoPermission(deptId);
        String name = dept.getName();
        String companyCode = "";
        if (name.contains("瑞恒")) {
            companyCode = "rh";
        } else if (name.contains("瑞都")) {
            companyCode = "rd";
        } else if (name.contains("瑞鹰")) {
            companyCode = "ry";
        } else if (name.contains("科瑞石油技术")) {
            companyCode = "jt";
        } else {
            Long parentId = dept.getParentId();
            DeptRespDTO dept1 = deptApi.getDeptNoPermission(parentId);
            String name1 = dept1.getName();
            if (name1.contains("瑞恒")) {
                companyCode = "rh";
            } else if (name1.contains("瑞都")) {
                companyCode = "rd";
            } else if (name1.contains("瑞鹰")) {
                companyCode = "ry";
            } else if (name1.contains("科瑞石油技术")) {
                companyCode = "jt";
            } else {
                DeptRespDTO dept2 = deptApi.getDeptNoPermission(dept1.getParentId());
                String name2 = dept2.getName();
                if (name2.contains("瑞恒")) {
                    companyCode = "rh";
                } else if (name2.contains("瑞都")) {
                    companyCode = "rd";
                } else if (name2.contains("瑞鹰")) {
                    companyCode = "ry";
                } else {
                    DeptRespDTO dept3= deptApi.getDeptNoPermission(dept2.getParentId());
                    String name3 = dept3.getName();
                    if (name3.contains("瑞恒")) {
                        companyCode = "rh";
                    } else if (name3.contains("瑞都")) {
                        companyCode = "rd";
                    } else if (name3.contains("瑞鹰")) {
                        companyCode = "ry";
                    }
                }
            }
        }
        return companyCode;
    }

    public Set<Long> getDeptIds(String dept) {
        Set<Long> ids = new HashSet<>();
        List<DeptRespDTO> depts = new ArrayList<>();
        if ("rd".equals(dept)) {
            depts = deptApi.getDeptByName("四川瑞都");
        } else if ("rh".equals(dept)) {
            depts = deptApi.getDeptByName("瑞恒兴域");
        } else if ("ry".equals(dept)) {
            depts = deptApi.getDeptByName("瑞鹰国际");
        } else if ("jt".equals(dept)) {
            depts = deptApi.getDeptByName("科瑞石油技术");
        } else {
            Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
            DeptRespDTO dto = deptApi.getDept(loginUserDeptId);
            depts = deptApi.getDeptByName(dto.getName());
        }
        if (CollUtil.isNotEmpty(depts)) {
            Long deptId = depts.get(0).getId();
            if (Objects.nonNull(depts.get(0).getId())) {
                ids = deptService.getChildDeptIdListFromCache(depts.get(0).getId());
                ids.add(deptId);
            }
        }
        return  ids;
    }
}
