package com.engine.organization.thread;

import com.alibaba.fastjson.JSONObject;
import com.engine.organization.entity.map.JclOrgMap;
import com.engine.organization.mapper.jclorgmap.JclOrgMapper;
import com.engine.organization.mapper.trigger.CompTriggerMapper;
import com.engine.organization.mapper.trigger.HrmResourceTriggerMapper;
import com.engine.organization.util.OrganizationDateUtil;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.lang.StringUtils;
import weaver.common.DateUtil;
import weaver.hrm.passwordprotection.domain.HrmResource;

import java.sql.Date;
import java.util.Calendar;

/**
 * @author:dxfeng
 * @createTime: 2022/09/01
 * @version: 1.0
 */
public class HrmResourceTriggerRunnable implements Runnable {
    Long userId;

    private CompTriggerMapper getCompTriggerMapper() {
        return MapperProxyFactory.getProxy(CompTriggerMapper.class);
    }

    private HrmResourceTriggerMapper getHrmResourceTriggerMapper() {
        return MapperProxyFactory.getProxy(HrmResourceTriggerMapper.class);
    }

    public HrmResourceTriggerRunnable(Long id) {
        this.userId = id;
    }

    @Override
    public void run() {
        int st = 100000000;
        int sj = 200000000;
        int sk = 300000000;
        Integer delete = null;
        JclOrgMap jclMap = new JclOrgMap();
        jclMap.setFType(4);
        // 查询当前人员信息数据
        HrmResource hrmResource = getHrmResourceTriggerMapper().getHrmResource(userId);
        if (null != hrmResource) {
            jclMap.setFObjId(hrmResource.getId());
            jclMap.setId(hrmResource.getId() + sk);
            jclMap.setFNumber(hrmResource.getWorkcode());
            jclMap.setFName(hrmResource.getLastname());
            delete = hrmResource.getStatus() <= 3 ? 0 : 1;
            jclMap.setFLeaderImg(hrmResource.getMessagerurl());
            jclMap.setFLeaderName(hrmResource.getLastname());
            jclMap.setFLeaderJobId(hrmResource.getJobtitle());
            JSONObject cusFieldDataById = getHrmResourceTriggerMapper().getCusFieldDataById(jclMap.getFObjId());
            if (null != cusFieldDataById) {
                String field100002 = cusFieldDataById.getString("field100002");
                if (StringUtils.isNotBlank(field100002)) {
                    int index = field100002.indexOf('_');
                    jclMap.setFObjParentId(Integer.parseInt(field100002.substring(index + 1)));
                    jclMap.setFParentId(jclMap.getFObjParentId() + sj);
                }
                jclMap.setFLeaderSt(cusFieldDataById.getString("field100008"));
                jclMap.setFLeaderLv(cusFieldDataById.getString("field100007"));
            }
            jclMap.setFClass(0);
            jclMap.setFClassName("行政维度");
            jclMap.setFLeaderJob(getCompTriggerMapper().getJobTitleMarkById(jclMap.getFLeaderJobId()));
            jclMap.setFDateBegin(new Date(System.currentTimeMillis()));
            jclMap.setFDateEnd(new Date(OrganizationDateUtil.stringToDate("2099-12-31").getTime()));
            jclMap.setFIsVitual(0);

            Calendar cal = Calendar.getInstance();
            cal.setTime(jclMap.getFDateBegin());
            Calendar calendar = DateUtil.addDay(cal, -1);
            Date time = new Date(calendar.getTime().getTime());

            getCompTriggerMapper().deleteMap(jclMap.getFType(), jclMap.getFObjId(), jclMap.getFDateBegin());
            getCompTriggerMapper().updateMap(jclMap.getFType(), jclMap.getFObjId(), jclMap.getFDateBegin(), time);
            if (0 == delete) {
                MapperProxyFactory.getProxy(JclOrgMapper.class).insertMap(jclMap);
            }
        }


    }
}
