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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import  com.weaver.seconddev.interfaces.easoa.EAS_YszxfpApplyResult;
import java.util.*;
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 java.util.Map;
import java.util.HashMap;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.soa.workflow.request.*;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.DetailTable;
import com.weaver.verupgrade.soa.workflow.request.Row;
import com.weaver.verupgrade.soa.workflow.request.Cell;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

@org.springframework.stereotype.Service("workflow_action_SCFAndEASApplyUserCheckAction")
public class SCFAndEASApplyUserCheckAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private EAS_YszxfpApplyResult mGetResult;

    private static final Logger logger_f844853c = LoggerFactory.getLogger(SCFAndEASApplyUserCheckAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_f844853c.error("SCFAndEASApplyUserCheckAction start");
        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();
            String applyNo = "";
            String companyCode = "";
            Property[] properties = request.getMainTableInfo().getProperty();
            for (int i = 0; i < properties.length; i++) {
                String name = properties[i].getName().toUpperCase();
                String value = Util.null2String(properties[i].getValue());
                if ("LCBH".equals(name)) {
                    applyNo = value;
                }
                if ("GSBH".equals(name)) {
                    companyCode = value;
                }
            }
            DetailTable dtq = request.getDetailTableInfo().getDetailTable(0);
            Row[] sq = dtq.getRow();
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            Set<String> set = new HashSet<String>();
            for (int j = 0; j < sq.length; j++) {
                Row rq = sq[j];
                Cell[] cq = rq.getCell();
                Map<String, String> map = new HashMap<String, String>();
                for (int k = 0; k < cq.length; k++) {
                    Cell cq1 = cq[k];
                    String nameq = cq1.getName().toLowerCase();
                    String valueq = Util.null2String(cq1.getValue());
                    if ("lclx".equals(nameq)) {
                        map.put("flowType", Integer.parseInt(valueq) + 1 + "");
                        if (!set.add(valueq)) {
                            request.getRequestManager().setMessageid("Error");
                            request.getRequestManager().setMessagecontent("流程类型不能重复申请");
                            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
                        }
                    }
                    if ("easfqrgh".equals(nameq)) {
                        map.put("employeeNumber", valueq);
                    }
                    if ("bm".equals(nameq)) {
                        map.put("attribute1", valueq);
                    }
                }
                if (!"".equals(map.get("employeeNumber"))) {
                    list.add(map);
                }
            }
            if (list != null && list.size() > 0) {
                try {
                    JSONObject obj = new JSONObject();
                    obj.put("systemFlag", "OA");
                    obj.put("companyCode", companyCode);
                    obj.put("requestId", requestid);
                    obj.put("applyNo", applyNo);
                    obj.put("checkFlag", "1");
                    obj.put("ITFID", "HT01BZ033");
                    obj.put("items", JSONArray.parseArray(JSON.toJSONString(list)));
                    String result = mGetResult.getApproveResult(obj, "SCFAndEASApplyUser");
                    logger_f844853c.error(String.valueOf(requestid + "调用报账系统校验流程发起人信息返回值：" + result));
                    if (!"E".equals(result)) {
                        JSONObject jsonObject = JSONObject.parseObject(result);
                        // 接口处理状态 S-成功，E-失败
                        String isTat = jsonObject.getString("istat");
                        // 处理消息
                        String srMessage = jsonObject.getString("srmsage");
                        if (!"S".equals(isTat)) {
                            // 失败
                            logger_f844853c.error(String.valueOf(requestid + "报账系统返回错误信息：" + srMessage));
                            request.getRequestManager().setMessageid("ERROR");
                            request.getRequestManager().setMessagecontent("报账系统返回错误信息：" + srMessage);
                            return  WeaResult.fail(500, "SCFAndEASApplyUserCheckAction srMessage"+ srMessage, getResultMapForAction(weaverResultMap, "result", srMessage, request.getRequestManager()));
                        }
                    }
                } catch (Exception e) {
                    logger_f844853c.error(requestid + "调用报账系统校验流程发起人信息接口出错：" , e );
                    request.getRequestManager().setMessageid("ERROR");
                    request.getRequestManager().setMessagecontent("调用报账系统接口返回失败，请联系报账系统管理员！");
                    return  WeaResult.fail(500, "SCFAndEASApplyUserCheckAction err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
                }
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_f844853c.error("SCFAndEASApplyUserCheckAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "SCFAndEASApplyUserCheckAction 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;
    }
}
