package com.sw.mes.yb.wms.controller;

import com.sw.mes.core.dto.RankResultDto;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.yb.wms.dto.*;
import com.sw.mes.yb.wms.service.RatingResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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: GaoXinJian
 * @Date: 2022-03-08 16:28
 */
@RestController
@RequestMapping("/mesToWmsController")
@Slf4j
@Api(description = "mes与wms通信接口")
public class RatingResultController {
    @Autowired
    RatingResultService ratingResultService;

    @ApiOperation(value = "评级结果下发接口")
    @PostMapping("/ratingResult")
    public Result ratingResult(@RequestBody RankResultDto rankResultDto, HttpServletRequest request) {
        ratingResultService.ratingResult(rankResultDto);
//        log.info(segmentCheckInfoDto.toString());

        return ResultGenerator.success();
    }

    @ApiOperation(value = "出库反馈")
    @PostMapping("/roundSNReverse")
    public Result roundSNReverse(@RequestBody WmsRequest<RoundSNReverseDto> roundSNReverseDto, HttpServletRequest request) {
        ratingResultService.roundSNReverse(roundSNReverseDto);
//        log.info(segmentCheckInfoDto.toString());

        return ResultGenerator.success();
    }

    @ApiOperation(value = "入出库上报")
    @PostMapping("/inventoryFeedback")
    public Result inventoryFeedback(@RequestBody ToMesInventoryFeedback toMesInventoryFeedback, HttpServletRequest request) {
//    public Result inventoryFeedback(@RequestBody WmsRequest<Body> inventoryFeedbackDto, HttpServletRequest request) {
        try {
            if (null == toMesInventoryFeedback){
                return ResultGenerator.fail("请求参数为空！");
            }
            WmsRequest<Body> inventoryFeedbackDto = new WmsRequest<>();
            Body body = new Body();
            if (null != toMesInventoryFeedback.getMoveFlag() && !toMesInventoryFeedback.getMoveFlag().isEmpty() && StringUtils.isNumeric(toMesInventoryFeedback.getMoveFlag())){
                body.setMoveFlag(Integer.parseInt(toMesInventoryFeedback.getMoveFlag()));
            }
            body.setReason(toMesInventoryFeedback.getReason());
            body.setRoundSn(toMesInventoryFeedback.getRoundSn());
            body.setStockCode(toMesInventoryFeedback.getStockCode());
            inventoryFeedbackDto.setBody(body);
            return ratingResultService.inventoryFeedback(inventoryFeedbackDto);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.message(400,e.getMessage(),null);
        }
    }

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

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

}
