package com.bonus.canteen.core.order.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bonus.canteen.core.common.export.api.ExportApi;
import com.bonus.canteen.core.order.common.constants.OrderTypeEnum;
import com.bonus.canteen.core.order.common.dto.OrderPreBookQueryDTO;
import com.bonus.canteen.core.order.common.vo.OrderPreBookVO;
import com.bonus.canteen.core.order.custom.OrderCustomWebBusiness;
import com.bonus.canteen.core.order.deprecated.dto.OrderDetailSumGetDTO;
import com.bonus.canteen.core.order.deprecated.vo.OrderDetailSumGetVO;
import com.bonus.canteen.core.order.web.business.OrderWebBusiness;
import com.bonus.canteen.core.order.web.dto.OrderDeliveryDetailDTO;
import com.bonus.canteen.core.order.web.dto.OrderDetailWebDTO;
import com.bonus.canteen.core.order.web.dto.OrderRefundWebDTO;
import com.bonus.canteen.core.order.web.vo.OrderDeliveryDetailVO;
import com.bonus.canteen.core.order.web.vo.OrderListWebVO;
import com.bonus.canteen.core.order.web.vo.OrderRefundWebVO;
import com.bonus.canteen.core.order.web.vo.export.OrderRoomListExportVO;
import com.bonus.canteen.core.order.web.vo.export.OrderSupermarketDeliveryDetailExportVO;
import com.bonus.canteen.core.order.web.vo.export.OrderSupermarketDetailExportVO;
import com.bonus.canteen.core.order.web.vo.export.OrderSupermarketRefundExportVO;
import com.bonus.common.core.web.domain.AjaxResult;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api/v2/web/order/export"})
@Api(
   value = "webOrderExport",
   tags = {"web端订单相关信息导出"}
)
public class OrderInfoExportWebController {
   private static final Logger log = LoggerFactory.getLogger(OrderInfoExportWebController.class);
   @Autowired
   protected OrderWebBusiness orderWebBusiness;
   @Autowired
   protected OrderCustomWebBusiness orderCustomWebBusiness;
   @Autowired
   protected ExportApi exportApi;

   @PostMapping({"/detail"})
   @ApiOperation(
      value = "订单明细导出",
      notes = "cmt-订单明细导出"
   )
   public Object exportOrderDetail(@RequestBody OrderDetailWebDTO orderDetailDTO) {
      if (!this.orderCustomWebBusiness.willExportOrderInfoWebByPageV2(orderDetailDTO)) {
         return AjaxResult.success();
      } else {
         if (orderDetailDTO.getOrderTypeList().stream().allMatch(OrderTypeEnum::isShopCategory)) {
            this.exportApi.startExcelExportTaskByPage("订单明细", "详情", OrderSupermarketDetailExportVO.class,
                    orderDetailDTO.getExportCols(), orderDetailDTO, null, () -> {
               return this.orderWebBusiness.queryOrderInfoWebByPage(orderDetailDTO, orderDetailDTO);
            }, (s) -> {
               return OrderSupermarketDetailExportVO.of(s, s.getOrderType());
            });
         } else if (orderDetailDTO.getOrderTypeList().stream().allMatch(OrderTypeEnum::isRoomCategory)) {
            this.exportApi.startExcelExportTaskByPage("订单明细", "详情", OrderRoomListExportVO.class,
                    orderDetailDTO.getExportCols(), orderDetailDTO, null, () -> {
               return this.orderWebBusiness.queryOrderInfoWebByPage(orderDetailDTO, orderDetailDTO);
            }, OrderRoomListExportVO::of);
         } else {
            this.exportApi.startExcelExportTaskByPage("订单明细", "详情", OrderListWebVO.class,
                    orderDetailDTO.getExportCols(), orderDetailDTO, null, () -> {
               return this.orderWebBusiness.queryOrderInfoWebByPage(orderDetailDTO, orderDetailDTO);
            });
         }

         return AjaxResult.success();
      }
   }

   @PostMapping({"/delivery/detail"})
   @ApiOperation(
      value = "配送明细",
      notes = "配送明细"
   )
   public Object exportOrderDeliveryDetail(@RequestBody OrderDeliveryDetailDTO content) {
      if (!this.orderCustomWebBusiness.willExportOrderDeliveryDetailByPageV2(content)) {
         return AjaxResult.success();
      } else {
         if (content.getOrderTypeList().stream().allMatch(OrderTypeEnum::isShopCategory)) {
            this.exportApi.startExcelExportTaskByPage("配送明细", "详情",
                    OrderSupermarketDeliveryDetailExportVO.class, content.getExportCols(), content, null, () -> {
               return this.orderWebBusiness.queryOrderDeliveryDetailByPage(content, content);
            }, (s) -> {
               return BeanUtil.copyProperties(s, OrderSupermarketDeliveryDetailExportVO.class);
            });
         } else {
            this.exportApi.startExcelExportTaskByPage("配送明细", "详情", OrderDeliveryDetailVO.class,
                    content.getExportCols(), content, null, () -> {
               return this.orderWebBusiness.queryOrderDeliveryDetailByPage(content, content);
            });
         }

         return AjaxResult.success();
      }
   }

   @PostMapping({"/take/sum"})
//   @RequiresAuthentication
   public Object exportOrderDetailSumGet(@RequestBody OrderDetailSumGetDTO content) {
      if (!this.orderCustomWebBusiness.willExportOrderDetailSumGetV2(content)) {
         return AjaxResult.success();
      } else {
         this.exportApi.startExcelExportTaskByPage("菜品领取", "详情", OrderDetailSumGetVO.class,
                 content.getExportCols(), content, null, () -> {
            return this.orderWebBusiness.queryOrderDetailSumGet(content, content);
         });
         return AjaxResult.success();
      }
   }

   @PostMapping({"/refund"})
   @ApiOperation(
      value = "退款审核导出",
      notes = "cmt-退款审核导出"
   )
   public Object pageOrderRefundList(@RequestBody OrderRefundWebDTO content) {
      if (!this.orderCustomWebBusiness.willExportRefundPageV2(content)) {
         return AjaxResult.success();
      } else {
         if (content.getOrderTypeList().stream().allMatch(OrderTypeEnum::isShopCategory)) {
            this.exportApi.startExcelExportTaskByPage("退款审核列表", "详情",
                    OrderSupermarketRefundExportVO.class, content.getExportCols(), content, null, () -> {
               return this.orderWebBusiness.pageOrderRefundList(content, content);
            }, (s) -> {
               return OrderSupermarketRefundExportVO.of(s, s.getOrderType());
            });
         } else {
            this.exportApi.startExcelExportTaskByPage("退款审核列表", "详情",
                    OrderRefundWebVO.class, content.getExportCols(), content, null, () -> {
               return this.orderWebBusiness.pageOrderRefundList(content, content);
            });
         }

         return AjaxResult.success();
      }
   }

   @PostMapping({"/page/pre-book"})
//   @RequiresAuthentication
   public Object exportPreBook(@RequestBody OrderPreBookQueryDTO content) {
      this.exportApi.startExcelExportTaskByPage("预报申请列表", "详情", OrderPreBookVO.class,
              content.getExportCols(), content, null, () -> {
         return this.orderWebBusiness.queryPreBookPage(content, content);
      });
      return AjaxResult.success();
   }
}
