package org.springblade.modules.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.activities.entity.Prize;
import org.springblade.modules.activities.entity.PrizeRecord;
import org.springblade.modules.activities.service.IPrizeRecordService;
import org.springblade.modules.activities.service.IPrizeService;
import org.springblade.modules.activities.vo.PrizeRecordVO;
import org.springblade.modules.activities.vo.PrizeVO;
import org.springblade.modules.activities.wrapper.PrizeRecordWrapper;
import org.springblade.modules.activities.wrapper.PrizeWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * Author pangqijun
 * Date 2023/5/29
 * Description
 */
@RestController
@RequestMapping(CommonConstant.API_URL + "/prize")
@Api(value = "抽奖相关接口", tags = "13.抽奖相关接口")
public class AppPrizeController {

    @Resource
    private IPrizeService prizeService;
    @Resource
    private IPrizeRecordService prizeRecordService;

    @GetMapping("/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "奖品列表")
    public R<List<PrizeVO>> list() {
        return R.data(PrizeWrapper.build().listVO(prizeService.getPrizeList()));
    }


    @GetMapping("/draw")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "抽奖接口")
    public R<PrizeVO> draw() {
        PrizeVO draw = prizeService.draw(AuthUtil.getUserId());
        return R.data(draw);
    }

    @GetMapping("/winning-record")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "中奖记录")
    public R<List<PrizeRecordVO>> winningRecord(Boolean flag, Query query) {
        Long userId = null;
        if (flag) {
            userId = AuthUtil.getUserId();
        }
        IPage<PrizeRecordVO> pages = prizeRecordService.winningRecord(Condition.getPage(query), userId);
        return R.data(pages.getRecords());
    }

    @GetMapping("/winning-record-detail")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "中奖记录详情")
    public R<PrizeRecordVO> winningRecordDetail(Long id) {
        PrizeRecordVO prizeRecordVO = prizeRecordService.winningRecordDetail(id);
        return R.data(prizeRecordVO);
    }

    @GetMapping("/drawCount")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "获取抽奖次数")
    public R<Long> getDrawCount() {
        return R.data(prizeService.getDrawCount(AuthUtil.getUserId()));
    }
}
