package com.webchat.controller.client;

import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.enums.ClickEvent;
import com.webchat.common.helper.SessionHelper;
import com.webchat.config.annotation.SafeClick;
import com.webchat.config.annotation.ValidateLogin;
import com.webchat.domain.vo.request.SendRedPacketRequestVO;
import com.webchat.service.RedPacketService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 程序员七七， https://www.coderutil.com网站作者
 * @date 2024/11/9 03:27
 */
@Api(tags = "【客户端】红包收发服务接口")
@RestController
@RequestMapping("/api/red-packet")
public class RedPacketController {


    @Autowired
    private RedPacketService redPacketService;

    /**
     * 发红包
     *
     */
    @ValidateLogin
    @SafeClick(event = ClickEvent.SEND_RED_PACKET, time = 5000, message = "5s内只能发一次红包")
    @PostMapping("/send")
    public APIResponseBean<Long> sendRedPacket(@RequestBody SendRedPacketRequestVO sendRedPacketRequest) {
        // 校验红包参数
        sendRedPacketRequest.validateRequestParam();
        // 设置红包发送人为当前登录人
        String userId = SessionHelper.getCurrentUserId();
        sendRedPacketRequest.setSendUserId(userId);
        Long redPacketId = redPacketService.sendRedPacket(sendRedPacketRequest);
        return APIResponseBeanUtil.success(redPacketId);
    }


    /**
     * 拆红包
     *
     */
    @ValidateLogin
    @SafeClick(event = ClickEvent.OPEN_RED_PACKET, time = 1000, message = "1s内只能抢一次红包")
    @GetMapping("/open/{redPacketId}")
    public APIResponseBean<Long> openRedPacket(@PathVariable Long redPacketId) {
        String userId = SessionHelper.getCurrentUserId();
        String money = redPacketService.openRedPacket(redPacketId, userId);
        return APIResponseBeanUtil.success(money);
    }

}
