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

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

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 com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.jip.JipISBCINFO;
import com.sw.mes.sto.dto.StoDto;
import com.sw.mes.sto.entity.LogStoMesInteractive;
import com.sw.mes.sto.service.LogStoMesInteractiveService;
import com.sw.mes.sto.service.StoOutputService;
import com.sw.mes.utils.StringUtils;

@Service
public class StoOutputServiceImpl implements StoOutputService {

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private JipHeaderConfigService jipHeaderConfigService;

	@Autowired
	private LogStoMesInteractiveService logStoMesInteractiveService;

	@Override
	public void sendData(List<StoDto> data) throws JsonProcessingException {
		JipHeaderConfig jipHeaderConfig = jipHeaderConfigService.getByKeyword(IFKeyWordEnums.STO.getKeyword());

		if (jipHeaderConfig == null)
			throw new RuntimeException("MES中未配置MES->S4:大基地直流STO调拨创建和冲销接口信息");

		if (StringUtils.isBlank(jipHeaderConfig.getUrl()))
			throw new RuntimeException("MES中未配置MES->S4:大基地直流STO调拨创建和冲销接口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("IS_INPUT", data);

		String dataJson = objectMapper.writeValueAsString(dataObj);

		// 2.发送工单消耗数据到SAP
		JSONObject result = null;
		try {
			result = JipRestUtils.post(jipHeaderConfig.getUrl(), JSONObject.parseObject(dataJson), token);
			JSONObject ES_BC_INFO = result.getJSONObject("ES_BC_INFO");
			if (!ES_BC_INFO.getString("TYPE").equals("S")) {
				throw new RuntimeException(ES_BC_INFO.getString("MSG"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}

		// 保存交互记录
		saveInteractiveLog("mes->sto", "大基地直流STO调拨创建和冲销接口", "sendData", dataObj, result);
	}

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

}
