using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace Application.Web.Controllers.Pub;
[Route("[controller]/[action]")]
[ApiController]
[Authorize]
public class TradeController : ControllerBase
{
private readonly IGameGoodsService _goodsService;
private readonly IGameEquService _equService;
private readonly IUnitUserWeight _weightService;
private readonly IUnitUserService _userService;
private readonly IGameChatService _chatService;
private readonly IUnitUserAttrService _attrService;
private readonly ITradeService _tradeService;
public TradeController(IGameGoodsService goodsService, IGameEquService equService, IUnitUserWeight weightService,
IUnitUserService userService, IGameChatService chatService,
IUnitUserAttrService attrService, ITradeService tradeService)
{
_goodsService = goodsService;
_equService = equService;
_weightService = weightService;
_userService = userService;
_chatService = chatService;
_attrService = attrService;
_tradeService = tradeService;
}
///
/// 寄售物品
///
///
///
///
///
[HttpGet]
public async Task Trade(int type, string prop, long price, int count)
{
count = count < 1 ? 1 : count;
string userId = StateHelper.userId;
int lev = await _attrService.GetUserLev(userId);
if (lev < 60)
{
return PoAction.Message("60级后才可以寄售物品哦!");
}
var tradeCount = await _tradeService.GetUserTradeCount(userId);
if (tradeCount >= 5)
{
return PoAction.Message("玩家最多同时寄售5件物品哦!");
}
if (type == 0) //装备
{
var equInfo = await _equService.GetUserEquInfo(prop);
if (equInfo == null)
{
return PoAction.Message("装备不存在!");
}
if (equInfo.userId != userId || equInfo.isOn == 1 || equInfo.isLock == 1 || equInfo.isDeal == 0)
{
return PoAction.Message("该装备无法寄售!");
}
long minPrice = (int)equInfo.lev * 100;
if (price < minPrice)
{
return PoAction.Message($"该装备最低寄售价格{minPrice}铜贝!");
}
if (await _equService.DeductUserEqu(userId, [equInfo], "寄售装备"))
{
string data = JsonConvert.SerializeObject(equInfo);
if (await _tradeService.AddTrade(userId, nameof(GameEnum.PropCode.Equ), equInfo.unitEquName,
equInfo.ueId, 1, price, data))
{
return PoAction.Ok(true);
}
else
{
await _equService.AddUserEqu(equInfo, "寄售失败返还!");
return PoAction.Message("寄售失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("寄售失败,请稍后尝试!");
}
}
else if (type == 1) //物品
{
if (count > 99)
{
return PoAction.Message("单次寄售数量最多为99个!");
}
int goodsId = Convert.ToInt32(prop);
var MyGoods = await _goodsService.GetUserGoodsInfo(userId, goodsId);
if (MyGoods == null)
{
return PoAction.Message("物品数量不足!");
}
if (MyGoods.count < count)
{
return PoAction.Message("物品数量不足!");
}
if (MyGoods.isDeal == 0)
{
return PoAction.Message("该物品无法寄售!");
}
long minPrice = (int)MyGoods.lev * 100;
if (price < minPrice)
{
return PoAction.Message($"该物品最低寄售价格{minPrice}铜贝!");
}
if (await _goodsService.UpdateUserGoods(userId, 0, goodsId, count, "寄售物品"))
{
if (await _tradeService.AddTrade(userId, nameof(GameEnum.PropCode.Goods), MyGoods.goodsName, prop,
count, price,
""))
{
return PoAction.Ok(true);
}
else
{
await _goodsService.UpdateUserGoods(userId, 1, goodsId, count, "寄售失败返还!");
return PoAction.Message("寄售失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("寄售失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("该物品不允许寄售!");
}
}
}