package com.bonus.core.order.mobile.controller;

import com.bonus.common.core.domain.R;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.constant.LeConstants;
import com.bonus.core.common.page.PageVO;
import com.bonus.core.common.utils.HeaderFetchUtil;
import com.bonus.core.common.utils.JavaxValidateUtils;
import com.bonus.core.order.common.business.OrderRefundBusiness;
import com.bonus.core.order.common.constants.OrderRefundBizEnum;
import com.bonus.core.order.common.dto.OrderRefundDTO;
import com.bonus.core.order.common.dto.RequestHeaderDTO;
import com.bonus.core.order.common.vo.OrderRefundResultVO;
import com.bonus.core.order.mobile.dto.*;
import com.bonus.core.order.mobile.service.OrderInfoMobileBusiness;
import com.bonus.core.order.mobile.service.ShoppingCartBusiness;
import com.bonus.core.order.mobile.vo.OrderInfoMobileVO;
import com.bonus.core.order.mobile.vo.OrderListMobileVO;
import com.bonus.core.order.mobile.vo.OrderShoppingCartAddResultVO;
import com.bonus.core.order.mobile.vo.OrderShoppingCartListMobileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/api/v2/mobile/order"})
@Api(
   value = "mobileOrder",
   tags = {"移动端订单查询"}
)
public class OrderInfoMobileController extends BaseController {
   @Autowired
   private OrderInfoMobileBusiness orderInfoMobileBusiness;
   @Autowired
   private OrderRefundBusiness orderRefundBusiness;
   @Autowired
   private ShoppingCartBusiness shoppingCartBusiness;
////   @Autowired
////   private ReportOrderApi reportOrderApi;
//
   @PostMapping({"/list"})
   @ApiOperation(
      value = "订单列表查询",
      notes = "cmt-订单列表查询"
   )
   public PageVO<OrderListMobileVO> orderList(@RequestHeader Map<String, String> headers, @RequestBody OrderListMobileDTO request) {
      RequestHeaderDTO headerDTO = RequestHeaderDTO.of(headers);
      PageVO<OrderListMobileVO> orderPage = this.orderInfoMobileBusiness.orderList(request, headerDTO);
      return orderPage;
   }
//
//   @PostMapping({"/group"})
//   public LeResponse<PageVO<OrderGroupMobileVO>> orderGroup(@RequestHeader Map<String, String> headers, @RequestBody LeRequest<OrderGroupMobileQueryDTO> request) {
//      RequestHeaderDTO headerDTO = RequestHeaderDTO.of(headers);
//      PageVO<OrderGroupMobileVO> orderPage = this.orderInfoMobileBusiness.orderGroup((OrderGroupMobileQueryDTO)request.getContent(), headerDTO);
//      return LeResponse.succ(orderPage);
//   }
//
//   @PostMapping({"/refund-list"})
//   public LeResponse<PageVO<OrderRefundListMobileVO>> orderRefundList(@RequestHeader Map<String, String> headers, @RequestBody LeRequest<OrderRefundListMobileQueryDTO> request) {
//      RequestHeaderDTO headerDTO = RequestHeaderDTO.of(headers);
//      PageVO<OrderRefundListMobileVO> orderPage = this.orderInfoMobileBusiness.orderRefundList((OrderRefundListMobileQueryDTO)request.getContent(), headerDTO);
//      return LeResponse.succ(orderPage);
//   }

   @PostMapping({"/info"})
   @ApiOperation(
      value = "获取订单详情",
      notes = "cmt-获取订单详情"
   )
   public R<OrderInfoMobileVO> orderInfo(@RequestBody OrderIdMobileDTO request) {
      JavaxValidateUtils.validate((OrderIdMobileDTO)request);
      OrderInfoMobileVO orderInfo = this.orderInfoMobileBusiness.orderInfo(request.getOrderId());
      return R.ok(orderInfo);
   }
//
//   @PostMapping({"/sync-pay-state"})
//   public LeResponse<UnifyPaySelectVO> orderSyncPayState(@RequestBody LeRequest<OrderSyncPayStateMobileDTO> request) {
//      JavaxValidateUtils.validate((OrderSyncPayStateMobileDTO)request.getContent());
//      return LeResponse.succ(this.orderInfoMobileBusiness.orderSyncPayState((OrderSyncPayStateMobileDTO)request.getContent()));
//   }
//
//   @PostMapping({"/weight-info"})
//   @ApiOperation("获取自助餐订单详情")
//   public LeResponse<OrderInfoMobileVO> weightOrderInfo(@RequestBody LeRequest<OrderInfoWeightMobileDTO> request) {
//      JavaxValidateUtils.validate((OrderInfoWeightMobileDTO)request.getContent());
//      OrderInfoMobileVO orderInfo = this.orderInfoMobileBusiness.weightOrderInfo((OrderInfoWeightMobileDTO)request.getContent());
//      return LeResponse.succ(orderInfo);
//   }
//
//   @PostMapping({"/pay/cancel"})
//   @ApiOperation(
//      value = "取消支付",
//      notes = "cmt-取消支付"
//   )
//   public LeResponse<Object> orderPayCancel(@RequestBody @Valid LeRequest<OrderIdMobileDTO> request) {
//      this.orderInfoMobileBusiness.orderPayCancel(((OrderIdMobileDTO)request.getContent()).getOrderId());
//      return LeResponse.succ();
//   }
//
//   @PostMapping({"/part/refund"})
//   @ApiOperation(
//      value = "部分退单",
//      notes = "cmt-部分退单"
//   )
//   public LeResponse<OrderRefundResultVO> orderPartRefundMobile(@RequestBody LeRequest<OrderPartRefundDTO> request) {
//      ((OrderPartRefundDTO)request.getContent()).getDetailRefundList().forEach((detailRefundDTO) -> {
//         detailRefundDTO.setRestoreQuantity(0);
//      });
//      OrderRefundResultVO resultVO = this.orderRefundBusiness.orderRefund(((OrderPartRefundDTO)request.getContent()).convertToOrderRefundParam(), OrderRefundBizEnum.MOBILE_PART);
//      return resultVO.ifSuccess() ? LeResponse.succ(resultVO) : LeResponse.fail(resultVO.getResultCode(), resultVO.getResultMsg());
//   }

