using Microsoft.AspNetCore.Mvc;
namespace Application.Web.Controllers.Pub;
///
/// Cdk接口
///
[Route("[controller]/[action]")]
[ApiController]
[Authorize]
public class CdkController : ControllerBase
{
private readonly IGameCdkService _cdkService;
public CdkController(IGameCdkService cdkService)
{
_cdkService = cdkService;
}
///
/// 兑换CDK
///
///
///
[HttpGet]
public async Task Exchange(string cdk)
{
if (string.IsNullOrEmpty(cdk))
{
return PoAction.Message("CDK不能为空!");
}
var cdkItem = await _cdkService.GetCdkItemInfo(cdk);
if (cdkItem == null)
{
return PoAction.Message("CDK无效!");
}
if (cdkItem.userId != "0")
{
return PoAction.Message("CDK已被使用!");
}
var cdkInfo = await _cdkService.GetCdkInfo((int)cdkItem.cdkId);
if (cdkInfo == null)
{
return PoAction.Message("CDK无效!");
}
if (cdkInfo.endTime < DateTime.Now)
{
return PoAction.Message("CDK已过期!");
}
if (cdkInfo.area != 0)
{
if (cdkInfo.area != StateHelper.areaId)
{
return PoAction.Message($"该CDK仅可在{cdkInfo.area}区使用!");
}
}
string userId = StateHelper.userId;
if (cdkInfo.limit > 0)
{
int onCount = await _cdkService.GetUserCdkCount(cdkInfo.cdkId, userId);
if (onCount >= cdkInfo.limit)
{
return PoAction.Message($"该类型CDK限制兑换{cdkInfo.limit}个!");
}
}
if (await _cdkService.UpdateCdkUser(cdk, userId))
{
if (await GameBus.UpdateBag(userId, 1, cdkInfo.award, "CDK获得"))
{
string message = "兑换成功,获得:";
foreach (var item in cdkInfo.award)
{
message += $"{item.name}+{item.count},";
}
message = message.TrimEnd(',');
return PoAction.Ok(true,message);
}
else
{
return PoAction.Message("兑换失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("兑换失败,请稍后尝试!");
}
}
}