package com.youchain.businessdata.rest;

import com.youchain.businessdata.service.MesService;
import com.youchain.exception.handler.ApiResult;
import org.springframework.http.HttpStatus;
import com.youchain.RequestData.*;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

/**
 * @author 92525
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "mes接口管理")
@RequestMapping("/api/mes")
@Slf4j
public class MesController {
    private final MesService mesService;


    @PostMapping("/yclbl")
    @Log("mes-原材料备料")
    @ApiOperation("mes-原材料备料")
    @AnonymousAccess
    public ResponseEntity<Object> yclbl(@Validated @RequestBody Yclbl yclbl) {
        try {
            mesService.materialBl(yclbl);
            return successResponse("备料成功!");
        } catch (Exception e) {
            return badResponse("备料失败:" + e.getMessage());
        }
    }

    @PostMapping("/zpjlZj")
    @Log("mes-其他整件叫料")
    @ApiOperation("mes-其他整件叫料")
    @AnonymousAccess
    public ResponseEntity<Object> zpjlZj(@Validated @RequestBody ZpjlZj zpjlZj) {
        try {
            String orderNo = zpjlZj.getOrderNo();//工单号
            String itemCode = zpjlZj.getItemCode();//物料代码
            String station = zpjlZj.getStation();//需求工位
            String pointCode = zpjlZj.getPointCode();//需求点位
            mesService.itemCall(orderNo, itemCode, station, pointCode);
            return successResponse("叫料成功!");
        } catch (Exception e) {
            return badResponse("叫料失败:" + e.getMessage());
        }
    }

    @PostMapping("/zpjlCt")
    @Log("mes-成套叫料")
    @ApiOperation("mes-成套叫料")
    @AnonymousAccess
    public ResponseEntity<Object> zpjlCt(@Validated @RequestBody ZpjlCt zpjlCt) {
        try {
            String orderNo = zpjlCt.getOrderNo();//工单号
            String completeCode = zpjlCt.getCompleteCode();//成套代码
            String station = zpjlCt.getStation();//需求工位
            String pointCode = zpjlCt.getPointCode();//需求点位
            mesService.bigItemCall(orderNo, completeCode, station, pointCode);
            return successResponse("叫料成功!");
        } catch (Exception e) {
            return badResponse("叫料失败：" + e.getMessage());
        }
    }

    @PostMapping("/zpjlJj")
    @Log("mes-紧急叫料")
    @ApiOperation("mes-紧急叫料")
    @AnonymousAccess
    public ResponseEntity<Object> zpjlJj(@Validated @RequestBody ZpjlJj zpjlJj) {

        return new ResponseEntity<>(ApiResult.result(200, "成功", null), HttpStatus.OK);
    }

    @PostMapping("/transTask")
    @Log("mes-搬运任务")
    @ApiOperation("mes-搬运任务")
    @AnonymousAccess
    public ResponseEntity<Object> transTask(@Validated @RequestBody TransTask transTask) {
        try {
            mesService.performTransportTask(transTask);
            return successResponse("下发成功!");
        } catch (Exception e) {
            return badResponse("下发失败!" + e.getMessage());
        }
    }


    private ResponseEntity<Object> badResponse(String message) {
        return new ResponseEntity<>(ApiResult.fail(BAD_REQUEST.value(), message, ""), HttpStatus.BAD_REQUEST);
    }


    private ResponseEntity<Object> successResponse(String message) {
        return new ResponseEntity<>(ApiResult.fail(OK.value(), message, ""), HttpStatus.OK);
    }
}
