package com.engine.organization.util.relation;

import com.engine.organization.entity.commom.RecordInfo;
import com.engine.organization.entity.company.po.CompPO;
import com.engine.organization.entity.department.po.DepartmentPO;
import com.engine.organization.entity.job.po.JobPO;
import com.engine.organization.mapper.comp.CompMapper;
import com.engine.organization.mapper.department.DepartmentMapper;
import com.engine.organization.mapper.hrmresource.SystemDataMapper;
import com.engine.organization.mapper.job.JobMapper;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * @author:dxfeng
 * @createTime: 2022/07/13
 * @version: 1.0
 */
public class EcHrmRelationUtil {

    private static final String HRM_COMPANY = "hrmsubcompany";
    private static final String HRM_DEPARTMENT = "hrmdepartment";


    private static SystemDataMapper getSystemDataMapper() {
        return MapperProxyFactory.getProxy(SystemDataMapper.class);
    }

    private static CompMapper getCompMapper() {
        return MapperProxyFactory.getProxy(CompMapper.class);
    }

    private static DepartmentMapper getDepartmentMapper() {
        return MapperProxyFactory.getProxy(DepartmentMapper.class);
    }

    private static JobMapper getJobMapper() {
        return MapperProxyFactory.getProxy(JobMapper.class);
    }

    /**
     * 根据jcl_org_comp.id获取HrmSubCompany.id
     *
     * @param companyId
     * @return
     */
    public static String getEcCompanyId(String companyId) {
        CompPO compPO = getCompMapper().listById(Long.parseLong(companyId));
        RecordInfo supSubCompany = getSystemDataMapper().getHrmObjectByUUID(HRM_COMPANY, compPO.getUuid());
        if (null == supSubCompany) {
            return "0";
        }
        return supSubCompany.getId();
    }

    /**
     * 根据jcl_org_dept.id获取HrmDepartment.id
     *
     * @param departmentId
     * @return
     */
    public static String getEcDepartmentId(String departmentId) {
        DepartmentPO departmentPO = getDepartmentMapper().getDeptById(Long.parseLong(departmentId));
        RecordInfo supDepartment = getSystemDataMapper().getHrmObjectByUUID(HRM_DEPARTMENT, departmentPO.getUuid());
        return supDepartment.getId();
    }

    public static CompPO getJclCompanyId(String ecCompanyId) {
        if (StringUtils.isBlank(ecCompanyId)) {
            return null;
        }
        RecordInfo ecCompany = getSystemDataMapper().getHrmObjectByID(HRM_COMPANY, ecCompanyId);
        if (null == ecCompany) {
            return null;
        }
        String uuid = ecCompany.getUuid();
        return getCompMapper().getCompanyByUUID(uuid);
    }


    public static DepartmentPO getJclDepartmentId(String ecDepartmentId) {
        if (StringUtils.isBlank(ecDepartmentId)) {
            return null;
        }
        RecordInfo ecDepartment = getSystemDataMapper().getHrmObjectByID(HRM_DEPARTMENT, ecDepartmentId);
        if (null == ecDepartment) {
            return null;
        }
        String uuid = ecDepartment.getUuid();
        return getDepartmentMapper().getDepartmentByUUID(uuid);
    }


    /**
     * 判断岗位名称是否共用
     * <p>存在返回true</p>
     *
     * @param jobName
     * @param id
     * @return
     */
    public static boolean isExistJob(String jobName, Long id) {
        List<JobPO> jobPOS = getJobMapper().listByNameExceptId(jobName, id);
        return CollectionUtils.isNotEmpty(jobPOS);
    }

    /**
     * 判断岗位名称是否共用
     * <p>不存在返回true</p>
     *
     * @param jobName
     * @param id
     * @return
     */
    public static boolean isNotExistJob(String jobName, Long id) {
        return !isExistJob(jobName, id);
    }

}
