package com.sw.mes.wms.controller;

import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.wms.dto.FbInventoryFeedbackDto;
import com.sw.mes.wms.dto.FbSpellValidationDto;
import com.sw.mes.wms.dto.OrderLockDto;
import com.sw.mes.wms.dto.SquareOrderDto;
import com.sw.mes.wms.service.WmsToMesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
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 javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Author wanghao
 * @Date: 2022/6/10
 * @Modified By:
 */
@RestController
@Api(description = "Wms调用Mes接口")
@RequestMapping("/wmsToMesController")
public class WmsToMesController {
    @Autowired
    private WmsToMesService wmsToMesService;

    @ApiOperation(value = "出入库上报")
    @PostMapping("/inventoryFeedback")
    public Result inventoryFeedback(@RequestBody FbInventoryFeedbackDto fbInventoryFeedbackDto, HttpServletRequest request) {
        try {
            return wmsToMesService.inventoryFeedback(fbInventoryFeedbackDto);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.message(400,e.getMessage(),null);
        }
    }

    @ApiOperation(value = "圆棒订单匹配接口")
    @PostMapping("/squareOrder")
    public Result squareOrder(@RequestBody SquareOrderDto squareOrderDto, HttpServletRequest request) {
        try {
            return wmsToMesService.squareOrder(squareOrderDto);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.message(400,e.getMessage(),null);
        }
    }

    @ApiOperation(value = "拼棒晶棒解锁锁定接口")
    @PostMapping("/orderLock")
    public Result orderLock(@Valid @RequestBody OrderLockDto orderLockDto, HttpServletRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            return wmsToMesService.dealLockOrder(orderLockDto);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.message(400,e.getMessage(),null);
        }
    }

    @ApiOperation(value = "验证是否可以拼棒")
    @PostMapping("/spellValidation")
    public Result spellValidation(@RequestBody @Valid @ApiParam(value = "SpellValidationDto对象") FbSpellValidationDto fbSpellValidationDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            return wmsToMesService.spellValidation(fbSpellValidationDto);
        } catch (Exception e) {
            return ResultGenerator.fail(e.getMessage());
        }
    }

}
