package com.liquidnet.service.goblin.controller;

import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbCouponVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbOrderCreateVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbOrderDetailVo;
import com.liquidnet.service.goblin.service.IGoblinSqbOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 收钱吧订单 Controller
 */
@Slf4j
@Api(tags = "收钱吧订单")
@RestController
@RequestMapping("/goblin/sqb/order")
public class GoblinSqbOrderController {

    @Autowired
    private IGoblinSqbOrderService goblinSqbOrderService;

    /**
     * 创建收钱吧订单
     */
    @PostMapping("/create")
    @ApiOperation("创建收钱吧订单")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String",  name = "spuId",          value = "商品 SPU ID"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String",  name = "skuId",          value = "商品 SKU ID"),
            @ApiImplicitParam(type = "form", required = true, dataType = "Integer", name = "quantity",       value = "购买数量", example = "1"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String",  name = "performancesId", value = "关联演出 ID"),
    })
    public ResponseDto<GoblinSqbOrderCreateVo> createOrder(
            @NotBlank(message = "spuId 不能为空")          @RequestParam("spuId")          String spuId,
            @NotBlank(message = "skuId 不能为空")          @RequestParam("skuId")          String skuId,
            @NotNull(message = "quantity 不能为空")
            @Min(value = 1, message = "购买数量至少为 1")   @RequestParam("quantity")       Integer quantity,
            @NotBlank(message = "performancesId 不能为空") @RequestParam("performancesId") String performancesId) {

        String userId = CurrentUtil.getCurrentUid();
        log.info("[收钱吧下单] 接收请求 userId={}, spuId={}, skuId={}, quantity={}, performancesId={}",
                userId, spuId, skuId, quantity, performancesId);

        return goblinSqbOrderService.createOrder(userId, spuId, skuId, quantity, performancesId);
    }

    /**
     * 查询支付状态
     */
    @GetMapping("/pay/status")
    @ApiOperation("查询支付状态")
    @ApiImplicitParam(type = "query", required = true, dataType = "String", name = "orderId", value = "本地订单ID")
    public ResponseDto<Integer> queryPayStatus(
            @NotBlank(message = "orderId 不能为空") @RequestParam("orderId") String orderId) {

        String userId = CurrentUtil.getCurrentUid();
        log.info("[收钱吧支付状态] 接收请求 userId={}, orderId={}", userId, orderId);

        return goblinSqbOrderService.queryPayStatus(userId, orderId);
    }

    /**
     * 获取核销二维码（券码）
     */
    @GetMapping("/coupon/{orderId}")
    @ApiOperation("获取核销二维码")
    @ApiImplicitParam(type = "path", required = true, dataType = "String", name = "orderId", value = "本地订单ID")
    public ResponseDto<GoblinSqbCouponVo> queryCoupon(
            @NotBlank(message = "orderId 不能为空") @PathVariable("orderId") String orderId) {

        String userId = CurrentUtil.getCurrentUid();
        log.info("[收钱吧券码] 接收请求 userId={}, orderId={}", userId, orderId);

        return goblinSqbOrderService.queryCoupon(userId, orderId);
    }

    /**
     * 申请退款
     */
    @PostMapping("/refund")
    @ApiOperation("申请退款")
    @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "本地订单ID")
    public ResponseDto<Boolean> refund(
            @NotBlank(message = "orderId 不能为空") @RequestParam("orderId") String orderId) {

        String userId = CurrentUtil.getCurrentUid();
        log.info("[收钱吧退款] 接收请求 userId={}, orderId={}", userId, orderId);

        return goblinSqbOrderService.refund(userId, orderId);
    }

    /**
     * 主动同步核销状态
     */
    @PostMapping("/coupon/sync")
    @ApiOperation("主动同步核销状态")
    @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "本地订单ID")
    public ResponseDto<Boolean> syncCouponStatus(
            @NotBlank(message = "orderId 不能为空") @RequestParam("orderId") String orderId) {
        String userId = CurrentUtil.getCurrentUid();
        log.info("[收钱吧核销同步] 接收请求 userId={}, orderId={}", userId, orderId);
        return goblinSqbOrderService.syncCouponStatus(userId, orderId);
    }

    /**
     * 用户收钱吧订单列表
     */
    @GetMapping("/list")
    @ApiOperation("用户收钱吧订单列表")
    public ResponseDto<java.util.List<GoblinSqbOrderDetailVo>> getOrderList() {
        String userId = CurrentUtil.getCurrentUid();
        log.info("[收钱吧订单列表] 接收请求 userId={}", userId);
        return goblinSqbOrderService.getOrderList(userId);
    }

    /**
     * 订单详情
     */
    @GetMapping("/detail/{orderId}")
    @ApiOperation("收钱吧订单详情")
    @ApiImplicitParam(type = "path", required = true, dataType = "String", name = "orderId", value = "本地订单ID")
    public ResponseDto<GoblinSqbOrderDetailVo> getOrderDetail(
            @NotBlank(message = "orderId 不能为空") @PathVariable("orderId") String orderId) {
        String userId = CurrentUtil.getCurrentUid();
        log.info("[收钱吧订单详情] 接收请求 userId={}, orderId={}", userId, orderId);
        return goblinSqbOrderService.getOrderDetail(userId, orderId);
    }

    /**
     * 再次付款（待支付状态下重新拉起微信支付）
     */
    @PostMapping("/repay")
    @ApiOperation("再次付款")
    @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "本地订单ID")
    public ResponseDto<GoblinSqbOrderCreateVo> repay(
            @NotBlank(message = "orderId 不能为空") @RequestParam("orderId") String orderId) {
        String userId = CurrentUtil.getCurrentUid();
        log.info("[收钱吧再次付款] 接收请求 userId={}, orderId={}", userId, orderId);
        return goblinSqbOrderService.repay(userId, orderId);
    }
}
