package com.engine.organization.thread;

import com.alibaba.fastjson.JSONObject;
import com.engine.organization.entity.department.po.DepartmentPO;
import com.engine.organization.entity.logview.bo.FieldBaseEquator;
import com.engine.organization.entity.map.JclOrgMap;
import com.engine.organization.mapper.department.DepartmentMapper;
import com.engine.organization.mapper.jclorgmap.JclOrgMapper;
import com.engine.organization.mapper.trigger.CompTriggerMapper;
import com.engine.organization.mapper.trigger.DepartmentTriggerMapper;
import com.engine.organization.util.OrganizationDateUtil;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.collections.CollectionUtils;
import weaver.common.DateUtil;
import weaver.hrm.passwordprotection.domain.HrmResource;

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

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

    private final DepartmentPO oldDepartment;
    private final DepartmentPO newDepartment;

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

    private DepartmentTriggerMapper getDepartmentTriggerMapper() {
        return MapperProxyFactory.getProxy(DepartmentTriggerMapper.class);
    }

    public DepartmentTriggerRunnable(DepartmentPO oldDepartment, DepartmentPO newDepartment) {
        this.oldDepartment = oldDepartment;
        this.newDepartment = newDepartment;
    }

    public DepartmentTriggerRunnable(Long departmentId) {
        this.oldDepartment = new DepartmentPO();
        this.newDepartment = MapperProxyFactory.getProxy(DepartmentMapper.class).getDeptById(departmentId);
    }

    public DepartmentTriggerRunnable(DepartmentPO newDepartment) {
        this.oldDepartment = new DepartmentPO();
        this.newDepartment = newDepartment;
        this.newDepartment.setDeleteType(1);
    }

    @Override
    public void run() {
        FieldBaseEquator fieldBaseEquator = new FieldBaseEquator();
        List<String> diffFields = fieldBaseEquator.getDiffFieldList(oldDepartment, newDepartment);
        if (CollectionUtils.isEmpty(diffFields)) {
            return;
        }
        // 判断
        if (diffFields.contains("deptName") || diffFields.contains("deptPrincipal") || diffFields.contains("parentComp") || diffFields.contains("parentDept") || diffFields.contains("forbiddenTag") || diffFields.contains("deleteType")) {
            JclOrgMap jclMap = new JclOrgMap();
            jclMap.setFType(2);
            int st = 100000000;
            // 更新逻辑
            jclMap.setFObjId(newDepartment.getId().intValue());
            jclMap.setId(newDepartment.getId().intValue() + st);
            jclMap.setUuid(newDepartment.getUuid());
            jclMap.setFNumber(newDepartment.getDeptNo());
            jclMap.setFName(newDepartment.getDeptName());
            jclMap.setFLeader(null == newDepartment.getDeptPrincipal() ? null : newDepartment.getDeptPrincipal().intValue());
            jclMap.setFParentId(null == newDepartment.getParentDept() ? newDepartment.getParentComp().intValue() : newDepartment.getParentDept().intValue() + st);
            jclMap.setFObjParentId(null == newDepartment.getParentDept() ? newDepartment.getParentComp().intValue() : newDepartment.getParentDept().intValue());

            jclMap.setFEcId(getDepartmentTriggerMapper().getEcDepartmentIdByUuid(jclMap.getUuid()));


            jclMap.setFClass(0);
            jclMap.setFClassName("行政维度");
            HrmResource hrmResourceById = getCompTriggerMapper().getHrmResourceById(jclMap.getFLeader());


            if (null != hrmResourceById) {
                jclMap.setFLeaderImg(hrmResourceById.getMessagerurl());
                jclMap.setFLeaderName(hrmResourceById.getLastname());
                jclMap.setFLeaderJobId(hrmResourceById.getJobtitle());
            }

            jclMap.setFLeaderJob(getCompTriggerMapper().getJobTitleMarkById(jclMap.getFLeaderJobId()));

            JSONObject cusFieldDataById = getCompTriggerMapper().getCusFieldDataById(jclMap.getFLeader());
            if (null != cusFieldDataById) {
                jclMap.setFLeaderSt(cusFieldDataById.getString("field100008"));
                jclMap.setFLeaderLv(cusFieldDataById.getString("field100007"));
            }
            jclMap.setFDateBegin(new Date(System.currentTimeMillis()));
            jclMap.setFDateEnd(new Date(OrganizationDateUtil.stringToDate("2099-12-31").getTime()));


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