492 lines
16 KiB
C#
492 lines
16 KiB
C#
using System.Linq.Expressions;
|
|
using AdminDomain.BeautifulNumber;
|
|
using AdminDomain.Entity;
|
|
using Common;
|
|
using Common.Utils;
|
|
using CommonInfrastructure.Response;
|
|
using Microsoft.Extensions.Logging;
|
|
using MongoDB.Driver;
|
|
using MongoRepository;
|
|
using RedisService;
|
|
using UserDomain.Entity;
|
|
using XAct;
|
|
|
|
namespace AdminService.BeautifulNumber
|
|
{
|
|
public class BeautifulNumberService : IBeautifulNumberService
|
|
{
|
|
private readonly IMongoBaseRepository<BeautifulNumberConfig> _beautifulNumberMongo;
|
|
private readonly IMongoBaseRepository<BeautifulNumberRule> _ruleMongo;
|
|
private readonly IMongoBaseRepository<UserEntity> _userMongo;
|
|
private readonly IRedisService _cache;
|
|
|
|
public BeautifulNumberService(
|
|
IMongoBaseRepository<BeautifulNumberConfig> beautifulNumberMongo,
|
|
IMongoBaseRepository<BeautifulNumberRule> ruleMongo,
|
|
IMongoBaseRepository<UserEntity> userMongo,
|
|
IRedisService cache)
|
|
{
|
|
_beautifulNumberMongo = beautifulNumberMongo;
|
|
_ruleMongo = ruleMongo;
|
|
_userMongo = userMongo;
|
|
_cache = cache;
|
|
}
|
|
|
|
public async Task<(List<BeautifulNumberConfig> list, long count)> 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 (list, total);
|
|
}
|
|
|
|
public async Task<(List<BeautifulNumberConfig> list, long count)> 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)> Delete(string id)
|
|
{
|
|
var config = await _beautifulNumberMongo.FindByIdAsync(id);
|
|
if (config == null)
|
|
{
|
|
return (false, "靓号不存在");
|
|
}
|
|
|
|
if (config.Status == BeautifulNumberStatus.Sold)
|
|
{
|
|
return (false, "靓号已售出,无法删除");
|
|
}
|
|
|
|
await _beautifulNumberMongo.DeleteByIdAsync(id);
|
|
return (true, "删除成功");
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> SetToNormal(string id)
|
|
{
|
|
var config = await _beautifulNumberMongo.FindByIdAsync(id);
|
|
if (config == null)
|
|
{
|
|
return (false, "靓号不存在");
|
|
}
|
|
|
|
config.Status = BeautifulNumberStatus.Normal;
|
|
await _beautifulNumberMongo.ReplaceOneAsync(config);
|
|
return (true, "设置成功");
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> Freeze(string id)
|
|
{
|
|
var config = await _beautifulNumberMongo.FindByIdAsync(id);
|
|
if (config == null)
|
|
{
|
|
return (false, "靓号不存在");
|
|
}
|
|
|
|
config.Status = BeautifulNumberStatus.Frozen;
|
|
await _beautifulNumberMongo.ReplaceOneAsync(config);
|
|
return (true, "冻结成功");
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> Unfreeze(string id)
|
|
{
|
|
var config = await _beautifulNumberMongo.FindByIdAsync(id);
|
|
if (config == null)
|
|
{
|
|
return (false, "靓号不存在");
|
|
}
|
|
|
|
config.Status = BeautifulNumberStatus.Available;
|
|
await _beautifulNumberMongo.ReplaceOneAsync(config);
|
|
return (true, "解冻成功");
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> UpdatePrice(string id, long price)
|
|
{
|
|
var config = await _beautifulNumberMongo.FindByIdAsync(id);
|
|
if (config == null)
|
|
{
|
|
return (false, "靓号不存在");
|
|
}
|
|
|
|
config.Price = price;
|
|
await _beautifulNumberMongo.ReplaceOneAsync(config);
|
|
return (true, "价格修改成功");
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> UpdateNumber(string id, string level, long price)
|
|
{
|
|
var config = await _beautifulNumberMongo.FindByIdAsync(id);
|
|
if (config == null)
|
|
{
|
|
return (false, "靓号不存在");
|
|
}
|
|
|
|
config.Level = level;
|
|
config.Price = price;
|
|
await _beautifulNumberMongo.ReplaceOneAsync(config);
|
|
return (true, "修改成功");
|
|
}
|
|
|
|
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, "绑定成功");
|
|
}
|
|
|
|
public async Task<List<BeautifulNumberRule>> GetRules()
|
|
{
|
|
var filter = Builders<BeautifulNumberRule>.Filter.Empty;
|
|
var sort = Builders<BeautifulNumberRule>.Sort.Ascending(c => c.Sort);
|
|
var (_, list) = await _ruleMongo.QueryPageAsync(filter, sort, true, 100, 1);
|
|
return list;
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> AddRule(BeautifulNumberRule rule)
|
|
{
|
|
try
|
|
{
|
|
await _ruleMongo.InsertOneAsync(rule);
|
|
return (true, "添加成功");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (false, "添加失败: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> UpdateRule(BeautifulNumberRule rule)
|
|
{
|
|
try
|
|
{
|
|
var existing = await _ruleMongo.FindByIdAsync(rule.Id);
|
|
if (existing == null)
|
|
return (false, "规则不存在");
|
|
|
|
existing.RuleName = rule.RuleName;
|
|
|
|
existing.Level = rule.Level;
|
|
existing.Price = rule.Price;
|
|
existing.Sort = rule.Sort;
|
|
existing.IsEnable = rule.IsEnable;
|
|
|
|
await _ruleMongo.ReplaceOneAsync(existing);
|
|
return (true, "更新成功");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (false, "更新失败: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> DeleteRule(string id)
|
|
{
|
|
try
|
|
{
|
|
await _ruleMongo.DeleteByIdAsync(id);
|
|
return (true, "删除成功");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (false, "删除失败: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> GiveToUser(string number, string userId)
|
|
{
|
|
try
|
|
{
|
|
var user = await _userMongo.FindByIdAsync(userId);
|
|
if (user == null)
|
|
return (false, "用户不存在");
|
|
|
|
var config = await _beautifulNumberMongo.FindOneAsync(c => c.Number == number);
|
|
if (config != null)
|
|
{
|
|
if (config.Status == BeautifulNumberStatus.Sold)
|
|
return (false, "靓号已售出");
|
|
|
|
config.Status = BeautifulNumberStatus.Sold;
|
|
config.UserId = userId;
|
|
config.BuyerUserId = userId;
|
|
config.SaleTime = DateTime.Now.ToTimeStamp();
|
|
await _beautifulNumberMongo.ReplaceOneAsync(config);
|
|
}
|
|
|
|
user.BaseInfo.Number = number;
|
|
await _userMongo.ReplaceOneAsync(user);
|
|
|
|
return (true, "赠送成功");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (false, "赠送失败: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<(bool isok, string msg, int count)> InitBeautifulNumbers()
|
|
{
|
|
try
|
|
{
|
|
var rules = await GetRules();
|
|
if (rules == null || rules.Count == 0)
|
|
{
|
|
return (false, "请先初始化规则", 0);
|
|
}
|
|
|
|
var soldNumbers = new HashSet<string>();
|
|
var allConfigs = await _beautifulNumberMongo.FilterByAsync(c => c.Status == BeautifulNumberStatus.Sold);
|
|
foreach (var config in allConfigs)
|
|
{
|
|
soldNumbers.Add(config.Number);
|
|
}
|
|
|
|
var existingNumbers = new HashSet<string>();
|
|
var existingConfigs = await _beautifulNumberMongo.FilterByAsync(c => true);
|
|
foreach (var config in existingConfigs)
|
|
{
|
|
existingNumbers.Add(config.Number);
|
|
}
|
|
|
|
var newNumbers = new List<BeautifulNumberConfig>();
|
|
foreach (var rule in rules)
|
|
{
|
|
if (!rule.IsEnable) continue;
|
|
var numbers = BeautifulNumberGenerator.GenerateAllPossibleNumbersForRule(rule.RuleName);
|
|
|
|
foreach (var num in numbers)
|
|
{
|
|
if (existingNumbers.Contains(num) || soldNumbers.Contains(num) || newNumbers.Any(c=>c.Number==num))
|
|
continue;
|
|
|
|
newNumbers.Add(new BeautifulNumberConfig() { Level=rule.Level, Number=num, Price=rule.Price, RuleName=rule.RuleName
|
|
});
|
|
existingNumbers.Add(num);
|
|
}
|
|
}
|
|
|
|
if (newNumbers.Count > 0)
|
|
{
|
|
await _beautifulNumberMongo.InsertManyAsync(newNumbers);
|
|
}
|
|
|
|
return (true, $"初始化成功,新增 {newNumbers.Count} 个靓号", newNumbers.Count);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (false, "初始化失败: " + ex.Message, 0);
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<(bool isok, string msg)> UpdateRules(List<BeautifulNumberRule> rules)
|
|
{
|
|
try
|
|
{
|
|
await _ruleMongo.DeleteManyAsync(c => true);
|
|
|
|
if (rules.Count > 0)
|
|
{
|
|
await _ruleMongo.InsertManyAsync(rules);
|
|
}
|
|
|
|
return (true, "规则更新成功");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (false, "规则更新失败: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> InitRules()
|
|
{
|
|
var existingRules = _ruleMongo.Count(c => true);
|
|
if (existingRules > 0)
|
|
{
|
|
return (true, "规则已初始化");
|
|
}
|
|
|
|
var rules = new List<BeautifulNumberRule>
|
|
{
|
|
|
|
|
|
};
|
|
|
|
await _ruleMongo.InsertManyAsync(rules);
|
|
return (true, "规则初始化成功");
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> AddNumber(string number, string level, long price)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(number))
|
|
return (false, "靓号不能为空");
|
|
|
|
number = number.Trim();
|
|
|
|
var existing = await _beautifulNumberMongo.FindOneAsync(c => c.Number == number);
|
|
if (existing != null)
|
|
return (false, "靓号已存在: " + number);
|
|
|
|
var config = new BeautifulNumberConfig
|
|
{
|
|
Number = number,
|
|
Level = level ?? "D",
|
|
Price = price,
|
|
|
|
Status = 0,
|
|
CreateTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
|
};
|
|
|
|
await _beautifulNumberMongo.InsertOneAsync(config);
|
|
return (true, "添加成功");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (false, "添加失败: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<(bool isok, string msg, int successCount, int failCount)> BatchAddNumbers(List<string> numbers, string level, long price)
|
|
{
|
|
if (numbers == null || numbers.Count == 0)
|
|
return (false, "号码列表不能为空", 0, 0);
|
|
|
|
var successCount = 0;
|
|
var failCount = 0;
|
|
var failNumbers = new List<string>();
|
|
|
|
foreach (var number in numbers)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(number))
|
|
{
|
|
failCount++;
|
|
failNumbers.Add(number);
|
|
continue;
|
|
}
|
|
|
|
var num = number.Trim();
|
|
try
|
|
{
|
|
var existing = await _beautifulNumberMongo.FindOneAsync(c => c.Number == num);
|
|
if (existing != null)
|
|
{
|
|
failCount++;
|
|
failNumbers.Add(num + "(已存在)");
|
|
continue;
|
|
}
|
|
|
|
var config = new BeautifulNumberConfig
|
|
{
|
|
Number = num,
|
|
Level = level ?? "D",
|
|
Price = price,
|
|
Status = 0,
|
|
CreateTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
|
};
|
|
|
|
await _beautifulNumberMongo.InsertOneAsync(config);
|
|
successCount++;
|
|
}
|
|
catch
|
|
{
|
|
failCount++;
|
|
failNumbers.Add(num);
|
|
}
|
|
}
|
|
|
|
var msg = string.IsNullOrEmpty(failNumbers.FirstOrDefault())
|
|
? $"成功添加 {successCount} 个靓号"
|
|
: $"成功 {successCount} 个,失败 {failCount} 个: {string.Join(", ", failNumbers.Take(10))}";
|
|
|
|
return (true, msg, successCount, failCount);
|
|
}
|
|
|
|
|
|
}
|
|
}
|