package com.sw.mes.wms.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.sap.dto.jip.JipProductionOrderResult;
import com.sw.mes.sap.dto.jip.JipWMSResult;
import com.sw.mes.wms.entity.LogWmsMesInteractive;
import com.sw.mes.wms.service.LogWmsMesInteractiveService;
import com.sw.mes.wms.service.WMSInputService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


/**
* @Author 李儒
* @Description：晶科SAP数据交互接口
*/
@RestController
@RequestMapping("/wms/dock")
@Slf4j
@Api(description = "晶科WSM数据交互接口")
@Jinko(value="晶科WMS对接接口控制器",author = "liru")
public class WMSInputController
{

	@Autowired
	private LogWmsMesInteractiveService logWmsMesInteractiveService;

	@Autowired
	private WMSInputService wmsInputService;

//

	@ApiOperation(value = "原辅料发线边仓")
	@PostMapping("/rawmaterialEdgewarehouse")
	public JSONObject rawmaterialEdgewarehouse(@RequestBody JSONObject data) {
		//每条记录处理情况返回值集合
		JSONObject ES_BC_INFO = new JSONObject();

		//解析工单数据
		try {
			JSONObject IT_DATA = data.getJSONObject("IT_DATA");
			ES_BC_INFO = data.getJSONObject("IS_BC_INFO");
			if(IT_DATA != null){
				try {
					wmsInputService.handlerRawmaterialEdgewarehouse(data);
					ES_BC_INFO.put("TYPE", JipWMSResult.SUCCESS);
					ES_BC_INFO.put("MSG", "成功");
				} catch (RuntimeException e) {
					ES_BC_INFO.put("TYPE", JipWMSResult.ERROR);
					ES_BC_INFO.put("MSG", e.getMessage());
				}
			}else{
				ES_BC_INFO.put("TYPE", JipWMSResult.ERROR);
				ES_BC_INFO.put("MSG", "数据格式错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		JSONObject resultData = new JSONObject();
		resultData.put("ES_BC_INFO",ES_BC_INFO);

		//保存交互记录
		saveInteractiveLog("wms->mes","晶科WMS原辅料发线边仓","rawmaterialEdgewarehouse",data,resultData);
		return resultData;
	}
	
	
	
	
	@ApiOperation(value = "退料反馈接口")
	@PostMapping("/returnFeedback")
	public JSONObject returnFeedback(@RequestBody JSONObject data) {
		//每条记录处理情况返回值集合
		JSONObject ES_BC_INFO = new JSONObject();
		
		List<JipWMSResult> results = new ArrayList<>();

		//解析工单数据
		try {
			JSONArray IT_DATA = data.getJSONArray("IT_DATA");
			ES_BC_INFO = data.getJSONObject("IS_BC_INFO");
			if(IT_DATA != null){
				try {
					wmsInputService.handlerReturnFeedback(data,results);
					ES_BC_INFO.put("TYPE", JipWMSResult.SUCCESS);
					ES_BC_INFO.put("MSG", "成功");
				} catch (RuntimeException e) {
					JipWMSResult result = new JipWMSResult(JipProductionOrderResult.ERROR,e.getMessage());
					results.add(result);
				}
			}else{
				JipWMSResult result = new JipWMSResult(JipWMSResult.ERROR,"数据格式错误");
				results.add(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		for (JipWMSResult jipWMSResult : results) {
			if(jipWMSResult.getType().equals(jipWMSResult.ERROR)) {
				ES_BC_INFO.put("TYPE", jipWMSResult.ERROR);
				ES_BC_INFO.put("MSG", jipWMSResult.getMessage());
				break;
			}else {
				ES_BC_INFO.put("TYPE", jipWMSResult.SUCCESS);
				ES_BC_INFO.put("MSG", jipWMSResult.getMessage());
			}
		}
		
		JSONObject resultData = new JSONObject();
		resultData.put("ES_BC_INFO",ES_BC_INFO);
		resultData.put("ET_DATA",results);
		
		//保存交互记录
		saveInteractiveLog("wms->mes","退料反馈接口","returnFeedback",data,resultData);
		return resultData;
	}
	
	
	
	
	@ApiOperation(value = "方棒入库反馈接口")
	@PostMapping("/fbInboundFeedback")
	public JSONObject fbInboundFeedback(@RequestBody JSONObject data) {
		//每条记录处理情况返回值集合
		List<JipWMSResult> results = new ArrayList<>();

		JSONObject ES_BC_INFO = new JSONObject();

		//解析工单数据
		try {
			JSONArray IT_DATA = data.getJSONArray("IT_DATA");
			ES_BC_INFO = data.getJSONObject("IS_BC_INFO");
			if(IT_DATA != null){
				try {
					wmsInputService.handlerfbInboundFeedback(data,results);
				} catch (RuntimeException e) {
					JipWMSResult result = new JipWMSResult(JipProductionOrderResult.ERROR,e.getMessage());
					results.add(result);
				}
			}else{
				JipWMSResult result = new JipWMSResult(JipWMSResult.ERROR,"数据格式错误");
				results.add(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for (JipWMSResult jipWMSResult : results) {
			if(jipWMSResult.getType().equals(jipWMSResult.ERROR)) {
				ES_BC_INFO.put("TYPE", jipWMSResult.ERROR);
				ES_BC_INFO.put("MSG", jipWMSResult.getMessage());
				break;
			}else {
				ES_BC_INFO.put("TYPE", jipWMSResult.SUCCESS);
				ES_BC_INFO.put("MSG", jipWMSResult.getMessage());
			}
		}
		
		JSONObject resultData = new JSONObject();
		resultData.put("ES_BC_INFO",ES_BC_INFO);
		resultData.put("ET_DATA",results);

		//保存交互记录
		saveInteractiveLog("wms->mes","方棒入库反馈接口","fbInboundFeedback",data,resultData);
		return resultData;
	}
	
	
	
	@ApiOperation(value = "方棒调拨出库")
	@PostMapping("/fbTransferOutbound")
	public JSONObject fbTransferOutbound(@RequestBody JSONObject data) {
		//每条记录处理情况返回值集合
		JSONObject ES_BC_INFO = new JSONObject();

		//解析工单数据
		try {
			JSONObject IT_DATA = data.getJSONObject("IT_DATA");
			ES_BC_INFO = data.getJSONObject("IS_BC_INFO");
			if(IT_DATA != null){
				try {
					wmsInputService.handlerfbTransferOutbound(data);
					ES_BC_INFO.put("TYPE", JipWMSResult.SUCCESS);
					ES_BC_INFO.put("MSG", "成功");
				} catch (RuntimeException e) {
					ES_BC_INFO.put("TYPE", JipWMSResult.ERROR);
					ES_BC_INFO.put("MSG", e.getMessage());
				}
			}else{
				ES_BC_INFO.put("TYPE", JipWMSResult.ERROR);
				ES_BC_INFO.put("MSG", "数据格式错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		JSONObject resultData = new JSONObject();
		resultData.put("ES_BC_INFO",ES_BC_INFO);

		//保存交互记录
		saveInteractiveLog("wms->mes","方棒调拨出库","fbTransferOutbound",data,resultData);
		return resultData;
	}
	
	
	

	// 保存原始记录到数据表
	private void saveInteractiveLog(String direction,String controllerName,String oprMethod,Object data,Object result) {
		try {
			LogWmsMesInteractive log = new LogWmsMesInteractive();
			log.setDataDirection(direction);
			log.setControllerName(controllerName);
			log.setOprMethod(oprMethod);
			log.setContext(JSONObject.toJSONString(data));
			log.setReturnContext(JSONObject.toJSONString(result));
			log.setCreateTime(new Date());
			logWmsMesInteractiveService.saveOrUpdate(log);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
