package com.sunda.spmsweb.ibmmqcontroller;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.service.IEsbRfcService;
import com.sunda.spmsweb.ibmmqservice.SendMessageInterface;
import com.sunda.spmswms.service.ISapPackageListService;
import com.sunda.spmswms.service.IWhsDumpService;
import com.sunda.spmswms.service.IWhsReturnRequestService;
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.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @program: spms
 * @description: ESB Controller
 * @author: hzc
 * @create: 2022-12-28 09:40:52
 **/
@Slf4j
@RestController
@RequestMapping("/esb")
@Api(tags = "ESB相关接口", description = "ESB相关接口")
public class esbcontroller {


    @Autowired
    IEsbRfcService esbRfcService;

    @Autowired
    IWhsReturnRequestService iWhsReturnRequestService;
    
    @Autowired
    ISapPackageListService iSapPackageListService;
    
    @Autowired
    IWhsDumpService iWhsDumpService;
    
    @Autowired
    SendMessageInterface sendMessageInterface;


    @RequestMapping("/reserveInventory")
    @ApiOperation(value = "EAM预留库存信息接口", notes = "SPMS增加RFC接口供ESB调用\n" +
            "1、新增预留表，记录预留信息（物料+数量+单位+储位+预留号+删除标识）\n" +
            "2、根据传入的物料编码+数量+单位+储位，查询是否有足够库存进行预留，如果没有，则返回E消息，如果有，则进行后续操作\n" +
            "3、根据传入数据写入预留表，储位表扣减对应库存数量\n" +
            "4、如果预留取消，根据取消标识和请求字段，查询预留表对应条目，打上删除标志，储位表增加库存；\n" +
            "示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult reserveInventory(@RequestBody JSONObject doc) {
        try {
            return esbRfcService.reserveInventory(doc);
        } catch (Exception e) {
            log.error("EAM预留库存信息接口请求异常:" + e.getMessage());
            return ResponseResult.error("请求异常请重试:" + e.getMessage());
        }
    }

    @RequestMapping("/eamRequisitionlist")
    @ApiOperation(value = "EAM领料单接口", notes = "SPMS增加RFC接口供ESB调用\n" +
            "1、接口执行后，生成SPMS领料单，状态为审批通过\n" +
            "2、领用单表头新增字段，“预留号”\n" +
            "3、如果领用单表头预留号字段非空，生成出库单查询库存时，通过预留表查询库存，进行可用性的判断；\n" +
            "示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult eamRequisitionlist(@RequestBody JSONObject doc) {
        try {
            return esbRfcService.eamRequisitionlist(doc);
        } catch (Exception e) {
            log.error("EAM领料单接口请求异常:" + e.getMessage());
            return ResponseResult.error("请求异常请重试:" + e.getMessage());
        }
    }

    @RequestMapping("/eamReturnSkus")
    @ApiOperation(value = "EAM退库单接口", notes = "SPMS增加RFC接口供ESB调用\n" +
            "1、接口执行后，生成SPMS退库单，状态为审批通过\n" +
            "示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult eamReturnSkus(@RequestBody JSONObject doc) {
        try {
            return esbRfcService.eamReturnSkus(doc);
        } catch (Exception e) {
            log.error("EAM退库单接口请求异常:" + e.getMessage());
            return ResponseResult.error("请求异常请重试:" + e.getMessage());
        }
    }

    @RequestMapping("/zSpmsSrmReturnTo")
    @ApiOperation(value = "SRM回调退货订单状态", notes = "SPMS增加RFC接口供ESB调用\n" +
            "1、接口执行后，修改SPMS国内仓退货单状态\n" +
            "示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult zSpmsSrmReturnTo(@RequestBody JSONObject doc) {
        try {
            return iWhsReturnRequestService.zSpmsSrmReturnTo(doc);
        } catch (Exception e) {
            log.error("EAM退库单接口请求异常:" + e.getMessage());
            return ResponseResult.error("请求异常请重试:" + e.getMessage());
        }
    }
    
