package com.cim.idm.controller;

import com.alibaba.fastjson.JSONArray;
import com.cim.idm.service.Impl.MaterialUndoServiceImpl;
import com.cim.idm.service.MESToWMSService;
import com.cim.idm.service.Impl.MESToWMSServiceImpl;
import com.cim.idm.utils.AjaxResult;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api("接收MES请求接口")
@RestController
@RequestMapping("/api/wms")
@Slf4j
public class MESToWMSController {

    @Autowired
    private MESToWMSServiceImpl mesToWMSService;


    @ApiOperation(value = "物料消耗请求")
    @RequestMapping(value = "/meswms_materialconsume_request", method = RequestMethod.POST)
    public AjaxResult materialConsume_Request(@RequestBody JSONArray in) {
        log.info("Received material consume request: {}", in.toJSONString());
        try {
            return mesToWMSService.materialConsume_Request(in);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "二次确认物料出库请求")
    @RequestMapping(value = "/meswms_materialout_request", method = RequestMethod.POST)
    public AjaxResult materialOutByMES(@RequestBody JSONArray in) {
        log.info("Received material out request: {}", in.toJSONString());
        try {
            return mesToWMSService.materialOutByMES(in);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "取消物料入库请求")
    @RequestMapping(value = "/meswms_cancelship_request", method = RequestMethod.POST)
    public AjaxResult cancelShipByMES(@RequestBody JSONArray in)  {
        log.info("Received cancel ship request: {}", in.toJSONString());
        String undoId = in.getJSONObject(0).getString("undoId");
        String userId = in.getJSONObject(0).getString("userId");
//        return mesToWMSService.cancelShipByMES(in);
        try {
            return mesToWMSService.sapcprkUndo(undoId, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "物料入库请求")
    @RequestMapping(value = "/meswms_ship_request", method = RequestMethod.POST)
    public AjaxResult shipByMES(@RequestBody JSONArray in)   {
        log.info("Received ship request: {}", in.toJSONString());
        try {
            return mesToWMSService.shipByMES(in);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}