package com.kexue.skills.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.kexue.skills.annotation.RequireAuth;
import com.kexue.skills.common.CommonResult;
import com.kexue.skills.entity.ContentPurchase;
import com.kexue.skills.entity.dto.ContentPurchaseDto;
import com.kexue.skills.service.ContentPurchaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ContentPurchase)表控制层
 * 内容购买控制器
 *
 * @author 王志维
 * @since 2025-02-21 23:01:48
 */
@Tag(name = "内容购买管理 Api")
@RestController
@RequestMapping("/api/contentPurchase")
public class ContentPurchaseController {
    /**
     * 服务对象
     */
    @Resource
    private ContentPurchaseService contentPurchaseService;

    /**
     * 分页查询
     *
     * @param queryDto 查询参数
     * @return 分页结果
     */
    @Operation(summary = "分页查询内容购买记录", description = "分页查询内容购买记录")
    @PostMapping("/pageList")
    @RequireAuth
    public CommonResult<PageInfo<ContentPurchase>> getPageList(@RequestBody ContentPurchaseDto queryDto) {
        return CommonResult.success(this.contentPurchaseService.getPageList(queryDto));
    }

    /**
     * 查询列表
     *
     * @param queryDto 查询参数
     * @return 列表结果
     */
    @Operation(summary = "查询内容购买记录列表", description = "查询内容购买记录列表")
    @PostMapping("/list")
    @RequireAuth
    public CommonResult<List<ContentPurchase>> getList(@RequestBody ContentPurchaseDto queryDto) {
        return CommonResult.success(this.contentPurchaseService.getList(queryDto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param purchaseId 主键
     * @return 单条数据
     */
    @Operation(summary = "通过ID查询内容购买记录", description = "通过ID查询内容购买记录")
    @PostMapping("/queryById/{purchaseId}")
    @RequireAuth
    public CommonResult<ContentPurchase> queryById(@Parameter(description = "购买记录ID") @PathVariable("purchaseId") Long purchaseId) {
        return CommonResult.success(this.contentPurchaseService.queryById(purchaseId));
    }

    /**
     * 购买内容
     *
     * @param contentId 内容ID
     * @param payType 支付方式：1.余额支付 2.积分支付
     * @return 购买结果
     */
    @Operation(summary = "购买内容", description = "购买内容")
    @PostMapping("/purchase")
    @RequireAuth
    public CommonResult<ContentPurchase> purchaseContent(
            @Parameter(description = "内容ID") @RequestParam("contentId") Long contentId,
            @Parameter(description = "支付方式：1.余额支付 2.积分支付") @RequestParam("payType") Integer payType) {
        Long userId = Long.parseLong(StpUtil.getLoginId().toString());
        return CommonResult.success(this.contentPurchaseService.purchaseContent(userId, contentId, payType));
    }

    /**
     * 检查用户是否有权限访问内容
     *
     * @param contentId 内容ID
     * @return 是否有权限
     */
    @Operation(summary = "检查内容访问权限", description = "检查内容访问权限")
    @PostMapping("/checkPermission")
    @RequireAuth
    public CommonResult<Boolean> checkAccessPermission(
            @Parameter(description = "内容ID") @RequestParam("contentId") Long contentId) {
        Long userId = Long.parseLong(StpUtil.getLoginId().toString());
        boolean hasPermission = this.contentPurchaseService.checkAccessPermission(userId, contentId);
        return CommonResult.success(hasPermission);
    }
}