package com.weaver.seconddev.interfaces.workflow.action;

import java.util.HashMap;
import java.util.Map;
//import weaver.matrix.MatrixUtil;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import java.lang.*;
import com.weaver.verupgrade.workflow.request.RequestManager;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.common.base.entity.result.WeaResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.hrm.company.DepartmentComInfo;
import com.weaver.verupgrade.hrm.company.SubCompanyComInfo;
import com.weaver.verupgrade.hrm.resource.ResourceComInfo;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;



//不启用
@org.springframework.stereotype.Service("workflow_action_ChangeOrgRoleAction")
public class ChangeOrgRoleAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;

    private static final Logger logger_c08ea2e1 = LoggerFactory.getLogger(ChangeOrgRoleAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        Map<String, Object> weaverResultMap = new HashMap<>();
        Long requestIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("requestid", -1)));
        Long userIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("userid", -1)));
        RequestInfo request = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            String requestid = request.getRequestid();
            //TODO custom 不启用
//            try {
//                logger_c08ea2e1.error(String.valueOf(requestid + "开始修改组织架构权限"));
//                Property[] properties = request.getMainTableInfo().getProperty();
//                Map<String, String> map = new HashMap<String, String>();
//                for (int i = 0; i < properties.length; i++) {
//                    String name = properties[i].getName().toUpperCase();
//                    String value = Util.null2String(properties[i].getValue());
//                    map.put(name, value);
//                }
//                String org = "";
//                String role = "";
//                String sql = "";
//                String resorceid = map.get("XJSYY");
//                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
//                if ("0".equals(map.get("ZZJGGG"))) {
//                    org = map.get("XTZGS");
//                    role = map.get("XTZFBJS");
//                    role = role.substring(role.indexOf("_") + 1);
//                    sql = "update formdata_detail as  HrmSubcompanyDefined set " + role + "='" + resorceid + "' where subcomid=" + org;
//                    String poolname_i8 = CONN_TYPE.workflow.getType();
//                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//                    rs.executeSql(sql, poolname_i8);
//                    SubCompanyComInfo subCompanyComInfo = I18nContextUtil.getBean(SubCompanyComInfo.class);
//                    subCompanyComInfo.removeCompanyCache();
//                    String poolname_v1 = CONN_TYPE.workflow.getType();
//                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//                    rs.executeSql("UPDATE orgchartstate SET orgchartstate.needupdate = 1 WHERE orgchartstate.delete_type = 0  AND orgchartstate.tenant_key = 't6kj9c07jr'", poolname_v1);
                    //TODO custom 不启用 矩阵
////                    MatrixUtil.updateSubcompayData(org);
//                } else if ("1".equals(map.get("ZZJGGG"))) {
//                    org = map.get("XTZBM");
//                    role = map.get("XTZBMJS");
//                    role = role.substring(role.indexOf("_") + 1);
//                    sql = "update HrmDepartmentDefined set " + role + "='" + resorceid + "' where deptid=" + org;
//                    String poolname = CONN_TYPE.workflow.getType();
//                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//                    rs.executeSql(sql, poolname);
//                    DepartmentComInfo departmentComInfo = I18nContextUtil.getBean(DepartmentComInfo.class);
//                    departmentComInfo.removeCompanyCache();
//                    ResourceComInfo ResourceComInfo = I18nContextUtil.getBean(ResourceComInfo.class);
//                    ResourceComInfo.removeResourceCache();
//                    String poolname_ou = CONN_TYPE.workflow.getType();
                   // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//                    rs.executeSql("UPDATE orgchartstate SET orgchartstate.needupdate = 1 WHERE orgchartstate.delete_type = 0  AND orgchartstate.tenant_key = 't6kj9c07jr'", poolname_ou);
                    //TODO custom 不启用 矩阵
////                    MatrixUtil.updateDepartmentData(org);
//                }
//                logger_c08ea2e1.error(String.valueOf(requestid + "修改组织架构权限结束"));
//            } catch (Exception e) {
//                logger_c08ea2e1.error(String.valueOf(requestid + "修改组织架构权限出错：" + e.toString()));
//            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_c08ea2e1.error("ChangeOrgRoleAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "ChangeOrgRoleAction e: {}"+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), request.getRequestManager()));
        }
    }

    Map<String, Object> getResultMapForAction(Map<String, Object> map, String key, Object value, RequestManager requestManager) {
        if (key != null && !key.isEmpty()) {
            map.put(key, value);
        }
        String msgContent = requestManager.getMessagecontent();
        if (msgContent != null && !msgContent.isEmpty()) {
            map.put("msgContent", msgContent);
        }
        String msgId = requestManager.getMessageid();
        if (msgId != null && !msgId.isEmpty()) {
            map.put("msgId", msgId);
        }
        return map;
    }
}
