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

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.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * SAP--信贷解锁接口--qingf20170327
 * @author Administrator
 */
@org.springframework.stereotype.Service("workflow_action_ZOA_SD_CREDIT_HANDLE_QUERYAction")
public class ZOA_SD_CREDIT_HANDLE_QUERYAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_270bcd1 = LoggerFactory.getLogger(ZOA_SD_CREDIT_HANDLE_QUERYAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_270bcd1.error("ZOA_SD_CREDIT_HANDLE_QUERYAction 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 {
            try {
                String requestId = "";
                // 销售组织
                String xszz = "";
                // 单号类别0交货单、1销售单
                String spdhlb = "";
                // 对应单号
                String dydh = "";
                // 客户编码
                String khbm = "";
                // 操作类型APPROVAL批准
                String czlx = "QUERY";
                String[] prdetail = null;
                prdetail = new String[7];
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                BillFieldUtil unit = new BillFieldUtil();
                // 流程请求ID
                requestId = request.getRequestid();
                logger_270bcd1.error(String.valueOf("ZOA_SD_CREDIT_HANDLE_QUERYAction-Request: " + requestId + "开始"));
                // 获取表单主字段信息
                Property[] properties = request.getMainTableInfo().getProperty();
                for (int i = 0; i < properties.length; i++) {
                    // 主表数据
                    // 字段名
                    String name = properties[i].getName().toLowerCase();
                    // 值
                    String value = Util.null2String(properties[i].getValue());
                    // 单据主信息使用
                    if (name.equals("xszz")) {
                        // 销售组织
                        xszz = value.trim();
                    }
                    if (name.equals("spdhlb")) {
                        // 单号类别0交货单、1销售单
                        spdhlb = value;
                    }
                    if (name.equals("dydh1")) {
                        // 对应单号
                        dydh = value.trim();
                    }
                    if (name.equals("khbm")) {
                        // 客户编码
                        khbm = value.trim();
                    }
                }
                if (spdhlb.equals("0")) {
                    prdetail[1] = dydh;
                }
                if (spdhlb.equals("1")) {
                    prdetail[0] = dydh;
                }
                prdetail[2] = xszz;
                prdetail[6] = khbm;
                logger_270bcd1.error(String.valueOf("------获取主表数据成功------"));
                ZOA_SD_CREDIT_APPROVAL_HANDLE po = new ZOA_SD_CREDIT_APPROVAL_HANDLE();
                String[] fhz = po.Getexecute(czlx, prdetail);
                // EXISTANCE返回值
                String sta = fhz[3];
                String meg = fhz[5].replace("'", "’");
                String tablename = "";
                String wfid = request.getWorkflowid();
//                tablename = unit.getTablename(wfid);
                tablename = request.getRequestManager().getBillTableName();
                String logsql = "update e10_core_business.dbo." + tablename + " set sta ='" + sta + "', meg = '" + meg + "' where id = " + requestId;
                logger_270bcd1.error(String.valueOf("logsql:" + logsql));
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b1 = rs.executeSql(logsql, poolname);
                logger_270bcd1.error(String.valueOf("logsql flag:" + b1));
                logger_270bcd1.error(String.valueOf("ZOA_SD_CREDIT_HANDLE_QUERYAction-Request: " + requestId + "完成"));
                if (!sta.equals("S")) {
                    request.getRequestManager().setMessageid("100001");
                    request.getRequestManager().setMessagecontent(meg);
                    return  WeaResult.fail(500, "ZOA_SD_CREDIT_HANDLE_QUERYAction e: {}" +meg, getResultMapForAction(weaverResultMap, "result", "", request.getRequestManager()));
                }
                System.out.println(meg);
            } catch (Exception e) {
                logger_270bcd1.error("调用接口报错-- " ,e);
                return  WeaResult.fail(500, "调用接口报错" + e, getResultMapForAction(weaverResultMap, "result", "", request.getRequestManager()));
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_270bcd1.error("ZOA_SD_CREDIT_HANDLE_QUERYAction e: {}", e);
            return  WeaResult.fail(500, "ZOA_SD_CREDIT_HANDLE_QUERYAction e: {}" + e, getResultMapForAction(weaverResultMap, "result", "", 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;
    }
}
