package com.sw.mes.sap.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sw.mes.base.factory.init.entity.FactoryModel;
import com.sw.mes.base.factory.init.service.FactoryModelService;
import com.sw.mes.base.factory.material.entity.MaterialType;
import com.sw.mes.base.factory.material.service.MaterialModelChartsService;
import com.sw.mes.base.factory.material.service.MaterialModelMaktService;
import com.sw.mes.base.factory.material.service.MaterialModelMarcService;
import com.sw.mes.base.factory.material.service.MaterialModelService;
import com.sw.mes.base.factory.material.service.MaterialTypeService;
import com.sw.mes.base.factory.material.service.UnitService;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.base.system.service.DictionaryThemeService;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.dto.sap.ReportSapGoodsmvtDto;
import com.sw.mes.core.enums.FactoryModelNodeTypeEnum;
import com.sw.mes.core.exception.BusinessException;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.jip.dto.TokenDto;
import com.sw.mes.jip.entity.JipHeaderConfig;
import com.sw.mes.jip.enums.IFKeyWordEnums;
import com.sw.mes.jip.service.JipHeaderConfigService;
import com.sw.mes.jip.utils.JipRestUtils;
import com.sw.mes.jip.utils.JipUtil;
import com.sw.mes.sap.dto.MblnrDataParam;
import com.sw.mes.sap.dto.jip.JipISBCINFO;
import com.sw.mes.sap.entity.LogSapMesInteractive;
import com.sw.mes.sap.entity.LogSapStock;
import com.sw.mes.sap.entity.WarehouseLocation;
import com.sw.mes.sap.service.LogSapMesInteractiveService;
import com.sw.mes.sap.service.LogSapStockService;
import com.sw.mes.sap.service.SAPOutputServiceService;
import com.sw.mes.sap.service.WarehouseLocationService;
import com.sw.mes.sap.task.MaterialDataHandlerTask;
import com.sw.mes.utils.CodeUtil;
import com.sw.mes.utils.StringUtils;
import com.sw.mes.yb.plan.entity.ProductionOrder;
import com.sw.mes.yb.plan.service.ProductionOrderService;
import com.xxl.job.core.handler.annotation.XxlJob;

import cc.cczn.matrix.commons.tasks.queue.Task;
import cc.cczn.matrix.commons.tasks.queue.block.LinkedBlockDataQueue;
import lombok.extern.slf4j.Slf4j;

@Service
@Jinko(value = "晶科MES传输至SAP操作业务逻辑Service", author = "liru")
@Slf4j
public class SAPOutputServiceImpl implements SAPOutputServiceService {

	@Autowired
	private JipHeaderConfigService jipHeaderConfigService;

	@Autowired
	private LogSapMesInteractiveService logSapMesInteractiveService;

	@Autowired
	private FactoryModelService factoryModelService;

	@Autowired
	private MaterialTypeService materialTypeService;

	@Autowired
	private MaterialModelService materialModelService;

	@Autowired
	private UnitService unitService;

	@Autowired
	private WarehouseLocationService WarehouseLocationService;

	@Autowired
	private MaterialModelMaktService materialModelMaktService;

	@Autowired
	private MaterialModelMarcService materialModelMarcService;

	@Autowired
	private MaterialModelChartsService materialModelChartsService;

	@Autowired
	private DictionaryItemService dictionaryItemService;
	@Autowired
	private DictionaryThemeService dictionaryThemeService;

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private LinkedBlockDataQueue queue;
	
	
	@Autowired
	private LinkedBlockDataQueue mblnrQueryQueue;

	@Autowired
	private MaterialDataHandlerTask task;

	@Autowired
	private LogSapStockService logSapStockService;
	@Autowired
	private WarehouseLocationService warehouseLocationService;
	@Autowired
	private ProductionOrderService productionOrderService;

