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.GoblinFrontSelectGoodVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbCouponVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSqbOrderDetailVo;
import com.liquidnet.service.goblin.service.GoblinFrontService;
import com.liquidnet.service.goblin.service.IGoblinSqbService;
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.NotBlank;
import java.util.List;

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

    @Autowired
    private IGoblinSqbService goblinSqbService;
    @Autowired
    private GoblinFrontService goblinFrontService;


    /**
     * 获取核销二维码（券码）
     */
    @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 goblinSqbService.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 goblinSqbService.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 goblinSqbService.syncCouponStatus(userId, orderId);
    }

    /**
     * 用户收钱吧订单列表
     */
    @GetMapping("/list")
    @ApiOperation("用户收钱吧订单列表")
    public ResponseDto<List<GoblinSqbOrderDetailVo>> getOrderList() {
        String userId = CurrentUtil.getCurrentUid();
        log.info("[收钱吧订单列表] 接收请求 userId={}", userId);
        return goblinSqbService.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 goblinSqbService.getOrderDetail(userId, orderId);
    }

    @GetMapping("/performance/selectGoods")
    @ApiOperation("按演出ID获得已关联商品列表(pageNumber从0开始)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", required = true, dataType = "String", name = "performancesId", value = "演出ID"),
            @ApiImplicitParam(paramType = "query", required = true, dataType = "int", name = "pageSize", value = "分页大小"),
            @ApiImplicitParam(paramType = "query", required = true, dataType = "int", name = "pageNumber", value = "页码(从0开始)")
    })
    public ResponseDto<GoblinFrontSelectGoodVo> getPerformanceSelectGoods(
            @RequestParam(name = "performancesId", required = true) String performancesId,
            @RequestParam(name = "pageSize", required = true) int pageSize,
            @RequestParam(name = "pageNumber", required = true) int pageNumber) {
        return ResponseDto.success(goblinFrontService.getPerformanceSelectGoods(performancesId, pageNumber, pageSize));
    }


}
