using Microsoft.AspNetCore.Mvc;
namespace Application.Web.Controllers.Pub
{
///
/// 船队接口
///
[Route("[controller]/[action]")]
[ApiController]
[Authorize]
public class ShipController : ControllerBase
{
private readonly IUnitUserWeight weightService;
private readonly IGameGoodsService goodsService;
private readonly IUnitUserAccService accService;
public ShipController(IUnitUserWeight _weightService, IGameGoodsService _goodsService, IUnitUserAccService _accService)
{
accService = _accService;
goodsService = _goodsService;
weightService = _weightService;
}
///
/// 获取用户船只
///
///
[HttpGet]
public async Task GetUserShip()
{
string userId = StateHelper.userId;
var data = await weightService.GetUserShip(userId);
//个人负重
var weightInfo = await weightService.GetUserWeightInfo(userId);
return PoAction.Ok(new { data, weightInfo });
}
///
/// 获取船只详情
///
///
///
[HttpGet]
public async Task GetShipInfo(int shipId)
{
var data = await goodsService.GetShipInfo(shipId);
if (data == null)
{
return PoAction.Message("船只详情不存在!");
}
return PoAction.Ok(new { data });
}
///
/// 出售船只
///
///
///
[HttpPost]
public async Task BuyShip([FromBody] BuyShipParms parms)
{
var userShip = await weightService.GetUserShipInfo(parms.usId);
if (userShip == null)
{
return PoAction.Message("船只不存在!");
}
string userId = StateHelper.userId;
if (userShip.userId != userId)
{
return PoAction.Message("船只不存在!");
}
var shipInfo = await goodsService.GetShipInfo((int)userShip.goodsId);
if (shipInfo == null)
{
return PoAction.Message("船只信息不存在!");
}
if (await weightService.DeleteUserShip(userShip.usId, userId))
{
if (shipInfo.payType == AccEnum.AccType.copper.ToString())
{
if (await accService.UpdateUserCopper(userId, 1, Convert.ToInt64(shipInfo.salePrice), "出售船只获得!"))
{
return PoAction.Ok(new { price = shipInfo.salePrice });
}
else
{
return PoAction.Message("出售失败!");
}
}
else if (shipInfo.payType == AccEnum.AccType.gold.ToString())
{
if (await accService.UpdateUserAcc(userId, 1, AccEnum.AccType.gold.ToString(), Convert.ToInt64(shipInfo.salePrice)))
{
return PoAction.Ok(new { price = shipInfo.salePrice });
}
else
{
return PoAction.Message("出售失败!");
}
}
else
{
return PoAction.Message("出售失败!");
}
}
else
{
return PoAction.Message("船只出售失败!");
}
}
}
}