Files
BuYuHouDuan/UserSrc/UserApi/Lottery/LotteryController.cs
T

107 lines
3.7 KiB
C#

using Common;
using CommonDomain;
using CommonInfrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UserDomain.Entity;
using UserService;
using UserService.GameConfigs;
namespace UserApi.Lottery
{
[ApiController, Route("api/[controller]")]
[Authorize]
public class LotteryController : BaseController
{
private readonly ILotteryService _lotteryService;
private readonly IActivityConfigService _activityConfigService;
private readonly IGameConfigService _gameConfigService;
public LotteryController(ILotteryService lotteryService, IActivityConfigService activityConfigService,
IGameConfigService gameConfigService)
{
_lotteryService = lotteryService;
_activityConfigService = activityConfigService;
_gameConfigService = gameConfigService;
}
/// <summary>
/// 获取奖池抽奖活动配置(含用户已抽状态)
/// </summary>
[HttpGet("Config")]
public async Task<BaseResponse<LotteryConfigResponse>> GetConfig()
{
var config = await _activityConfigService.Get();
if (config?.LotteryActivityConfig == null)
return this.ToFailResult<LotteryConfigResponse>("配置不存在");
LotteryPrizeProductHelper.Enrich(config.LotteryActivityConfig, await _gameConfigService.Get());
var drawnPrizes = await _lotteryService.GetUserDrawnPrizes(loginUser.UserId);
return this.ToResult(new LotteryConfigResponse
{
Config = config.LotteryActivityConfig,
DrawnPrizeIds = drawnPrizes ?? new List<string>()
});
}
/// <summary>
/// 抽奖 - 进行一次奖池抽奖(poolType 指定奖池类型)
/// </summary>
[HttpGet("Draw")]
public async Task<BaseResponse<LotteryDrawResponse>> Draw([FromQuery] int poolType = 0)
{
var res = await _lotteryService.Draw(loginUser.UserId, poolType);
if (!res.success)
return this.ToFailResult<LotteryDrawResponse>(res.message);
return this.ToResult(new LotteryDrawResponse
{
PrizeId = res.prize.PrizeId,
Name = res.prize.Name,
Type = res.prize.Type,
Count = res.prize.Count,
OtherId = res.prize.OtherId,
Cost = res.cost,
PoolType = poolType
});
}
/// <summary>
/// 获取用户抽奖状态
/// </summary>
[HttpGet("UserStatus")]
public async Task<BaseResponse<LotteryUserStatus>> UserStatus()
{
var status = await _lotteryService.GetUserStatus(loginUser.UserId);
return this.ToResult(status);
}
/// <summary>
/// 获取用户已抽中的奖品列表
/// </summary>
[HttpGet("DrawnPrizes")]
public async Task<BaseResponse<List<string>>> DrawnPrizes()
{
var list = await _lotteryService.GetUserDrawnPrizes(loginUser.UserId);
return this.ToResult(list);
}
}
public class LotteryDrawResponse
{
public string PrizeId { get; set; }
public string Name { get; set; }
public int Type { get; set; }
public long Count { get; set; }
public string OtherId { get; set; }
public long Cost { get; set; }
public int PoolType { get; set; }
}
public class LotteryConfigResponse
{
public LotteryActivityConfig Config { get; set; }
public List<string> DrawnPrizeIds { get; set; } = new();
}
}