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

import com.bonus.canteen.core.common.page.PageDTO;
import com.bonus.canteen.core.common.utils.JavaxValidateUtils;
import com.bonus.canteen.core.order.common.business.OrderRefundBusiness;
import com.bonus.canteen.core.order.common.constants.OrderRefundBizEnum;
import com.bonus.canteen.core.order.common.dto.OrderCheckDTO;
import com.bonus.canteen.core.order.common.vo.OrderRefundResultVO;
import com.bonus.canteen.core.order.web.business.OrderWebBusiness;
import com.bonus.canteen.core.order.web.dto.*;
import com.bonus.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping({"/api/v2/web/order"})
@Api(
   value = "webOrder",
   tags = {"web端订单信息"}
)
public class OrderInfoWebController {
   @Autowired
   private OrderWebBusiness orderWebBusiness;
   @Autowired
   private OrderRefundBusiness orderRefundBusiness;

   @PostMapping({"/info"})
   public AjaxResult info(@RequestBody OrderInfoWebDTO content) {
      JavaxValidateUtils.validate(content);
      return AjaxResult.success(this.orderWebBusiness.getOrderInfo((content).getOrderId()));
   }

   @PostMapping({"/page/detail"})
   @ApiOperation("分页查询订单列表")
   public AjaxResult pageOrderDetail(@RequestBody OrderDetailWebDTO content) {
      return AjaxResult.success(this.orderWebBusiness.queryOrderInfoWebByPage(PageDTO.of(content.getCurrent(),
              content.getSize()), content));
   }

//   @PostMapping({"/page/delivery/detail"})
//   @ApiOperation(
//      value = "配送明细",
//      notes = "配送明细"
//   )
//   public LeResponse<PageVO<OrderDeliveryDetailVO>> pageOrderDeliveryDetail(@RequestBody LeRequest<OrderDeliveryDetailDTO> request) {
//      OrderDeliveryDetailDTO content = (OrderDeliveryDetailDTO)request.getContent();
//      return LeResponse.succ(this.orderWebBusiness.queryOrderDeliveryDetailByPage(content, content));
//   }

   @PostMapping({"/page/refund"})
   @ApiOperation(
      value = "退单审核列表",
      notes = "cmt-退单审核列表"
   )
   public AjaxResult pageOrderRefundList(@RequestBody OrderRefundWebDTO request) {
      PageDTO page = PageDTO.of(request.getCurrent(), request.getSize());
      return AjaxResult.success(this.orderWebBusiness.pageOrderRefundList(page, request));
   }
//
//   @PostMapping({"/page/take/sum"})
//   @RequiresAuthentication
//   public LeResponse<PageVO<OrderDetailSumGetVO>> pageOrderTakeSum(@RequestBody LeRequest<OrderDetailSumGetDTO> request) {
//      return LeResponse.succ(this.orderWebBusiness.queryOrderDetailSumGet(PageDTO.of(((OrderDetailSumGetDTO)request.getContent()).getCurrent(), ((OrderDetailSumGetDTO)request.getContent()).getSize()), (OrderDetailSumGetDTO)request.getContent()));
//   }
//
//   @PostMapping({"/page/pre-book"})
//   @RequiresAuthentication
//   public LeResponse<PageVO<OrderPreBookVO>> pagePreBook(@RequestBody LeRequest<OrderPreBookQueryDTO> request) {
//      return LeResponse.succ(this.orderWebBusiness.queryPreBookPage((PageDTO)request.getContent(), (OrderPreBookQueryDTO)request.getContent()));
//   }

   @PostMapping({"/part/refund"})
   @ApiOperation(
      value = "部分退单(web端)",
      notes = "cmt-部分退单(web端)"
   )
   public AjaxResult orderPartRefundWeb(@RequestBody OrderRefundSubmitWebDTO request) {
      OrderRefundResultVO orderRefundResultVO = this.orderRefundBusiness.orderRefund(request.convertToOrderRefundParam(), OrderRefundBizEnum.WEB_PART);
      return orderRefundResultVO.ifSuccess() ? AjaxResult.success(orderRefundResultVO) : AjaxResult.success(orderRefundResultVO.getResultMsg(), orderRefundResultVO.getResultCode());
   }

   @PostMapping({"/refund"})
   @ApiOperation(
      value = "整单退单",
      notes = "cmt-整单退单"
   )
   public AjaxResult orderRefundWeb(@RequestBody OrderRefundSubmitWebDTO request) {
      OrderRefundResultVO orderRefundResultVO = this.orderRefundBusiness.orderRefund(request.convertToOrderRefundParam(), OrderRefundBizEnum.WEB);
      return orderRefundResultVO.ifSuccess() ? AjaxResult.success(orderRefundResultVO) : AjaxResult.error(orderRefundResultVO.getResultCode(), orderRefundResultVO.getResultMsg());
   }

   @PostMapping({"/write/off"})
   @ApiOperation(
      value = "订单核销",
      notes = "cmt-订单核销"
   )
   public AjaxResult orderWriteOffWeb(@RequestParam Long orderId) {
      this.orderWebBusiness.orderWriteOffWeb(orderId);
      return AjaxResult.success();
   }

   @PostMapping({"/check"})
   @ApiOperation(
      value = "退单审核",
      notes = "cmt-退单审核"
   )
   public AjaxResult orderCheckWeb(@RequestBody OrderCheckDTO request) {
      this.orderRefundBusiness.orderCheck(request);
      return AjaxResult.success();
   }
//
//   @ApiOperation("修改订单为待取货(web端)")
//   @PostMapping({"/goods/update/status"})
//   public LeResponse<Object> goodsUpdateStatusById(@RequestBody LeRequest<OrdPartWriteOffDTO> request) {
//      this.orderWebBusiness.goodsUpdateStatusById((OrdPartWriteOffDTO)request.getContent());
//      return LeResponse.succ();
//   }

   @PostMapping({"/sync-pay-state"})
   public AjaxResult syncPayState(@RequestBody @Valid OrderSyncPayStateDTO request) {
      return AjaxResult.success(this.orderWebBusiness.orderSyncPayState(request.getOrderId()));
   }

   @PostMapping({"/sync-refund-state"})
   public AjaxResult syncRefundState(@RequestBody @Valid OrderSyncRefundStateDTO request) {
      return AjaxResult.success(this.orderWebBusiness.orderSyncRefundState(request.getOrderRefundId()));
   }

   @GetMapping({"/refund-wait-num"})
   public AjaxResult getRefundWaitNum() {
      return AjaxResult.success(this.orderWebBusiness.getRefundWaitNum());
   }
}
