package com.youchain.businessdata.rest;

import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.repository.PointRepository;
import com.youchain.businessdata.domain.*;
import com.youchain.businessdata.inputJson.*;
import com.youchain.businessdata.repository.InventoryRepository;
import com.youchain.businessdata.service.MlsService;
import com.youchain.exception.handler.ApiResult;
import com.youchain.utils.BaseCode;
import com.youchain.utils.BaseStatus;
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.transaction.annotation.Transactional;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

import static com.youchain.utils.BaseStatus.FREE;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequiredArgsConstructor
@Api(tags = "MLS")
@RequestMapping("/api/mls/")
@Slf4j
public class MlsController {

    private final MlsService mlsService;
    private final InventoryRepository inventoryRepository;
    private final PointRepository pointRepository;

    @PostMapping("/getOrderInfo")
    @Log("获取送货单信息")
    @ApiOperation("获取送货单信息")
    @AnonymousAccess
    public ResponseEntity<Object> getOrderInfo(@RequestBody OrderInfo orderInfo) {

        try {
            String resultJson = mlsService.getOrderJson(orderInfo);//获取送货单JSON
            if (resultJson == null || resultJson.isEmpty()) {
                return ResponseEntity.badRequest().body(ApiResult.success(BAD_REQUEST.value(), "获取送货单接口异常!", ""));
            } else {
                mlsService.getOrderInfo(resultJson);//获取送货单
                return ResponseEntity.ok().body(ApiResult.success(OK.value(), "", resultJson));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResult.success(BAD_REQUEST.value(), e.getMessage(), ""));
        }

    }

    @PostMapping("/getMoInfo")
    @AnonymousAccess
    @Log("获取MO票信息")
    @ApiOperation("获取MO票信息")
    public ResponseEntity<Object> getMoInfo(@RequestBody MoInfo moInfo) {

        try {
            String resultJson = mlsService.getMoJson(moInfo);//获取Mo票JSON
            if (resultJson == null || resultJson.isEmpty()) {
                return ResponseEntity.badRequest().body(ApiResult.success(BAD_REQUEST.value(), "获取MO票接口异常!", ""));
            } else {
                mlsService.getMoInfo(resultJson);//获取Mo票
                return ResponseEntity.ok().body(ApiResult.success(OK.value(), "", resultJson));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResult.success(BAD_REQUEST.value(), e.getMessage(), ""));
        }
    }

    @PostMapping("/returnMo")
    @Log("按MO票入库")
    @ApiOperation("按MO票入库")
    @AnonymousAccess
    public ResponseEntity<Object> returnMo(@RequestBody ReturnMoInfo returnMoInfo) {
        try {
            String resultJson = mlsService.returnMo(returnMoInfo);//回传MO票
            return new ResponseEntity<>(ApiResult.success(OK.value(), "", resultJson), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), e.getMessage(), ""), HttpStatus.BAD_REQUEST);
        }
    }

    @PostMapping("/getIssueInfo")
    @AnonymousAccess
    @Log("叫料指令")
    @ApiOperation("叫料指令")
    public synchronized ResponseEntity<Object> getIssueInfo(@RequestBody IssueInfo issueInfo) {
        try {
            mlsService.getIssueInfo(issueInfo);
        } catch (Exception e) {
            return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), e.getMessage(), ""), HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(ApiResult.success(OK.value(), "", ""), HttpStatus.OK);
    }
    @PostMapping("/createTasks")
    @AnonymousAccess
    @Log("叫料指令-物料")
    @ApiOperation("叫料指令-物料")
    public synchronized ResponseEntity<Object> createTasks(@RequestBody CreateTaskInfo createTaskInfo) {
        synchronized ("ct") {
            if (createTaskInfo == null) {
                return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), "传入的 IssueInfo 对象为空", ""), HttpStatus.OK);
            }
            // 查询未占用库存
            List<Inventory> inventoryList = inventoryRepository.findByItemCode(createTaskInfo.getItemCode());//根据物料查询库存信息
            if (inventoryList.isEmpty()) {
                return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), "无库存信息!", ""), HttpStatus.OK);
            }
            Inventory inventory = inventoryList.get(0);
            Point endPoint = pointRepository.findByCode(createTaskInfo.getPositionCode());
            // 验证库位
            if (endPoint == null) {
                return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), "未查询到目标位!", ""), HttpStatus.OK);
            }
            // 验证库位状态
           /* if (!endPoint.getStatus().equals(FREE)) {
                return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), "目标位置状态错误！", ""), HttpStatus.OK);
            }*/
            // 验证库存 是否校验
            /*if (inventory.getBeLock() && BaseCode.PICK_YH_QR.equals("1")) {
                return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), "库存未验货!", ""), HttpStatus.OK);
            }*/
            if (inventory.getQueuedQty() > 0) {
                return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), "库存已被占用!", ""), HttpStatus.OK);
            }

            Task task = mlsService.getCreateTasks(endPoint, inventory, createTaskInfo.getReqCode());
            return new ResponseEntity<>(ApiResult.success(OK.value(), "", ""), HttpStatus.OK);
        }
    }
    @PostMapping("/returnIssue")
    @AnonymousAccess
    @Log("叫料结果回传")
    @ApiOperation("叫料结果回传")
    public ResponseEntity<Object> returnIssue(@RequestBody ReturnIssueInfo returnMoInfo) {
        try {
            String resultJson = mlsService.returnIssue(returnMoInfo);//叫料结果回传
            return new ResponseEntity<>(ApiResult.success(OK.value(), "", resultJson), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), e.getMessage(), ""), HttpStatus.BAD_REQUEST);
        }
    }


}
