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

import  com.weaver.seconddev.interfaces.k3.GiftQtyService;
import  com.weaver.seconddev.interfaces.k3.GiftQtyServiceStub.ArrayOfGiftStockDt;
import  com.weaver.seconddev.interfaces.k3.GiftQtyServiceStub.GiftStockDt;
import java.util.HashMap;
import java.util.Map;
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.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;

@org.springframework.stereotype.Service("workflow_action_CheckGiftNumFromK3Action")
public class CheckGiftNumFromK3Action extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private  GiftQtyService service;

    private static final Logger logger_33d4b5 = LoggerFactory.getLogger(CheckGiftNumFromK3Action.class);

    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);

        try {
            String requestid = requestinfo.getRequestid();
            try {
                logger_33d4b5.error(String.valueOf("CheckGiftNumFromK3Action--start--" + requestid));
                logger_33d4b5.error("requestIdTemp"+requestIdTemp);
                logger_33d4b5.error("userIdTemp"+userIdTemp);

                DetailTable[] detailtable = requestinfo.getDetailTableInfo().getDetailTable();
                DetailTable dtq = detailtable[0];
                Row[] sq = dtq.getRow();
                ArrayOfGiftStockDt giftStockDt = new ArrayOfGiftStockDt();
                GiftStockDt[] arr = new GiftStockDt[sq.length];
                Map<String, String> mcMap = new HashMap<String, String>();
                Map<String, String> slMap = new HashMap<String, String>();
                for (int j = 0; j < sq.length; j++) {
                    Row rq = sq[j];
                    Cell[] cq = rq.getCell();
                    String mc = "";
                    String sl = "";
                    String bm = "";
                    // 公司编码
                    String gsbm = "";
                    GiftStockDt param = new GiftStockDt();
                    for (int k = 0; k < cq.length; k++) {
                        Cell cq1 = cq[k];
                        String nameq = cq1.getName().toUpperCase();
                        String valueq = Util.null2String(cq1.getValue());
                        if ("LPBM".equals(nameq)) {
                            param.setFNumber(valueq);
                            bm = valueq;
                        }
                        if ("CKBM".equals(nameq)) {
                            param.setFStockNumber(valueq);
                        }
                        if ("MXWPMC".equals(nameq)) {
                            mc = valueq;
                        }
                        if ("MXSL".equals(nameq)) {
                            sl = valueq;
                        }
                        if ("GSBM".equals(nameq)) {
                            gsbm = valueq;
                            param.setFCompanyNumber(valueq);
                        }
                    }
                    arr[j] = param;
                    mcMap.put(bm, mc);
                    slMap.put(bm, sl);
                }
                logger_33d4b5.error("arr --- " + arr.toString());
                giftStockDt.setGiftStockDt(arr);
                logger_33d4b5.error("giftStockDt --- " + giftStockDt.toString());
//                GiftQtyService service = new GiftQtyService();
                logger_33d4b5.error("service --- " + service.toString());
                Map<String, String> map = service.call(giftStockDt);
                StringBuilder sb = new StringBuilder();
                logger_33d4b5.error("code "+map.get("code"));
                if ("1".equals(map.get("code"))) {
                    for (String key : slMap.keySet()) {
                        if ("0".equals(map.get(key))) {
                            sb.append(mcMap.get(key) + "，当前库存为0，请选择其他礼品；");
                            continue;
                        }
                        if (Double.parseDouble(slMap.get(key)) > Double.parseDouble(map.get(key))) {
                            sb.append(mcMap.get(key) + "，当前库存为" + map.get(key) + "，请修改领用数量；");
                        }
                    }
                } else {
                    sb.append("调用K3Cloud接口返回信息：" + map.get("msg"));
                }
                if (sb.length() > 0) {
                    requestinfo.getRequestManager().setMessageid("ERROR");
                    requestinfo.getRequestManager().setMessagecontent(sb.toString());
                    logger_33d4b5.error(String.valueOf("CheckGiftNumFromK3Action--error--" + requestid + "[" + sb.toString() + "]"));
                    return  WeaResult.fail(500, "CheckGiftNumFromK3Action--error--" + requestid + "[" + sb.toString() + "]", getResultMapForAction(weaverResultMap, "result", sb, requestinfo.getRequestManager()));

                }
            } catch (Exception e) {
                requestinfo.getRequestManager().setMessageid("ERROR");
                requestinfo.getRequestManager().setMessagecontent("调用K3Cloud接口返回信息：" + e.toString());
                logger_33d4b5.error("e->" + e);
                logger_33d4b5.error(String.valueOf("CheckGiftNumFromK3Action--error--" + requestid + "[" + e.toString() + "]"));
                return  WeaResult.fail(500, "CheckGiftNumFromK3Action--error--" + requestid + "[" + e.toString() + "]", getResultMapForAction(weaverResultMap, "result", e.getMessage(), requestinfo.getRequestManager()));

            }
            logger_33d4b5.error(String.valueOf("CheckGiftNumFromK3Action--end--" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_33d4b5.error("CheckGiftNumFromK3Action e: {}", e.getMessage());
            return  WeaResult.fail(500, "CheckGiftNumFromK3Action 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;
    }
}
