package com.youchain.appupdate.rest;

import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.appupdate.ReturnJson.ReturnTaskVo;
import com.youchain.appupdate.inputJson.*;
import com.youchain.basicdata.service.StockService;
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.web.bind.annotation.*;

import java.util.List;


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

    private final StockService stockService;

    @PostMapping("/materialIn")
    @Log("原材料入库")
    @ApiOperation("原材料入库")
    @AnonymousAccess
    public ResponseEntity<Object> materialIn(@RequestBody BindStock bindStock) {
        try {
            //P:A17A;V:148795;M:10441666-00/PCS;B:240512;Lot:20240427;S:124051200181132;PO:5913490946/00020;Q:36/20/32;D:2024-04-27;SN:;YX:2025-04-26;DN:D12405120005031AS
            String stockCode = bindStock.getStockCode();//容器编号
            List<String> boxNumbers = bindStock.getBoxNumbers();//箱号集合
            String pointCode = bindStock.getPointCode();//点位编号
            stockService.materialIn(stockCode, boxNumbers, pointCode);
            return successResponse("入库成功!");
        } catch (Exception e) {
            return badRequest("入库失败:" + e.getMessage());
        }

    }

    @PostMapping("/scanStock")
    @Log("扫描原托盘")
    @ApiOperation("扫描原托盘")
    @AnonymousAccess
    public ResponseEntity<Object> scanStock(@RequestBody FbPick fbPick) {
        try {
            String srcStockCode = fbPick.getSrcStockCode();//原托盘
            ReturnTaskVo taskVo = stockService.scanStock(srcStockCode);
            return successResponse(taskVo);
        } catch (Exception e) {
            return badRequest("扫描失败:" + e.getMessage());
        }
    }

    @PostMapping("/fbPicking")
    @Log("翻包拣货")
    @ApiOperation("翻包拣货")
    @AnonymousAccess
    public ResponseEntity<Object> fbPicking(@RequestBody FbPick fbPick) {
        try {
            Long taskId = fbPick.getTaskId();//任务id
            String orderNumber = fbPick.getDstOrderNumber();//箱号
            String dstStockCode = fbPick.getDstStockCode();//目标托盘
            stockService.fbPicking(taskId, orderNumber, dstStockCode);
            return successResponse("拣货成功!");
        } catch (Exception e) {
            return badRequest("拣货失败:" + e.getMessage());
        }
    }

    @PostMapping("/unBindContainer")
    @Log("容器解绑")
    @ApiOperation("容器解绑")
    @AnonymousAccess
    public ResponseEntity<Object> unBindContainer(@RequestBody BindStock bindStock) {
        try {
            String stockCode = bindStock.getStockCode();//容器编号
            stockService.unBindContainer(stockCode);
            return successResponse("解绑成功!");
        } catch (Exception e) {
            return badRequest("解绑失败:" + e.getMessage());
        }
    }


    @PostMapping("/containerIn")
    @Log("容器入场")
    @ApiOperation("容器入场")
    @AnonymousAccess
    public ResponseEntity<Object> containerIn(@RequestBody ContainerIn containerIn) {
        try {
            String containerCode = containerIn.getContainerCode();//容器号
            String position = containerIn.getPosition();//点位
            stockService.containerIn(containerCode, position);
        } catch (Exception e) {
            return badRequest(e.getMessage());
        }
        return successResponse("入场成功!");
    }


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

    @PostMapping("/operationFeedback")
    @Log("任务放行")
    @ApiOperation("任务放行")
    @AnonymousAccess
    public ResponseEntity<Object> operationFeedback(@RequestBody ContainerIn containerIn) {
        try {
            String containerCode = containerIn.getContainerCode();
            stockService.operationFeedback(containerCode);
            return successResponse("放行成功!");
        } catch (Exception e) {
            return badRequest(e.getMessage());
        }
    }


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

    private ResponseEntity<Object> successResponse(Object data) {
        return new ResponseEntity<>(data, HttpStatus.OK);
    }
}
