package com.zm.order.tool.server.controller;

import com.zm.order.tool.server.request.history.HistoryOrderRequest;
import com.zm.order.tool.server.response.OrderVO;
import com.zm.order.tool.server.service.ExceptionConstants;
import com.zm.order.tool.server.service.HistoryOrderService;
import com.zm.util.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 * @packageName: com.zm.order.tool.server.controller
 * @className: TransferController
 * @author zhuxiaofeng
 * @date 2023/4/28 15:12
 */
@Api(value = "归档数据查询接口", tags = "归档数据查询接口")
@RestController
@SuppressWarnings("all")
@RequestMapping("/qapi/order/history")
public class HistoryDataController {

    private static final Logger logger = LoggerFactory.getLogger(ThirdOrderController.class);

    @Autowired
    private HistoryOrderService orderService;


    @ApiOperation("查询冷库订单")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Result<List<OrderVO>> dataByOrderNo(@RequestBody HistoryOrderRequest request) {
        logger.info("mallTransfer start!");
        Result<List<OrderVO>> result = new Result<>();
        try {
            result = orderService.select(request);
        } catch (Exception e) {
            logger.error("HistoryDataController[dataByOrderNo]failed,request:{},cause by: {}", request, e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("查询冷库订单并插入业务库")
    @RequestMapping(value = "/select/insert", method = RequestMethod.POST)
    public Result<List<OrderVO>> dataByOrderNoAndInsert(@RequestBody HistoryOrderRequest request) {
        logger.info("dataByOrderNoAndInsert start!");
        Result<List<OrderVO>> result = new Result<>();
        try {
            result = orderService.selectAndInsert(request);
        } catch (Exception e) {
            logger.error("HistoryDataController[dataByOrderNoAndInsert]failed,request:{},cause by: {}", request, e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("批量查询冷库订单并插入业务库")
    @RequestMapping(value = "/batch-select-insert", method = RequestMethod.POST)
    public Result<List<OrderVO>> batchSelectInsert(@RequestBody HistoryOrderRequest request) {
        logger.info("batchSelectInsert start!");
        Result<List<OrderVO>> result = new Result<>();
        try {
            orderService.batchSelectAndInsert(request.getOrderNoList(), request.getSyncFlag());
        } catch (Exception e) {
            logger.error("HistoryDataController[batchSelectInsert]failed,request:{},cause by: {}", request, e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("根据商家查询未核销冷库订单并插入业务库")
    @RequestMapping(value = "/in-history-order", method = RequestMethod.POST)
    public Result<Boolean> inHistoryOrder(@RequestBody HistoryOrderRequest request) {
        logger.info("inHistoryOrder start!");
        Result<Boolean> result = new Result<>();
        try {
            if(request.getSellerId() == null){
                return Result.failureData("商家id为空");
            }
            orderService.inHistoryOrder(request.getSellerId());
        } catch (Exception e) {
            logger.error("HistoryDataController[inHistoryOrder]failed,request:{},cause by: {}", request, e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }
}
