package com.engine.organization.util.detach;

import com.engine.organization.entity.company.po.CompPO;
import com.engine.organization.entity.department.po.DepartmentPO;
import com.engine.organization.entity.hrmresource.po.ResourcePO;
import com.engine.organization.entity.job.dto.JobListDTO;
import com.engine.organization.entity.job.po.JobPO;
import com.engine.organization.service.impl.ManagerDetachServiceImpl;
import com.weaver.general.BaseBean;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:dxfeng
 * @createTime: 2022/10/28
 * @version: 1.0
 */
public class DetachUtil {
    private boolean DETACH = "true".equals(new BaseBean().getPropValue("hrmOrganization", "detach"));

    private final List<Integer> jclRoleLevels;

    public DetachUtil(User user) {
        if (1 == user.getUID() || user.isAdmin()) {
            DETACH = false;
        }
        if (DETACH) {
            jclRoleLevels = ManagerDetachServiceImpl.getJclRoleLevels(user.getUID());
        } else {
            jclRoleLevels = new ArrayList<>();
        }
    }

    /**
     * 根据分权配置过滤分部
     */
    public void filterCompanyList(List<CompPO> companyList) {
        if (DETACH && CollectionUtils.isNotEmpty(companyList)) {
            companyList.removeIf(item -> !jclRoleLevels.contains(item.getId()));
        }
    }

    /**
     * 根据分权配置过滤分部
     */
    public void filterDepartmentList(List<DepartmentPO> departmentList) {
        if (DETACH && CollectionUtils.isNotEmpty(departmentList)) {
            departmentList.removeIf(item -> !jclRoleLevels.contains(item.getSubCompanyId1()));
        }
    }

    /**
     * 根据分权配置过滤分部
     */
    public void filterJobDTOList(List<JobListDTO> jobList) {
        if (DETACH && CollectionUtils.isNotEmpty(jobList)) {
            jobList.removeIf(item -> !jclRoleLevels.contains(item.getEcCompany()));
        }
    }

    /**
     * 根据分权配置过滤分部
     */
    public void filterJobList(List<JobPO> jobList) {
        if (DETACH && CollectionUtils.isNotEmpty(jobList)) {
            jobList.removeIf(item -> !jclRoleLevels.contains(item.getEcCompany()));
        }
    }

    public void filterResourceList(List<ResourcePO> resourcePOS) {
        if (DETACH && CollectionUtils.isNotEmpty(resourcePOS)) {
            resourcePOS.removeIf(item -> !jclRoleLevels.contains(item.getSubcompanyid1()));
        }
    }

    public String getJclRoleLevels() {
        return StringUtils.join(jclRoleLevels, ",");
    }

    public boolean isDETACH() {
        return DETACH;
    }
}
