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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import  com.weaver.seconddev.interfaces.easoa.CallSieOAVoucherService;
import  com.weaver.seconddev.interfaces.easoa.SieOaVoucherServiceStub;
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;


//不启用
/**
 * EAS007电子档案凭证号修改流程
 * 接口账号密码 测试环境-都为空  正式环境：eas/qwerfdsa
 */
@org.springframework.stereotype.Service("workflow_action_EAS_VoucherModifyAction")
public class EAS_VoucherModifyAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;
    @Autowired
    private ConfigProp configProp;
    private static final Logger logger_57237c37 = LoggerFactory.getLogger(EAS_VoucherModifyAction.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 {
            /**
             * 请求ID*
             */
            String requestID = "";
            /**
             * 系统标识*
             */
            String SYSID = "OA";
            /**
             * 接口ID*
             */
            String ITFID = "HTEFSR006";
            // 接口账号
            String USERNAME = "eas";
            // 接口密码
            String PASSWORD = "qwerfdsa";
            //TODO custom 不启用
//            try {
//                ResourceComInfo hr = I18nContextUtil.getBean(ResourceComInfo.class);
//                BillFieldUtil unit = new BillFieldUtil();
//                Property[] properties = requestInfo.getMainTableInfo().getProperty();
//                requestID = requestInfo.getRequestid();
//                logger_57237c37.error(String.valueOf("EAS_VoucherModifyAction--start--" + requestID));
//                String lastopertorid = requestInfo.getLastoperator();
//                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 boeNum = mMap.get("DJH");
//                logger_57237c37.error(String.valueOf("报账单号：" + boeNum));
//                String kjqj = mMap.get("BGPZKJQJ");
//                if (kjqj != null && !kjqj.equals("")) {
//                    kjqj = kjqj.replaceAll("-", "");
//                }
//                logger_57237c37.error(String.valueOf("凭证变更会计期间：" + kjqj));
//                String sqrq = mMap.get("SQRQ");
//                if (sqrq != null && !sqrq.equals("")) {
//                    sqrq = sqrq.replaceAll("-", "");
//                }
//                logger_57237c37.error(String.valueOf("申请日期：" + sqrq));
//                String sqr = mMap.get("SQR");
//                String sqrcode = "";
//                if (!sqr.equals("")) {
//                    // 申请人工号
//                    sqrcode = unit.getWorkcode(sqr);
//                }
//                logger_57237c37.error(String.valueOf("申请人工号：" + sqrcode));
//                String hsdwbm = mMap.get("HSDWDM");
//                logger_57237c37.error(String.valueOf("核算单位编码：" + hsdwbm));
//                String lcbh = mMap.get("LCBH");
//                logger_57237c37.error(String.valueOf("OA流程号：" + lcbh));
//                String zt = mMap.get("ZT");
//                logger_57237c37.error(String.valueOf("主题：" + zt));
//                String bgqpzh = mMap.get("BGQPZH");
//                logger_57237c37.error(String.valueOf("变更前凭证号：" + bgqpzh));
//                String bghpzh = mMap.get("BGHPZH");
//                logger_57237c37.error(String.valueOf("变更后凭证号：" + bghpzh));
//                SieOaVoucherServiceStub.SieOaVoucherDO param = new SieOaVoucherServiceStub.SieOaVoucherDO();
//                param.setITFID(ITFID);
//                param.setSYSID(SYSID);
//                param.setUSERNAME(USERNAME);
//                param.setPASSWORD(PASSWORD);
//                // 保障单号
//                param.setBoeNum(boeNum);
//                // 会计期间
//                param.setAccPeriod(kjqj);
//                // 申请日期
//                param.setApplyDate(sqrq);
//                // 申请人工号
//                param.setApplyEmployeeNo(sqrcode);
//                // 核算单位编码
//                param.setCompanyCode(hsdwbm);
//                // oa流程号
//                param.setFlowNoOA(lcbh);
//                param.setRequestId(requestID);
//                // 主题
//                param.setSubject(zt);
//                // 新凭证号
//                param.setVoucherNoNew(bghpzh);
//                // 原凭证号
//                param.setVoucherNoOld(bgqpzh);
//                // 调用接口
////                String url = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("hteasoasync.updatevoucherurl")).trim();
//                String url = configProp.getUpdatevoucherurl();
//                logger_57237c37.error("url == " + url);
//                CallSieOAVoucherService service = new CallSieOAVoucherService(url);
//                String[] result = service.syncVoucherInfo(param);
//                logger_57237c37.error(String.valueOf("调用借阅审批回调接口返回值：" + result));
//                if (!"S".equals(result[0])) {
//                    // 失败
//                    logger_57237c37.error(String.valueOf("报账系统返回错误信息：" + result[1]));
//                    requestInfo.getRequestManager().setMessageid("ERROR");
//                    requestInfo.getRequestManager().setMessagecontent("报账系统返回错误信息：" + result[1]);
//                    return WeaResult.fail(500, "执行失败", getResultMapForAction(weaverResultMap, "result", FAILURE_AND_CONTINUE, requestInfo.getRequestManager()));
//                }
//            } catch (Exception e) {
//                logger_57237c37.error(String.valueOf("提交失败！"));
//                logger_57237c37.error(String.valueOf("Error======" + e.getMessage()));
//                requestInfo.getRequestManager().setMessageid("ERROR");
//                requestInfo.getRequestManager().setMessagecontent("提交失败！");
//                // return  FAILURE_AND_CONTINUE;
//                return WeaResult.fail(500, "执行失败", getResultMapForAction(weaverResultMap, "result", FAILURE_AND_CONTINUE, requestInfo.getRequestManager()));
//            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestInfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_57237c37.error("EAS_VoucherModifyAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "EAS_VoucherModifyAction 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;
    }
}
