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

import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import java.util.HashMap;
import java.util.Map;
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 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.Row;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * OEM无有效信息记录的物料价格
 */
@org.springframework.stereotype.Service("workflow_action_ZOEM_MATRIAL_PRICE_Action")
public class ZOEM_MATRIAL_PRICE_Action extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_8aa7358c = LoggerFactory.getLogger(ZOEM_MATRIAL_PRICE_Action.class);

    @Override
    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 requestinfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);

        logger_8aa7358c.error("----- workflow_action_ZOEM_MATRIAL_PRICE_Action new -----");
        logger_8aa7358c.error("requestIdTemp -----"+ requestIdTemp);
        logger_8aa7358c.error("userIdTemp -----"+ userIdTemp);
        try {
            String requestid = requestinfo.getRequestid();
            String wfid = requestinfo.getWorkflowid();
            try {
                logger_8aa7358c.error(String.valueOf("ZOEM_PRICE_DIFFERENCE_RATE_Action-start-" + requestid));
                // 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());
                // 
                // }
                DetailTable[] detailtable = requestinfo.getDetailTableInfo().getDetailTable();
                DetailTable dtq = detailtable[0];
                Row[] sq = dtq.getRow();
                StringBuilder sb = new StringBuilder();
                String state = "";
                if (sq.length > 0) {
                    String[][] str = new String[sq.length][9];
                    for (int j = 0; j < sq.length; j++) {
                        Row rq = sq[j];
                        Cell[] cq = rq.getCell();
                        Map<String, String> map = new HashMap<String, String>();
                        for (int k = 0; k < cq.length; k++) {
                            Cell cq1 = cq[k];
                            String nameq = cq1.getName().toUpperCase();
                            String valueq = Util.null2String(cq1.getValue());
                            map.put(nameq, valueq);
                        }
                        // str[j][0] = requestid+"_"+j;
                        // 采购组织
                        str[j][0] = map.get("CGZZ");
                        // 物料编号
                        str[j][1] = map.get("WLH");
                        // str[j][2] = map.get("WLMS");//物料描述
                        // 订单单位
                        str[j][3] = map.get("DDDW");
                        // 条件定价单位
                        str[j][4] = map.get("TJDJDW");
                        // 价格
                        str[j][5] = map.get("JG");
                        // 开始生效日期
                        str[j][6] = map.get("TJJLYXQSRQ").replaceAll("-", "");
                        // 有效截至日期
                        str[j][7] = map.get("TJJLYXJZRQ").replaceAll("-", "");
                        // 供应商物料组
                        str[j][8] = map.get("GYSWLZ");
                    }
                    ZOEM_MATRIAL_PRICE info = new ZOEM_MATRIAL_PRICE();
                    String[] ret = info.Getexecute(str);
                    state = ret[0];
                    logger_8aa7358c.error(String.valueOf("ZOEM_PRICE_DIFFERENCE_RATE_Action-return-" + requestid + "-[" + ret[0] + "," + ret[1] + "]"));
                    sb.append("OEM无有效信息记录的物料价格结果：" + ret[1]);
                }
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                BillFieldUtil util = new BillFieldUtil();
//                String tablename = util.getTablename(wfid);
                String tablename = requestinfo.getRequestManager().getBillTableName();
                String sql = "update e10_core_business.dbo." + tablename + " set state='" + state + "',message='" + sb.toString() + "' where id=" + requestid;

                logger_8aa7358c.error("sql -----"+ sql);
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(sql, poolname);
                logger_8aa7358c.error("b -----"+ b);
            } catch (Exception e) {
                logger_8aa7358c.error(String.valueOf("ZOEM_PRICE_DIFFERENCE_RATE_Action-error-" + requestid + "-[" + e.toString() + "]"));
                requestinfo.getRequestManager().setMessagecontent("调用SAP接口同步数据出错：" + e.toString());
                return  WeaResult.fail(500, "调用SAP接口同步数据出错：" + e, getResultMapForAction(weaverResultMap, "result", "", requestinfo.getRequestManager()));
            }
            logger_8aa7358c.error(String.valueOf("ZOEM_PRICE_DIFFERENCE_RATE_Action-end-" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_8aa7358c.error("ZOEM_MATRIAL_PRICE_Action e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOEM_MATRIAL_PRICE_Action e: {}" + e, getResultMapForAction(weaverResultMap, "result", "", 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;
    }
}
