Files
BuYuHouDuan/UserSrc/UserService/BeautifulNumber/BeautifulNumberService.cs
T
2026-10-12 16:22:04 +08:00

146 lines
4.6 KiB
C#

using System.Linq.Expressions;
using System.Text.RegularExpressions;
using AdminDomain.BeautifulNumber;
using AdminDomain.Entity;
using Common;
using Common.CommonModel;
using Common.Utils;
using CommonInfrastructure.Response;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using MongoRepository;
using RedisService;
using UserDomain.Entity;
using UserService.BeautifulNumber;
using XAct;
namespace UserService.BeautifulNumber
{
public class BeautifulNumberService : IBeautifulNumberService
{
private readonly IMongoBaseRepository<BeautifulNumberConfig> _beautifulNumberMongo;
private readonly IMongoBaseRepository<UserEntity> _userMongo;
private readonly IRedisService _cache;
public BeautifulNumberService(
IMongoBaseRepository<BeautifulNumberConfig> beautifulNumberMongo,
IMongoBaseRepository<UserEntity> userMongo,
IRedisService cache)
{
_beautifulNumberMongo = beautifulNumberMongo;
_userMongo = userMongo;
_cache = cache;
}
public async Task<PageResponse<BeautifulNumberConfig>> GetList(BeautifulNumberListParam param)
{
Expression<Func<BeautifulNumberConfig, bool>> filter = c => true;
if (!string.IsNullOrEmpty(param.level))
{
filter = filter.And(c => c.Level == param.level);
}
if (param.status.HasValue)
{
filter = filter.And(c => c.Status == (BeautifulNumberStatus)param.status.Value);
}
if (!string.IsNullOrEmpty(param.numberSearch))
{
filter = filter.And(c => c.Number.Contains(param.numberSearch));
}
var (total, list) = await _beautifulNumberMongo.QueryPageAsync(filter, c => c.CreateTime, false, param.size, param.page);
return new PageResponse<BeautifulNumberConfig>
{
Data = list,
Count = (int)total
};
}
public async Task<PageResponse<BeautifulNumberConfig>> GetAvailableList(BeautifulNumberListParam param)
{
param.status = (int)BeautifulNumberStatus.Available;
return await GetList(param);
}
public async Task<(bool isok, string msg)> Purchase(string number, LoginUser loginUser)
{
var config = await _beautifulNumberMongo.FindOneAsync(c => c.Number == number);
if (config == null)
{
return (false, "靓号不存在");
}
if (config.Status != BeautifulNumberStatus.Available)
{
return (false, "靓号不可购买");
}
var user = await _userMongo.FindByIdAsync(loginUser.UserId);
if (user == null)
{
return (false, "用户不存在");
}
if (user.Assets.Gold < config.Price)
{
return (false, "金币不足");
}
user.Assets.Gold -= config.Price;
config.Status = BeautifulNumberStatus.Sold;
config.SaleTime = DateTime.Now.ToTimeStamp();
config.BuyerUserId = loginUser.UserId;
config.UserId = loginUser.UserId;
await _userMongo.ReplaceOneAsync(user);
await _beautifulNumberMongo.ReplaceOneAsync(config);
return (true, "购买成功");
}
public async Task<BeautifulNumberConfig> CheckNumber(string number)
{
var config = await _beautifulNumberMongo.FindOneAsync(c => c.Number == number);
if (config != null)
{
return config;
}
var isUsed = _userMongo.Count(c => c.BaseInfo.Number == number) > 0;
return new BeautifulNumberConfig
{
Number = number,
RuleName = config.RuleName,
Level = config.Level,
Price = config.Price,
Status = isUsed ? BeautifulNumberStatus.Sold : BeautifulNumberStatus.Normal
};
}
public async Task<(bool isok, string msg)> BindToUser(string number, string userId)
{
var config = await _beautifulNumberMongo.FindOneAsync(c => c.Number == number);
if (config == null)
{
return (false, "靓号不存在");
}
config.UserId = userId;
await _beautifulNumberMongo.ReplaceOneAsync(config);
return (true, "绑定成功");
}
}
}