package com.cool.store.controller.webc;

import com.cool.store.context.CurrentUserHolder;
import com.cool.store.context.PartnerUserHolder;
import com.cool.store.request.order.*;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.ExportService;
import com.cool.store.service.order.MiniStoreOrderService;
import com.cool.store.vo.PartnerUserInfoVO;
import com.cool.store.vo.order.MiniStoreOrderDetailVO;
import com.cool.store.vo.order.MiniStoreOrderListVO;
import com.cool.store.vo.order.MiniStoreWishlistDetailVO;
import com.cool.store.vo.order.MiniStoreWishlistListVO;
import com.cool.store.vo.order.OrderDeviceDetailVO;
import com.cool.store.vo.order.OrderDeviceConfigItemVO;

import com.github.pagehelper.PageInfo;

import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "小程序-店型下单")
@RestController
@RequestMapping("/mini/storeOrder")
@RequiredArgsConstructor
public class MiniStoreOrderController {

    private final MiniStoreOrderService miniStoreOrderService;
    private final ExportService exportService;

    @ApiOperation("创建订单")
    @PostMapping("/create")
    public ResponseResult<Long> create(@RequestBody @Valid MiniStoreOrderCreateRequest request) {
        return ResponseResult.success(miniStoreOrderService.createOrder(request,PartnerUserHolder.getUser()));
    }

    @ApiOperation("订单详情")
    @GetMapping("/detail")
    public ResponseResult<MiniStoreOrderDetailVO> detail(@RequestParam("orderId") Long orderId) {
        return ResponseResult.success(miniStoreOrderService.getDetail(orderId));
    }

    @ApiOperation("获取当前门店草稿订单详情")
    @GetMapping("/getCurrentShopDraft")
    public ResponseResult<MiniStoreOrderDetailVO> getCurrentShopDraft(@RequestParam("shopId") Long shopId) {
        return ResponseResult.success(miniStoreOrderService.getCurrentShopDraft(shopId));
    }

    @ApiOperation("订单分页")
    @PostMapping("/queryPage")
    public ResponseResult<PageInfo<MiniStoreOrderListVO>> queryPage(@RequestBody MiniStoreOrderQueryRequest request) {
        return ResponseResult.success(miniStoreOrderService.queryPage(request));
    }

    @ApiOperation("更新订单")
    @PostMapping("/update")
    public ResponseResult<Long> update(@RequestBody @Valid MiniStoreOrderUpdateRequest request) {
        return ResponseResult.success(miniStoreOrderService.updateOrder(request,PartnerUserHolder.getUser()));
    }

//    @ApiOperation("撤回订单到草稿")
//    @PostMapping("/withdraw")
//    public ResponseResult<Boolean> withdraw(@RequestBody MiniStoreOrderRequest request) {
//        return ResponseResult.success(miniStoreOrderService.withdraw(request.getOrderId()));
//    }
//
//    @ApiOperation("作废订单")
//    @PostMapping("/cancel")
//    public ResponseResult<Boolean> cancel(@RequestBody MiniStoreOrderRequest request) {
//        return ResponseResult.success(miniStoreOrderService.cancel(request.getOrderId()));
//    }

    @ApiOperation("门店预定列表")
    @PostMapping("/queryPageByLineId")
    public ResponseResult<PageInfo<MiniStoreOrderListVO>> queryPageByLineId(@RequestBody @Valid MiniStoreOrderLineQueryRequest request) {
        return ResponseResult.success(miniStoreOrderService.queryPageByLineId(request));
    }

    @ApiOperation("支付订单")
    @PostMapping("/pay")
    public ResponseResult<Boolean> pay(@RequestBody @Valid MiniStoreOrderPayRequest request) {
        return ResponseResult.success(miniStoreOrderService.payOrder(request, PartnerUserHolder.getUser()));
    }

    @ApiOperation("获取当前门店最新的订单")
    @GetMapping("/getLatestOrderByShopId")
    public ResponseResult<MiniStoreOrderDetailVO> getLatestOrderByShopId(@RequestParam("shopId") Long shopId) {
        return ResponseResult.success(miniStoreOrderService.getLatestOrderByShopId(shopId));
    }

    @ApiOperation("新增/更新心愿单")
    @PostMapping("/wishlist/saveOrUpdate")
    public ResponseResult<Boolean> saveOrUpdateWishlist(@RequestBody @Valid MiniStoreWishlistSaveRequest request) {
        return ResponseResult.success(miniStoreOrderService.saveOrUpdateWishlist(request, PartnerUserHolder.getUser()));
    }

    @ApiOperation("删除心愿单")
    @PostMapping("/wishlist/deleted")
    public ResponseResult<Boolean> deleted(@RequestBody @Valid MiniStoreWishlistDelRequest request) {
        return ResponseResult.success(miniStoreOrderService.deleted(request, PartnerUserHolder.getUser()));
    }

    @ApiOperation("心愿单详情")
    @GetMapping("/wishlist/detail")
    public ResponseResult<MiniStoreWishlistDetailVO> wishlistDetail(@RequestParam("wishlistId") Long wishlistId) {
        return ResponseResult.success(miniStoreOrderService.wishlistDetail(wishlistId));
    }

    @ApiOperation("心愿单列表")
    @GetMapping("/wishlist/list")
    public ResponseResult<List<MiniStoreWishlistListVO>> wishlistList() {
        return ResponseResult.success(miniStoreOrderService.wishlistList( PartnerUserHolder.getUser().getLineId(),null));
    }

    @ApiOperation("心愿单绑定铺位")
    @PostMapping("/wishlist/wishlistBindPoint")
    public ResponseResult<Boolean> wishlistBindPoint(@RequestBody @Valid WishlistBindPointRequest request) {
        return ResponseResult.success(miniStoreOrderService.wishlistBindPoint(request));
    }

    @ApiOperation("查询当前门店铺位绑定的心愿单")
    @GetMapping("/wishlist/getCurrentShopBindWishlist")
    public ResponseResult<MiniStoreWishlistDetailVO> getCurrentShopBindWishlist(@RequestParam("shopId") Long shopId) {
        return ResponseResult.success(miniStoreOrderService.getCurrentShopBindWishlist(shopId));
    }

    @ApiOperation("订单 加盟商确认")
    @PostMapping("/passOrder")
    public ResponseResult<Boolean> passOrder(@RequestBody OrderAuditRecordRequest request) {
        return ResponseResult.success(miniStoreOrderService.passOrder(request, null));
    }

    @ApiOperation("查询订单设备明细")
    @GetMapping("/queryOrderDeviceDetail")
    public ResponseResult<List<OrderDeviceDetailVO>> queryOrderDeviceDetail(@RequestParam("shopId") Long shopId) {
        return ResponseResult.success(miniStoreOrderService.queryOrderDeviceDetailByShopId(shopId));
    }

}
