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

import com.github.dozermapper.core.Mapper;
import com.google.common.base.Throwables;
import com.zm.order.tool.server.convert.BussinessOrderConverter;
import com.zm.order.tool.server.convert.BuyerOrderConverter;
import com.zm.order.tool.server.dto.OrderDetailDTO;
import com.zm.order.tool.server.request.GetOrderDetailRequest;
import com.zm.order.tool.server.request.OrderDetailRequest;
import com.zm.order.tool.server.request.OrderRepairRequest;
import com.zm.order.tool.server.response.OrderDetailVO;
import com.zm.order.tool.server.response.OrderNewVO;
import com.zm.order.tool.server.service.ExceptionConstants;
import com.zm.order.tool.server.service.OrderReadService;
import com.zm.order.tool.server.service.OrderRepairService;
import com.zm.order.tool.server.utils.DateUtils;
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.*;

import java.util.Calendar;
import java.util.Objects;

/**
 * 订单修复接口
 * @author zhuxiaofeng
 * @date 2021/8/12 10:29
 */
@Api(value = "订单修复工具", tags = "订单修复工具")
@RestController
@RequestMapping(value = "/qapi/order/repair")
public class OrderRepairController {

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

    @Autowired
    private OrderRepairService service;

    @Autowired
    private Mapper mapper;

    @Autowired
    private BussinessOrderConverter bussinessOrderConverter;

    @Autowired
    private BuyerOrderConverter converter;

    @ApiOperation("订单原始数据")
    @PostMapping(value = "/detail")
    public Result<OrderNewVO> orderDetail(@RequestBody OrderDetailRequest request) {
        Result result = new Result();
        try {
            result = service.orderDetail(request);
        } catch (Throwable e) {
            logger.error("OrderRepairController[orderDetail] failed!, cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }

    @ApiOperation("对比根据diff修改订单")
    @PostMapping(value = "/update/diff")
    public Result updateByDiff(@RequestBody OrderRepairRequest request) {
        Result result = new Result();
        try {
            result = service.updateByDiff(request);
        } catch (Throwable e) {
            logger.error("OrderRepairController[updateByDiff] failed!, cause by:{}", e);
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }


    @ApiOperation("查询订单详情")
    @RequestMapping(value = "/detail/order", method = RequestMethod.POST)
    public Result<OrderDetailVO> getOrderDetail(@RequestBody GetOrderDetailRequest request) {
        Result<OrderDetailVO> result = new Result<>();
        try {
            Result<OrderDetailDTO> getResult = service.getOrderDetail(request.getOrderNo(), request.getUserId());
            if (!getResult.isStatus()) {
                result.setException(getResult.exceptionInfo());
                return result;
            }
            OrderDetailDTO order = getResult.getData();
            OrderDetailVO vo = converter.convert(order);
            result.setData(vo);
            result.setStatus(true);
        } catch (Throwable throwable) {
            logger.error("BuyerOrderController[getOrderDetail] failed, request:{}, cause by:{}",
                    request.toString(), Throwables.getStackTraceAsString(throwable));
            result.setException(ExceptionConstants.SYSTEM_ERROR);
        }
        return result;
    }
}
