package com.engine.organization.thread;

import com.engine.common.util.ServiceUtil;
import com.engine.hrm.service.impl.OrganizationServiceImpl;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.util.OrganizationAssert;
import com.engine.organization.util.relation.ResourceSyncUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;

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

/**
 * @author:dxfeng
 * @createTime: 2022/08/26
 * @version: 1.0
 */
public class OrganizationSyncEc {
    private final LogModuleNameEnum moduleName;
    private final OperateTypeEnum operateType;
    private Map<String, Object> params;
    private final User user;
    Map<String, Object> resultMap;
    private boolean throwException = true;



    public OrganizationSyncEc(User user, LogModuleNameEnum moduleName, OperateTypeEnum operateType, Map<String, Object> params) {
        this.moduleName = moduleName;
        this.operateType = operateType;
        this.params = params;
        this.user = user;
    }

    public OrganizationSyncEc(User user, LogModuleNameEnum moduleName, OperateTypeEnum operateType, Map<String, Object> params, boolean throwException) {
        this.moduleName = moduleName;
        this.operateType = operateType;
        this.params = params;
        this.user = user;
        this.throwException = throwException;
    }


    public Map<String, Object> sync() {
        switch (moduleName) {
            case COMPANY:
                refreshCompany();
                break;
            case DEPARTMENT:
                refreshDepartment();
                break;
            case RESOURCE:
                refreshResource();
            default:
                break;
        }
        if (throwException && null != resultMap) {
            OrganizationAssert.isTrue("1".equals(Util.null2String(resultMap.get("status"))) || "1".equals(Util.null2String(resultMap.get("sign"))), Util.null2String(resultMap.get("message")));
        }
        return resultMap;
    }


    /**
     * 刷新HrmSubCompany表数据
     */
    private void refreshCompany() {
        switch (operateType) {
            case ADD:
                addCompany();
                break;
            case UPDATE:
                updateCompany();
                break;
            case CANCELED:
                cancelCompany();
                break;
            default:
                break;
        }
    }

    /**
     * 刷新HrmDepartment表数据
     */
    private void refreshDepartment() {
        switch (operateType) {
            case ADD:
                addDepartment();
                break;
            case UPDATE:
                updateDepartment();
                break;
            case CANCELED:
                cancelDepartment();
                break;
            default:
                break;
        }
    }


    private void refreshResource() {
        switch (operateType) {
            case ADD:
                addResource();
                break;
            case UPDATE:
                updateResource();
                break;
            case CANCELED:
            default:
                break;
        }
    }

    private void addResource() {
        Map<String, Object> convertParams = ResourceSyncUtil.convertEcResourceParams(params);
        this.resultMap = ResourceSyncUtil.addResource(user, convertParams);
    }

    private void updateResource() {
        Map<String, Object> convertParams = ResourceSyncUtil.convertEcResourceParams(params);
        this.resultMap = ResourceSyncUtil.editResourceBase(user, convertParams);
    }


    /**
     * 新增部门
     */
    private void addDepartment() {
        this.resultMap = ServiceUtil.getService(OrganizationServiceImpl.class, user).addDepartment(params, user);
    }

    /**
     * 封存、解封部门
     */
    private void cancelDepartment() {
        String forbiddenTag = Util.null2String(params.get("forbiddenTag"));
        if ("0".equals(forbiddenTag)) {
            // 解封
            this.resultMap = ServiceUtil.getService(OrganizationServiceImpl.class, user).doDepartmentISCanceled(params, user);
        } else {
            // 封存
            this.resultMap = ServiceUtil.getService(OrganizationServiceImpl.class, user).doDepartmentCancel(params, user);
        }
    }

    /**
     * 更新部门
     */
    private void updateDepartment() {
        Map<String, Object> map = new HashMap<>();
        buildEcDepartmentData(map);
        map.putAll(params);
        this.resultMap = ServiceUtil.getService(OrganizationServiceImpl.class, user).editDepartment(map, user);
    }

    /**
     * 新增分部
     */
    private void addCompany() {
        this.resultMap = ServiceUtil.getService(OrganizationServiceImpl.class, user).addSubCompany(params, user);
    }

    /**
     * 更新分部
     */
    private void updateCompany() {
        Map<String, Object> map = new HashMap<>();
        buildEcCompanyData(map);
        map.putAll(params);
        this.resultMap = ServiceUtil.getService(OrganizationServiceImpl.class, user).editSubCompany(map, user);
    }

    /**
     * 封存、解封分部
     */
    private void cancelCompany() {
        // TODO
        String forbiddenTag = Util.null2String(params.get("forbiddenTag"));
        if ("0".equals(forbiddenTag)) {
            // 解封
            this.resultMap = ServiceUtil.getService(OrganizationServiceImpl.class, user).doSubCompanyISCanceled(params, user);
        } else {
            // 删除封存、禁用封存
            this.resultMap = ServiceUtil.getService(OrganizationServiceImpl.class, user).doSubCompanyCancel(params, user);
        }
    }

    private void buildEcCompanyData(Map<String, Object> map) {
        String ecCompanyId = Util.null2String(params.get("id"));

        map.put("id", ecCompanyId);
        RecordSet rs = new RecordSet();
        // 先查拓展表
        rs.execute("select * from hrmsubcompanydefined where subcomid = '" + ecCompanyId + "'");
        int colcount = rs.getColCounts();
        if (rs.next()) {
            for (int i = 1; i <= colcount; i++) {
                map.put(rs.getColumnName(i).toLowerCase(), Util.null2String(rs.getString(i)));
            }
        }
        // 再查主表
        rs.execute("select * from hrmsubcompany where id = '" + ecCompanyId + "'");
        colcount = rs.getColCounts();
        if (rs.next()) {
            for (int i = 1; i <= colcount; i++) {
                map.put(rs.getColumnName(i).toLowerCase(), Util.null2String(rs.getString(i)));
            }
        }
    }

    private void buildEcDepartmentData(Map<String, Object> map) {
        String ecDepartment = Util.null2String(params.get("id"));

        map.put("id", ecDepartment);
        RecordSet rs = new RecordSet();
        // 先查拓展表
        rs.execute("select * from hrmdepartmentdefined where deptid = '" + ecDepartment + "'");
        int colcount = rs.getColCounts();
        if (rs.next()) {
            for (int i = 1; i <= colcount; i++) {
                map.put(rs.getColumnName(i).toLowerCase(), Util.null2String(rs.getString(i)));
            }
        }
        // 再查主表
        rs.execute("select * from hrmdepartment where id = '" + ecDepartment + "'");
        colcount = rs.getColCounts();
        if (rs.next()) {
            for (int i = 1; i <= colcount; i++) {
                map.put(rs.getColumnName(i).toLowerCase(), Util.null2String(rs.getString(i)));
            }
        }
    }

}
