using Microsoft.AspNetCore.Mvc;
namespace Application.Web.Controllers.Pub;
///
/// 商城接口
///
[Route("[controller]/[action]")]
[ApiController]
[Authorize]
public class MallController : ControllerBase
{
private readonly IGameMallService _mallService;
private readonly IUnitUserAccService _accService;
public MallController(IGameMallService mallService, IUnitUserAccService accService)
{
_mallService = mallService;
_accService = accService;
}
///
/// 获取商城列表
///
///
///
///
[HttpGet]
public async Task GetMall(string type, int page)
{
int area = StateHelper.areaId;
string userId = StateHelper.userId;
var accInfo = await _accService.GetUserAccInfo(userId);
RefAsync total = 0;
var data = await _mallService.GetMallData(area, type, page, 10, total);
return PoAction.Ok(new { data, total = total.Value, accInfo.gold, accInfo.cowry });
}
///
/// 购买物品
///
///
///
[HttpPost]
public async Task Buy([FromBody] MallBuyParms parms)
{
string userId = StateHelper.userId;
int area = StateHelper.areaId;
parms.count = parms.count < 1 ? 1 : parms.count;
var mallInfo = await _mallService.GetMallInfo(parms.id);
if (mallInfo == null)
{
return PoAction.Message("商品已下架!");
}
if (mallInfo.endTime < DateTime.Now)
{
return PoAction.Message("商品已下架!");
}
if (GameTool.AreaVerify(area, mallInfo.areaId) == false)
{
return PoAction.Message("商品已下架!");
}
var myAcc = await _accService.GetUserAccInfo(userId, mallInfo.payType);
long need = (long)mallInfo.price * parms.count;
if (myAcc < need)
{
return PoAction.Message($"您的{GameTool.GetCurrencyName(mallInfo.payType)}不足!");
}
if (await _accService.UpdateUserAcc(userId, 0, mallInfo.payType, need, nameof(AccEnum.Name.系统商城), "系统商店购买"))
{
if (await _mallService.AddLog(mallInfo, userId, parms.count))
{
await GameBus.UpdateBag(userId, 1, mallInfo.type, mallInfo.goodsId.ToString(), parms.count, "系统商店购买");
}
return PoAction.Ok(true);
}
else
{
return PoAction.Message("购买失败,请稍后尝试!");
}
}
}