package com.sunda.spmsweb.ibmmqservice.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmscommon.service.ISysLogMqService;
import com.sunda.spmscommon.service.ISysLogService;
import com.sunda.spmsorder.service.IOrderWerksService;
import com.sunda.spmsoversea.service.IOverseaWhsDumpService;
import com.sunda.spmsweb.ibmmq.JmsConfigOut;
import com.sunda.spmsweb.ibmmqservice.SendMessageInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsOperations;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.sunda.spmscommon.Constans.*;

/**
 * @program: spms
 * @description: send message interface implements
 * @author: Wayne Wu
 * @create: 2020-11-03 14:21
 **/
@Component
@Slf4j
public class SendMessageInterfaceImpl implements SendMessageInterface {

    @Autowired
    IOrderWerksService iOrderWerksService;

    @Autowired
    ISysLogMqService iSysLogMqService;

    @Autowired
    ISysLogService iSysLogService;
    
    @Autowired
    IOverseaWhsDumpService iOverseaWhsDumpService;

    @Autowired
    @Qualifier(value = "PIN_OUT3")
    JmsOperations jmsOperations;

    @Override
    public ResponseResult Z_RFC_STORE_09() {
        String msg = "{\"REQUEST\":{\"ESB_ATTRS\":{\"Application_ID\":\"00020000000004\"," +
                "\"Transaction_ID\":" + "\"" + UUID.randomUUID() + "\"" +
                "},\"REQUEST_DATA\":{\"reqAppId\":\"SPMS\",\"rfcName\":\"Z_RFC_STORE_09\"," +
                "\"date\":\"\",\"tableName\":\"\",\"isSyn\":\"0\"}}}";
        try {
            log.info("====Z_RFC_STORE_09===== SendMessageInterface 开始发送消息......\n\n" + msg);
            jmsOperations.convertAndSend(JmsConfigOut.queue, msg);
            log.info("========= SendMessageInterface 发送消息成功......");
            return ResponseResult.success().add("msg", "SendMessageInterface ========= 发送队列消息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("========= SendMessageInterface 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= 发送队列消息错误: " + e);
        }
    }

    @Override
    public ResponseResult Z_RFC_STORE_06() {
        String msg = "{\"REQUEST\":{\"ESB_ATTRS\":{\"Application_ID\":\"00020000000004\"," +
                "\"Transaction_ID\":" + "\"" + UUID.randomUUID() + "\"" +
                "},\"REQUEST_DATA\":{\"reqAppId\":\"SPMS\",\"rfcName\":\"Z_RFC_STORE_06\"," +
                "\"date\":\"\",\"tableName\":\"IT_T002T\",\"isSyn\":\"0\"}}}";
        try {
            log.info("=====Z_RFC_STORE_06==== SendMessageInterface  开始发送消息......");
            jmsOperations.convertAndSend(JmsConfigOut.queue, msg);
            log.info("========= SendMessageInterface 发送消息成功...... ");
            return ResponseResult.success().add("msg", "SendMessageInterface ========= 发送队列消息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("========= SendMessageInterface 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= 发送队列消息错误: " + e);
        }
    }


    @Override
    public ResponseResult Z_RFC_STORE_03() {
        String msg = "{\"REQUEST\":{\"ESB_ATTRS\":{\"Application_ID\":\"00020000000004\"," +
                "\"Transaction_ID\":" + "\"" + UUID.randomUUID() + "\"" +
                "},\"REQUEST_DATA\":{\"reqAppId\":\"SPMS\",\"rfcName\":\"Z_RFC_STORE_03\"," +
                "\"date\":\"\",\"tableName\":\"\",\"isSyn\":\"0\"}}}";
        try {
            log.info("=====Z_RFC_STORE_03==== SendMessageInterface 开始发送消息......");
            jmsOperations.convertAndSend(JmsConfigOut.queue, msg);
            log.info("========= SendMessageInterface 发送消息成功...... ");
            return ResponseResult.success().add("msg", "SendMessageInterface ========= 发送队列消息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("========= SendMessageInterface 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= 发送队列消息错误: " + e);
        }
    }

    @Override
    public ResponseResult Z_RFC_STORE_01() {
        String msg = "{\"REQUEST\":{\"ESB_ATTRS\":{\"Application_ID\":\"00020000000004\"," +
                "\"Transaction_ID\":" + "\"" + UUID.randomUUID() + "\"" +
                "},\"REQUEST_DATA\":{\"reqAppId\":\"SPMS\",\"rfcName\":\"Z_RFC_STORE_01\"," +
                "\"date\":\"\",\"tableName\":\"IT_MAKT\",\"isSyn\":\"0\"}}}";
        try {
            log.info("=====Z_RFC_STORE_03==== SendMessageInterface 开始发送消息......");
            jmsOperations.convertAndSend(JmsConfigOut.queue, msg);
            log.info("========= SendMessageInterface 发送消息成功......  ");
            return ResponseResult.success().add("msg", "SendMessageInterface ========= 发送队列消息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("========= SendMessageInterface 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= 发送队列消息错误: " + e);
        }
    }

    @Override
    public ResponseResult sendMessage(JSONObject doc) {
        try {

            String uuid = UUID.randomUUID().toString();
            JSONObject REQUEST = doc.getJSONObject("REQUEST");
            JSONObject ESB_ATTRS = REQUEST.getJSONObject("ESB_ATTRS");
            ESB_ATTRS.put("Transaction_ID", uuid);
            REQUEST.put("ESB_ATTRS", ESB_ATTRS);
            doc.put("REQUEST", REQUEST);

            //String message = StringEscapeUtils.escapeJavaScript(doc.toString());
            //message = "\"" + message + "\"";

            log.info("SendMessageInterface ========= sendOneTime 开始发送消息......\n\n" + doc.toString());
            jmsOperations.convertAndSend(JmsConfigOut.queue, doc.toString());
            log.info("SendMessageInterface ========= sendOneTime 发送队列消息成功\n");
            return ResponseResult.success().add("msg", "SendMessageInterface ========= 发送队列消息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("SendMessageInterface ========= sendOneTime 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= 发送队列消息错误: " + e);
        }
    }


    /**
     * {
     * 	"REQUEST": {
     * 		"ESB_ATTRS": {
     * 			"Application_ID": "00020000000004",
     * 			"Transaction_ID": ""
     *                },
     * 		"REQUEST_DATA": {
     * 			"reqAppId": "SPMS",
     * 			"rfcName": "Z_RFC_STORE_03",
     * 			"date": "",
     * 			"tableName": "",
     * 			"isSyn": "0"
     *        }
     *    }
     * }
     * @return
     */

    /**
    @Override
    public ResponseResult sendSRM() {
        try {
            String uuid = UUID.randomUUID().toString();
            JSONObject doc = new JSONObject();
            JSONObject REQUEST = new JSONObject();
            JSONObject ESB_ATTRS = new JSONObject();
            JSONObject REQUEST_DATA = new JSONObject();
            JSONObject DATA = new JSONObject();

            DATA.put("applicantOrder", iOrderTService.getOrderByOrderId("GF01202011190001"));
            DATA.put("applicantOrderDtlList", iOrderTService.getOrderDtlList("GF01202011190001"));

            ESB_ATTRS.put("Application_ID", "00020000000004");
            ESB_ATTRS.put("Transaction_ID", "");

            REQUEST_DATA.put("reqAppId", "SPMS");
            REQUEST_DATA.put("rfcName", "Z_RFC_STORE_09");
            REQUEST_DATA.put("date", "");
            REQUEST_DATA.put("tableName", "");
            REQUEST_DATA.put("isSyn", "0");
            REQUEST_DATA.put("data", DATA);

            REQUEST.put("ESB_ATTRS", ESB_ATTRS);
            REQUEST.put("REQUEST_DATA", REQUEST_DATA);

            doc.put("REQUEST", REQUEST);

            log.info("SendMessageInterface ========= sendOneTime 开始发送消息......\n\n" + doc.toString());
//            jmsOperations.convertAndSend(JmsConfigOut.queue, doc.toString());
//            log.info("SendMessageInterface ========= sendOneTime 发送队列消息成功\n");
//            return ResponseResult.success().add("msg", "SendMessageInterface ========= sendOneTime 发送队列消息成功");
            return ResponseResult.success().add("doc", doc);

        }catch (Exception e){
            log.info("SendMessageInterface ========= sendOneTime 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= 发送队列消息错误: " + e);
        }
    }


    @Override
    public ResponseResult getApplicantOrder() {
        return ResponseResult.success()
                .add("applicantOrder", iOrderTService.getOrderByOrderId("GF01202011190001"))
                .add("applicantOrderDtlList", iOrderTService.getOrderDtlList("GF01202011190001"));
    }
    **/

    /** 从SAP获取公司工厂信息 */
    @Override
    public ResponseResult Z_SPMS_GETFACTORY() {
        String msg = "{\"REQUEST\":{\"ESB_ATTRS\":{\"Application_ID\":\"00020000000004\"," +
                "\"Transaction_ID\":" + "\"" + UUID.randomUUID() + "\"" +
                "},\"REQUEST_DATA\":{\"reqAppId\":\"SPMS\",\"rfcName\":\"Z_SPMS_GETFACTORY\"," +
                "\"date\":\"\",\"tableName\":\"\",\"isSyn\":\"0\"}}}";
        try {
            log.info("=====Z_SPMS_GETFACTORY==== SendMessageInterface 开始发送消息 ......");
            jmsOperations.convertAndSend(JmsConfigOut.queue, msg);
            log.info("========= SendMessageInterface 发送消息成功......  ");
            return ResponseResult.success().add("msg", "SendMessageInterface ========= 发送队列消息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("========= SendMessageInterface 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= 发送队列消息错误: " + e);
        }
    }

    /** 从SAP获取仓库信息 */
    @Override
    public ResponseResult Z_SPMS_GETWAREHOUSE() {
        String msg = "{\"REQUEST\":{\"ESB_ATTRS\":{\"Application_ID\":\"00020000000004\"," +
                "\"Transaction_ID\":" + "\"" + UUID.randomUUID() + "\"" +
                "},\"REQUEST_DATA\":{\"reqAppId\":\"SPMS\",\"rfcName\":\"Z_SPMS_GETWAREHOUSE\"," +
                "\"date\":\"\",\"tableName\":\"\",\"isSyn\":\"0\"}}}";
        try {
            log.info("=====Z_SPMS_GETWAREHOUSE==== SendMessageInterface 开始发送消息 ......");
            jmsOperations.convertAndSend(JmsConfigOut.queue, msg);
            log.info("========= SendMessageInterface 发送消息成功......  ");
            return ResponseResult.success().add("msg", "SendMessageInterface ========= sendOneTime 发送队列消息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("========= SendMessageInterface 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= sendOneTime 发送队列消息错误: " + e);
        }
    }

    /** 从SAP获取成本中心信息 */
    @Override
    public ResponseResult Z_SPMS_GETCOSTCENTER() {
//        String msg = "{\"REQUEST\":{\"ESB_ATTRS\":{\"Application_ID\":\"00020000000004\"," +
//                "\"Transaction_ID\":" + "\"" + UUID.randomUUID() + "\"" +
//                "},\"REQUEST_DATA\":{\"reqAppId\":\"SPMS\",\"rfcName\":\"Z_SPMS_GETCOSTCENTER\"," +
//                "\"date\":\"\",\"tableName\":\"\",\"isSyn\":\"0\"}}}";
        try {
//            log.info("=====Z_SPMS_GETCOSTCENTER==== SendMessageInterface 开始发送消息 ......");
//            jmsOperations.convertAndSend(JmsConfigOut.queue, msg);
//            log.info("========= SendMessageInterface 发送消息成功......  ");
            return ResponseResult.success().add("msg", "SendMessageInterface ========= sendOneTime 发送队列消息成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("========= SendMessageInterface 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= sendOneTime 发送队列消息错误: " + e);
        }
    }


    /******************* 发送工厂请购单MQ消息给SRM **********************/
    @Override
    public ResponseResult spms_srm_purchase_info(String uuid) {
        try {
            //String mqUuid = UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String mqUuid = UUID.randomUUID().toString();
            JSONObject doc = new JSONObject();
            JSONObject REQUEST = new JSONObject();
            JSONObject ESB_ATTRS = new JSONObject();
            JSONObject REQUEST_DATA = new JSONObject();
            Map<String, Object> DATA = iOrderWerksService.orderWerksToSrm(uuid);

            ESB_ATTRS.put("Application_ID", "00020000000004");
            ESB_ATTRS.put("Transaction_ID", mqUuid);

            REQUEST_DATA.put("ReqAppId", "SPMS");
            REQUEST_DATA.put("FunctionId", "spms_srm_purchase_info");
            REQUEST_DATA.put("date", sdf.format(new Date()));
            REQUEST_DATA.put("isSyn", "0");
            REQUEST_DATA.put("Head", DATA.get("Head"));
            REQUEST_DATA.put("Items", DATA.get("Items"));

            REQUEST.put("ESB_ATTRS", ESB_ATTRS);
            REQUEST.put("RequestData", REQUEST_DATA);

            doc.put("REQUEST", REQUEST);

            log.info("SendMessageInterface ========= sendOneTime 开始发送消息......\n\n");
            jmsOperations.convertAndSend(JmsConfigOut.queue, doc.toString());
            System.out.println("\n\n" + doc.toString() + "\n\n");
            log.info("SendMessageInterface ========= sendOneTime 发送队列消息成功\n");
            return ResponseResult.success().add("msg", "SendMessageInterface ========= sendOneTime 发送队列消息成功\n\n" + doc.toString());
            //return ResponseResult.success().add("doc", doc);

        }catch (Exception e){
            e.printStackTrace();
            log.info("SendMessageInterface ========= sendOneTime 发送队列消息错误: " + e);
            return ResponseResult.error().add("msg", "SendMessageInterface ========= 发送队列消息错误: " + e);
        }
    }

    @Override
    public int spms_srm_purchase_info2(String oaBianhao) {
        JSONObject doc = new JSONObject();
        Map<String, Object> DATA = new HashMap<>();
        try {
            //String mqUuid = UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String mqUuid = UUID.randomUUID().toString();
            JSONObject REQUEST = new JSONObject();
            JSONObject ESB_ATTRS = new JSONObject();
            JSONObject REQUEST_DATA = new JSONObject();
            DATA = iOrderWerksService.orderWerksToSrm2(oaBianhao);

            if (DATA.get("Items") == null || DATA.get("Items").toString().length() == 0){
                return 0;
            }

            ESB_ATTRS.put("Application_ID", "00020000000004");
            ESB_ATTRS.put("Transaction_ID", mqUuid);

            REQUEST_DATA.put("ReqAppId", "SPMS");
            REQUEST_DATA.put("FunctionId", "spms_srm_purchase_info");
            REQUEST_DATA.put("date", sdf.format(new Date()));
            REQUEST_DATA.put("isSyn", "0");
            REQUEST_DATA.put("Head", DATA.get("Head"));
            REQUEST_DATA.put("Items", DATA.get("Items"));

            REQUEST.put("ESB_ATTRS", ESB_ATTRS);
            REQUEST.put("RequestData", REQUEST_DATA);

            doc.put("REQUEST", REQUEST);

            System.out.println("SendMessageInterface ====spms_srm_purchase_info===== sendOneTime 开始发送消息......\n\n" + doc);
            jmsOperations.convertAndSend(JmsConfigOut.queue, doc.toString());

            iSysLogMqService.addSysLogMq(SEND_MSG, "spms_srm_purchase_info", REQUEST_DESC_SEND, "spms_srm_purchase_info", doc.toJSONString(), null, oaBianhao);
            System.out.println("SendMessageInterface =====spms_srm_purchase_info==== sendOneTime 发送队列消息成功\n");
            return 1;
        }catch (Exception e){
            iSysLogMqService.addSysLogMq(SEND_MSG, "spms_srm_purchase_info", REQUEST_DESC_SEND_ERROR, "spms_srm_purchase_info", doc.toJSONString(), null, oaBianhao);
            e.printStackTrace();
            System.out.println("SendMessageInterface ====spms_srm_purchase_info===== sendOneTime 发送队列消息错误: " + e);
            return 0;
        }
    }

}
