package com.engine.organization.trigger.cusfielddata;

import com.engine.organization.entity.commom.RecordInfo;
import com.engine.organization.entity.cusfielddata.po.CusFieldData;
import com.engine.organization.entity.extend.param.ExtendInfoParams;
import com.engine.organization.entity.job.po.JobPO;
import com.engine.organization.entity.postion.po.PostInfoPO;
import com.engine.organization.entity.postion.po.PostPO;
import com.engine.organization.entity.scheme.po.GradePO;
import com.engine.organization.entity.scheme.po.LevelPO;
import com.engine.organization.entity.scheme.po.SchemePO;
import com.engine.organization.entity.sequence.po.SequencePO;
import com.engine.organization.mapper.extend.ExtMapper;
import com.engine.organization.mapper.hrmresource.SystemDataMapper;
import com.engine.organization.mapper.job.JobMapper;
import com.engine.organization.mapper.post.PostInfoMapper;
import com.engine.organization.mapper.post.PostMapper;
import com.engine.organization.mapper.scheme.GradeMapper;
import com.engine.organization.mapper.scheme.LevelMapper;
import com.engine.organization.mapper.scheme.SchemeMapper;
import com.engine.organization.mapper.sequence.SequenceMapper;
import com.engine.organization.thread.HrmResourceTriggerRunnable;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:dxfeng
 * @createTime: 2022/08/01
 * @version: 1.0
 */
public class CusFieldDataTrigger {


    public static void run(Long id) {
        if (null == id) {
            // 查询人员表最新插入的ID
            id = MapperProxyFactory.getProxy(SystemDataMapper.class).getHrmResourceMaxId();
            if (null == id) {
                return;
            }
        }
        boolean updateFlag = false;
        // 查询当前操作的数据
        CusFieldData sourceData = MapperProxyFactory.getProxy(SystemDataMapper.class).getHrmCusFieldDataByIdAndScopeId(id, -1L);
        String sourceField100001 = sourceData.getField100001();
        String sourceField100002 = sourceData.getField100002();
        String sourceField100003 = sourceData.getField100003();

        // 查询需要更新的数据，没有则创建数据
        CusFieldData directionData = MapperProxyFactory.getProxy(SystemDataMapper.class).getHrmCusFieldDataByIdAndScopeId(id, 3L);
        if (null == directionData) {
            directionData = new CusFieldData();
            directionData.setScope("HrmCustomFieldByInfoType");
            directionData.setScopeId(3L);
            directionData.setId(id);
            MapperProxyFactory.getProxy(SystemDataMapper.class).insertCusFieldData(directionData);
        }

        // field100001更新,职等职级
        if (null != sourceField100001 && sourceField100001.split("A").length == 3 && !sourceField100001.equals(directionData.getField100001())) {
            directionData.setField100001(sourceField100001);
            String[] field100001Array = sourceField100001.split("A");
            // 职等
            long zdId = Long.parseLong(field100001Array[2]);
            long zjId = Long.parseLong(field100001Array[1]);
            long xlId = Long.parseLong(field100001Array[0].split("_")[1]);

            LevelPO levelByID = MapperProxyFactory.getProxy(LevelMapper.class).getLevelByID(zdId);
            directionData.setField100008(levelByID.getLevelName());
            // 职等职级方案
            SchemePO schemeByID = MapperProxyFactory.getProxy(SchemeMapper.class).getSchemeByID(levelByID.getSchemeId());
            directionData.setField100006(schemeByID.getSchemeName());
            // 职级
            GradePO gradeByLevelId = MapperProxyFactory.getProxy(GradeMapper.class).getGradeByID(zjId);
            directionData.setField100007(gradeByLevelId.getGradeName());
            // 岗位序列
            SequencePO sequenceBySchemeId = MapperProxyFactory.getProxy(SequenceMapper.class).getSequenceByID(xlId);
            directionData.setField100005(sequenceBySchemeId.getSequenceName());
            updateFlag = true;
        }

        // field100002更新
        if (StringUtils.isNotBlank(sourceField100002) && !sourceField100002.equals(directionData.getField100002())) {
            directionData.setField100002(sourceField100002);
            Long gwId = Long.parseLong(sourceField100002);
            JobPO jobById = MapperProxyFactory.getProxy(JobMapper.class).getJobById(gwId);
            RecordInfo hrmJobTitleByName = MapperProxyFactory.getProxy(SystemDataMapper.class).getHrmJobTitleByName(jobById.getJobName());
            Long ecGwId = StringUtils.isNotBlank(hrmJobTitleByName.getId()) ? Long.parseLong(hrmJobTitleByName.getId()) : null;
            Long ecBmId = jobById.getEcDepartment();
            Long ecGsId = jobById.getEcCompany();
            Map<String, Object> hrmResourceMap = new HashMap<>();
            // 岗位
            hrmResourceMap.put("jobtitle", ecGwId);
            // 部门
            hrmResourceMap.put("departmentid", ecBmId);
            // 公司
            hrmResourceMap.put("subcompanyid1", ecGsId);
            MapperProxyFactory.getProxy(ExtMapper.class).updateTable(ExtendInfoParams.builder().id(id).tableName("hrmresource").params(hrmResourceMap).build());
            updateFlag = true;
        }

        if (null != sourceField100003 && sourceField100003.split("_").length == 2 && !sourceField100003.equals(directionData.getField100003())) {
            directionData.setField100003(sourceField100003);
            String[] field100003Array = sourceField100003.split("_");
            long zwId = Long.parseLong(field100003Array[1]);
            // 职务
            PostInfoPO postInfoByID = MapperProxyFactory.getProxy(PostInfoMapper.class).getPostInfoByID(zwId);
            directionData.setField100010(postInfoByID.getPostInfoName());
            // 职务分类
            PostPO postByID = MapperProxyFactory.getProxy(PostMapper.class).getPostByID(postInfoByID.getPostId());
            directionData.setField100009(postByID.getPostName());
            updateFlag = true;
        }

        // 更新数据
        if (updateFlag) {
            MapperProxyFactory.getProxy(SystemDataMapper.class).updateCusFieldData(directionData);
        }
        // 刷新组织结构图
        new Thread(new HrmResourceTriggerRunnable(id)).start();
    }
}
