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

import com.alibaba.fastjson.JSONObject;
import  com.weaver.seconddev.interfaces.iuroa.SyncDataService;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import java.lang.*;
import java.util.*;
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 java.util.Map;
import java.util.HashMap;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
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_Iur_syncProject")
public class Iur_syncProject extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_72949ae6 = LoggerFactory.getLogger(Iur_syncProject.class);

    @Override
    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 requestinfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            String requestid = requestinfo.getRequestid();
            String workflowid = requestinfo.getWorkflowid();
            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
            BillFieldUtil util = new BillFieldUtil();
//            String tablename = util.getTablename(workflowid);
            String tablename = requestinfo.getRequestManager().getBillTableName();
            try {
                logger_72949ae6.error(String.valueOf("Iur_syncProject--start:" + requestid));
                JSONObject map = new JSONObject();
                Property[] properties = requestinfo.getMainTableInfo().getProperty();
                for (int i = 0; i < properties.length; i++) {
                    String name = properties[i].getName().toUpperCase();
                    String value = Util.null2String(properties[i].getValue());
                    if ("XMMC".equals(name)) {
                        map.put("name", value);
                    }
                    if ("XMFZR".equals(name)) {
                        ResourceComInfo res = I18nContextUtil.getBean(ResourceComInfo.class);
                        map.put("employeeName", res.getResourcename(value));
                        map.put("userNo", res.getWorkcode(value));
                    }
                    if ("HZDW".equals(name)) {
                        map.put("cooperationUnitName", value);
                    }
                    if ("SQRQ".equals(name)) {
                        map.put("approvalDateTime", value);
                    }
                }
                map.put("state", "0");
                map.put("requestId", requestid);
                SyncDataService service = new SyncDataService();
                String[] result = service.call(map, "syncProject");
                logger_72949ae6.error(requestid + "--Iur_syncProject--data:" + map.toString());
                logger_72949ae6.error(String.valueOf(requestid + "--Iur_syncProject--info:[" + result[0] + "|" + result[1] + "]"));
                String poolname_9l = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql("update e10_core_business.dbo." + tablename + " set status='" + result[0] + "',message='" + result[1] + "' where id=" + requestid, poolname_9l);
                if (b) {
                    logger_72949ae6.error(String.valueOf(requestid + "--Iur_syncProject--update success"));
                } else {
                    logger_72949ae6.error(String.valueOf(requestid + "--Iur_syncProject--update fail"));
                }
            } catch (Exception e) {
                logger_72949ae6.error(String.valueOf("Iur_syncProject--error:" + e.toString()));
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql("update e10_core_business.dbo." + tablename + " set status='E',message='" + e.toString() + "' where id=" + requestid, poolname);
                if (b) {
                    logger_72949ae6.error(String.valueOf(requestid + "--Iur_syncProject--update success"));
                    return  WeaResult.fail(500, requestid + "--Iur_syncProject--update success" + e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), requestinfo.getRequestManager()));

                } else {
                    logger_72949ae6.error(String.valueOf(requestid + "--Iur_syncProject--update fail"));
                    return  WeaResult.fail(500, requestid + "--Iur_syncProject--update fail" + e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), requestinfo.getRequestManager()));

                }
            }
            logger_72949ae6.error(String.valueOf("Iur_syncProject--end:" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_72949ae6.error("Iur_syncProject e: {}", e.getMessage());
            return  WeaResult.fail(500, "Iur_syncProject e: {}"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), requestinfo.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;
    }
}
