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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.seconddev.interfaces.prop.ConfigProp;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import org.springframework.core.env.Environment;
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 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.*;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * 电子档案废单审批--总账
 * 接口账号密码：测试环境--hengtong/abc,,,123    正式环境--htefs/efs2@2@@9!@HT
 */
@org.springframework.stereotype.Service("workflow_action_ZZ_DZDAFDApproveAction")
public class ZZ_DZDAFDApproveAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;
    @Autowired
    private ConfigProp configProp;
    private static final Logger logger_ccc3e432 = LoggerFactory.getLogger(ZZ_DZDAFDApproveAction.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);

        logger_ccc3e432.error("----- workflow_action_ZZ_DZDAFDApproveAction new -----");
        logger_ccc3e432.error("requestIdTemp -----"+ requestIdTemp);
        logger_ccc3e432.error("userIdTemp -----"+ userIdTemp);
        try {
            /**
             * 请求ID*
             */
            String requestID = "";
            /**
             * 系统标识*
             */
            String SYSID = "OA";
            /**
             * 接口ID*
             */
            String ITFID = "HTEFS006";
            // 接口账号
//            String USERNAME = "htefs";
            String USERNAME = configProp.getEfsaccount();
            // 接口密码
//            String PASSWORD = "efs2@2@@9!@HT";
            String PASSWORD = configProp.getEfspassword();
            try {
                ResourceComInfo hr = I18nContextUtil.getBean(ResourceComInfo.class);
                BillFieldUtil unit = new BillFieldUtil();
                Property[] properties = requestInfo.getMainTableInfo().getProperty();
                requestID = requestInfo.getRequestid();
                logger_ccc3e432.error(String.valueOf("ZZ_DZDAFDApproveAction--start--" + requestID));
                Map<String, String> mMap = 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());
                    mMap.put(name, value);
                }
                String pzlx = mMap.get("PZLX");
                logger_ccc3e432.error(String.valueOf("凭证类型：" + pzlx));
                JSONObject mJson = new JSONObject();
                mJson.put("USERNAME", USERNAME);
                mJson.put("PASSWORD", PASSWORD);
                // 系统标识
                mJson.put("ITFID", ITFID);
                mJson.put("SYSID", SYSID);
                String url = "";
                if (pzlx.equals("0")) {
                    // 总账类
                    // 调用接口
//                    url = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("hteasoasync.dzdafd_zz_url")).trim();
                    url = configProp.getDzdafd_zz_url();
                    logger_ccc3e432.error("url" + url);
                    String yoaywbh = mMap.get("YOAYWBH");
                    mJson.put("receiptNO", yoaywbh);
                    mJson.put("voucherSysFrom", "OA");
                    // 无效
                    mJson.put("businessStatus", "0");
                } else {
                    // 报账类
                }
                String errorMsg = "";
                logger_ccc3e432.error("调用电子档案废单接口请求数据：=====" + mJson.toString());
                ZZ_SendPostRequest mGetResult = new ZZ_SendPostRequest();
                String result = mGetResult.getResult(mJson, url);
                logger_ccc3e432.error(String.valueOf("调用电子档案废单接口请求数据：" + result));
                if (!"E".equals(result)) {
                    JSONObject jsonObject = JSONObject.parseObject(result);
                    // 接口处理状态 S-成功，F-失败
                    String isTat = jsonObject.getString("status");
                    // 处理消息
                    String srMessage = jsonObject.getString("message");
                    logger_ccc3e432.error("报账系统返回数据：" + jsonObject.toString());
                    if (!"S".equals(isTat)) {
                        // 失败
                        logger_ccc3e432.error(String.valueOf("报账系统返回错误信息：" + srMessage));
                        requestInfo.getRequestManager().setMessageid("ERROR");
                        requestInfo.getRequestManager().setMessagecontent("报账系统返回错误信息：" + srMessage);
                        return  WeaResult.fail(500, "报账系统返回错误信息：" + srMessage, getResultMapForAction(weaverResultMap, "result", srMessage, requestInfo.getRequestManager()));

                    }
                } else {
                    logger_ccc3e432.error(String.valueOf("调用电子档案废单接口返回失败，请联系报账系统管理员！"));
                    requestInfo.getRequestManager().setMessageid("ERROR");
                    requestInfo.getRequestManager().setMessagecontent("调用电子档案废单接口返回失败，请联系报账系统管理员！");
                }
            } catch (Exception e) {
                logger_ccc3e432.error(String.valueOf("提交失败！"));
                logger_ccc3e432.error(String.valueOf("Error======" + e.getMessage()));
                requestInfo.getRequestManager().setMessageid("ERROR");
                requestInfo.getRequestManager().setMessagecontent("提交失败！");
                return  WeaResult.fail(500, "Error======" + e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), requestInfo.getRequestManager()));

            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestInfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_ccc3e432.error("ZZ_DZDAFDApproveAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZZ_DZDAFDApproveAction 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;
    }
}
