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

import  com.weaver.seconddev.interfaces.easoa.CallEasServices;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
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.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.soa.workflow.request.Cell;
import com.weaver.verupgrade.soa.workflow.request.DetailTable;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.Row;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * 付款卡控
 * @author libin
 */
@org.springframework.stereotype.Service("workflow_action_EasDoPayControlAction")
public class EasDoPayControlAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private CallEasServices services;

    private static final Logger logger_6cdff8ae = LoggerFactory.getLogger(EasDoPayControlAction.class);

    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 request = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            logger_6cdff8ae.error(String.valueOf("do EasDoPayControlAction on request:" + request.getRequestid()));
            try {
                BillFieldUtil util = new BillFieldUtil();
                // 获取主表数据
                // 申请类型 	1供应商、2报账单
                String type1 = "";
                // 操作类型  	Y冻结、N解冻
                String type2 = "";
                String requestid = request.getRequestid();
                // 流程编号
                String lcbh = "";
                String creator = "";
                // 申请事由
                String reason = "";
                String system_flag = "OA";
                Property[] aproperty = request.getMainTableInfo().getProperty();
                for (int i = 0; i < aproperty.length; i++) {
                    String name = aproperty[i].getName().toUpperCase();
                    String value = Util.null2String(aproperty[i].getValue());
                    if (name.equals("TYPE1")) {
                        // 申请类型
                        type1 = value;
                        logger_6cdff8ae.error(String.valueOf("得到申请类型" + type1));
                    }
                    if (name.equals("TYPE2")) {
                        // 申请类型
                        type2 = value;
                        logger_6cdff8ae.error(String.valueOf("得到操作类型" + type2));
                    }
                    if (name.equals("LCBH")) {
                        // lcbh
                        lcbh = value;
                        logger_6cdff8ae.error(String.valueOf("得到lcbh" + lcbh));
                    }
                    if (name.equals("SQR")) {
                        // 申请人
                        creator = util.getName(value);
                        logger_6cdff8ae.error(String.valueOf("得到申请人id(" + value + ") 姓名：" + creator));
                    }
                    if (name.equals("SQSY")) {
                        reason = value;
                        logger_6cdff8ae.error(String.valueOf("申请事由" + reason));
                    }
                }
                // 获取明细表
                logger_6cdff8ae.error(String.valueOf("明细表开始取值"));
                DetailTable[] detailtable = request.getDetailTableInfo().getDetailTable();
                // 指定明细表
                DetailTable dtq = detailtable[0];
                // 当前明细表的所有数据,按行存储
                Row[] sq = dtq.getRow();
                String[] bzdhArray = new String[sq.length];
                String[] lxArray = new String[sq.length];
                String[] djjeArray = new String[sq.length];
                String[] gysidArray = new String[sq.length];
                String[] gsdmArray = new String[sq.length];
                String[] sfhmdArray = new String[sq.length];
                logger_6cdff8ae.error(String.valueOf("For循环开始校验"));
                for (int j = 0; j < sq.length; j++) {
                    // 指定行
                    Row rq = sq[j];
                    // 每行数据再按列存储
                    Cell[] cq = rq.getCell();
                    for (int k = 0; k < cq.length; k++) {
                        // 指定列
                        Cell cq1 = cq[k];
                        // 明细字段名称
                        String nameq = cq1.getName().toUpperCase();
                        // 明细字段的值
                        String valueq = Util.null2String(cq1.getValue());
                        if (nameq.equals("BZDH")) {
                            // 报账单号
                            String bzdh = valueq.trim();
                            bzdhArray[j] = bzdh;
                            logger_6cdff8ae.error(String.valueOf("报账单号：" + bzdh));
                        }
                        if (nameq.equals("LX")) {
                            // 类型
                            String lx = valueq.trim();
                            lxArray[j] = lx;
                            logger_6cdff8ae.error(String.valueOf("类型：" + lx));
                        }
                        if (nameq.equals("DJJE")) {
                            // 冻结金额
                            String djje = valueq.trim();
                            djjeArray[j] = djje;
                            logger_6cdff8ae.error(String.valueOf("冻结金额：" + djje));
                        }
                        if (nameq.equals("GSDM")) {
                            // 公司代码
                            String gsdm = valueq.trim();
                            gsdmArray[j] = gsdm;
                            logger_6cdff8ae.error(String.valueOf("公司代码：" + gsdm));
                        }
                        if (nameq.equals("GSMC")) {
                            // 公司名称
                            String gsmc = valueq.trim();
                            logger_6cdff8ae.error(String.valueOf("公司名称：" + gsmc));
                        }
                        if (nameq.equals("GYSDM")) {
                            // 供应商代码
                            String gysdm = valueq.trim();
                            logger_6cdff8ae.error(String.valueOf("供应商代码：" + gysdm));
                        }
                        if (nameq.equals("GYSMC")) {
                            // 供应商名称
                            String gysmc = valueq.trim();
                            logger_6cdff8ae.error(String.valueOf("供应商名称：" + gysmc));
                        }
                        if (nameq.equals("GYSID")) {
                            // 供应商id
                            String gysid = valueq.trim();
                            gysidArray[j] = gysid;
                            logger_6cdff8ae.error(String.valueOf("供应商id：" + gysid));
                        }
                        if (nameq.equals("SFHMD")) {
                            String check = valueq.trim();
                            if ("0".equals(check)) {
                                sfhmdArray[j] = "Y";
                            } else {
                                sfhmdArray[j] = "N";
                            }
                        }
                    }
                }
                // 调用接口
                logger_6cdff8ae.error(" services.doPayControl");
                logger_6cdff8ae.error("type2：" + type2);
                logger_6cdff8ae.error("type1：" + type1);
                logger_6cdff8ae.error("requestid：" + requestid);
                logger_6cdff8ae.error("lcbh：" + lcbh);
                logger_6cdff8ae.error("creator：" + creator);
                logger_6cdff8ae.error("reason：" + reason);
                logger_6cdff8ae.error("system_flag：" + system_flag);
                logger_6cdff8ae.error("gysidArray：" + gysidArray.toString());
                logger_6cdff8ae.error("bzdhArray：" + bzdhArray.toString());
                logger_6cdff8ae.error("gsdmArray：" + gsdmArray.toString());
                logger_6cdff8ae.error("djjeArray：" + djjeArray.toString());
                logger_6cdff8ae.error("sfhmdArray：" + sfhmdArray.toString());
                logger_6cdff8ae.error("lxArray：" + lxArray.toString());
                String[] result = services.doPayControl(type2, type1, requestid, lcbh, creator, reason, system_flag, gysidArray, bzdhArray, gsdmArray, djjeArray, sfhmdArray, lxArray);

                String status = result[0];
                String msg = result[1];
                logger_6cdff8ae.error(String.valueOf("EasDoPayControlAction result status：" + status + " , msg:" + msg));
                if (!"S".equals(status)) {
                    request.getRequestManager().setMessageid("ERROR");
                    request.getRequestManager().setMessagecontent(msg);
                    return  WeaResult.fail(500, "EasDoPayControlAction result status：" + status + " , msg:" + msg, getResultMapForAction(weaverResultMap, "result", "EasDoPayControlAction result status：" + status + " , msg:" + msg, request.getRequestManager()));
                }
            } catch (Exception e) {
                logger_6cdff8ae.error("error : " , e);
                request.getRequestManager().setMessageid("ERROR");
                request.getRequestManager().setMessagecontent(e.getMessage());
                return  WeaResult.fail(500, "EasDoPayControlAction 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_6cdff8ae.error("EasDoPayControlAction e: {}", e);
            return  WeaResult.fail(500, "EasDoPayControlAction 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;
    }
}