    @RequestMapping("/sapPackageListToSpms")
    @ApiOperation(value = "SAP装柜安排信息推送SPMS接口", notes = "SPMS增加RFC接口供ESB调用\n" +
            "1、SAP装柜安排信息推送SPMS接口\n" +
            "示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult sapPackageListToSpms(@RequestBody JSONObject doc) {
        try {
            return iSapPackageListService.zSapPackageListToSpms(doc);
        } catch (Exception e) {
            log.error("SAP装柜安排信息推送SPMS接口请求异常:" + e.getMessage());
            return ResponseResult.error("SAP装柜安排信息推送SPMS接口请求异常:" + e.getMessage()).add("STATUS", "E");
        }
    }
    
    @RequestMapping("/sapCabinetNumberToSpms")
    @ApiOperation(value = "SAP柜号信息推送SPMS接口", notes = "SPMS增加RFC接口供ESB调用\n" +
    		"1、SAP柜号信息推送SPMS接口\n" +
    		"示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult sapCabinetNumberToSpms(@RequestBody JSONObject doc) {
    	try {
    		return iSapPackageListService.zsapCabinetNumberToSpms(doc);
    	} catch (Exception e) {
    		log.error("SAP柜号信息推送SPMS接口请求异常:" + e.getMessage());
    		return ResponseResult.error("SAP柜号信息推送SPMS接口请求异常:" + e.getMessage()).add("STATUS", "E");
    	}
    }
    
    @RequestMapping("/sapStoOrPoToSpms")
    @ApiOperation(value = "SAP将订单和交货单回传SPMS接口", notes = "SPMS增加RFC接口供ESB调用\n" +
    		"1、SAP将订单和交货单回传SPMS接口\n" +
    		"示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult sapStoOrPoToSpms(@RequestBody JSONObject doc) {
    	try {
    		return iWhsDumpService.zSapStoOrPoToSpms(doc);
    	} catch (Exception e) {
    		log.error("SAP将订单和交货单回传SPMS接口请求异常:" + e.getMessage());
    		return ResponseResult.error("请求异常请重试:" + e.getMessage()).add("STATUS", "E");
    	}
    }

    @RequestMapping("/eamFormerSkus")
    @ApiOperation(value = "EAM旧货单接口", notes = "SPMS增加RFC接口供ESB调用\n" +
            "1、接口执行后，生成SPMS旧件单，状态为审批通过\n" +
            "示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult eamFormerSkus(@RequestBody JSONObject doc) {
        try {
            //Z_EAM_FORMER_TO_SPMS
            return esbRfcService.eamFormerSkus(doc);
        } catch (Exception e) {
            log.error("EAM旧件单接口请求异常:" + e.getMessage());
            return ResponseResult.error("请求异常请重试:" + e.getMessage());
        }
    }
    
    @RequestMapping("/eamGetStorageInventory")
    @ApiOperation(value = "EAM获取储位库存数量接口", notes = "SPMS增加RFC接口供ESB调用\n" +
            "EAM获取储位库存数量接口\n" +
            "示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult eamGetStorageInventory(@RequestBody JSONObject doc) {
        try {
            // Z_SPMS_TOTAL_STORAGE_INVENTORY_TO_EAM
            return esbRfcService.eamGetAllStorageInventory(doc);
        } catch (Exception e) {
            log.error("EAM获取储位库存数量接口请求异常:" + e.getMessage());
            return ResponseResult.error("EAM获取储位库存数量接口请求异常:" + e.getMessage());
        }
    }
    
    @RequestMapping("/oaReturnOverseaWhsLoss")
    @ApiOperation(value = "OA返回海外存货损益单审批结果", notes = "SPMS增加RFC接口供ESB调用\n" +
    		"OA返回海外存货损益单审批结果\n" +
    		"示例参数 doc:[{},{}]\n" , httpMethod = "POST")
    public ResponseResult oaReturnOverseaWhsLoss(@RequestBody JSONObject doc) {
    	try {
    		// Z_OA_RETURN_WHSLOSS_RESULT_SPMS
    		return esbRfcService.oaReturnOverseaWhsLoss(doc);
    	} catch (Exception e) {
    		log.error("海外存货损益OA返回审批结果接口请求异常:" + e.getMessage());
    		return ResponseResult.error("海外存货损益OA返回审批结果接口请求异常:" + e.getMessage());
    	}
    }



}
