package com.sw.mes.sap.controller;


import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.jip.constant.JIPConstant;
import com.sw.mes.jip.utils.JipUtil;
import com.sw.mes.sap.dto.LogSapMesInteractiveDto;
import com.sw.mes.sap.dto.MblnrDataParam;
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.entity.WarehouseLocation;
import com.sw.mes.sap.service.SAPInputServiceService;
import com.sw.mes.sap.service.SAPOutputServiceService;
import com.sw.mes.sap.service.WarehouseLocationService;
import com.sw.mes.sap.service.LogSapMesInteractiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


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

	@Autowired
	private SAPOutputServiceService sAPOutputServiceService;
	@Autowired
	private WarehouseLocationService warehouseLocationService;

	@ApiOperation(value = "测试-发送工单消耗数据至SAP")
	@GetMapping("/sendWorkOrderConsumptionToSap")
	public Result sendWorkOrderConsumptionToSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.sendWorkOrderConsumptionToSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}
	
	
	@ApiOperation(value = "同步SAP订单状态")
	@GetMapping("/synOrderState")
	public Result synOrderState() throws JsonProcessingException {
		try {
			sAPOutputServiceService.synOrderState();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}
	

	@ApiOperation(value = "测试-发送工单收货数据至SAP")
	@GetMapping("/sendWorkOrderReceiptToSap")
	public Result sendWorkOrderReceiptToSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.sendWorkOrderReceiptToSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "测试-发送物料损耗数据至SAP")
	@GetMapping("/sendMaterialLossToSap")
	public Result sendMaterialLossToSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.sendWorkOrderReceiptToSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "测试-发送调拨同步数据至SAP")
	@GetMapping("/sendTransferSynchronizationToSap")
	public Result sendTransferSynchronizationToSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.sendTransferSynchronizationToSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "测试-发送成本中心消耗数据至SAP")
	@GetMapping("/sendCostCenterConsumptionToSap")
	public Result sendCostCenterConsumptionToSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.sendCostCenterConsumptionToSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "测试-发送生产订单报工时数据至SAP")
	@GetMapping("/sendReportWorkingHoursToSap")
	public Result sendReportWorkingHoursToSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.sendReportWorkingHoursToSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "测试-拉取同步SAP的工厂信息")
	@GetMapping("/getFactoryFromSap")
	public Result getFactoryFromSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.getFactoryFromSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "测试-拉取同步SAP的物料组信息")
	@GetMapping("/getMaterialGroupDataFromSap")
	public Result getMaterialGroupDataFromSap() throws JsonProcessingException {

		try {
			sAPOutputServiceService.getMaterialGroupDataFromSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "测试-拉取同步SAP的物料信息")
	@GetMapping("/getMaterialDataFromSap")
	public Result getMaterialDataFromSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.getMaterialDataFromSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}
	
	@ApiOperation(value = "测试-物料凭证查询接口")
	@PostMapping("/getMblnrDataFromSap")
	public Result getMblnrDataFromSap(@RequestBody MblnrDataParam param) throws JsonProcessingException {
		try {
			sAPOutputServiceService.getMblnrDataFromSap(param);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}


	@ApiOperation(value = "测试-拉取物料批次，成品收货批次（工单收货传入）信息")
	@GetMapping("/getMaterialBatchFromSap")
	public Result getMaterialBatchFromSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.getMaterialBatchFromSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "测试-晶科SAP库存地点同步MES")
	@GetMapping("/getWarehouseLocationFromSap")
	public Result getWarehouseLocationFromSap() throws JsonProcessingException {
		try {
			sAPOutputServiceService.getWarehouseLocationFromSap();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		return ResultGenerator.success();
	}

	@ApiOperation(value = "获取SAP库存")
	@GetMapping("/getSapStock")
	public Result getTest() throws JsonProcessingException {
		//http://172.16.202.34:8000/JIP/S4_MMQM/S4_MMQM_ZMM_OA_O_GET_STOCK
		try {
			List<WarehouseLocation> warehouseLocations = warehouseLocationService.findAllPWarehouse();
            for (WarehouseLocation warehouseLocation : warehouseLocations) {
	    		//同步sapStock
	    		sAPOutputServiceService.getSapStock(warehouseLocation.getLgort(),null);
            }
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.fail(e.getMessage());
		}
		
		
		return ResultGenerator.success();
	}


	@ApiOperation(value = "测试-空请求")
	@GetMapping("/sendWorkWMSBackToSap")
	public Result sendWorkWMSBackToSap() throws JsonProcessingException {
		sAPOutputServiceService.sendWorkWMSBackToSap();
		return ResultGenerator.success();
	}

}