package com.engine.organization.thread;

import com.engine.organization.entity.job.po.JobPO;
import com.engine.organization.entity.logview.bo.FieldBaseEquator;
import com.engine.organization.entity.map.JclOrgMap;
import com.engine.organization.mapper.jclorgmap.JclOrgMapper;
import com.engine.organization.mapper.job.JobMapper;
import com.engine.organization.mapper.trigger.CompTriggerMapper;
import com.engine.organization.mapper.trigger.JobTriggerMapper;
import com.engine.organization.util.OrganizationDateUtil;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.collections.CollectionUtils;
import weaver.common.DateUtil;

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

/**
 * @author:dxfeng
 * @createTime: 2022/08/30
 * @version: 1.0
 */
public class JobTriggerRunnable implements Runnable {

    private final JobPO oldJob;
    private final JobPO newJob;

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

    private JobTriggerMapper getJobTriggerMapper() {
        return MapperProxyFactory.getProxy(JobTriggerMapper.class);
    }

    public JobTriggerRunnable(JobPO oldJob, JobPO newJob) {
        this.oldJob = oldJob;
        this.newJob = newJob;
    }

    public JobTriggerRunnable(Long jobId) {
        this.oldJob = new JobPO();
        this.newJob = MapperProxyFactory.getProxy(JobMapper.class).getJobById(jobId);
    }

    public JobTriggerRunnable(JobPO newJob) {
        this.oldJob = new JobPO();
        this.newJob = newJob;
        this.newJob.setDeleteType(1);
    }

    @Override
    public void run() {
        FieldBaseEquator fieldBaseEquator = new FieldBaseEquator();
        List<String> diffFields = fieldBaseEquator.getDiffFieldList(oldJob, newJob);
        if (CollectionUtils.isEmpty(diffFields)) {
            return;
        }
        // 判断
        if (diffFields.contains("jobName") || diffFields.contains("parentDept") || diffFields.contains("parentJob") || diffFields.contains("forbiddenTag") || diffFields.contains("deleteType")) {
            JclOrgMap jclMap = new JclOrgMap();
            int st = 100000000;
            int sj = 200000000;
            jclMap.setFType(3);
            // 更新逻辑
            jclMap.setFObjId(newJob.getId().intValue());
            jclMap.setId(newJob.getId().intValue() + sj);
            jclMap.setFNumber(newJob.getJobNo());
            jclMap.setFName(newJob.getJobName());
            jclMap.setFParentId(null == newJob.getParentJob() ? newJob.getParentDept().intValue() + st : newJob.getParentJob().intValue() + sj);
            jclMap.setFObjParentId(null == newJob.getParentJob() ? newJob.getParentDept().intValue() : newJob.getParentJob().intValue());
            Integer parentdept = newJob.getParentDept().intValue();

            jclMap.setFClass(0);
            jclMap.setFClassName("行政维度");

            jclMap.setFDateBegin(new Date(System.currentTimeMillis()));
            jclMap.setFDateEnd(new Date(OrganizationDateUtil.stringToDate("2099-12-31").getTime()));


            jclMap.setFPlan(getJobTriggerMapper().sumStaffNum(jclMap.getFDateBegin(), jclMap.getFObjId()));
            jclMap.setFOnJob(getJobTriggerMapper().countHrmResource(parentdept, jclMap.getFName()));
            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 (1 != newJob.getDeleteType() && 1 != newJob.getForbiddenTag()) {
                MapperProxyFactory.getProxy(JclOrgMapper.class).insertMap(jclMap);
            }


        }
    }
}
