feat: 完成账号绑定功能
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Domain.Entities;
|
||||
|
||||
namespace Yi.Framework.DigitalCollectibles.Domain.Entities;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 藏品用户信息表
|
||||
/// </summary>
|
||||
[SugarTable("DC_CollectiblesUserExtraInfo")]
|
||||
public class CollectiblesUserExtraInfoEntity: Entity<Guid>
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户id
|
||||
/// </summary>
|
||||
public Guid UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 手机号
|
||||
/// </summary>
|
||||
public string Phone{ get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 微信openid
|
||||
/// </summary>
|
||||
public string WeChatOpenId { get; set; }
|
||||
}
|
||||
@@ -4,8 +4,18 @@ using Volo.Abp.Domain.Entities.Auditing;
|
||||
namespace Yi.Framework.DigitalCollectibles.Domain.Entities;
|
||||
|
||||
[SugarTable("DC_InvitationCode")]
|
||||
public class InvitationCodeAggregateRoot:FullAuditedAggregateRoot<Guid>
|
||||
public class InvitationCodeAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
{
|
||||
public InvitationCodeAggregateRoot()
|
||||
{
|
||||
}
|
||||
public InvitationCodeAggregateRoot(Guid userId, string invitationCode)
|
||||
{
|
||||
this.UserId = userId;
|
||||
this.InvitationCode = invitationCode;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 谁的邀请码
|
||||
/// </summary>
|
||||
@@ -14,14 +24,35 @@ public class InvitationCodeAggregateRoot:FullAuditedAggregateRoot<Guid>
|
||||
/// <summary>
|
||||
/// 是否填写了邀请码(是否被邀请)
|
||||
/// </summary>
|
||||
public bool IsInvited { get; set; }
|
||||
public bool IsInvited { get; set; } = false;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 积分-邀请数量
|
||||
/// </summary>
|
||||
public int PointsNumber { get; set; }
|
||||
public int PointsNumber { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 邀请码
|
||||
/// </summary>
|
||||
public string InvitationCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 这个人填写了邀请码(不能再进行填写)
|
||||
/// </summary>
|
||||
public void SetInvite()
|
||||
{
|
||||
IsInvited = true;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 别人填写了这个用户的邀请码(这个用户积分+1)
|
||||
/// </summary>
|
||||
public void SetInvited()
|
||||
{
|
||||
PointsNumber += 1;
|
||||
}
|
||||
|
||||
//不做记录
|
||||
// /// <summary>
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.EventBus;
|
||||
using Yi.Framework.Bbs.Domain.Shared.Etos;
|
||||
using Yi.Framework.DigitalCollectibles.Domain.Entities;
|
||||
using Yi.Framework.DigitalCollectibles.Domain.Entities.Record;
|
||||
using Yi.Framework.DigitalCollectibles.Domain.Managers;
|
||||
using Yi.Framework.DigitalCollectibles.Domain.Shared.Etos;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.DigitalCollectibles.Domain.EventHandlers;
|
||||
|
||||
/// <summary>
|
||||
/// 临时账号绑定到正式账号,价值、积分 (累加)
|
||||
/// </summary>
|
||||
public class BindAccountForCollectiblesEventHandler : ILocalEventHandler<BindAccountEto>, ITransientDependency
|
||||
{
|
||||
private readonly InvitationCodeManager _invitationCodeManager;
|
||||
|
||||
private readonly ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> _collectiblesUserStoreRepository;
|
||||
|
||||
|
||||
private readonly ISqlSugarRepository<MiningPoolRecordAggregateRoot> _miningPoolRecordRepository;
|
||||
private readonly ISqlSugarRepository<MarketRecordAggregateRoot> _marketRecordRepository;
|
||||
private readonly ISqlSugarRepository<MarketGoodsAggregateRoot> _marketGoodsRepository;
|
||||
public BindAccountForCollectiblesEventHandler(InvitationCodeManager invitationCodeManager, ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> collectiblesUserStoreRepository, ISqlSugarRepository<MiningPoolRecordAggregateRoot> miningPoolRecordRepository, ISqlSugarRepository<MarketRecordAggregateRoot> marketRecordRepository, ISqlSugarRepository<MarketGoodsAggregateRoot> marketGoodsRepository)
|
||||
{
|
||||
_invitationCodeManager = invitationCodeManager;
|
||||
_collectiblesUserStoreRepository = collectiblesUserStoreRepository;
|
||||
_miningPoolRecordRepository = miningPoolRecordRepository;
|
||||
_marketRecordRepository = marketRecordRepository;
|
||||
_marketGoodsRepository = marketGoodsRepository;
|
||||
}
|
||||
|
||||
public async Task HandleEventAsync(BindAccountEto eventData)
|
||||
{
|
||||
var oldEntity = await _invitationCodeManager.TryGetOrAddAsync(eventData.OldUserId);
|
||||
var newEntity = await _invitationCodeManager.TryGetOrAddAsync(eventData.NewUserId);
|
||||
|
||||
newEntity.PointsNumber += oldEntity.PointsNumber;
|
||||
|
||||
//临时账号邀请了,老的账号没有邀请,覆盖
|
||||
if (newEntity.IsInvited == false && oldEntity.IsInvited == true)
|
||||
{
|
||||
newEntity.IsInvited = true;
|
||||
}
|
||||
|
||||
await _invitationCodeManager._repository.UpdateAsync(newEntity);
|
||||
|
||||
//藏品转移
|
||||
var oldUserStore= await _collectiblesUserStoreRepository.GetListAsync(x => x.UserId == eventData.OldUserId);
|
||||
if (oldUserStore.Count>0)
|
||||
{
|
||||
oldUserStore?.ForEach(x=>x.UserId=eventData.NewUserId);
|
||||
await _collectiblesUserStoreRepository.UpdateRangeAsync(oldUserStore);
|
||||
}
|
||||
|
||||
//挖矿记录转移
|
||||
var miningPoolRecord= await _miningPoolRecordRepository.GetListAsync(x => x.UserId == eventData.OldUserId);
|
||||
if (miningPoolRecord.Count>0)
|
||||
{
|
||||
miningPoolRecord?.ForEach(x=>x.UserId=eventData.NewUserId);
|
||||
await _miningPoolRecordRepository.UpdateRangeAsync(miningPoolRecord);
|
||||
}
|
||||
|
||||
//交易记录转移
|
||||
var marketRecord= await _marketRecordRepository.GetListAsync(x => x.SellUserId == eventData.OldUserId||x.BuyId==eventData.OldUserId);
|
||||
if (marketRecord.Count>0)
|
||||
{
|
||||
marketRecord?.ForEach(x =>
|
||||
{
|
||||
if (x.SellUserId == eventData.OldUserId)
|
||||
{
|
||||
x.SellUserId = eventData.NewUserId;
|
||||
}
|
||||
|
||||
if (x.BuyId == eventData.OldUserId)
|
||||
{
|
||||
x.BuyId = eventData.NewUserId;
|
||||
}
|
||||
});
|
||||
await _marketRecordRepository.UpdateRangeAsync(marketRecord);
|
||||
}
|
||||
|
||||
|
||||
//商城物品交易转移
|
||||
var marketGoods= await _marketGoodsRepository.GetListAsync(x => x.SellUserId == eventData.OldUserId);
|
||||
if (marketGoods.Count>0)
|
||||
{
|
||||
marketGoods?.ForEach(x=>x.SellUserId=eventData.NewUserId);
|
||||
await _marketGoodsRepository.UpdateRangeAsync(marketGoods);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Yi.Framework.DigitalCollectibles.Domain.Entities;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.DigitalCollectibles.Domain.Managers;
|
||||
|
||||
/// <summary>
|
||||
/// 邀请码领域服务
|
||||
/// </summary>
|
||||
public class InvitationCodeManager : DomainService
|
||||
{
|
||||
public readonly ISqlSugarRepository<InvitationCodeAggregateRoot> _repository;
|
||||
|
||||
public InvitationCodeManager(ISqlSugarRepository<InvitationCodeAggregateRoot> repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 填写邀请码
|
||||
/// </summary>
|
||||
public async Task SetAsync(Guid writeUserId, string invitationCode)
|
||||
{
|
||||
var entityOrNull = await _repository.GetFirstAsync(x => x.InvitationCode == invitationCode);
|
||||
if (entityOrNull is null)
|
||||
{
|
||||
throw new UserFriendlyException("无效邀请码,请检查");
|
||||
}
|
||||
if (entityOrNull.UserId==writeUserId)
|
||||
{
|
||||
throw new UserFriendlyException("你不能邀请自己");
|
||||
}
|
||||
|
||||
//被邀请的人
|
||||
var entity = entityOrNull;
|
||||
entity.SetInvited();
|
||||
|
||||
//填写邀请码的人
|
||||
var writeEntity = await TryGetOrAddAsync(writeUserId);
|
||||
writeEntity.SetInvite();
|
||||
|
||||
await _repository.UpdateRangeAsync(new List<InvitationCodeAggregateRoot> { entity, writeEntity });
|
||||
}
|
||||
|
||||
public async Task<InvitationCodeAggregateRoot> TryGetOrAddAsync(Guid userId, int InitPointsNumber = 0)
|
||||
{
|
||||
var entity = await _repository.FindAsync(x => x.UserId == userId);
|
||||
if (entity is null)
|
||||
{
|
||||
string invitationCode = string.Empty;
|
||||
|
||||
//循环到邀请码没有重复为止
|
||||
var isExist = true;
|
||||
while (isExist)
|
||||
{
|
||||
invitationCode = CreateInvitationCode(4);
|
||||
if (!await _repository.IsAnyAsync(x => x.InvitationCode == invitationCode))
|
||||
{
|
||||
isExist = false;
|
||||
}
|
||||
}
|
||||
|
||||
var insertEntity = new InvitationCodeAggregateRoot(userId, invitationCode)
|
||||
{
|
||||
PointsNumber = InitPointsNumber
|
||||
};
|
||||
entity = await _repository.InsertReturnEntityAsync(insertEntity);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
private string CreateInvitationCode(int length)
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
Random random = new Random();
|
||||
char[] stringChars = new char[length];
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
stringChars[i] = chars[random.Next(chars.Length)];
|
||||
}
|
||||
|
||||
return new string(stringChars);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user