package com.youchain.businessdata.rest;

import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.businessdata.inputJson.*;
import com.youchain.businessdata.service.MlsService;
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.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.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

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;

    @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  ResponseEntity<Object> getIssueInfo(@RequestBody IssueInfo issueInfo) {
        /*try {
        } catch (Exception e) {
            return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), e.getMessage(), ""), HttpStatus.BAD_REQUEST);
        }*/
        mlsService.getIssueInfo(issueInfo);
        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);
        }
    }


}
