237 lines
9.8 KiB
C#
237 lines
9.8 KiB
C#
using AdminService.GameConfigs;
|
|||
|
|
using Common;
|
||
|
|
using CommonDomain;
|
||
|
|
using CommonDomain.BaseModel;
|
||
|
|
using CommonInfrastructure;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
|
||
|
|
namespace AdminApi.Lottery
|
||
|
|
{
|
||
|
|
[ApiController, Route("adminapi/[controller]")]
|
||
|
|
public class LotteryController : BaseController
|
||
|
|
{
|
||
|
|
private readonly IActivityConfigService _activityConfigService;
|
||
|
|
private readonly IGameConfigService _gameConfigService;
|
||
|
|
|
||
|
|
public LotteryController(IActivityConfigService activityConfigService, IGameConfigService gameConfigService)
|
||
|
|
{
|
||
|
|
_activityConfigService = activityConfigService;
|
||
|
|
_gameConfigService = gameConfigService;
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("list")]
|
||
|
|
public async Task<BaseResponse<List<LotteryActivityVm>>> List()
|
||
|
|
{
|
||
|
|
var config = await _activityConfigService.Get();
|
||
|
|
if (config == null)
|
||
|
|
return this.ToResult(new List<LotteryActivityVm>());
|
||
|
|
|
||
|
|
var lotto = config.LotteryActivityConfig;
|
||
|
|
if (lotto == null || string.IsNullOrWhiteSpace(lotto.ActivityName))
|
||
|
|
return this.ToResult(new List<LotteryActivityVm>());
|
||
|
|
|
||
|
|
LotteryPrizeProductHelper.Enrich(lotto, await _gameConfigService.Get());
|
||
|
|
|
||
|
|
var vm = new LotteryActivityVm
|
||
|
|
{
|
||
|
|
ActivityId = config.Id,
|
||
|
|
ActivityName = lotto.ActivityName,
|
||
|
|
IsOpen = lotto.IsOpen,
|
||
|
|
StartTime = lotto.StartTime,
|
||
|
|
EndTime = lotto.EndTime,
|
||
|
|
CostsByPool = lotto.CostsByPool,
|
||
|
|
UserMaxDraw = lotto.UserMaxDraw,
|
||
|
|
Pools = lotto.Pools
|
||
|
|
};
|
||
|
|
|
||
|
|
return this.ToResult(new List<LotteryActivityVm> { vm });
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("detail")]
|
||
|
|
public async Task<BaseResponse<LotteryActivityVm>> Detail([FromQuery] string activityId)
|
||
|
|
{
|
||
|
|
var config = await _activityConfigService.Get();
|
||
|
|
if (config == null)
|
||
|
|
return this.ToFailResult<LotteryActivityVm>("活动配置不存在");
|
||
|
|
|
||
|
|
LotteryPrizeProductHelper.Enrich(config.LotteryActivityConfig, await _gameConfigService.Get());
|
||
|
|
|
||
|
|
var vm = new LotteryActivityVm
|
||
|
|
{
|
||
|
|
ActivityId = config.Id,
|
||
|
|
ActivityName = config.LotteryActivityConfig.ActivityName,
|
||
|
|
IsOpen = config.LotteryActivityConfig.IsOpen,
|
||
|
|
StartTime = config.LotteryActivityConfig.StartTime,
|
||
|
|
EndTime = config.LotteryActivityConfig.EndTime,
|
||
|
|
CostsByPool = config.LotteryActivityConfig.CostsByPool,
|
||
|
|
UserMaxDraw = config.LotteryActivityConfig.UserMaxDraw,
|
||
|
|
Pools = config.LotteryActivityConfig.Pools
|
||
|
|
};
|
||
|
|
|
||
|
|
return this.ToResult(vm);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("save")]
|
||
|
|
public async Task<BaseResponse<string>> Save([FromBody] LotteryActivityVm param)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(param.ActivityName))
|
||
|
|
return this.ToFailResult<string>("活动名称不能为空");
|
||
|
|
|
||
|
|
var pools = param.Pools ?? new List<LotteryPoolLevel>();
|
||
|
|
if (pools.SelectMany(pool => pool.PrizeList ?? new List<LotteryPrize>())
|
||
|
|
.Any(prize => !string.IsNullOrWhiteSpace(prize.OtherId)))
|
||
|
|
{
|
||
|
|
var gameConfig = await _gameConfigService.Get();
|
||
|
|
if (gameConfig == null)
|
||
|
|
return this.ToFailResult<string>("道具配置不存在,无法补全奖项信息");
|
||
|
|
EnrichPrizeProducts(pools, gameConfig);
|
||
|
|
}
|
||
|
|
|
||
|
|
var activityConfig = await _activityConfigService.Get() ?? new ActivityConfig();
|
||
|
|
var previousLottery = activityConfig.LotteryActivityConfig;
|
||
|
|
var cycleChanged = previousLottery == null
|
||
|
|
|| previousLottery.StartTime != param.StartTime
|
||
|
|
|| previousLottery.EndTime != param.EndTime;
|
||
|
|
|
||
|
|
if (cycleChanged)
|
||
|
|
{
|
||
|
|
foreach (var prize in pools.SelectMany(pool => pool.PrizeList ?? new List<LotteryPrize>()))
|
||
|
|
prize.DrawnCount = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
var normalizedCostsByPool = new Dictionary<int, List<long>>();
|
||
|
|
var poolNames = new Dictionary<int, string> { [1] = "炮台", [2] = "战神", [3] = "翅膀" };
|
||
|
|
for (var poolType = 1; poolType <= 3; poolType++)
|
||
|
|
{
|
||
|
|
List<long>? poolCosts = null;
|
||
|
|
param.CostsByPool?.TryGetValue(poolType, out poolCosts);
|
||
|
|
var validPoolCosts = poolCosts?.Where(cost => cost > 0).ToList();
|
||
|
|
if (validPoolCosts == null || validPoolCosts.Count == 0)
|
||
|
|
return this.ToFailResult<string>($"请配置{poolNames[poolType]}奖池消耗金币");
|
||
|
|
|
||
|
|
normalizedCostsByPool[poolType] = validPoolCosts;
|
||
|
|
}
|
||
|
|
|
||
|
|
activityConfig.LotteryActivityConfig = new LotteryActivityConfig
|
||
|
|
{
|
||
|
|
ActivityName = param.ActivityName,
|
||
|
|
IsOpen = param.IsOpen,
|
||
|
|
StartTime = param.StartTime,
|
||
|
|
EndTime = param.EndTime,
|
||
|
|
CostsByPool = normalizedCostsByPool,
|
||
|
|
UserMaxDraw = param.UserMaxDraw,
|
||
|
|
Pools = pools
|
||
|
|
};
|
||
|
|
|
||
|
|
var res = await _activityConfigService.Update(activityConfig);
|
||
|
|
if (res.isok)
|
||
|
|
return this.ToResult("保存成功");
|
||
|
|
else
|
||
|
|
return this.ToFailResult<string>(res.msg);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void EnrichPrizeProducts(List<LotteryPoolLevel> pools, GameConfig gameConfig)
|
||
|
|
{
|
||
|
|
LotteryPrizeProductHelper.Enrich(new LotteryActivityConfig { Pools = pools }, gameConfig);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("toggle")]
|
||
|
|
public async Task<BaseResponse<string>> Toggle([FromBody] LotteryToggleParam param)
|
||
|
|
{
|
||
|
|
var activityConfig = await _activityConfigService.Get();
|
||
|
|
if (activityConfig == null)
|
||
|
|
return this.ToFailResult<string>("活动配置不存在");
|
||
|
|
|
||
|
|
activityConfig.LotteryActivityConfig.IsOpen = param.IsOpen;
|
||
|
|
|
||
|
|
var res = await _activityConfigService.Update(activityConfig);
|
||
|
|
if (res.isok)
|
||
|
|
return this.ToResult(param.IsOpen ? "活动已开启" : "活动已关闭");
|
||
|
|
else
|
||
|
|
return this.ToFailResult<string>(res.msg);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("initialize")]
|
||
|
|
public async Task<BaseResponse<string>> Initialize()
|
||
|
|
{
|
||
|
|
var path = Path.Combine(AppContext.BaseDirectory, "Config", "lottery-init.json");
|
||
|
|
if (!global::System.IO.File.Exists(path))
|
||
|
|
return this.ToFailResult<string>("奖池初始化模板不存在");
|
||
|
|
|
||
|
|
LotteryActivityConfig? template;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var json = await global::System.IO.File.ReadAllTextAsync(path);
|
||
|
|
template = global::System.Text.Json.JsonSerializer.Deserialize<LotteryActivityConfig>(json,
|
||
|
|
new global::System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||
|
|
}
|
||
|
|
catch (global::System.Text.Json.JsonException)
|
||
|
|
{
|
||
|
|
return this.ToFailResult<string>("奖池初始化模板格式错误");
|
||
|
|
}
|
||
|
|
|
||
|
|
var pools = template?.Pools;
|
||
|
|
var prizes = pools?.SelectMany(pool => pool.PrizeList ?? new List<LotteryPrize>()).ToList();
|
||
|
|
if (template == null || string.IsNullOrWhiteSpace(template.ActivityName)
|
||
|
|
|| template.CostsByPool == null
|
||
|
|
|| new[] { 1, 2, 3 }.Any(type => !template.CostsByPool.TryGetValue(type, out var costs) || costs == null || costs.Count == 0)
|
||
|
|
|| pools == null || pools.Count == 0 || prizes == null || prizes.Count == 0
|
||
|
|
|| new[] { 1, 2, 3 }.Any(type => !pools.Any(pool => pool.PoolType == type && pool.PrizeList?.Count > 0))
|
||
|
|
|| prizes.Any(prize => string.IsNullOrWhiteSpace(prize.PrizeId))
|
||
|
|
|| prizes.Select(prize => prize.PrizeId).Distinct().Count() != prizes.Count)
|
||
|
|
return this.ToFailResult<string>("奖池初始化模板不完整,未修改现有配置");
|
||
|
|
|
||
|
|
template.IsOpen = false;
|
||
|
|
foreach (var prize in prizes) prize.DrawnCount = 0;
|
||
|
|
var config = await _activityConfigService.Get() ?? new ActivityConfig();
|
||
|
|
config.LotteryActivityConfig = template;
|
||
|
|
var result = await _activityConfigService.Update(config);
|
||
|
|
return result.isok
|
||
|
|
? this.ToResult("奖池配置已初始化,活动保持关闭")
|
||
|
|
: this.ToFailResult<string>(result.msg);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpDelete("delete")]
|
||
|
|
public async Task<BaseResponse<string>> Delete()
|
||
|
|
{
|
||
|
|
var activityConfig = await _activityConfigService.Get();
|
||
|
|
if (activityConfig == null)
|
||
|
|
return this.ToFailResult<string>("活动配置不存在");
|
||
|
|
|
||
|
|
activityConfig.LotteryActivityConfig = new LotteryActivityConfig();
|
||
|
|
|
||
|
|
var res = await _activityConfigService.Update(activityConfig);
|
||
|
|
if (res.isok)
|
||
|
|
return this.ToResult("删除成功");
|
||
|
|
else
|
||
|
|
return this.ToFailResult<string>(res.msg);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class LotteryActivityVm
|
||
|
|
{
|
||
|
|
public string ActivityId { get; set; } = string.Empty;
|
||
|
|
|
||
|
|
public string ActivityName { get; set; } = string.Empty;
|
||
|
|
|
||
|
|
public bool IsOpen { get; set; } = false;
|
||
|
|
|
||
|
|
public long StartTime { get; set; } = 0;
|
||
|
|
|
||
|
|
public long EndTime { get; set; } = 0;
|
||
|
|
|
||
|
|
public Dictionary<int, List<long>> CostsByPool { get; set; } = new Dictionary<int, List<long>>();
|
||
|
|
|
||
|
|
public int UserMaxDraw { get; set; } = 100;
|
||
|
|
|
||
|
|
public List<LotteryPoolLevel> Pools { get; set; } = new List<LotteryPoolLevel>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public class LotteryToggleParam
|
||
|
|
{
|
||
|
|
public string ActivityId { get; set; } = string.Empty;
|
||
|
|
|
||
|
|
public bool IsOpen { get; set; }
|
||
|
|
}
|
||
|
|
}
|