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_PROCESSING_CHARGE_Action")
public class ZOEM_PROCESSING_CHARGE_Action extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_9b3f3cb8 = LoggerFactory.getLogger(ZOEM_PROCESSING_CHARGE_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_9b3f3cb8.error("----- workflow_action_ZOEM_PROCESSING_CHARGE_Action new -----");
        logger_9b3f3cb8.error("requestIdTemp -----"+ requestIdTemp);
        logger_9b3f3cb8.error("userIdTemp -----"+ userIdTemp);

        try {
            String requestid = requestinfo.getRequestid();
            String wfid = requestinfo.getWorkflowid();
            try {
                logger_9b3f3cb8.error(String.valueOf("ZOEM_PROCESSING_CHARGE_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][8];
                    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] = map.get("CGZZ");
                        // 物料编号
                        str[j][1] = map.get("CPWLBM");
                        // 供应商ERP编号
                        str[j][2] = map.get("CSERPDM");
                        // 订单单位
                        str[j][3] = map.get("DW");
                        // 价格单位
                        str[j][4] = map.get("JGDW");
                        // 价格
                        str[j][5] = map.get("JGF");
                        // 开始生效日期
                        str[j][6] = map.get("TJJLYXQSRQ").replaceAll("-", "");
                        // 有效截至日期
                        str[j][7] = map.get("TJJLYXJZRQ").replaceAll("-", "");
                    }
                    ZOEM_PROCESSING_CHARGE info = new ZOEM_PROCESSING_CHARGE();
                    String[] ret = info.Getexecute(str);
                    state = ret[0];
                    logger_9b3f3cb8.error(String.valueOf("ZOEM_PROCESSING_CHARGE_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_9b3f3cb8.error("sql -----"+ sql);
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(sql, poolname);
                logger_9b3f3cb8.error("b -----"+ b);
            } catch (Exception e) {
                logger_9b3f3cb8.error(String.valueOf("ZOEM_PROCESSING_CHARGE_Action-error-" + requestid + "-[" + e.toString() + "]"));
                requestinfo.getRequestManager().setMessagecontent("调用SAP接口同步数据出错：" + e.toString());
                return  WeaResult.fail(500, "调用SAP接口同步数据出错：" + e, getResultMapForAction(weaverResultMap, "result", "", requestinfo.getRequestManager()));
            }
            logger_9b3f3cb8.error(String.valueOf("ZOEM_PROCESSING_CHARGE_Action-end-" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_9b3f3cb8.error("ZOEM_PROCESSING_CHARGE_Action e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOEM_PROCESSING_CHARGE_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;
    }
}
