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

import  com.weaver.seconddev.interfaces.oames.CallMesNewMaterialTestService;
import  com.weaver.seconddev.interfaces.oames.OA_KSJXCL_SERVICEStub;
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.hrm.resource.ResourceComInfo;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * HTGD-GY101-(新)新材料试用
 */
@org.springframework.stereotype.Service("workflow_action_OaMesNewMaterialTestAction")
public class OaMesNewMaterialTestAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_908d3105 = LoggerFactory.getLogger(OaMesNewMaterialTestAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_908d3105.error("OaMesNewMaterialTestAction 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 lcbh = "";
                // 推荐人
                String tjr = "";
                // 材料名称
                String clmc = "";
                // 相关资料资质证明
                String xgzz = "";
                // 新材料试用工厂
                String sygc = "";
                // 试用预定完成时间
                String wcsj = "";
                // 项目责任人
                String xmzrr = "";
                // 项目申请人
                String xmsqr = "";
                // 物料号
                String wlh = "";
                // 材料型号规格
                String clxhgg = "";
                // 项目类别
                String xmlb = "";
                // 物料特征值
                String wltzz = "";
                // 需求材料数量
                String xqclsl = "";
                // 需求到货周期
                String xqdhzq = "";
                // 试制是否与材料检测同步进行
                String szsftbjx = "";
                // 建议供应商1
                String jygys1 = "";
                // 建议供应商2
                String jygys2 = "";
                // 建议供应商3
                String jygys3 = "";
                // 建议供应商4
                String jygys4 = "";
                // 建议供应商5
                String jygys5 = "";
                // 建议供应商6
                String jygys6 = "";
                // 项目参与人
                String xmcyr = "";
                // 原因说明
                String yysm = "";
                // 申请部门
                String sqbm = "";
                // 申请人姓名
                String sqrxm = "";
                // 产品名称
                String cpmc = "";
                // 申请单位
                String sqdw = "";
                // 获取工作流ID
                String id = request.getWorkflowid();
                // 流程请求ID
                String requestId = request.getRequestid();
                // 主表字段赋值
                // 获取表单主字段信息
                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("lcbh")) {
                        // 流程编号
                        lcbh = value.trim();
                    }
                    if (name.equals("tjr")) {
                        // 推荐人
                        tjr = value.trim();
                    }
                    if (name.equals("clmc")) {
                        // 材料名称
                        clmc = value.trim();
                    }
                    if (name.equals("xgzz")) {
                        // 相关资料资质证明
                        xgzz = value.trim();
                    }
                    if (name.equals("sygc")) {
                        // 新材料试用工厂
                        sygc = value.trim();
                    }
                    if (name.equals("wcsj")) {
                        // 试用预定完成时间
                        wcsj = value;
                    }
                    if (name.equals("xmzrr")) {
                        // 项目责任人
                        xmzrr = value;
                    }
                    if (name.equals("xmsqr")) {
                        // 项目申请人
                        xmsqr = value;
                    }
                    if (name.equals("wlh")) {
                        // 物料号
                        wlh = value.trim();
                    }
                    if (name.equals("clxhgg")) {
                        // 材料型号规格
                        clxhgg = value.trim();
                    }
                    if (name.equals("xmlb")) {
                        // 项目类别
                        xmlb = value.trim();
                    }
                    if (name.equals("wltzz")) {
                        // 物料特征值
                        wltzz = value.trim();
                    }
                    if (name.equals("xqclsl")) {
                        // 需求材料数量
                        xqclsl = value.trim();
                    }
                    if (name.equals("xqdhzq")) {
                        // 需求到货周期
                        xqdhzq = value.trim();
                    }
                    if (name.equals("szsftbjx")) {
                        // 试制是否与材料检测同步进行
                        szsftbjx = value.trim();
                    }
                    if (name.equals("jygys1")) {
                        // 建议供应商1
                        jygys1 = value.trim();
                    }
                    if (name.equals("jygys2")) {
                        // 建议供应商2
                        jygys2 = value.trim();
                    }
                    if (name.equals("jygys3")) {
                        // 建议供应商3
                        jygys3 = value.trim();
                    }
                    if (name.equals("jygys4")) {
                        // 建议供应商4
                        jygys4 = value.trim();
                    }
                    if (name.equals("jygys5")) {
                        // 建议供应商5
                        jygys5 = value.trim();
                    }
                    if (name.equals("jygys6")) {
                        // 建议供应商6
                        jygys6 = value.trim();
                    }
                    if (name.equals("xmcyr")) {
                        // 项目参与人
                        xmcyr = value.trim();
                    }
                    if (name.equals("yysm")) {
                        // 原因说明
                        yysm = value.trim();
                    }
                    if (name.equals("sqbm")) {
                        // 申请部门
                        sqbm = value.trim();
                    }
                    if (name.equals("cpmc")) {
                        // 产品名称
                        cpmc = value.trim();
                    }
                    if (name.equals("sqdw")) {
                        // 申请单位
                        sqdw = value.trim();
                    }
                }
                // 将申请人信息由代号转换为真实信息
                BillFieldUtil unit = new BillFieldUtil();
                ResourceComInfo hr = I18nContextUtil.getBean(ResourceComInfo.class);
                logger_908d3105.error(String.valueOf("开始 将申请人信息由代号转换为真实信息"));
                if (!xmsqr.equals("")) {
                    // 获取项目申请人姓名
                    sqrxm = hr.getResourcename(xmsqr);
                }
                if (!xmcyr.equals("")) {
                    // 获取项目参与人姓名
                    xmcyr = hr.getResourcename(xmcyr);
                }
                if (!xmzrr.equals("")) {
                    // 获取项目责任人姓名
                    xmzrr = hr.getResourcename(xmzrr);
                }
                logger_908d3105.error(String.valueOf("代号转为真实信息结束" + "\t" + "申请人" + sqrxm));
                OA_KSJXCL_SERVICEStub.OA_XCLSYSQLC_HEADER_TYPE param_data = new OA_KSJXCL_SERVICEStub.OA_XCLSYSQLC_HEADER_TYPE();
                OA_KSJXCL_SERVICEStub.ArrayOfOA_XCLSYSQLC_HEADER_TYPE param_datas = new OA_KSJXCL_SERVICEStub.ArrayOfOA_XCLSYSQLC_HEADER_TYPE();
                OA_KSJXCL_SERVICEStub.OA_XCLSYSQLC resultin = new OA_KSJXCL_SERVICEStub.OA_XCLSYSQLC();
                try {
//                    param_data.setID(Integer.parseInt(id));
//                    param_data.setREQUESTID(Integer.parseInt(requestId));
                    param_data.setLCBH(lcbh);
                    param_data.setSQBM(sqbm);
                    param_data.setSKH("GDBJ/EQ-0063B");
                    param_data.setXMSQR(sqrxm);
                    param_data.setXGZZ(xgzz);
                    param_data.setSYGC(sygc);
                    param_data.setWCSJ(wcsj);
                    param_data.setWLH(wlh);
                    param_data.setCPMC(cpmc);
                    param_data.setSQDW(sqdw);
                    param_data.setYYSM(yysm);
                    param_data.setXQCLSL(xqclsl);
                    param_data.setXMCYR(xmcyr);
                    param_data.setXMSQR(xmsqr);
                    param_data.setXMZRR(xmzrr);
                    param_data.setXMLB(xmlb);
                    param_data.setWLTZZ(wltzz);
                    param_data.setTJR(tjr);
                    param_data.setSZSFTBJX(szsftbjx);
                    // 材料名称
                    param_data.setCLMC(clmc);
                    param_data.setCLXHGG(clxhgg);
                    param_data.setXQCLSL(xqclsl);
                    param_data.setCLXHGG(clxhgg);
                    param_data.setJYGYS1(jygys1);
                    param_data.setJYGYS2(jygys2);
                    param_data.setJYGYS3(jygys3);
                    param_data.setJYGYS4(jygys4);
                    param_data.setJYGYS5(jygys5);
                    param_data.setJYGYS6(jygys6);
                    param_data.setXQDHZQ(Integer.parseInt(xqdhzq));
                    param_data.setXQDHZQ(Integer.parseInt(xqdhzq));
                } catch (NumberFormatException e) {
                    System.err.println(e.toString());
                    logger_908d3105.error(String.valueOf("主表强制转换传值报错 " + e.toString()));
                }
                try {
                    // 封装信息
                    param_datas.addOA_XCLSYSQLC_HEADER_TYPE(param_data);
                    resultin.setITFID("IFTIF001");
                    resultin.setWERKS("A000");
                    resultin.setDATAS(param_datas);
                } catch (NumberFormatException e) {
                    System.err.println(e.toString());
                    logger_908d3105.error(String.valueOf("封装报错 " + e.toString()));
                }
                logger_908d3105.error(String.valueOf("调用接口开始0 "));
                logger_908d3105.error(String.valueOf("调用接口完成RequestId--" + requestId + "--sqr--" + sqrxm + "--lcbh--" + lcbh + "--sqbm--" + sqbm));
                String sta = "";
                String meg = "";
                CallMesNewMaterialTestService mes = new CallMesNewMaterialTestService();
                // 修改过
                mes.setOainput(resultin);
                String[] rtInfo = mes.call();
                logger_908d3105.error(String.valueOf("调用接口完成RequestId--" + rtInfo[0] + "--Istat--" + rtInfo[1] + "--Srmsage--" + rtInfo[2]));
                sta = rtInfo[1];
                meg = rtInfo[2].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_908d3105.error(String.valueOf("logsql:" + logsql));
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(logsql, poolname);
                logger_908d3105.error(String.valueOf("b--"+b));
                logger_908d3105.error(String.valueOf("CallMesNewMaterialTestAction-Request: " + requestId + "完成"));
                if (!sta.equals("S")) {
                    request.getRequestManager().setMessageid("100001");
                    request.getRequestManager().setMessagecontent(meg);
                    return  WeaResult.fail(500, "OaMesNewMaterialTestAction meg"+ meg, getResultMapForAction(weaverResultMap, "result",meg, request.getRequestManager()));

                }
                System.out.println(meg);
            } catch (Exception e) {
                System.err.println(e.toString());
                logger_908d3105.error(String.valueOf("调用接口报错 " + e.toString()));
                return  WeaResult.fail(500, "OaMesNewMaterialTestAction 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_908d3105.error("OaMesNewMaterialTestAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "OaMesNewMaterialTestAction 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;
    }
}
