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

import com.alibaba.fastjson.JSONObject;
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 com.weaver.verupgrade.workflow.request.RequestManager;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.common.base.entity.result.WeaResult;
import lombok.extern.slf4j.Slf4j;
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.DetailTable;
import com.weaver.verupgrade.soa.workflow.request.Row;
import com.weaver.verupgrade.soa.workflow.request.Cell;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * oem�����
 */
@org.springframework.stereotype.Service("workflow_action_ZOEM_PRICE_DIFFERENCE_RATE_Action")
@Slf4j
public class ZOEM_PRICE_DIFFERENCE_RATE_Action extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;
    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        log.error("ZOEM_PRICE_DIFFERENCE_RATE_Action 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);

        log.error("----- workflow_action_ZOEM_PRICE_DIFFERENCE_RATE_Action new -----");
        log.error("requestIdTemp -----"+ requestIdTemp);
        log.error("userIdTemp -----"+ userIdTemp);
        try {
            String requestid = requestinfo.getRequestid();
            String wfid = requestinfo.getWorkflowid();
            try {
                log.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());
                // 
                // }
                // ��ϸ��1
                DetailTable[] detailtable = requestinfo.getDetailTableInfo().getDetailTable();
            //    log.error("ZOEM_PRICE_DIFFERENCE_RATE_Action detailtable===>"+ JSONObject.toJSONString(detailtable));
                DetailTable dtq = detailtable[0];
                log.error("length" + detailtable.length);
                Row[] sq = dtq.getRow();
                StringBuilder sb = new StringBuilder();
                String state = "";
                String[][] str = new String[sq.length][9];
                if (sq.length > 0) {
                    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);
                        }
                        log.error("map--" + map);
                        // str[j][0] = requestid+"_"+j;
                        // �ɹ���֯
                        str[j][0] = map.get("CGZZ");
                        // ���ϱ��
                        str[j][1] = map.get("CPWLBM");
                        // ��������
                        str[j][2] = map.get("WLMS");
                        // ��Ӧ��ERP���
                        str[j][3] = map.get("CSERPDM");
                        // ��Ӧ������
                        str[j][4] = map.get("CSMC");
                        // �����
                        str[j][5] = map.get("CJL");
                        // ��ʼ��Ч����
                        str[j][6] = map.get("TJJLYXQSRQ").replaceAll("-", "");
                        // ��Ч��������
                        str[j][7] = map.get("TJJLYXJZRQ").replaceAll("-", "");
                        String gz = map.get("TLZFGZ");
                        String gzbm = "";
                        if ("0".equals(gz)) {
                            gzbm = "A";
                        } else if ("1".equals(gz)) {
                            gzbm = "B";
                        } else if ("2".equals(gz)) {
                            gzbm = "C";
                        } else if ("3".equals(gz)) {
                            gzbm = "D";
                        }
                        // ͭ���Ƿ�����
                        str[j][8] = gzbm;
                    }
                }
                // ��ϸ��2

                DetailTable dtq2 ;
                String[][] str2 = new String[][]{};
                if(detailtable.length >1){
                    dtq2  =detailtable[1];
                    Row[] sq2 = dtq2.getRow();
                    StringBuilder sb2 = new StringBuilder();
                    str2= new String[sq2.length][7];
                    if (sq2.length > 0) {
                        for (int j = 0; j < sq2.length; j++) {
                            Row rq = sq2[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);
                            }
                            log.error("map2--" + map);
                            String gz = map.get("TLZFGZ");
                            String gzbm = "";
                            if ("0".equals(gz)) {
                                gzbm = "B";
                            } else if ("1".equals(gz)) {
                                gzbm = "C";
                            } else if ("2".equals(gz)) {
                                gzbm = "D";
                            }
                            // ͭ
                            str2[j][0] = gzbm;
                            //
                            str2[j][1] = map.get("JZTJ");
                            // ͭ
                            str2[j][2] = map.get("TJDW");
                            //
                            str2[j][3] = map.get("TJCJLDZS");
                            //
                            str2[j][4] = map.get("JZLJ");
                            //
                            str2[j][5] = map.get("LJDW");
                            //
                            str2[j][6] = map.get("LJCJLDZS");
                        }
                    }

                }


                ZOEM_PRICE_DIFFERENCE_RATE info = new ZOEM_PRICE_DIFFERENCE_RATE();
                String[] ret = info.Getexecute(str, str2);
                state = ret[0];
                log.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;
                log.error("sql -----"+ sql);
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(sql, poolname);
                log.error("b -----"+ b);
                if(ret[0].equals("E")){
                    return  WeaResult.fail(500, "OEM成品差价率失败：" + sb, getResultMapForAction(weaverResultMap, "result", "", requestinfo.getRequestManager()));

                }

            } catch (Exception e) {
                log.error("ZOEM_PRICE_DIFFERENCE_RATE_Action-error-" , e);
                requestinfo.getRequestManager().setMessagecontent("OEM成品差价率返回结果：" + e.toString());
                return  WeaResult.fail(500, "OEM成品差价率返回结果：" + e, getResultMapForAction(weaverResultMap, "result", "", requestinfo.getRequestManager()));
            }
            log.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();
            log.error("ZOEM_PRICE_DIFFERENCE_RATE_Action e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOEM_PRICE_DIFFERENCE_RATE_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;
    }
}