	/**
	 * 发送货物移动数据到SAP
	 * 
	 * @throws JsonProcessingException
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public JSONObject sendWorkGoodsMvtToSap(JSONArray data, ReportSapGoodsmvtDto reportSapGoodsmvtDto)
			throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig, reportSapGoodsmvtDto);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", data);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			try {
				result = jipHeaderConfigService.httpRequestJip(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
			}catch (BusinessException e) {
				log.info("Nacos线路上报失败,直接走JIP线路重新上报",e);
				result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
			}
			// 保存交互记录
			saveInteractiveLog("mes->sap", "发送工单消耗数据至SAP", "sendWorkOrderConsumptionToSap", dataObj, result);
		} catch (Exception e) {
			log.error("SAP请求异常", e);
			saveInteractiveLog("mes->sap", "发送工单消耗数据至SAP", "sendWorkOrderConsumptionToSap", dataObj, e);
			throw new RuntimeException(e.getMessage());
		}

		return result;
	}

	@Override
	public JSONObject sendWorkinghoursToSap(JSONArray items) throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.REPORTWORKINGHOURS.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置生产订单报工时接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置生产订单报工时接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_IN", items);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送成本中心消耗数据到SAP
		JSONObject result = null;
		try {
			try {
				result = jipHeaderConfigService.httpRequestJip(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
			}catch (BusinessException e) {
				log.info("Nacos线路上报失败,直接走JIP线路重新上报",e);
				result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
			}
			
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送生产订单报工时数据至SAP", "sendReportWorkingHoursToSap", dataObj, result);

		return result;
	}

	@Override
	public void sendWorkWMSBackToSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		String json = "";

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(json), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送工单工时数据至SAP", "sendWorkWMSBackToSap", new JSONObject(JSONObject.parseObject(json)), result);

	}

	/**
	 * 发送工单消耗数据至SAP
	 */
	@Override
	public void sendWorkOrderConsumptionToSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "261");// 移动类型
		item.put("AUFNR", "71000093");
		item.put("EBELN", "");// sap生产订单号
		item.put("EBELP", "");// 采购订单号
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");
		item.put("LGORT", "P12A");
		item.put("MATNR", "A32N06R12022");
		item.put("CHARG", "2024011901");// 批次
		item.put("MENGE", 1.000);
		item.put("MEINS", "KG");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);

		// TODO 填充工单消耗数据 等待工单消耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			
			
			
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送工单消耗数据至SAP", "sendWorkOrderConsumptionToSap", new JSONObject(dataObj),
				result);

		// sendWorkOrderConsumptionToSap1();
	}

	public void sendWorkOrderConsumptionToSap1() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "261");// 移动类型
		item.put("AUFNR", "71000061");// sap生产订单号
		item.put("EBELN", "");
		item.put("EBELP", "");// 采购订单号
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");// 工厂
		item.put("LGORT", "P12A");// 库存地点
		item.put("MATNR", "A33N06R11022");// 物料编码
		item.put("CHARG", "2024011901");
		item.put("MENGE", 1.000);
		item.put("MEINS", "KG");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);

		// TODO 填充工单消耗数据 等待工单消耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送工单消耗数据至SAP", "sendWorkOrderConsumptionToSap", new JSONObject(dataObj),
				result);
		sendWorkOrderConsumptionToSap2();
	}

	public void sendWorkOrderConsumptionToSap2() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "261");// 移动类型
		item.put("AUFNR", "71000062");// sap生产订单号
		item.put("EBELN", "");
		item.put("EBELP", "");// 采购订单号
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");// 工厂
		item.put("LGORT", "P12A");// 库存地点
		item.put("MATNR", "A33N06R11022");// 物料编码
		item.put("CHARG", "2024011901");
		item.put("MENGE", 1.000);
		item.put("MEINS", "KG");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);

		// TODO 填充工单消耗数据 等待工单消耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送工单消耗数据至SAP", "sendWorkOrderConsumptionToSap", new JSONObject(dataObj),
				result);
		sendWorkOrderConsumptionToSap3();
	}

	public void sendWorkOrderConsumptionToSap3() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "261");// 移动类型
		item.put("AUFNR", "71000093");// sap生产订单号
		item.put("EBELN", "");
		item.put("EBELP", "");// 采购订单号
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");// 工厂
		item.put("LGORT", "P12A");// 库存地点
		item.put("MATNR", "A31N06R13022");// 物料编码
		item.put("CHARG", "2024011901");// 批次号
		item.put("MENGE", 1.000);
		item.put("MEINS", "KG");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);

		// TODO 填充工单消耗数据 等待工单消耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送工单消耗数据至SAP", "sendWorkOrderConsumptionToSap", new JSONObject(dataObj),
				result);
		sendWorkOrderConsumptionToSap4();
	}

	public void sendWorkOrderConsumptionToSap4() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "261");// 移动类型
		item.put("AUFNR", "71000094");// sap生产订单号
		item.put("EBELN", "");
		item.put("EBELP", "");// 采购订单号
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");// 工厂
		item.put("LGORT", "P12A");// 库存地点
		item.put("MATNR", "A31N06R13022");// 物料编码
		item.put("CHARG", "2024011901");
		item.put("MENGE", 1.000);
		item.put("MEINS", "KG");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);

		// TODO 填充工单消耗数据 等待工单消耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送工单消耗数据至SAP", "sendWorkOrderConsumptionToSap", new JSONObject(dataObj),
				result);
		sendWorkOrderConsumptionToSap5();
	}

	public void sendWorkOrderConsumptionToSap5() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "261");// 移动类型
		item.put("AUFNR", "71000101");// sap生产订单号
		item.put("EBELN", "");
		item.put("EBELP", "");// 采购订单号
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");// 工厂
		item.put("LGORT", "P12A");// 库存地点
		item.put("MATNR", "D32213102322");// 物料编码
		item.put("CHARG", "2024011901");
		item.put("MENGE", 1.000);
		item.put("MEINS", "KG");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);

		// TODO 填充工单消耗数据 等待工单消耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送工单消耗数据至SAP", "sendWorkOrderConsumptionToSap", new JSONObject(dataObj),
				result);
		sendWorkOrderConsumptionToSap6();
	}

	public void sendWorkOrderConsumptionToSap6() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "261");// 移动类型
		item.put("AUFNR", "71000102");// sap生产订单号
		item.put("EBELN", "");
		item.put("EBELP", "");// 采购订单号
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");// 工厂
		item.put("LGORT", "P11A");// 库存地点
		item.put("MATNR", "D32214302122");// 物料编码
		item.put("CHARG", "COTST108BY");
		item.put("MENGE", 1.000);
		item.put("MEINS", "KG");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);

		// TODO 填充工单消耗数据 等待工单消耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送工单消耗数据至SAP", "sendWorkOrderConsumptionToSap", new JSONObject(dataObj),
				result);
		sendWorkOrderConsumptionToSap7();
	}

	// 车间退料
	public void sendWorkOrderConsumptionToSap7() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "311");// 移动类型
		item.put("AUFNR", "71000102");// sap生产订单号
		item.put("EBELN", "");
		item.put("EBELP", "");// 采购订单号
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");// 工厂
		item.put("LGORT", "P11A");// 库存地点
		item.put("MATNR", "D32214302122");// 物料编码
		item.put("CHARG", "COTST108BY");
		item.put("MENGE", 1.000);
		item.put("MEINS", "KG");
		item.put("UMWRK", "5041");
		item.put("UMLGO", "R001");
		item.put("UMCHA", "COTST107BY");
		item.put("SGTXT", "");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);

		// TODO 填充工单消耗数据 等待工单消耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送车间退料至SAP", "sendWorkOrderConsumptionToSap", new JSONObject(dataObj), result);
	}

	/**
	 * 发送工单收货数据至SAP
	 */
	@Override
	public void sendWorkOrderReceiptToSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WORKORDERRECEIPT.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置工单收货接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置工单收货接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "101");
		item.put("AUFNR", "");
		item.put("EBELN", "4100021274");
		item.put("EBELP", "10");
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");
		item.put("LGORT", "R101");
		item.put("MATNR", "33000067");
		item.put("CHARG", "");
		item.put("MENGE", 2.000);
		item.put("MEINS", "PC");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "1340030270120221108");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);
		// TODO 填充工单收货数据 等待工单收货业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单收货数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送工单收货数据至SAP", "sendWorkOrderReceiptToSap", new JSONObject(dataObj), result);
	}

	/**
	 * 发送物料损耗数据至SAP
	 */
	@Override
	public void sendMaterialLossToSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService.getByKeyword(IFKeyWordEnums.MATERIALLOSS.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置物料损耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置物料损耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "261");
		item.put("AUFNR", "");
		item.put("EBELN", "4100021274");
		item.put("EBELP", "10");
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");
		item.put("LGORT", "R101");
		item.put("MATNR", "33000067");
		item.put("CHARG", "");
		item.put("MENGE", 2.000);
		item.put("MEINS", "PC");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "1340030270120221108");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);
		// TODO 填充物料损耗数据 等待物料损耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送物料损耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送物料损耗数据至SAP", "sendMaterialLossToSap", new JSONObject(dataObj), result);
	}

	/**
	 * 发送调拨同步数据至SAP
	 */
	@Override
	public void sendTransferSynchronizationToSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.TRANSFERSYNCHRONIZATION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置调拨同步接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置调拨同步接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "311");
		item.put("AUFNR", "");
		item.put("EBELN", "4100021274");
		item.put("EBELP", "10");
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");
		item.put("LGORT", "R101");
		item.put("MATNR", "33000067");
		item.put("CHARG", "");
		item.put("MENGE", 2.000);
		item.put("MEINS", "PC");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "1340030270120221108");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);
		// TODO 填充调拨同步数据 等待调拨同步业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送调拨同步数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送调拨同步数据至SAP", "sendTransferSynchronizationToSap", new JSONObject(dataObj),
				result);
	}

	/**
	 * 发送成本中心消耗数据至SAP
	 */
	@Override
	public void sendCostCenterConsumptionToSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.COSTCENTERCONSUMPTION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置成本中心消耗接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置成本中心消耗接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 测试数据
		JSONArray items = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("BWART", "201");
		item.put("AUFNR", "");
		item.put("EBELN", "4100021274");
		item.put("EBELP", "10");
		item.put("RSNUM", "");
		item.put("RSPOS", "");
		item.put("WERKS", "5041");
		item.put("LGORT", "R101");
		item.put("MATNR", "33000067");
		item.put("CHARG", "");
		item.put("MENGE", 2.000);
		item.put("MEINS", "PC");
		item.put("UMWRK", "");
		item.put("UMLGO", "");
		item.put("UMCHA", "");
		item.put("SGTXT", "1340030270120221108");
		items.add(item);

		JSONObject GOODS_ITEMS = new JSONObject();
		GOODS_ITEMS.put("item", items);

		JSONObject IT_DATA = new JSONObject();
		IT_DATA.put("ZUUID", CodeUtil.getUniqueCode(999l));
		IT_DATA.put("BUDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("BLDAT", DateUtil.formatDate(new Date(), DateUtil.shortFormat));
		IT_DATA.put("ERNAM", "MES");
		IT_DATA.put("GOODS_ITEMS", GOODS_ITEMS);
		// TODO 填充成本中心消耗数据 等待成本中心消耗业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", IT_DATA);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送成本中心消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送成本中心消耗数据至SAP", "sendCostCenterConsumptionToSap", new JSONObject(dataObj),
				result);
	}

	/**
	 * 发送生产订单报工时数据至SAP
	 */
	@Override
	public void sendReportWorkingHoursToSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.REPORTWORKINGHOURS.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置生产订单报工时接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置生产订单报工时接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// TODO 填充生产订单报工时数据 等待生产订单报工时业务明确后完成

		JSONArray array = new JSONArray();
		JSONObject obj = new JSONObject();
		obj.put("RECORD_ID", "DJWS3000049S223");
		obj.put("PRODUCTION_ORDER_ID", "71000101");
		obj.put("PLANT", "5041");
		obj.put("ORDER_TYPE", "0010");
		obj.put("OPERATION_ID", "0010");
		obj.put("WORKCENTER_ID", "SXLJ1012");
		obj.put("CNF_QTY1", 0);
		obj.put("CNF_QTY2", 0);
		obj.put("CNF_QTY3", 0);
		obj.put("CNF_UOM", "T");
		obj.put("CNF_TIME", "2");
		obj.put("CNF_TIME_UOM", "PC");
		obj.put("CNF_DATE", "20240109");
		array.add(obj);

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_IN", array);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送成本中心消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送生产订单报工时数据至SAP", "sendReportWorkingHoursToSap", new JSONObject(dataObj),
				result);
		sendReportWorkingHoursToSap1();
	}

	public void sendReportWorkingHoursToSap1() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.REPORTWORKINGHOURS.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置生产订单报工时接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置生产订单报工时接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// TODO 填充生产订单报工时数据 等待生产订单报工时业务明确后完成

		JSONArray array = new JSONArray();
		JSONObject obj = new JSONObject();
		obj.put("RECORD_ID", "DJWS3000049S221");
		obj.put("PRODUCTION_ORDER_ID", "71000101");
		obj.put("PLANT", "5041");
		obj.put("ORDER_TYPE", "0020");
		obj.put("OPERATION_ID", "0010");
		obj.put("WORKCENTER_ID", "SXLJ1012");
		obj.put("CNF_QTY1", 0);
		obj.put("CNF_QTY2", 0);
		obj.put("CNF_QTY3", 0);
		obj.put("CNF_UOM", "T");
		obj.put("CNF_TIME", "2");
		obj.put("CNF_TIME_UOM", "PC");
		obj.put("CNF_DATE", "20240109");
		array.add(obj);

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_IN", array);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送成本中心消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送生产订单报工时数据至SAP", "sendReportWorkingHoursToSap", new JSONObject(dataObj),
				result);
	}

	/**
	 * 拉取同步SAP的工厂信息 物料主数据
	 */
	@Override
	public void getFactoryFromSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService.getByKeyword(IFKeyWordEnums.FACTORY.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置同步SAP的工厂接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置同步SAP的工厂接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息

		String dataJson = objectMapper.writeValueAsString(dataObj);
		JSONObject result = null;

		try {
			// 2.发送成本中心消耗数据到SAP
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
			JSONArray etDatas = result.getJSONArray("ET_DATA");
			// 获取数组长度
			int length = etDatas.size();
			// 使用for循环遍历每个工厂信息数据
			for (int i = 0; i < length; i++) {
				JSONObject element = etDatas.getJSONObject(i);
				// 工厂代码
				String WERKS = element.getString("WERKS");
				// 工厂名称
				String NAME1 = element.getString("NAME1");
				// 公司代码
				String BUKRS = element.getString("BUKRS");
				// 公司名称
				String BUTXT = element.getString("BUTXT");

				FactoryModel factoryModel = factoryModelService.getByWorkCenterCode(WERKS);
				if (factoryModel == null) {
					factoryModel = new FactoryModel();
					factoryModel.setName(NAME1);
					factoryModel.setCode(WERKS);
//                    factoryModel.setWorkType(FactoryModelWorkTypeEnum.FACTORY.getKey());
				}

				factoryModel.setWorkCenterCode(WERKS);
				factoryModel.setWorkCenterName(NAME1);

				// 根据业务调整修改
				factoryModel.setEnable(true);
				factoryModel.setNodeType(FactoryModelNodeTypeEnum.SYSTEM_MODEL);
				factoryModel.setParentId(0);
				factoryModelService.saveOrUpdate(factoryModel);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送同步SAP的工厂数据至SAP", "getFactoryFromSap", new JSONObject(dataObj), result);
	}

	/**
	 * 拉取同步SAP的物料组信息 物料主数据
	 */
	@Override
	@XxlJob("getMaterialGroupDataFromSap")
	public void getMaterialGroupDataFromSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.MATERIALGROUPDATA.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置同步SAP的物料组接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置同步SAP的物料组接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// 根据工厂模型中工厂编号 参考【拉取同步SAP的工厂信息】中WERKS
		List<FactoryModel> factorys = factoryModelService.findByParentId(0);
		for (FactoryModel factory : factorys) {
			Map<String, Object> isData = new HashMap<>();
			isData.put("LANGU", "1");// 语言
			isData.put("WERKS", factory.getWorkCenterCode());// 工厂代码

			Map<String, Object> dataObj = new HashMap<>();
			dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
			dataObj.put("IT_DATA", isData);

			JSONObject result = null;
			try {
				String dataJson = objectMapper.writeValueAsString(dataObj);

				// 2.发送获取物料组到SAP
				log.info("请求sap主数据信息:url" + jipHeaderConfig.getUrl() + "data:" + JSONObject.parseObject(dataJson)
						+ "token" + token);
				result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
				// 物料组JSONArray
				JSONArray jsonArray = result.getJSONArray("ET_OUTPUT");
				// 获取数组长度
				int length = jsonArray.size();
				// 使用for循环遍历每个物料组信息数据
				for (int i = 0; i < length; i++) {
					JSONObject element = jsonArray.getJSONObject(i);

					// 物料组
					String MATKL = element.getString("MATKL");
					// 物料组描述
					String WGBEZ = element.getString("WGBEZ");
					// 工厂
					String WERKS = element.getString("WERKS");
					// 批次规则
					String ZBATCH_RULE = element.getString("ZBATCH_RULE");
					// 批次规则描述
					String ZBATCH_NAME = element.getString("ZBATCH_NAME");
					// 消息类型
					String TYPE = element.getString("TYPE");
					// 消息内容
					String MSG = element.getString("MSG");

					// 物料类型
					MaterialType materialType = materialTypeService.getByCode(MATKL);// MATKL 物料组
					if (materialType == null) {
						materialType = new MaterialType();
						materialType.setCreateDate(new Date());
						materialType.setCreateUser("SAP");
						materialType.setCode(MATKL);// 编码 - 物料组
					}
					materialType.setDel(false);// LVORM 删除标识
					materialType.setType("1");// 兼容jinko 只设置中类
					materialType.setName(WGBEZ);// WGBEZ 物料组描述
					materialType.setWerks(WERKS);// 工厂编码
					materialType.setZbatchName(ZBATCH_NAME); // 批次规则描述
					materialType.setZbatchRule(ZBATCH_RULE); // 批次规则

					materialType.setParentId(1);
					materialType.setGroupName("中类");

					// 添加或更新物料类型
					materialTypeService.saveOrUpdate(materialType);

				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			// 保存交互记录
			saveInteractiveLog("mes->sap", "发送同步SAP的物料组数据至SAP", "getMaterialGroupDataFromSap", new JSONObject(dataObj),
					result);
		}
	}

	/**
	 * 拉取同步SAP的物料信息 物料主数据
	 */
	@Override
	@XxlJob("getMaterialDataFromSap")
	public void getMaterialDataFromSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService.getByKeyword(IFKeyWordEnums.MATERIALDATA.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置同步SAP的物料接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置同步SAP的物料接口URL请求地址");

		int i = 0;
		while (true) {

			JSONObject result = null;
			JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
			TokenDto token = JipUtil.initToken(jipHeaderConfig);

			Map<String, Object> dataObj = new HashMap<>();
			dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息

			dataObj.put("IV_DATE_F", "");
			dataObj.put("IV_DATE_T", "");
			dataObj.put("IV_INC", "");

			dataObj.put("IV_TIME_F", "");
			dataObj.put("IV_TIME_T", "");

			dataObj.put("IV_PAGE", ++i);
			dataObj.put("IV_PLINE", 200);

			// 物料编码查询物料 MES中暂时未用到
			JSONArray IT_MATNR = new JSONArray();
			JSONObject MATNRObject = new JSONObject();
			MATNRObject.put("MATNR", "");
			IT_MATNR.add(MATNRObject);
			dataObj.put("IT_MATNR", IT_MATNR);

			try {
				// 按工厂查询 使用MM中同步工厂信息信息 - 工厂编码查询
				JSONArray IT_PLANT = new JSONArray();
				List<FactoryModel> factorys = factoryModelService.findByParentId(0);
				for (FactoryModel factory : factorys) {
					if (StringUtils.isNoneBlank(factory.getWorkCenterCode())) {
						JSONObject WERKS = new JSONObject();
						WERKS.put("WERKS", factory.getWorkCenterCode());
						IT_PLANT.add(WERKS);
					}
				}
				dataObj.put("IT_PLANT", IT_PLANT);

				String dataJson = objectMapper.writeValueAsString(dataObj);

				// 2.发送查询物料数据到SAP
				log.info("请求sap主数据信息:url" + jipHeaderConfig.getUrl() + "data:" + JSONObject.parseObject(dataJson)
						+ "token" + token);
				result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
				JSONArray jsonArray = result.getJSONArray("ET_DATA");

				JSONObject ES_BC_INFO = result.getJSONObject("ES_BC_INFO");

				// 处理数据
				// handlerData(jsonArray);

				// 转为jsonArray格式的数据
				JSONArray array = JSONArray.parseArray(JSON.toJSONString(jsonArray));

				// 业务逻辑处理
				Task<JSONArray> task = new Task<JSONArray>();
				task.setKey("material_page_" + i);
				task.setData(array);
				// bugfix 解决异步线程内获取context为空的问题
				task.setRequestAttributes(RequestContextHolder.getRequestAttributes());
				queue.submitTask(task, 10, TimeUnit.SECONDS);

				// 保存交互记录
				saveInteractiveLog("mes->sap", "发送同步SAP的物料数据至SAP", "getMaterialDataFromSap", new JSONObject(dataObj),
						result);

				if (!ES_BC_INFO.getString("TEXT2").equals("0001")) {
					break;
				}

				int totalPage = Integer.valueOf(ES_BC_INFO.getString("TEXT3"));
				if (i >= totalPage) {
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * 拉取物料批次，成品收货批次（工单收货传入）信息 物料批次
	 */
	@Override
	public void getMaterialBatchFromSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.MATERIALBATCH.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置物料批次，成品收货批次（工单收货传入）接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置物料批次，成品收货批次（工单收货传入）接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// TODO 填充物料批次，成品收货批次（工单收货传入）数据 等待物料批次，成品收货批次（工单收货传入）业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		// dataObj.put("IT_ORDER_GI",workOrderConsumptionList);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送成本中心消耗数据到SAP
		JSONObject result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送物料批次，成品收货批次（工单收货传入）数据至SAP", "getMaterialBatchFromSap",
				new JSONObject(dataObj), result);
	}

	/**
	 * 晶科SAP库存地点同步MES
	 */
	@Override
	public void getWarehouseLocationFromSap() throws JsonProcessingException {
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService
				.getByKeyword(IFKeyWordEnums.WAREHOUSELOCATION.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未同步库位信息接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中同步库位信息接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		List<Map<String, Object>> searchParams = new ArrayList<>();
		// 根据工厂模型中工厂编号 参考【拉取同步SAP的工厂信息】中WERKS
		List<FactoryModel> factorys = factoryModelService.findByParentId(0);
		for (FactoryModel factory : factorys) {
			Map<String, Object> factorySearch = new HashMap<>();
			factorySearch.put("WERKS", factory.getWorkCenterCode());
			searchParams.add(factorySearch);
		}

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息
		dataObj.put("IT_DATA", searchParams);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送成本中心消耗数据到SAP
		JSONObject result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);

//		DictionaryTheme dictionaryTheme = dictionaryThemeService.findByValue("WarehouseLocation");

		String msgty = result.getString("E_MSGTY");
		if (msgty != null && msgty.equals("S")) {
			JSONArray jsonArray = result.getJSONArray("ET_DATA");
			int length = jsonArray.size();
			for (int i = 0; i < length; i++) {
				JSONObject element = jsonArray.getJSONObject(i);
				String werks = element.getString("WERKS");
				String name1 = element.getString("NAME1");
				String lgort = element.getString("LGORT");
				String lgobe = element.getString("LGOBE");

				WarehouseLocation warehouseLocation = WarehouseLocationService.findByWerksAndLgort(werks, lgort);
				if (warehouseLocation == null)
					warehouseLocation = new WarehouseLocation();
				warehouseLocation.setWerks(werks);
				warehouseLocation.setName1(name1);
				warehouseLocation.setLgort(lgort);
				warehouseLocation.setLgobe(lgobe);
				warehouseLocation.setCreateTime(new Date());
				WarehouseLocationService.saveOrUpdate(warehouseLocation);

				// 同步数据字典
//				DictionaryItem dictionaryItem = dictionaryItemService.findByValueAndThemeId(lgort,
//						dictionaryTheme.getId());
//				if (dictionaryItem == null) {
//					dictionaryItem = new DictionaryItem();
//				}
//				dictionaryItem.setName(lgobe);
//				dictionaryItem.setValue(lgort);
//				dictionaryItem.setValue(lgort);
//				dictionaryItem.setExtendValue(name1);
//				dictionaryItem.setExtendValue1(werks);
//				dictionaryItem.setTheme(dictionaryTheme);
//				dictionaryItemService.saveOrUpdate(dictionaryItem);
			}
		}

		// 保存交互记录
		saveInteractiveLog("mes->sap", "发送同步库位信息至SAP", "getWarehouseLocationFromSap", new JSONObject(dataObj), result);
	}

	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void saveInteractiveLog(String direction, String controllerName, String oprMethod, Object data, Object result) {
		try {
			LogSapMesInteractive log = new LogSapMesInteractive();
			log.setDataDirection(direction);
			log.setControllerName(controllerName);
			log.setOprMethod(oprMethod);
			log.setContext(JSONObject.toJSONString(data));
			log.setReturnContext(JSONObject.toJSONString(result));
			log.setCreateTime(new Date());
			logSapMesInteractiveService.saveOrUpdate(log);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void getSapStock(String lgort,String matnr) throws Exception {
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService.getByKeyword(IFKeyWordEnums.SAPSTOCK.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置获取SAP库存接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置获取SAP库存接口URL请求地址");

		JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
		TokenDto token = JipUtil.initToken(jipHeaderConfig);

		// TODO 填充物料批次，成品收货批次（工单收货传入）数据 等待物料批次，成品收货批次（工单收货传入）业务明确后完成

		Map<String, Object> dataObj = new HashMap<>();
		dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息

		JSONObject IT_DATA = new JSONObject();
		JSONArray DATAS = new JSONArray();
		JSONObject DATA = new JSONObject();
		// {
		// "WERKS":"1500",
		// "LGORT":"F021",
		// "MATNR":"10000000"
		// }
		
		DATA.put("WERKS", "5041");
		DATA.put("LGORT", lgort);
		
		if(StringUtils.isNotBlank(matnr)) {
			DATA.put("MATNR", matnr);
		}
		

		DATAS.add(DATA);

		IT_DATA.put("DATA", DATAS);

		dataObj.put("IT_DATA", IT_DATA);

		// 参数示例
		// 查询条件 工厂
		// JSONArray T_WERKS = new JSONArray();
		// JSONObject WERKS = new JSONObject();
		// WERKS.put("WERKS", "5041");
		// T_WERKS.add(WERKS);
		// dataObj.put("T_WERKS", T_WERKS);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送成本中心消耗数据到SAP
		JSONObject result = null;
		try {
			// 报文示例
			//[{"WERKS":"5041","LGORT":"P21A","MATNR":"A31N06R13022","SOBKZ":"","KDAUF":"","KDPOS":"000000","LIFNR":"0001021130","CHARG":"TCN242011A","LABST":345.48,"INSME":0.0,"SPEME":0.0,"MEINS":"KG","T006":"","T107":"","T111":"","T119":"","T025":"","SALK3":0.0},{"WERKS":"5041","LGORT":"P21A","MATNR":"D32110402122","SOBKZ":"","KDAUF":"","KDPOS":"000000","LIFNR":"0001021130","CHARG":"TODN412510","LABST":20.0,"INSME":0.0,"SPEME":0.0,"MEINS":"KG","T006":"","T107":"","T111":"","T119":"","T025":"","SALK3":0.0}]
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
			JSONArray jsonArray = result.getJSONArray("ET_OUTPUT");
			List<LogSapStock> stocks = jsonArray.toJavaList(LogSapStock.class);
			
			List<LogSapStock> sources = null;
			
			if(matnr!=null) {
				sources = logSapStockService.findByLgortAndMatnr(lgort,matnr);
			}else {
				sources = logSapStockService.findByLgort(lgort);
			}
			
			
			if(stocks.size()>0) {
				
				Map<String,LogSapStock> sourceMaps = new HashMap<String, LogSapStock>();
				for (int index=0;index<sources.size();index++) {
					LogSapStock logstock = sources.get(index);
					sourceMaps.put(logstock.getLgort()+"-"+logstock.getMatnr()+"-"+logstock.getCharg(), logstock);
				}
				
				for (LogSapStock logSapStock : stocks) {
					//数据库中查询集合 下标
					LogSapStock source = sourceMaps.get(logSapStock.getLgort()+"-"+logSapStock.getMatnr()+"-"+logSapStock.getCharg());
					
					if(source == null) {//查询记录表中不存在sap库存 - 新增
						LogSapStock target = new LogSapStock();
						BeanUtil.copyProperties(logSapStock, target, "id");
						logSapStockService.saveOrUpdate(target);
					}else {//查询记录表中存在sap库存 - 修改
						BeanUtil.copyProperties(logSapStock, source, "id");
						logSapStockService.saveOrUpdate(source);
						
						//集合中移除已处理项
						sources.remove(source);
					}
				}
				
			}
			
			//修改剩余库存sap中已为0的记录项
			for (LogSapStock source : sources) {
				source.setLabst("0");
				source.setInsme("0");
				source.setSpeme("0");
				logSapStockService.saveOrUpdate(source);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// 保存交互记录
		//saveInteractiveLog("mes->sap", "获取SAP库存数据", "getSapStock", new JSONObject(dataObj), result);
	}

	@Override
	public void synOrderState() {
		List<Long> ids = new ArrayList<Long> ();
		List<LogSapMesInteractive> list = logSapMesInteractiveService.findByControllerName("晶科SAP生产工单下发");
		System.out.println("....");
		for (LogSapMesInteractive logSapMesInteractive : list) {
			String content = logSapMesInteractive.getContext();
			
			JSONObject data = JSON.parseObject(content);
			
			JSONArray IT_DATA = data.getJSONArray("IT_DATA");

	        int length = IT_DATA.size();
	        for (int i=0;i<length;i++){
	            JSONObject orderJson = IT_DATA.getJSONObject(i);

	            String WERKS = orderJson.getString("WERKS");
	            String AUFNR = orderJson.getString("AUFNR");//订单编号
	            String AUART = orderJson.getString("AUART");
	            String AUARTTXT = orderJson.getString("AUARTTXT");
	            Date GSTRP = orderJson.getDate("GSTRP");
	            Date GLTRP = orderJson.getDate("GLTRP");
	            String Z_ORIENTATION_IND = orderJson.getString("Z_ORIENTATION_IND");
	            String STAT = orderJson.getString("STAT");
	            
	            JSONArray ORDERLIST = orderJson.getJSONArray("ORDERLIST");
	            int o_length = ORDERLIST.size();
	            for (int j=0;j<o_length;j++){
	            	 //订单物料信息
	                JSONObject ORDER = ORDERLIST.getJSONObject(j);
	                String MATNR = ORDER.getString("MATNR");
	                
	                if(STAT.equals("TECO")) {
		            	ProductionOrder productionOrder = productionOrderService.findByAufnrAndMatnr(AUFNR,MATNR);
		            	productionOrder.setProductionStatus(false);
		            	productionOrderService.saveOrUpdate(productionOrder);
		            	ids.add(productionOrder.getId());
		            	System.out.println("update plan_production_order set production_status=0 where id="+productionOrder.getId());
		            }
	            }
	        }
		}
		
		System.out.println(JSON.toJSONString(ids));
	}

	@Override
	public void getMblnrDataFromSap(MblnrDataParam param) {
		//初始化时间
		param.initDate();
		
		// 1.构造工单消耗数据格式
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService.getByKeyword(IFKeyWordEnums.MBLNRDATA.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置同步SAP的物料接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置同步SAP的物料接口URL请求地址");

		while (true) {

			JSONObject result = null;
			JipISBCINFO jipISBCINFO = JipUtil.initISBCINFO(jipHeaderConfig);
			TokenDto token = JipUtil.initToken(jipHeaderConfig);

			Map<String, Object> dataObj = new HashMap<>();
			dataObj.put("IS_BC_INFO", jipISBCINFO);// header信息

			
			if(StringUtils.isNotBlank(param.getIvInc())) {
				dataObj.put("IV_INC", param.getIvInc());
			}
			
			if(StringUtils.isNotBlank(param.getIvDateF())) {
				dataObj.put("IV_DATE_F", param.getIvDateF());
			}
			if(StringUtils.isNotBlank(param.getIvDateT())) {
				dataObj.put("IV_DATE_T", param.getIvDateT());
			}
			if(StringUtils.isNotBlank(param.getIvTimeF())) {
				dataObj.put("IV_TIME_F", param.getIvTimeF());
			}
			if(StringUtils.isNotBlank(param.getIvTimeT())) {
				dataObj.put("IV_TIME_T", param.getIvTimeT());
			}
			
			
			dataObj.put("IV_PAGE", param.getIvPage());
			dataObj.put("IV_PLINE", param.getIvPline());
			
			
			//物料凭证查询
			if(param.getMblnrs().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getMblnrs()) {
					JSONObject obj = new JSONObject();
					obj.put("MBLNR", value);
					array.add(obj);
				}
				
				dataObj.put("IV_PLINE", array);
			}
			
			
			//物料号
			if(param.getMatnrs().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getMatnrs()) {
					JSONObject obj = new JSONObject();
					obj.put("MATNR", value);
					array.add(obj);
				}
				
				dataObj.put("IT_MATNR", array);
			}
			
			//工厂
			if(param.getWerkss().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getWerkss()) {
					JSONObject obj = new JSONObject();
					obj.put("WERKS", value);
					array.add(obj);
				}
				
				dataObj.put("IT_WERKS", array);
			}
			
			//物料类型
			if(param.getMtarts().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getMtarts()) {
					JSONObject obj = new JSONObject();
					obj.put("MTART", value);
					array.add(obj);
				}
				
				dataObj.put("IT_MTART", array);
			}
			
			//物料组
			if(param.getMatkls().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getMatkls()) {
					JSONObject obj = new JSONObject();
					obj.put("MATKL", value);
					array.add(obj);
				}
				
				dataObj.put("IT_MATKL", array);
			}
			
			//IT_WEMPF	WEMPF		多值	输入	标准档位    未做条件
			
			//移动类型
			if(param.getBwarts().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getBwarts()) {
					JSONObject obj = new JSONObject();
					obj.put("BWART", value);
					array.add(obj);
				}
				
				dataObj.put("IT_BWART", array);
			}
			
			//IT_BKTXT	BKTXT		多值	输入	凭证抬头文本  未做
			
			
			//采购订单
			if(param.getEbelns().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getEbelns()) {
					JSONObject obj = new JSONObject();
					obj.put("EBELN", value);
					array.add(obj);
				}
				
				dataObj.put("IT_EBELN", array);
			}
			
			//工单
			if(param.getAufnrs().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getAufnrs()) {
					JSONObject obj = new JSONObject();
					obj.put("AUFNR", value);
					array.add(obj);
				}
				
				dataObj.put("IT_AUFNR", array);
			}
			
			//内向交货单
			if(param.getVbelns().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getVbelns()) {
					JSONObject obj = new JSONObject();
					obj.put("VBELN", value);
					array.add(obj);
				}
				
				dataObj.put("IT_VBELN", array);
			}
			
			//过账日期
			if(param.getBudats().size()>0) {
				JSONArray array = new JSONArray();
				for (String value : param.getBudats()) {
					JSONObject obj = new JSONObject();
					obj.put("BUDAT", value);
					array.add(obj);
				}
				
				dataObj.put("IT_BUDAT", array);
			}
			
//			dataObj.put("IV_PAGE", ++i);
//			dataObj.put("IV_PLINE", 200);

			try {
				String dataJson = objectMapper.writeValueAsString(dataObj);

				// 2.发送查询物料数据到SAP
				log.info("请求sap物料凭证查询接口（通用）:url" + jipHeaderConfig.getUrl() + " param:" + JSONObject.parseObject(dataJson)
						+ " token" + token);
				result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
				JSONArray jsonArray = result.getJSONArray("ET_DATA");

				JSONObject ES_BC_INFO = result.getJSONObject("ES_BC_INFO");

				// 转为jsonArray格式的数据
				JSONArray array = JSONArray.parseArray(JSON.toJSONString(jsonArray));

				// 业务逻辑处理
				Task<JSONArray> task = new Task<JSONArray>();
				task.setKey("material_page_" + param.getIvPage());
				task.setData(array);
				// bugfix 解决异步线程内获取context为空的问题
				task.setRequestAttributes(RequestContextHolder.getRequestAttributes());
				mblnrQueryQueue.submitTask(task, 10, TimeUnit.SECONDS);

				// 保存交互记录
				saveInteractiveLog("mes->sap", "发送查询物料凭证查询接口（通用）", "getMblnrDataFromSap", new JSONObject(dataObj),
						result);

				if (!ES_BC_INFO.getString("TEXT2").equals("0001")) {
					break;
				}

				int totalPage = Integer.valueOf(ES_BC_INFO.getString("TEXT3"));
				
				if (param.getIvPage() >= totalPage) {
					break;
				}
				//分页数+1
				param.setIvPage(param.getIvPage()+1);
			} catch (Exception e) {
				e.printStackTrace();
				break;
			}

		}
	}

}
