package com.webchat.controller.admin;

import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.helper.SessionHelper;
import com.webchat.config.annotation.ValidatePermission;
import com.webchat.domain.vo.request.lottery.LotteryActivitySaveVO;
import com.webchat.domain.vo.request.lottery.LotteryActivityStatusUpdateVO;
import com.webchat.domain.vo.response.lottery.LotteryActivityBaseVO;
import com.webchat.domain.vo.response.lottery.LotteryActivityVO;
import com.webchat.service.UserWalletService;
import com.webchat.service.lottery.LotteryActivityService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author 程序员七七
 * @webSite https://www.coderutil.com
 * @Date 2022/11/16 23:40
 * @description
 */
@Api(tags = "【幸运抽奖】抽奖活动")
@RestController
@RequestMapping("/api/lottery/activity")
public class LotteryActivityController {

    @Autowired
    private LotteryActivityService lotteryActivityService;

    @Autowired
    private UserWalletService walletService;

    /**
     * 创建或更新抽奖活动
     *
     * @return
     */
    @ValidatePermission
    @PostMapping("/save")
    public APIResponseBean<Long> save(@RequestBody LotteryActivitySaveVO lotteryActivityVO) {
        String userId = SessionHelper.getCurrentUserId();
        return APIResponseBeanUtil.success(lotteryActivityService.save(lotteryActivityVO, userId));
    }

    /**
     * 更新活动状态
     *
     * @param lotteryActivityStatusUpdate
     * @return
     */
    @ValidatePermission
    @PostMapping("/updateStatus")
    public APIResponseBean<Boolean> updateStatus(@RequestBody LotteryActivityStatusUpdateVO lotteryActivityStatusUpdate) {
        String userId = SessionHelper.getCurrentUserId();
        lotteryActivityService.updateLotteryActivityStatus(lotteryActivityStatusUpdate, userId);
        return APIResponseBeanUtil.success(true);
    }

    /**
     * 抽奖活动详情
     *
     * @param lotteryActivityId
     * @return
     */
    @GetMapping("/detail/{lotteryActivityId}")
    public APIResponseBean<LotteryActivityVO> detail(@PathVariable String lotteryActivityId) {
        LotteryActivityVO lotteryActivity = lotteryActivityService.getLotteryActivityDetailFromCache(lotteryActivityId);
        BigDecimal walletBalance = walletService.getUserWalletBalanceFromCache(SessionHelper.getCurrentUserId());
        lotteryActivity.setUserInitWalletBalance(walletBalance);
        return APIResponseBeanUtil.success(lotteryActivity);
    }

    /**
     * 最新一期活动信息
     *
     * @return
     */
    @GetMapping("/last")
    public APIResponseBean<LotteryActivityVO> last() {
        return APIResponseBeanUtil.success(lotteryActivityService.getLastLotteryActivityBaseFromCache());
    }

    /**
     * 查询最新一期活动
     *
     * @return
     */
    @GetMapping("/lastId")
    public APIResponseBean<String> lastId() {
        return APIResponseBeanUtil.success(lotteryActivityService.getLastLotteryActivityIdFromCache());
    }

    /**
     * 历史列表查询
     *
     * @param lastId
     * @param size
     * @return
     */
    @GetMapping("/history/list")
    public APIResponseBean<List<LotteryActivityVO>> queryHistory(@RequestParam(value = "lastId", required = false) Long lastId,
                                                                 @RequestParam(value = "lastId", required = false, defaultValue = "8") Integer size) {


        return APIResponseBeanUtil.success(lotteryActivityService.getHistoryActivityList(lastId, size));
    }

    /**
     * 活动列表检索
     * @param name
     * @param status
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ValidatePermission
    @GetMapping("/page")
    public APIPageResponseBean<List<LotteryActivityBaseVO>> page(@RequestParam(value = "name", required = false) String name,
                                                                 @RequestParam(value = "status", required = false) Integer status,
                                                                 @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        return lotteryActivityService.pageList(name, status, pageNo, pageSize);
    }
}
