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; private readonly IUnitUserAccService _accService; public TradeController(IGameGoodsService goodsService, IGameEquService equService, IUnitUserWeight weightService, IUnitUserService userService, IGameChatService chatService, IUnitUserAttrService attrService, ITradeService tradeService, IUnitUserAccService accService) { _goodsService = goodsService; _equService = equService; _weightService = weightService; _userService = userService; _chatService = chatService; _attrService = attrService; _tradeService = tradeService; _accService = accService; } /// /// 寄售物品 /// /// /// /// /// [HttpGet] public async Task Trade(int type, string prop, long price, int count) { count = count < 1 ? 1 : count; string userId = StateHelper.userId; int areaId = StateHelper.areaId; 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(areaId, 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(areaId, 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("该物品不允许寄售!"); } } /// /// 购买 /// /// /// /// [HttpGet] public async Task 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(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("交易失败,请稍后尝试!"); } } /// /// 获取交易列表 /// /// /// /// /// /// [HttpGet] public async Task 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 total = 0; var data = await _tradeService.GetUserTradeData(areaId, userId, type, query, page, 10, total); return PoAction.Ok(new { data, total = total.Value }); } /// /// 获取个人交易列表 /// /// [HttpGet] public async Task GetMyTrade() { string userId = StateHelper.userId; var data = await _tradeService.GetUserTradeData(userId); return PoAction.Ok(data); } /// /// 下架交易 /// /// /// [HttpGet] public async Task 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(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("下架失败,请稍后尝试!"); } } /// /// 获取交易详情 /// /// /// [HttpGet] public async Task 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(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 }); } }