package com.youchain.appupdate.rest;

import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.appupdate.inputJson.CallStock;
import com.youchain.appupdate.inputJson.ContainerIn;
import com.youchain.appupdate.inputJson.BindStock;
import com.youchain.basicdata.service.StockService;
import com.youchain.exception.handler.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequiredArgsConstructor
@Api(tags = "app管理")
@RequestMapping("/api/app")
@Slf4j
public class BydAppController {

    private final StockService stockService;

    @PostMapping("/containerIn")
    @Log("容器入场")
    @ApiOperation("容器入场")
    @PreAuthorize("@el.check('app:containerIn')")
    public ResponseEntity<Object> containerIn(@RequestBody ContainerIn containerIn) {
        try {
            String containerCode = containerIn.getContainerCode();//容器号
            String position = containerIn.getPosition();//点位
            String itemCode = containerIn.getItemCode();//物料
            stockService.containerIn(containerCode, position, itemCode);
        } catch (Exception e) {
            return badRequest(e.getMessage());
        }
        return successRequest("入场成功!");
    }


    @PostMapping("/containerOut")
    @Log("容器出场")
    @ApiOperation("容器出场")
    @PreAuthorize("@el.check('app:containerOut')")
    public ResponseEntity<Object> containerOut(@RequestBody ContainerIn containerIn) {
        try {
            String containerCode = containerIn.getContainerCode();
            stockService.containerOut(containerCode);
        } catch (Exception e) {
            return badRequest(e.getMessage());
        }
        return successRequest("出场成功!");
    }

    @PostMapping("/bindStock")
    @Log("原材料入库")
    @ApiOperation("原材料入库")
    @AnonymousAccess
    public ResponseEntity<Object> bindStock(@RequestBody BindStock bindStock) {
        try {
            //二维码格式：P:A17A;V:148795;M:DP_001/PCS;B:240512JFET;Lot:20240427;S:124051200181131;PO:5913490946/00020;Q:36/20/32;D:2024-04-27;SN:;YX:2025-04-26;DN:D12405120005031AS
            String QRCode = bindStock.getItemCode();//Mo票二维码
            String stockCode = bindStock.getStockCode();//容器编号
            String pointCode = bindStock.getPointCode();//点位编号
            stockService.bindContainer(QRCode, stockCode, pointCode);
        } catch (Exception e) {
            return badRequest(e.getMessage());
        }
        return successRequest("入库成功!");
    }

    @PostMapping("/callStock")
    @Log("呼叫容器")
    @ApiOperation("呼叫容器")
    @AnonymousAccess
    public ResponseEntity<Object> callStock(@RequestBody CallStock callStock) {
        String pointCode = callStock.getPointCode();//目标点位
        String itemCode = callStock.getItemCode();//物料编号
        int scene = callStock.getScene();//业务场景
        try {
            stockService.callContainer(itemCode, pointCode, scene);
        } catch (Exception e) {
            return badRequest(e.getMessage());
        }
        return successRequest("呼叫成功!");
    }

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

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