package com.sw.mes.sap.controller;


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

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.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.jip.constant.JIPConstant;
import com.sw.mes.jip.utils.JipUtil;
import com.sw.mes.sap.dto.jip.JipCloseOrderVerifyResult;
import com.sw.mes.sap.dto.jip.JipISBCINFO;
import com.sw.mes.sap.dto.jip.JipOrderStateResult;
import com.sw.mes.sap.dto.jip.JipProductionOrderResult;
import com.sw.mes.sap.dto.jip.JipResultGenerator;
import com.sw.mes.sap.entity.LogSapMesInteractive;
import com.sw.mes.sap.service.LogSapMesInteractiveService;
import com.sw.mes.sap.service.SAPInputServiceService;

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


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

	@Autowired
	private LogSapMesInteractiveService logSapMesInteractiveService;

	@Autowired
	private SAPInputServiceService sAPInputServiceService;



	@ApiOperation(value = "晶科SAP生产工单下发")
	@PostMapping("/productionorder")
	public JSONObject productionorder(@RequestBody String jsonDataStr) {
		jsonDataStr = jsonDataStr.replace("\\", "\\\\");
		JSONObject data = JSON.parseObject(jsonDataStr);
		//每条记录处理情况返回值集合
		List<JipProductionOrderResult> 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 {
					sAPInputServiceService.handlerProductonOrderData(data,results);
				} catch (RuntimeException e) {
					e.printStackTrace();
					JipProductionOrderResult result = new JipProductionOrderResult("",JipProductionOrderResult.ERROR,e.getMessage());
					results.add(result);
				}
			}else{
				JipProductionOrderResult result = new JipProductionOrderResult("",JipProductionOrderResult.ERROR,"数据格式错误");
				results.add(result);
			}
		} catch (Exception e) {
		}

		ES_BC_INFO.put("TYPE","S");
		ES_BC_INFO.put("MSG","SUCCESS");

		JSONObject resultValue = new JSONObject();
		resultValue.put("ES_BC_INFO",ES_BC_INFO);
		resultValue.put("IT_RESULT",results);

		//保存交互记录
		saveInteractiveLog("sap->mes","晶科SAP生产工单下发","productionorder",data,resultValue);
		return resultValue;
	}

	@ApiOperation(value = "晶科SAP工单关单状态回传MES")
	@PostMapping("/orderstate")
	public Map<String, Object> orderstate(@RequestBody Object data) throws IllegalAccessException, InstantiationException {
//		Gson gson = new Gson();
//		JipOrderStateResult result = new JipOrderStateResult();
//		Map<String,Object> resultObj = null;
//		JipISBCINFO jipISBCINFO = null;
//		try {
//			Map<String,Object> mapData = JipUtil.getMap(data, JIPConstant.IT_DATA);
//			if(mapData != null){
//				sAPInputServiceService.handlerOrderState(data,result);
//			}else{
//				result = new JipOrderStateResult(JipOrderStateResult.ERROR,"数据格式错误");
//			}
//
//			Map<String,Object> headMap = JipUtil.getMap(data,"IS_BC_INFO");
//			jipISBCINFO = JipUtil.convert(JipISBCINFO.class,headMap);
//		} catch (IllegalAccessException e) {
//			result = new JipOrderStateResult(JipOrderStateResult.ERROR,"数据格式错误");
//		}
//		resultObj = JipResultGenerator.generatorOrderStateResult(result,jipISBCINFO);
		
		
		Gson gson = new Gson();
		JipCloseOrderVerifyResult result = new JipCloseOrderVerifyResult();
		Map<String,Object> resultObj = null;
		JipISBCINFO jipISBCINFO = null;
		try {
			Map<String,Object> mapData = JipUtil.getMap(data, JIPConstant.IT_DATA);
			if(mapData != null){
				sAPInputServiceService.handlerVerifyWorkOrderState(data,result);
			}else{
				result = new JipCloseOrderVerifyResult(false,"数据格式错误");
			}

			Map<String,Object> headMap = JipUtil.getMap(data,"IS_BC_INFO");
			jipISBCINFO = JipUtil.convert(JipISBCINFO.class,headMap);
		} catch (IllegalAccessException e) {
			result = new JipCloseOrderVerifyResult(false,"数据格式错误");
		}
		resultObj = JipResultGenerator.generatorVerifyWorkOrderStateResult(result,jipISBCINFO);

		saveInteractiveLog("sap->mes","晶科SAP工单关单状态回传MES","orderstate",data,resultObj);
		return resultObj;
	}
	
	@ApiOperation(value = "工单状态查询技术方案(关单咨询)")
	@PostMapping("/verifyWorkOrderState")
	public Map<String, Object> verifyWorkOrderState(@RequestBody Object data) throws IllegalAccessException, InstantiationException {
		Gson gson = new Gson();
		JipCloseOrderVerifyResult result = new JipCloseOrderVerifyResult();
		Map<String,Object> resultObj = null;
		JipISBCINFO jipISBCINFO = null;
		try {
			Map<String,Object> mapData = JipUtil.getMap(data, JIPConstant.IT_DATA);
			if(mapData != null){
				sAPInputServiceService.handlerVerifyWorkOrderState(data,result);
			}else{
				result = new JipCloseOrderVerifyResult(false,"数据格式错误");
			}

			Map<String,Object> headMap = JipUtil.getMap(data,"IS_BC_INFO");
			jipISBCINFO = JipUtil.convert(JipISBCINFO.class,headMap);
		} catch (IllegalAccessException e) {
			result = new JipCloseOrderVerifyResult(false,"数据格式错误");
		}
		resultObj = JipResultGenerator.generatorVerifyWorkOrderStateResult(result,jipISBCINFO);

		saveInteractiveLog("sap->mes","晶科SAP工单关单状态回传MES","verifyWorkOrderState",data,resultObj);
		return resultObj;
	}

	// 保存原始记录到数据表
	private 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();
		}
	}
//VERIFY_WORK_ORDER_STATE
	//http://172.21.3.28:9007/MES_PC_SERVER/sap/jinko/orderstate
}
