// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name:   ZOA_MM_PR_CREATE_BpAction.java
package com.weaver.seconddev.interfaces.workflow.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import  com.weaver.seconddev.interfaces.workflow.entity.GeneralEntity;
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.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.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;

// Referenced classes of package  com.weaver.seconddev.interfaces.workflow.action:
// ZOA_MM_PR_CREATE, Action
@org.springframework.stereotype.Service("workflow_action_ZOA_MM_PR_RELEASEAction")
public class ZOA_MM_PR_RELEASEAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_41a94d2d = LoggerFactory.getLogger(ZOA_MM_PR_RELEASEAction.class);

    public ZOA_MM_PR_RELEASEAction() {
    }

    @SuppressWarnings("unchecked")
    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_41a94d2d.error("ZOA_MM_PR_RELEASEAction 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 requestinfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            try {
                RecordSet recordset = I18nContextUtil.getBean(RecordSet.class);
                // SAP采购申请成组号
                String pr_grpno = "";
                String requestId = requestinfo.getRequestid();
                String tablename = requestinfo.getRequestManager().getBillTableName();
                Property[] aproperty = requestinfo.getMainTableInfo().getProperty();
                for (int j = 0; j < aproperty.length; j++) {
                    String name = aproperty[j].getName().toUpperCase();
                    String value = Util.null2String(aproperty[j].getValue());
                    if (name.equals("PRNO")) {
                        pr_grpno = value;
                    }
                }
                String id = "";
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                String sql1 = "select top 1 id from e10_core_business.dbo." + tablename + " where id = " + requestId;
                logger_41a94d2d.error("sql1 -----" + sql1);
                boolean b = recordset.executeSql(sql1, poolname);
                logger_41a94d2d.error("b -----" + b);
                if (recordset.next()) {
                    id = recordset.getString("id");
                }
                logger_41a94d2d.error(String.valueOf("ZOA_MM_PR_RELEASEAction ----- requestId(" + requestId + ") ----- id(" + id + ")"));
                DetailTable[] adetailtable = requestinfo.getDetailTableInfo().getDetailTable();
                DetailTable detailtable = adetailtable[0];
                Row[] arow = detailtable.getRow();
                List<String> list = new ArrayList<String>();
                for (int k = 0; k < arow.length; k++) {
                    Row row = arow[k];
                    Cell[] acell = row.getCell();
                    String cgdh = "";
                    String spyj = "";
                    for (int l = 0; l < acell.length; l++) {
                        Cell cell = acell[l];
                        String name = cell.getName().toUpperCase();
                        String value = Util.null2String(cell.getValue());
                        if (name.equals("CGDH")) {
                            // 采购申请单号
                            cgdh = value;
                        }
                        if (name.equals("SPYJ")) {
                            // 审批意见 0=批准 1=驳回
                            spyj = value;
                        }
                    }
                    if ("0".equals(spyj)) {
                        // 把所有的采购申请单号放入list
                        list.add(cgdh);
                    }
                }
                ZOA_MM_PR_RELEASE release = new ZOA_MM_PR_RELEASE();
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                Map<String, Object> map = release.Getexecute(pr_grpno, list);
                if (map != null && map.size() > 0) {
                    String msg = String.valueOf(map.get("msg"));
                    // 总的状态
                    String e_status = String.valueOf(map.get("E_STATUS"));
                    String e_message = String.valueOf(map.get("E_MESSAGE"));
                    // 得到明细数据
                    List<GeneralEntity> list2 = (List<GeneralEntity>) map.get("mx");
                    if ("ok".equals(msg)) {
                        if (list2 != null && list2.size() > 0) {
                            logger_41a94d2d.error("ZOA_MM_PR_RELEASEAction ----- list2的个数(" + list2.size() + ")");
                            for (int i = 0; i < list2.size(); i++) {
                                GeneralEntity ge = (GeneralEntity) list2.get(i);
                                String cgdh = ge.getExt01();
                                String status = ge.getExt02();
                                String msg2 = ge.getExt03();
                                Object[] obj = new Object[4];
                                obj[0] = status;
                                obj[1] = msg2;
                                obj[2] = id;
                                obj[3] = cgdh;
                                String sql = "update e10_core_business.dbo." + tablename + "_dt1 set state = ? , msg = ? where form_data_id = ? and cgdh= ?";
                                logger_41a94d2d.error(String.valueOf("ZOA_MM_PR_RELEASEAction ----- sql = " + sql));
                                String poolname_nt = CONN_TYPE.workflow.getType();
                                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                                rs.executeUpdate(sql, poolname_nt, obj);
                            }
                        }
                        Object[] obj1 = new Object[2];
                        obj1[0] = e_status;
                        obj1[1] = e_message;
                        String sql = "update " + tablename + " set status = ? , message = ? where id = " + requestId;
                        logger_41a94d2d.error(String.valueOf("ZOA_MM_PR_RELEASEAction ----- sql = " + sql));
                        String poolname_sb = CONN_TYPE.workflow.getType();
                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                        rs.executeUpdate(sql, poolname_sb, obj1);
                    } else {
                        logger_41a94d2d.error(String.valueOf("----------ZOA_MM_PR_RELEASEAction log 返回 map的msg = " + msg));
                        // requestinfo.getRequestManager().setMessageid("ERROR");
                        // requestinfo.getRequestManager().setMessagecontent(msg);
                        // return Action.SUCCESS;
                    }
                } else {
                    logger_41a94d2d.error(String.valueOf("----------ZOA_MM_PR_RELEASEAction log 返回 map 为空--------"));
                    // requestinfo.getRequestManager().setMessageid("ERROR");
                    // requestinfo.getRequestManager().setMessagecontent("返回 map 为空");
                    // return Action.SUCCESS;
                }
            } catch (Exception exception) {
                logger_41a94d2d.error(String.valueOf("start log--"));
                logger_41a94d2d.error(String.valueOf("------------------------------------------------------------------------"));
                logger_41a94d2d.error(String.valueOf((new StringBuilder()).append("ZOA_MM_PR_CREATE_BpAction error:").append(exception.toString()).toString()));
                logger_41a94d2d.error(String.valueOf("------------------------------------------------------------------------"));
                logger_41a94d2d.error(String.valueOf("end log--"));
                return  WeaResult.fail(500, "ZOA_MM_PR_CREATE_BpAction error:"+ exception, getResultMapForAction(weaverResultMap, "result", exception.getMessage(), requestinfo.getRequestManager()));
                // requestinfo.getRequestManager().setMessageid("ERROR");
                // requestinfo.getRequestManager().setMessagecontent(exception.getMessage());
                // return Action.SUCCESS;
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_41a94d2d.error("ZOA_MM_PR_RELEASEAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_MM_PR_RELEASEAction e: {}"+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), requestinfo.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;
    }
}
