package com.engine.organization.thread;

import com.alibaba.fastjson.JSONObject;
import com.engine.organization.entity.company.po.CompPO;
import com.engine.organization.entity.logview.bo.FieldBaseEquator;
import com.engine.organization.entity.map.JclOrgMap;
import com.engine.organization.mapper.comp.CompMapper;
import com.engine.organization.mapper.jclorgmap.JclOrgMapper;
import com.engine.organization.mapper.trigger.CompTriggerMapper;
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 CompanyTriggerRunnable implements Runnable {

    private final CompPO oldCompany;
    private final CompPO newCompany;

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

    public CompanyTriggerRunnable(CompPO oldCompany, CompPO newCompany) {
        this.oldCompany = oldCompany;
        this.newCompany = newCompany;
    }

    public CompanyTriggerRunnable(Long companyId) {
        this.oldCompany = new CompPO();
        this.newCompany = MapperProxyFactory.getProxy(CompMapper.class).listById(companyId);
    }

    public CompanyTriggerRunnable(CompPO newCompany) {
        this.oldCompany = new CompPO();
        this.newCompany = newCompany;
        this.newCompany.setDeleteType(1);
    }

    @Override
    public void run() {
        FieldBaseEquator fieldBaseEquator = new FieldBaseEquator();
        List<String> diffFields = fieldBaseEquator.getDiffFieldList(oldCompany, newCompany);
        if (CollectionUtils.isEmpty(diffFields)) {
            return;
        }
        // 判断
        if (diffFields.contains("compName") || diffFields.contains("compPrincipal") || diffFields.contains("parentCompany") || diffFields.contains("forbiddenTag") || diffFields.contains("deleteType")) {
            JclOrgMap jclMap = new JclOrgMap();

            jclMap.setFType(1);
            // 更新逻辑
            jclMap.setFObjId(newCompany.getId().intValue());
            jclMap.setId(newCompany.getId().intValue());
            jclMap.setUuid(newCompany.getUuid());
            jclMap.setFNumber(newCompany.getCompNo());
            jclMap.setFName(newCompany.getCompName());
            jclMap.setFLeader(newCompany.getCompPrincipal());
            jclMap.setFParentId(null == newCompany.getParentCompany() ? 0 : newCompany.getParentCompany().intValue());
            jclMap.setFObjParentId(null == newCompany.getParentCompany() ? 0 : newCompany.getParentCompany().intValue());
            jclMap.setFEcId(getCompTriggerMapper().getEcCompanyIdByUuid(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(getCompTriggerMapper().sumStaffNum(jclMap.getFDateBegin(), jclMap.getFObjId()));
            jclMap.setFOnJob(getCompTriggerMapper().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 != newCompany.getDeleteType() && 1 != newCompany.getForbiddenTag()) {
                MapperProxyFactory.getProxy(JclOrgMapper.class).insertMap(jclMap);
            }


        }
    }
}
