This commit is contained in:
Putoo
2026-07-13 19:52:55 +08:00
parent 5d375e94bd
commit b5de98a214
20 changed files with 873 additions and 64 deletions

View File

@@ -15,10 +15,11 @@ public class TradeController : ControllerBase
private readonly IGameChatService _chatService;
private readonly IUnitUserAttrService _attrService;
private readonly ITradeService _tradeService;
private readonly IUnitUserAccService _accService;
public TradeController(IGameGoodsService goodsService, IGameEquService equService, IUnitUserWeight weightService,
IUnitUserService userService, IGameChatService chatService,
IUnitUserAttrService attrService, ITradeService tradeService)
IUnitUserAttrService attrService, ITradeService tradeService, IUnitUserAccService accService)
{
_goodsService = goodsService;
_equService = equService;
@@ -27,6 +28,7 @@ public class TradeController : ControllerBase
_chatService = chatService;
_attrService = attrService;
_tradeService = tradeService;
_accService = accService;
}
/// <summary>
@@ -41,6 +43,7 @@ public class TradeController : ControllerBase
{
count = count < 1 ? 1 : count;
string userId = StateHelper.userId;
int areaId = StateHelper.areaId;
int lev = await _attrService.GetUserLev(userId);
if (lev < 60)
{
@@ -75,7 +78,7 @@ public class TradeController : ControllerBase
if (await _equService.DeductUserEqu(userId, [equInfo], "寄售装备"))
{
string data = JsonConvert.SerializeObject(equInfo);
if (await _tradeService.AddTrade(userId, nameof(GameEnum.PropCode.Equ), equInfo.unitEquName,
if (await _tradeService.AddTrade(areaId, userId, nameof(GameEnum.PropCode.Equ), equInfo.unitEquName,
equInfo.ueId, 1, price, data))
{
return PoAction.Ok(true);
@@ -123,7 +126,8 @@ public class TradeController : ControllerBase
if (await _goodsService.UpdateUserGoods(userId, 0, goodsId, count, "寄售物品"))
{
if (await _tradeService.AddTrade(userId, nameof(GameEnum.PropCode.Goods), MyGoods.goodsName, prop,
if (await _tradeService.AddTrade(areaId, userId, nameof(GameEnum.PropCode.Goods), MyGoods.goodsName,
prop,
count, price,
""))
{
@@ -145,4 +149,236 @@ public class TradeController : ControllerBase
return PoAction.Message("该物品不允许寄售!");
}
}
/// <summary>
/// 购买
/// </summary>
/// <param name="tradeId"></param>
/// <param name="count"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> Buy(string tradeId, int count)
{
count = count < 1 ? 1 : count;
string userId = StateHelper.userId;
int areaId = StateHelper.areaId;
var info = await _tradeService.GetTradeInfo(tradeId);
if (info == null)
{
return PoAction.Message("交易信息不存在!");
}
if (info.areaId != areaId)
{
return PoAction.Message("交易信息不存在!");
}
if (info.count < count)
{
return PoAction.Message("交易剩余数量不足!");
}
if (info.code == nameof(GameEnum.PropCode.Equ))
{
var myAcc = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper));
if (myAcc < info.price)
{
return PoAction.Message("您的铜贝不足!");
}
if (await _accService.UpdateUserCopper(userId, 0, (long)info.price, $"交易支付:{info.name}"))
{
if (await _tradeService.UpdateTradeCount(info.tradeId, 0, 1, true))
{
unit_user_equ equInfo = JsonConvert.DeserializeObject<unit_user_equ>(info.content);
equInfo.userId = userId;
await _equService.AddUserEqu(equInfo, "交易获得");
long GetCopper = Convert.ToInt64(info.price * 0.9M);
if (await _accService.UpdateUserCopper(info.userId, 1, GetCopper, $"交易收入:{info.name}"))
{
string msg = $"您的装备[{info.name}]成功交易,交易价格{info.price}铜贝,到账{GetCopper}铜贝!";
await _chatService.SendChat(info.userId, areaId, nameof(GameChatEnum.Code.Notice), msg);
}
return PoAction.Ok(true);
}
else
{
return PoAction.Message("交易失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("交易失败,请稍后尝试!");
}
}
else if (info.code == nameof(GameEnum.PropCode.Goods))
{
var myAcc = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper));
var needCopper = Convert.ToInt64(info.price) * count;
if (myAcc < needCopper)
{
return PoAction.Message("您的铜贝不足!");
}
if (await _accService.UpdateUserCopper(userId, 0, needCopper, $"交易支付:{info.name}×{count}"))
{
if (await _tradeService.UpdateTradeCount(info.tradeId, 0, count, info.count == count))
{
long GetCopper = Convert.ToInt64(needCopper * 0.9M);
if (await _accService.UpdateUserCopper(info.userId, 1, GetCopper, $"交易收入:{info.name}"))
{
await _goodsService.UpdateUserGoods(userId, 1, Convert.ToInt32(info.propId), count, "交易获得");
string msg = $"您成功交易物品[{info.name}]×{count},交易价格{needCopper}铜贝,到账{GetCopper}铜贝!";
await _chatService.SendChat(info.userId, areaId, nameof(GameChatEnum.Code.Notice), msg);
}
return PoAction.Ok(true);
}
else
{
return PoAction.Message("交易失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("交易失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("交易失败,请稍后尝试!");
}
}
/// <summary>
/// 获取交易列表
/// </summary>
/// <param name="no"></param>
/// <param name="type"></param>
/// <param name="query"></param>
/// <param name="page"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetTradeData(string? no, int type, string? query, int page)
{
string userId = string.Empty;
int areaId = StateHelper.areaId;
if (!string.IsNullOrEmpty(no))
{
var userInfo = await _userService.GetUserInfoByUserNo(no);
if (userInfo != null)
{
userId = userInfo.userId;
}
}
RefAsync<int> total = 0;
var data = await _tradeService.GetUserTradeData(areaId, userId, type, query, page, 10, total);
return PoAction.Ok(new { data, total = total.Value });
}
/// <summary>
/// 获取个人交易列表
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetMyTrade()
{
string userId = StateHelper.userId;
var data = await _tradeService.GetUserTradeData(userId);
return PoAction.Ok(data);
}
/// <summary>
/// 下架交易
/// </summary>
/// <param name="tradeId"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> DownTrade(string tradeId)
{
string userId = StateHelper.userId;
var info = await _tradeService.GetTradeInfo(tradeId);
if (info == null)
{
return PoAction.Message("交易信息不存在!");
}
if (info.userId != userId)
{
return PoAction.Message("交易信息不存在!");
}
if (info.code == nameof(GameEnum.PropCode.Equ))
{
if (await _tradeService.UpdateTradeCount(info.tradeId, 0, 1, true))
{
unit_user_equ equInfo = JsonConvert.DeserializeObject<unit_user_equ>(info.content);
await _equService.AddUserEqu(equInfo, "下架交易装备");
return PoAction.Ok(true);
}
else
{
return PoAction.Message("下架失败,请稍后尝试!");
}
}
else if (info.code == nameof(GameEnum.PropCode.Goods))
{
int count = Convert.ToInt32(info.count);
if (await _tradeService.UpdateTradeCount(info.tradeId, 0, count, true))
{
await _goodsService.UpdateUserGoods(userId, 1, Convert.ToInt32(info.propId), count, "下架交易物品");
return PoAction.Ok(true);
}
else
{
return PoAction.Message("下架失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("下架失败,请稍后尝试!");
}
}
/// <summary>
/// 获取交易详情
/// </summary>
/// <param name="tradeId"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetTradeInfo(string tradeId)
{
string userId = StateHelper.userId;
int areaId = StateHelper.areaId;
var info = await _tradeService.GetTradeInfo(tradeId);
if (info == null)
{
return PoAction.Message("交易信息不存在!");
}
if (info.areaId != areaId)
{
return PoAction.Message("交易信息不存在!");
}
if (info.code != nameof(GameEnum.PropCode.Equ))
{
return PoAction.Message(info.propId, 100);
}
var ueInfo = JsonConvert.DeserializeObject<unit_user_equ>(info.content);
var equInfo = await _equService.GetEquInfo((int)ueInfo.equId);
game_equ_suit suit = new game_equ_suit();
if (ueInfo.suitCode != "0" && !string.IsNullOrEmpty(ueInfo.suitCode))
{
suit = await _equService.GetEuqSuitInfo(ueInfo.suitCode);
}
UserModel user = await UserModelTool.GetUserView(ueInfo.owerId);
return PoAction.Ok(new { equ = equInfo, suit, equData = ueInfo, user });
}
}