//package com.engine.organization.thread;
//
//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.entity.map.JclOrgMap;
//import com.engine.organization.entity.staff.po.StaffPO;
//import com.engine.organization.entity.staff.po.StaffPlanPO;
//import com.engine.organization.enums.ModuleTypeEnum;
//import com.engine.organization.mapper.comp.CompMapper;
//import com.engine.organization.mapper.department.DepartmentMapper;
//import com.engine.organization.mapper.jclorgmap.JclOrgMapper;
//import com.engine.organization.mapper.job.JobMapper;
//import com.engine.organization.mapper.staff.StaffPlanMapper;
//import com.engine.organization.mapper.trigger.StaffTriggerMapper;
//import com.engine.organization.util.OrganizationDateUtil;
//import com.engine.organization.util.db.MapperProxyFactory;
//import org.apache.commons.lang3.StringUtils;
//import weaver.common.DateUtil;
//
//import java.sql.Date;
//
///**
// * @author:dxfeng
// * @createTime: 2022/09/21
// * @version: 1.0
// */
//public class StaffTriggerRunnable implements Runnable {
//    StaffPO staffPO;
//    StaffPlanPO staffPlanByID;
//
//    private StaffPlanMapper getStaffPlanMapper() {
//        return MapperProxyFactory.getProxy(StaffPlanMapper.class);
//    }
//
//    private StaffTriggerMapper getStaffTriggerMapper() {
//        return MapperProxyFactory.getProxy(StaffTriggerMapper.class);
//    }
//
//    private CompMapper getCompMapper() {
//        return MapperProxyFactory.getProxy(CompMapper.class);
//    }
//
//    private DepartmentMapper getDepartmentMapper() {
//        return MapperProxyFactory.getProxy(DepartmentMapper.class);
//    }
//
//    private JobMapper getJobMapper() {
//        return MapperProxyFactory.getProxy(JobMapper.class);
//    }
//
//    private JclOrgMapper getJclOrgMapper() {
//        return MapperProxyFactory.getProxy(JclOrgMapper.class);
//    }
//
//    private final Date currentDate;
//    private final Date yesterday;
//
//    public StaffTriggerRunnable(StaffPO staffPO) {
//        this.staffPO = staffPO;
//        this.staffPlanByID = getStaffPlanMapper().getStaffPlanByID(staffPO.getPlanId());
//
//        currentDate = new Date(OrganizationDateUtil.stringToDate(OrganizationDateUtil.getFormatLocalDate(new java.util.Date())).getTime());
//        yesterday = new Date(OrganizationDateUtil.stringToDate(DateUtil.addDate(currentDate.toString(), -1)).getTime());
//    }
//
//    public StaffTriggerRunnable(Integer jclJobId) {
//        if (null != jclJobId) {
//            JobPO jobById = getJobMapper().getJobById((long) jclJobId);
//            if (null != jobById) {
//                this.staffPO = StaffPO.builder().compId(jobById.getEcCompany()).deptId(jobById.getEcDepartment()).jobId(jobById.getId()).build();
//                this.staffPlanByID = StaffPlanPO.builder().controlDimension("3").build();
//            }
//        }
//        currentDate = new Date(OrganizationDateUtil.stringToDate(OrganizationDateUtil.getFormatLocalDate(new java.util.Date())).getTime());
//        yesterday = new Date(OrganizationDateUtil.stringToDate(DateUtil.addDate(currentDate.toString(), -1)).getTime());
//    }
//
//    @Override
//    public void run() {
//        if (null != staffPO) {
//            if (null != staffPlanByID && StringUtils.isNotBlank(staffPlanByID.getControlDimension())) {
//                String fType = staffPlanByID.getControlDimension();
//                switch (fType) {
//                    case "1":
//                        // 更新分部编制
//                        refreshCompanyStaff(staffPO.getCompId().intValue());
//                        break;
//                    case "2":
//                        // 更新部门编制
//                        refreshDepartmentStaff(staffPO.getDeptId());
//                        refreshCompanyStaff(staffPO.getCompId().intValue());
//                        break;
//                    case "3":
//                        // 更新岗位编制
//                        refreshJobStaff(staffPO.getJobId());
//                        refreshDepartmentStaff(staffPO.getDeptId());
//                        refreshCompanyStaff(staffPO.getCompId().intValue());
//                        break;
//                    default:
//                        break;
//                }
//            }
//        }
//    }
//
//    private void refreshCompanyStaff(Integer companyId) {
//        if (null == companyId) {
//            return;
//        }
//        CompPO compPO = getCompMapper().listById(companyId);
//        if (null != compPO) {
//            updateOrgMap(ModuleTypeEnum.subcompanyfielddefined.getValue().toString(), companyId.toString());
//            if (null != compPO.getSupSubComId() && 0 != compPO.getSupSubComId()) {
//                refreshCompanyStaff(compPO.getSupSubComId());
//            } else {
//                // 刷新集团数据
//                refreshGroupStaff();
//            }
//        }
//    }
//
//    private void refreshDepartmentStaff(Integer departmentId) {
//        if (null == departmentId) {
//            return;
//        }
//        DepartmentPO deptById = getDepartmentMapper().getDeptById(departmentId);
//        if (null != deptById) {
//            updateOrgMap(ModuleTypeEnum.departmentfielddefined.getValue().toString(), departmentId.toString());
//            if (null != deptById.getSupDepId() && 0 != deptById.getSupDepId()) {
//                refreshDepartmentStaff(deptById.getSupDepId());
//            }
//        }
//    }
//
//    private void refreshJobStaff(Long jobId) {
//        if (null == jobId) {
//            return;
//        }
//        JobPO jobById = getJobMapper().getJobById(jobId);
//        if (null != jobById) {
//            updateOrgMap(ModuleTypeEnum.jobfielddefined.getValue().toString(), jobId.toString());
//            if (null != jobById.getParentJob() && 0 != jobById.getParentJob()) {
//                refreshJobStaff(jobById.getParentJob());
//            }
//        }
//    }
//
//    private void refreshGroupStaff() {
//        updateOrgMap("0", "0");
//    }
//
//
//    private void updateOrgMap(String type, String objId) {
//        Integer planSum = 0;
//        Integer onJobSum = 0;
//        switch (type) {
//            case "1":
//                planSum = getStaffTriggerMapper().countCompanyStaffNum(currentDate, objId);
//                break;
//            case "2":
//                planSum = getStaffTriggerMapper().countDepartmentStaffNum(currentDate, objId);
//                break;
//            case "3":
//                planSum = getStaffTriggerMapper().countJobStaffNum(currentDate, objId);
//                onJobSum = getStaffTriggerMapper().countOnJobByJObId(objId);
//            default:
//                break;
//        }
//
//        JclOrgMap jclOrgMapByObjID = getJclOrgMapper().getJclOrgMapByObjID(currentDate, type, objId);
//        String mapInfoId = getStaffTriggerMapper().getIdByObjIdAndType(currentDate, objId, type);
//        Integer childPlanSum = getStaffTriggerMapper().sumPlanByParentId(currentDate, mapInfoId);
//        Integer childOnJobSum = getStaffTriggerMapper().sumOnJobByParentId(currentDate, mapInfoId);
//        getJclOrgMapper().deleteMap(type, objId, currentDate);
//        getJclOrgMapper().updateMap(type, objId, currentDate, yesterday);
//        jclOrgMapByObjID.setFPlan(addInteger(planSum, childPlanSum));
//        jclOrgMapByObjID.setFOnJob(addInteger(onJobSum, childOnJobSum));
//        jclOrgMapByObjID.setFDateBegin(currentDate);
//        getJclOrgMapper().insertMap(jclOrgMapByObjID);
//    }
//
//    private Integer addInteger(Integer num1, Integer num2) {
//        num1 = num1 == null ? 0 : num1;
//        num2 = num2 == null ? 0 : num2;
//        return num1 + num2;
//    }
//}