   @PostMapping({"/refund"})
   @ApiOperation(
      value = "整单退单",
      notes = "cmt-整单退单"
   )
   public AjaxResult orderRefundMobile(@RequestBody OrderRefundDTO request) {
      request.setRestoreInventory(LeConstants.COMMON_NO);
      OrderRefundResultVO resultVO = this.orderRefundBusiness.orderRefund(request.convertToOrderRefundParam(), OrderRefundBizEnum.MOBILE);
      return resultVO.ifSuccess() ? AjaxResult.success(resultVO) : AjaxResult.error(resultVO.getResultCode(), resultVO.getResultMsg());
   }
//
//   @PostMapping({"/book/stat"})
//   public LeResponse<OrderBookStatMobileVO> orderBookStat(@RequestHeader Map<String, String> headers, @RequestBody LeRequest<?> request) {
//      OrderBookStatMobileVO stat = this.reportOrderApi.getBookStat(HeaderFetchUtil.getCustId(headers));
//      return LeResponse.succ(stat);
//   }
//
//   @PostMapping({"/book/refund-stat"})
//   public LeResponse<OrderBookRefundStatMobileVO> orderBookRefundStat(@RequestHeader Map<String, String> headers, @RequestBody LeRequest<?> request) {
//      OrderBookRefundStatMobileVO stat = this.reportOrderApi.getBookRefundStat(HeaderFetchUtil.getCustId(headers));
//      return LeResponse.succ(stat);
//   }
//
//   @PostMapping({"/pre-book/exists"})
//   public LeResponse<Integer> orderPreBookExists(@RequestHeader Map<String, String> headers, @RequestBody LeRequest<OrderPreBookQueryDTO> request) {
//      boolean preBookExists = this.orderInfoMobileBusiness.getPreBookExists((OrderPreBookQueryDTO)request.getContent());
//      return LeResponse.succ(preBookExists ? LeConstants.COMMON_YES : LeConstants.COMMON_NO);
//   }
//
   @PostMapping({"/shopping-cart/add"})
   public R<OrderShoppingCartAddResultVO> addShoppingCart(@RequestHeader Map<String, String> headers, @RequestBody OrderShoppingCartAddDTO request) {
      OrderShoppingCartAddResultVO result = this.shoppingCartBusiness.add(request);
      return R.ok(result);
   }
//
   @PostMapping({"/shopping-cart/update"})
   public R<Object> updateShoppingCart(@RequestHeader Map<String, String> headers, @RequestBody OrderShoppingCartUpdateDTO request) {
      this.shoppingCartBusiness.update(request);
      return R.ok();
   }

   @PostMapping({"/shopping-cart/list-cust"})
   @ApiOperation(
           value = "获取订单详情"
   )
   public R<List<OrderShoppingCartListMobileVO>> listCustShoppingCart(@RequestHeader Map<String, String> headers, @RequestBody OrderShoppingSearchDTO request) {
      RequestHeaderDTO headerDTO = RequestHeaderDTO.of(headers);
      return R.ok(this.shoppingCartBusiness.listByCust(request));
   }

   @PostMapping({"/shopping-cart/clear"})
   public AjaxResult removeShoppingCart(@RequestHeader Map<String, String> headers, @RequestBody OrderShoppingCartClearDTO request) {
      request.setCustId(HeaderFetchUtil.getCustId(headers));
      this.shoppingCartBusiness.clear(request);
      return AjaxResult.success();
   }
}
