using Microsoft.AspNetCore.Mvc; namespace Application.Web.Controllers.Pub { /// /// 山寨修炼接口 /// [Route("[controller]/[action]")] [ApiController] [Authorize] public class PractiseController : ControllerBase { private readonly IGameGoodsService goodsService; private readonly IUnitUserAttrService attrService; private readonly IUnitUserPractiseService practiseService; public PractiseController(IUnitUserPractiseService _practiseService, IGameGoodsService goodsService, IUnitUserAttrService attrService) { practiseService = _practiseService; this.goodsService = goodsService; this.attrService = attrService; } /// /// 获取山寨修炼列表 /// /// /// [HttpGet] public async Task GetUserPractise(int page) { int limit = 10; RefAsync total = 0; string userId = StateHelper.userId; var list = await practiseService.GetUserPractiseList(userId, page, limit, total); return PoAction.Ok(new { list, limit, total }); } /// /// 修炼道具 /// /// [HttpGet] public async Task MakeGoodsList() { string userId = StateHelper.userId; var list = await goodsService.GetUserGoodsData(userId, GoodsEnum.Code.Practise.ToString()); return PoAction.Ok(new { list }); } /// /// 立即修炼 /// /// /// [HttpPost] public async Task MakeGoods([FromBody] MakePractiseParms par) { var userGoods = await goodsService.GetUserGoodsInfo(par.par); if (userGoods == null) { return PoAction.Message("道具不存在!"); } var goodsInfo = await goodsService.GetGoodsInfo((int)userGoods.goodsId); if (goodsInfo == null) { return PoAction.Message("道具不存在!"); } if (string.IsNullOrEmpty(goodsInfo.content)) { return PoAction.Message("暂时无法修炼!"); } string userId = StateHelper.userId; if (userGoods.userId != userId) { return PoAction.Message("道具不存在!"); } if (userGoods.count < 1) { return PoAction.Message("道具数量不足,无法修炼!"); } if (await goodsService.UpdateUserGoods(userId, 0, (int)userGoods.goodsId, 1, "用户修炼扣除!")) { string[] content = goodsInfo.content.Split("|"); unit_user_practise practise = new unit_user_practise(); practise.practiseId = StringAssist.NewGuid; practise.userId = userId; practise.name = userGoods.goodsName; practise.exp = Convert.ToDecimal(content[0]); practise.addTime = DateTime.Now; practise.endTime = DateTime.Now.AddMinutes(Convert.ToInt32(content[1])); if (await practiseService.AddPractise(practise)) { return PoAction.Ok("修炼成功!"); } else { await goodsService.UpdateUserGoods(userId, 1, (int)userGoods.goodsId, 1, "用户修炼失败扣除返还!"); return PoAction.Message("修炼失败!"); } } else { return PoAction.Message("修炼失败!"); } } /// /// 完成修炼 /// /// /// [HttpPost] public async Task OkPractise([FromBody] MakePractiseParms par) { var parctiseInfo = await practiseService.GetUserPractiseInfo(par.par); if (parctiseInfo == null) { return PoAction.Message("修炼信息不存在!"); } string userId = StateHelper.userId; if (parctiseInfo.userId != userId) { return PoAction.Message("修炼信息不存在!"); } if (parctiseInfo.endTime > DateTime.Now) { return PoAction.Message("该修炼道具还没有到时间呢!"); } if (await practiseService.DelPractise(parctiseInfo.practiseId)) { if (await attrService.UpdateUserExp(userId, Convert.ToInt64(parctiseInfo.exp))) { return PoAction.Ok(string.Format("修炼成功,获得{0}经验!", parctiseInfo.exp)); } else { return PoAction.Message("完成失败!"); } } else { return PoAction.Message("完成失败!"); } } } }