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

import  com.weaver.seconddev.interfaces.oames.CallMesNewMaterialTSBGSerivce;
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.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;

/**
 * HTGD-JS05-新材料调试报告
 */
@org.springframework.stereotype.Service("workflow_action_OaMesNewMaterialTSBGAction")
public class OaMesNewMaterialTSBGAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_f52b61d7 = LoggerFactory.getLogger(OaMesNewMaterialTSBGAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_f52b61d7.error("OaMesNewMaterialTSBGAction 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 zrbm = "";
                // 流程编号
                String bh = "";
                // 项目类别
                String xmlb1 = "";
                // 产品名称
                String cpmc = "";
                // 项目参与人
                String xmcyr = "";
                // 项目责任人
                String xmzrr = "";
                // 调试产品规格
                String tscpgg = "";
                // 生产机台
                String scjt = "";
                // 调试目的
                String tsmd = "";
                // 调试要求
                String tsyq = "";
                // 工装模具
                String gzmj = "";
                // 调试过程描述及小结
                String tsgcmsjxj = "";
                // 调试截止时间
                String tsqssj2 = "";
                // 调试起始时间
                String tsqssj1 = "";
                // 挤塑机温度
                String jsjwd = "";
                // 调试速度最小
                String tssdzx = "";
                // 调试平均生产速度
                String tspjscsd = "";
                // 调试速度最大
                String tssdzd = "";
                // 挤塑机温度备注
                String jsjwdbz = "";
                // 水槽设置温度
                String scszwd = "";
                // 水槽实际温度
                String scsjwd = "";
                // 机头与水槽距离
                String jtyscjl = "";
                // 工艺参数其他说明
                String gycsqtsm = "";
                // 原材料关键指标是否达标说明及建议
                String yclgjzbsmjjy = "";
                // 调试是否达需认证需求说明及建议
                String tsrzxqsmjjy = "";
                // 其他说明及建议
                String clqtsmjjy = "";
                // 调试结论
                String tsjl = "";
                // 调试速度备注
                String tssdbz = "";
                // 水槽温度备注
                String scwdbz = "";
                // 项目申请人
                String xmsqr = "";
                // 供应商1
                String gys1 = "";
                // 供应商2
                String gys2 = "";
                // 供应商3
                String gys3 = "";
                // 供应商4
                String gys4 = "";
                // 供应商5
                String gys5 = "";
                // 供应商6
                String gys6 = "";
                String[][] prdetail = null;
                // 流程请求ID
                String requestId = request.getRequestid();
                // 流程表单id
                String Id = request.getWorkflowid();
                // spr = hr.getResourcename(request.getLastoperator());//获取审批人姓名
                // 主表字段赋值
                // 获取表单主字段信息
                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 (name.equals("XMSQR")) {
                        // 项目申请人
                        xmsqr = value;
                    }
                    if (name.equals("BH")) {
                        // 流程编号
                        bh = value.trim();
                    }
                    if (name.equals("ZRBM")) {
                        // 责任部门
                        zrbm = value;
                    }
                    if (name.equals("XMLB1")) {
                        // 项目类别
                        xmlb1 = value;
                    }
                    if (name.equals("CPMC")) {
                        // 产品名称
                        cpmc = value.trim();
                    }
                    if (name.equals("XMCYR")) {
                        // 项目参与人
                        xmcyr = value;
                    }
                    if (name.equals("XMZRR")) {
                        // 项目责任人
                        xmzrr = value;
                    }
                    if (name.equals("TSCPGG")) {
                        // 调试产品规格
                        tscpgg = value.trim();
                    }
                    if (name.equals("SCJT")) {
                        // 生产机台
                        scjt = value.trim();
                    }
                    if (name.equals("TSMD")) {
                        // 调试目的
                        tsmd = value.trim();
                    }
                    if (name.equals("TSYQ")) {
                        // 调试要求
                        tsyq = value.trim();
                    }
                    if (name.equals("GZMJ")) {
                        // 工装模具
                        gzmj = value.trim();
                    }
                    if (name.equals("TSGCMSJXJ")) {
                        // 调试过程描述及小结
                        tsgcmsjxj = value.trim();
                    }
                    if (name.equals("TSQSSJ1")) {
                        // 调试起始时间
                        tsqssj1 = value;
                    }
                    if (name.equals("TSQSSJ2")) {
                        // 调试截止时间
                        tsqssj2 = value.trim();
                    }
                    if (name.equals("JSJWD")) {
                        // 挤塑机温度
                        jsjwd = value.trim();
                    }
                    if (name.equals("TSSDZX")) {
                        // 调试速度最小
                        tssdzx = value.trim();
                    }
                    if (name.equals("TSPJSCSD")) {
                        // 调试速度均值
                        tspjscsd = value.trim();
                    }
                    if (name.equals("TSSDZD")) {
                        // 调试速度最大
                        tssdzd = value.trim();
                    }
                    if (name.equals("JSJWDBZ")) {
                        // 挤塑机温度备注
                        jsjwdbz = value.trim();
                    }
                    if (name.equals("SCSZWD")) {
                        // 水槽设置温度
                        scszwd = value.trim();
                    }
                    if (name.equals("SCSJWD")) {
                        // 水槽实际温度
                        scsjwd = value.trim();
                    }
                    if (name.equals("JTYSCJL")) {
                        // 机头与水槽距离
                        jtyscjl = value.trim();
                    }
                    if (name.equals("GYCSQTSM")) {
                        // 工艺参数其他说明
                        gycsqtsm = value.trim();
                    }
                    if (name.equals("YCLGJZBSMJJY")) {
                        // 原材料关键指标
                        yclgjzbsmjjy = value.trim();
                    }
                    if (name.equals("TSRZXQSMJJY")) {
                        // 调试是否达需求认证及建议
                        tsrzxqsmjjy = value.trim();
                    }
                    if (name.equals("CLQTSMJJY")) {
                        // 其他说明及建议
                        clqtsmjjy = value.trim();
                    }
                    if (name.equals("TSJL")) {
                        // 调试结论
                        tsjl = value.trim();
                    }
                    if (name.equals("TSSDBZ")) {
                        // 调试速度备注
                        tssdbz = value.trim();
                    }
                    if (name.equals("SCWDBZ")) {
                        // 水槽温度备注
                        scwdbz = value.trim();
                    }
                    if (name.equals("XMSQR")) {
                        // 项目申请人
                        xmsqr = value;
                    }
                    if (name.equals("GYS1")) {
                        // 供应商1
                        gys1 = value.trim();
                    }
                    if (name.equals("GYS2")) {
                        // 供应商2
                        gys2 = value.trim();
                    }
                    if (name.equals("GYS3")) {
                        // 供应商3
                        gys3 = value.trim();
                    }
                    if (name.equals("GYS4")) {
                        // 供应商4
                        gys4 = value.trim();
                    }
                    if (name.equals("GYS5")) {
                        // 供应商5
                        gys5 = value.trim();
                    }
                    if (name.equals("GYS6")) {
                        // 供应商6
                        gys6 = value.trim();
                    }
                }
                // 将申请人信息由代号转换为真实信息
                ResourceComInfo hr = I18nContextUtil.getBean(ResourceComInfo.class);
                BillFieldUtil unit = new BillFieldUtil();
                logger_f52b61d7.error(String.valueOf("HTGD-新材料调试报告"));
                logger_f52b61d7.error(String.valueOf("开始 将申请人信息由代号转换为真实信息"));
                if (!xmsqr.equals("")) {
                    // 获取项目参与人姓名
                    xmsqr = hr.getResourcename(xmsqr);
                }
                if (!xmcyr.equals("")) {
                    // 获取项目参与人姓名
                    xmcyr = hr.getResourcename(xmcyr);
                }
                if (!xmzrr.equals("")) {
                    // 获取项目责任人姓名
                    xmzrr = hr.getResourcename(xmzrr);
                }
                if (!xmzrr.equals("")) {
                    // 获项目负责人部门
                    zrbm = unit.getDepartmentname(zrbm);
                }
                logger_f52b61d7.error(String.valueOf("代号转为真实信息结束" + "\t" + "项目责任人" + xmzrr + "责任部门：" + zrbm));
                // 获取明细表
                OA_KSJXCL_SERVICEStub.ArrayOfOA_XCLSTBG_ITEM_TYPE arrayItems = new OA_KSJXCL_SERVICEStub.ArrayOfOA_XCLSTBG_ITEM_TYPE();
                DetailTable[] detailtable = request.getDetailTableInfo().getDetailTable();
                // 指定明细表
                DetailTable dtq = detailtable[0];
                // 当前明细表的所有数据,按行存储
                Row[] sq = dtq.getRow();
                prdetail = new String[sq.length][5];
                for (int j = 0; j < sq.length; j++) {
                    OA_KSJXCL_SERVICEStub.OA_XCLSTBG_ITEM_TYPE item = new OA_KSJXCL_SERVICEStub.OA_XCLSTBG_ITEM_TYPE();
                    // 指定行
                    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("MXCLMC")) {
                            prdetail[j][0] = valueq.trim();
                        }
                        // 材料型号
                        if (nameq.equals("MXXHGG")) {
                            prdetail[j][1] = valueq.trim();
                        }
                        // 材料厂家
                        if (nameq.equals("MXCLCJ")) {
                            prdetail[j][2] = valueq.trim();
                        }
                        // 使用数量
                        if (nameq.equals("MSSYSL")) {
                            prdetail[j][3] = valueq.trim();
                        }
                    }
                    try {
                        logger_f52b61d7.error(String.valueOf("明细表开始赋值"));
                        item.setMXCLMC(prdetail[j][0]);
                        item.setMXXHGG(prdetail[j][1]);
                        item.setMXCLCJ(prdetail[j][2]);
                        item.setMSSYSL(prdetail[j][3]);
                        arrayItems.addOA_XCLSTBG_ITEM_TYPE(item);
                        logger_f52b61d7.error("明细表赋值结束，如材料厂家：" + item.getMXCLCJ());
                    } catch (NumberFormatException e) {
                        System.err.println(e.getMessage());
                        logger_f52b61d7.error(String.valueOf("明细传值报错 " + e.getMessage()));
                    }
                }
                OA_KSJXCL_SERVICEStub.OA_XCLSTBG_HEADER_TYPE param_data = new OA_KSJXCL_SERVICEStub.OA_XCLSTBG_HEADER_TYPE();
                OA_KSJXCL_SERVICEStub.ArrayOfOA_XCLSTBG_HEADER_TYPE param_datas = new OA_KSJXCL_SERVICEStub.ArrayOfOA_XCLSTBG_HEADER_TYPE();
                OA_KSJXCL_SERVICEStub.OA_XCLSTBG resultin = new OA_KSJXCL_SERVICEStub.OA_XCLSTBG();
                try {
                    logger_f52b61d7.error(String.valueOf("开始封装表单信息"));
//                    param_data.setID(Integer.parseInt(Id));
//                    param_data.setREQUESTID(Integer.parseInt(requestId));
                    param_data.setBH(bh);
                    param_data.setSKH("GDBJ/BQ-0041A");
                    param_data.setSQBM(zrbm);
                    param_data.setXMLB1(xmlb1);
                    param_data.setXMFZR(xmzrr);
                    param_data.setXMCYR(xmcyr);
                    // 项目申请人   ，用 申请人代替
                    param_data.setSQR(xmsqr);
                    param_data.setTSCPGG(tscpgg);
                    param_data.setSCJT(scjt);
                    param_data.setTSYQ(tsyq);
                    param_data.setTSMD(tsmd);
                    param_data.setCPMC(cpmc);
                    param_data.setGZMJ(gzmj);
                    param_data.setTSGCMSJXJ(tsgcmsjxj);
                    param_data.setTSQSSJ2(tsqssj2);
                    param_data.setTSQZSJ1(tsqssj1);
                    param_data.setJSJWD(jsjwd);
                    param_data.setTSSDZX(Double.parseDouble(tssdzx));
                    param_data.setTSPJSCSD(Double.parseDouble(tspjscsd));
                    param_data.setTSSDZD(Double.parseDouble(tssdzd));
                    param_data.setJSJWDBZ(jsjwdbz);
                    param_data.setSCSZWD(scszwd);
                    param_data.setSCSJWD(scsjwd);
                    param_data.setGYCSQTSM(gycsqtsm);
                    param_data.setYCLGJZBSMJJTY(yclgjzbsmjjy);
                    param_data.setTSRZXQSMJJY(tsrzxqsmjjy);
                    param_data.setCLQTSMJJY(clqtsmjjy);
                    param_data.setTSJL(tsjl);
                    param_data.setTSSDBZ(tssdbz);
                    param_data.setSCWDBZ(scwdbz);
                    param_data.setSQR(xmsqr);
                    param_data.setGYS1(gys1);
                    param_data.setGYS2(gys2);
                    param_data.setGYS3(gys3);
                    param_data.setGYS4(gys4);
                    param_data.setGYS5(gys5);
                    param_data.setGYS6(gys6);
                    try {
                        param_data.setJTYSCJL(Double.parseDouble(jtyscjl));
                        logger_f52b61d7.error(String.valueOf("明细表赋值结束"));
                    } catch (NumberFormatException e) {
                        System.err.println(e.getMessage());
                        logger_f52b61d7.error(String.valueOf("机头与水槽距离强制转换报错 " + e.getMessage()));
                    }
                    logger_f52b61d7.error("主表单信息封装结束，取供应商1" + param_data.getGYS1());
                } catch (NumberFormatException e) {
                    System.err.println(e.getMessage());
                    logger_f52b61d7.error(String.valueOf("主表强制转换传值报错 " + e.getMessage()));
                }
                try {
                    // 封装信息
                    logger_f52b61d7.error(String.valueOf("开始封装表单信息"));
                    param_data.setITEMS(arrayItems);
                    param_datas.addOA_XCLSTBG_HEADER_TYPE(param_data);
                    resultin.setITFID("IFTIF001");
                    resultin.setWERKS("A000");
                    resultin.setDATAS(param_datas);
                } catch (NumberFormatException e) {
                    System.err.println(e.getMessage());
                    logger_f52b61d7.error(String.valueOf("封装报错 " + e.getMessage()));
                }
                logger_f52b61d7.error(String.valueOf("调用接口开始0 "));
                logger_f52b61d7.error(String.valueOf("调用接口完成RequestId--" + requestId + "--流程编号--" + bh + "项目责任人" + xmzrr));
                String sta = "";
                String meg = "";
                CallMesNewMaterialTSBGSerivce mes = new CallMesNewMaterialTSBGSerivce();
                // 修改过
                mes.setOainput(resultin);
                String[] rtInfo = mes.call();
                logger_f52b61d7.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_f52b61d7.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_f52b61d7.error("b--"+b);
                logger_f52b61d7.error(String.valueOf("OaMesNewMaterialTSBGAction-Request: " + requestId + "完成"));
                if (!sta.equals("S")) {
                    request.getRequestManager().setMessageid("100001");
                    request.getRequestManager().setMessagecontent(meg);
                    return  WeaResult.fail(500, "OaMesNewMaterialTSBGAction meg"+ meg, getResultMapForAction(weaverResultMap, "result",meg, request.getRequestManager()));

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