package com.sunda.spmswms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmscommon.entity.ZSapOrgClient;
import com.sunda.spmscommon.feign.IFeignServiceCommon;
import com.sunda.spmscommon.mapper.ZSapOrgClientMapper;
import com.sunda.spmswms.service.IDataToSap;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: spms
 * @description:
 * @author: Wayne Wu
 * @create: 2021-07-05 11:03
 **/
@Service
public class DataToSap implements IDataToSap {

    @Autowired
    IFeignServiceCommon iFeignServiceCommon;
    @Autowired
    ZSapOrgClientMapper zSapOrgClientMapper;

    @Value("${zxd.sap.client}")
    private String sapClient;

    /** 其他出入库————无源的移仓，提交SAP */
    @Override
    public JSONObject submitDataToSap(JSONObject doc) {
        JSONObject message = JSONObject.parseObject(iFeignServiceCommon.sendRestToSapCommon(doc));
        return message;
    }

    @Override
    public JSONObject formatDataToSap(Map<String, Object> dataToSapHead, List<Map<String, Object>> dataToSapItem, String operation){
        JSONObject doc = new JSONObject();

        JSONObject REQUEST = new JSONObject();
        JSONObject ESB_ATTRS = new JSONObject();
        JSONObject REQUEST_DATA = new JSONObject();
        REQUEST_DATA.put("Target_ID", sapClient);//装箱单的接口只推送到装箱单的client

        ESB_ATTRS.put("App_ID", "SPMS");
        if( dataToSapHead.get("WERKS")!=null){
            // 根据工厂查询对应的SAP Clinet
            List<ZSapOrgClient> selectList = zSapOrgClientMapper.selectList(new QueryWrapper<ZSapOrgClient>().eq("WERKS", dataToSapHead.get("WERKS")));
            if (CollectionUtils.isNotEmpty(selectList)) {
                ESB_ATTRS.put("Target_ID", "SAP-"+selectList.get(0).getZmandt());
            } else {
                throw new RuntimeException("工厂："+dataToSapHead.get("werks")+"未找到对应的SAP Clinet");
            }
        }

        ESB_ATTRS.put("Application_ID", "00020000000002");
        ESB_ATTRS.put("Transaction_ID", UUID.randomUUID().toString());

        SimpleDateFormat simp = new SimpleDateFormat("yyyy:MM:ddHH:mm:ss:SS");//月日时分秒 毫秒
        String serialNumber=simp.format(new Date()).replace(":","" );
//        if (!"Z_SPMS_ANOMALY_POST".equals(operation)) {
//        	dataToSapHead.put("SPMSID", "SPMS"+serialNumber);
//        }

        if ("ZZXD_IF_SPMS_04".equals(operation) ) {
            Map<String, Object> tables = new HashMap<>();
            List<Object> head = new ArrayList<>();
            head.add(dataToSapHead);
            tables.put("T_HEAD", head);
            tables.put("T_ITEM", dataToSapItem);
            REQUEST_DATA.put("tables", tables);
        }else if ("ZZXD_IF_SPMS_10".equals(operation)) {
            Map<String, Object> tables = new HashMap<>();
            tables.put("IT_ITEM", dataToSapItem);
            REQUEST_DATA.put("tables", tables);
        }else{
            REQUEST_DATA.put("Items", dataToSapItem);
            REQUEST_DATA.put("Head", dataToSapHead);
        }

        if ("Z_SPMS_ANOMALY_POST".equals(operation)){
            REQUEST_DATA.put("Operation", "Z_SPMS_ANOMALY_POST");
            REQUEST_DATA.put("Type", "ANOMALY_POST");
            if("Z_SPMS_ANOMALY_POST".equals(operation)){
                Map<String, Object> fileds = new HashMap<>();
                fileds.put("SPMSID", "SPMS"+serialNumber);
                REQUEST_DATA.put("fileds", fileds);
                Map<String, Object> structures = new HashMap<>();
                structures.put("I_HEADER", dataToSapHead);
                REQUEST_DATA.put("structures", structures);

                Map<String, Object> tables = new HashMap<>();
                tables.put("IT_ITME", dataToSapItem);
                REQUEST_DATA.put("tables", tables);
            }
        }
        if ("ZZXD_IF_SPMS_10".equals(operation)){
            REQUEST_DATA.put("Operation", "ZZXD_IF_SPMS_10");
            REQUEST_DATA.put("Type", "ZZXD_IF_SPMS_10");
            ESB_ATTRS.put("Target_ID", sapClient);//装箱单的接口只推送到装箱单的client
        }
        if ("ZZXD_IF_SPMS_04".equals(operation)){
            REQUEST_DATA.put("Operation", "ZZXD_IF_SPMS_04");
            REQUEST_DATA.put("Type", "ZZXD_IF_SPMS_04");
            ESB_ATTRS.put("Target_ID", sapClient);//装箱单的接口只推送到装箱单的client
        }

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

        doc.put("REQUEST", REQUEST);
        return doc;
    }

}
