package com.youchain.businessdata.rest;

import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.appupdate.inputJson.MissionStateCallback;
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 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) {
        String resultJson = "";
        try {
            resultJson = mlsService.getOrderJson(orderInfo);//获取送货单JSON
        } catch (Exception e) {
            return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), e.getMessage(), ""), HttpStatus.BAD_REQUEST);
        }

        if (resultJson == null || resultJson.length() == 0) {
            return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), "接口异常!", ""), HttpStatus.BAD_REQUEST);
        } else {
            try {
                mlsService.getOrderInfo(resultJson);//获取送货单
                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("/getMoInfo")
    @AnonymousAccess
    @Log("获取MO票信息")
    @ApiOperation("获取MO票信息")
    public ResponseEntity<Object> getMoInfo(@RequestBody MoInfo moInfo) {
        String resultJson = "";
        try {
            resultJson = mlsService.getMoJson(moInfo);//获取Mo票JSON
        } catch (Exception e) {
            return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), e.getMessage(), ""), HttpStatus.BAD_REQUEST);
        }

        if (resultJson == null || resultJson.length() == 0) {
            return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), "接口异常!", ""), HttpStatus.BAD_REQUEST);
        } else {
            try {
                mlsService.getMoInfo(resultJson);//获取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("/returnMo")
    @Log("按MO票入库")
    @ApiOperation("按MO票入库")
    @AnonymousAccess
    public ResponseEntity<Object> returnMo(@RequestBody ReturnMoInfo returnMoInfo) {
        try {
            String resultJson = mlsService.returnMo(returnMoInfo);//回传MO票J
            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 {
            mlsService.getIssueInfo(issueInfo);
            return new ResponseEntity<>(ApiResult.success(OK.value(), "", ""), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(ApiResult.success(BAD_REQUEST.value(), e.getMessage(), ""), HttpStatus.BAD_REQUEST);
        }
    }

    @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);
        }
    }


}
