feat: 新增签到功能
This commit is contained in:
@@ -3,18 +3,33 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Volo.Abp.Users;
|
||||
using Yi.Framework.Bbs.Domain.Managers;
|
||||
|
||||
namespace Yi.Framework.Bbs.Application.Services.Integral
|
||||
{
|
||||
public class IntegralService:ApplicationService
|
||||
public class IntegralService : ApplicationService
|
||||
{
|
||||
private IntegralManager _integralManager;
|
||||
public IntegralService(IntegralManager integralManager)
|
||||
private ICurrentUser _currentUser;
|
||||
public IntegralService(IntegralManager integralManager, ICurrentUser currentUser)
|
||||
{
|
||||
_integralManager= integralManager;
|
||||
_integralManager = integralManager;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 签到
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Authorize]
|
||||
public async Task<object> PostSignInAsync()
|
||||
{
|
||||
var value = await _integralManager.SignInAsync(_currentUser.Id ?? Guid.Empty);
|
||||
return new { value };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Yi.Framework.Bbs.Domain.Entities
|
||||
/// 评论表
|
||||
/// </summary>
|
||||
[SugarTable("BbsUserExtraInfo")]
|
||||
[SugarIndex($"index_{nameof(UserId)}", nameof(UserId), OrderByType.Asc)]
|
||||
public class BbsUserExtraInfoEntity : Entity<Guid>
|
||||
{
|
||||
public BbsUserExtraInfoEntity() { }
|
||||
|
||||
@@ -5,6 +5,8 @@ using Volo.Abp.Domain.Entities;
|
||||
namespace Yi.Framework.Bbs.Domain.Entities.Forum
|
||||
{
|
||||
[SugarTable("Agree")]
|
||||
[SugarIndex($"index_{nameof(CreatorId)}_{nameof(DiscussId)}", nameof(CreatorId), OrderByType.Asc,
|
||||
nameof(DiscussId), OrderByType.Asc)]
|
||||
public class AgreeEntity : Entity<Guid>, ICreationAuditedObject
|
||||
{
|
||||
public AgreeEntity()
|
||||
|
||||
@@ -6,6 +6,9 @@ using Volo.Abp.Domain.Entities;
|
||||
namespace Yi.Framework.Bbs.Domain.Entities.Forum
|
||||
{
|
||||
[SugarTable("Article")]
|
||||
[SugarIndex($"index_{nameof(Name)}", nameof(Name), OrderByType.Asc)]
|
||||
[SugarIndex($"index_{nameof(ParentId)}", nameof(ParentId), OrderByType.Asc)]
|
||||
[SugarIndex($"index_{nameof(DiscussId)}", nameof(DiscussId), OrderByType.Asc)]
|
||||
public class ArticleEntity : Entity<Guid>, ISoftDelete, IAuditedObject
|
||||
{
|
||||
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
|
||||
|
||||
@@ -11,6 +11,8 @@ namespace Yi.Framework.Bbs.Domain.Entities.Forum
|
||||
/// 评论表
|
||||
/// </summary>
|
||||
[SugarTable("Comment")]
|
||||
[SugarIndex($"index_{nameof(DiscussId)}", nameof(DiscussId), OrderByType.Asc)]
|
||||
[SugarIndex($"index_{nameof(ParentId)}", nameof(ParentId), OrderByType.Asc)]
|
||||
public class CommentEntity : Entity<Guid>, ISoftDelete, IAuditedObject
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -7,6 +7,9 @@ using Yi.Framework.Bbs.Domain.Shared.Enums;
|
||||
namespace Yi.Framework.Bbs.Domain.Entities.Forum
|
||||
{
|
||||
[SugarTable("Discuss")]
|
||||
[SugarIndex($"index_{nameof(Title)}", nameof(Title), OrderByType.Asc)]
|
||||
[SugarIndex($"index_{nameof(PlateId)}", nameof(PlateId), OrderByType.Asc)]
|
||||
[SugarIndex($"index_{nameof(CreationTime)}", nameof(CreationTime), OrderByType.Desc)]
|
||||
public class DiscussEntity : Entity<Guid>, ISoftDelete, IAuditedObject
|
||||
{
|
||||
public DiscussEntity()
|
||||
|
||||
@@ -13,6 +13,8 @@ namespace Yi.Framework.Bbs.Domain.Entities.Integral
|
||||
/// 签到表
|
||||
/// </summary>
|
||||
[SugarTable("SignIn")]
|
||||
|
||||
[SugarIndex($"index_{nameof(CreatorId)}", nameof(CreatorId), OrderByType.Asc)]
|
||||
public class SignInEntity : Entity<Guid>, ICreationAuditedObject
|
||||
{
|
||||
|
||||
@@ -27,5 +29,10 @@ namespace Yi.Framework.Bbs.Domain.Entities.Integral
|
||||
//签到用户
|
||||
public Guid? CreatorId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 连续签到次数
|
||||
/// </summary>
|
||||
public int ContinuousNumber { get; set; } = 1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Volo.Abp.EventBus.Local;
|
||||
using Volo.Abp.Uow;
|
||||
using Yi.Framework.Bbs.Domain.Entities.Integral;
|
||||
using Yi.Framework.Bbs.Domain.Shared.Etos;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
@@ -23,7 +25,8 @@ namespace Yi.Framework.Bbs.Domain.Managers
|
||||
/// 签到
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task PostSignInAsync(Guid userId)
|
||||
[UnitOfWork]
|
||||
public async Task<decimal> SignInAsync(Guid userId)
|
||||
{
|
||||
//签到,添加用户钱钱
|
||||
//发送一个充值的领域事件即可
|
||||
@@ -33,17 +36,96 @@ namespace Yi.Framework.Bbs.Domain.Managers
|
||||
//每天随机(3-10),连续签到每次累加多1点,最多一天30
|
||||
|
||||
//额外
|
||||
//如果随机数数字都相同,额外再获取乘10倍
|
||||
//如果随机数数字以9结尾,额外再获取乘1倍
|
||||
|
||||
//这种逻辑,就是属于核心领域业务了
|
||||
decimal number = 3;
|
||||
|
||||
|
||||
var sigInLast = await _signInRepository._DbQueryable.Where(x => x.CreatorId == userId).OrderByDescending(x => x.CreationTime).FirstAsync();
|
||||
|
||||
//verify 效验是否允许签到了
|
||||
if (sigInLast is not null)
|
||||
{
|
||||
VerifySignInTime(sigInLast.CreationTime);
|
||||
}
|
||||
|
||||
//连续签到次数
|
||||
var continuousNumber = GetContinuousNumber(sigInLast);
|
||||
//签到奖励值
|
||||
var value = GetSignInValue(continuousNumber);
|
||||
|
||||
|
||||
//插入记录
|
||||
var entity = new SignInEntity() { ContinuousNumber = continuousNumber };
|
||||
await _signInRepository.InsertAsync(entity);
|
||||
|
||||
//发布一个其他领域的事件
|
||||
await _localEventBus.PublishAsync(new MoneyChangeEventArgs() { UserId = userId, Number = number });
|
||||
await _localEventBus.PublishAsync(new MoneyChangeEventArgs() { UserId = userId, Number = value });
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 效验签到时间
|
||||
/// </summary>
|
||||
/// <param name="dataTime"></param>
|
||||
/// <exception cref="UserFriendlyException"></exception>
|
||||
private void VerifySignInTime(DateTime dataTime)
|
||||
{
|
||||
if (dataTime.Date == DateTime.Now.Date)
|
||||
{
|
||||
throw new UserFriendlyException("今日你已经签到过了~");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取签到值
|
||||
/// </summary>
|
||||
/// <param name="continuousNumber"></param>
|
||||
/// <returns></returns>
|
||||
private decimal GetSignInValue(int continuousNumber)
|
||||
{
|
||||
//基础数值
|
||||
var baseValue = new Random().Next(300, 1100) / 100m;
|
||||
|
||||
|
||||
//累加额外的奖励
|
||||
var extraValue = 0m;
|
||||
if (baseValue.ToString().EndsWith("9"))
|
||||
{
|
||||
extraValue = 1 * baseValue;
|
||||
}
|
||||
|
||||
//累加连续签到的奖励
|
||||
var signInValue = continuousNumber * 1m;
|
||||
|
||||
|
||||
//获取添加的值
|
||||
var value = baseValue + extraValue + signInValue;
|
||||
if (value >= 30)
|
||||
{
|
||||
value = 30;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取连续次数
|
||||
/// </summary>
|
||||
private int GetContinuousNumber(SignInEntity sigInLast)
|
||||
{
|
||||
var continuousNumber = 1;
|
||||
|
||||
//已签到过
|
||||
if (sigInLast is not null)
|
||||
{
|
||||
//签到过,且昨天已签到过,直接使用昨天的连续次数+1
|
||||
if (sigInLast.CreationTime == DateTime.Now.Date.AddDays(-1))
|
||||
{
|
||||
continuousNumber = sigInLast.ContinuousNumber + 1;
|
||||
}
|
||||
}
|
||||
return continuousNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ using Yi.Framework.Core.Extensions;
|
||||
namespace Yi.Framework.Rbac.Domain.Entities
|
||||
{
|
||||
[SugarTable("LoginLog")]
|
||||
[SugarIndex($"index_{nameof(LoginUser)}", nameof(LoginUser), OrderByType.Asc)]
|
||||
public class LoginLogEntity : Entity<Guid>, ICreationAuditedObject
|
||||
{
|
||||
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Yi.Framework.Rbac.Domain.Entities
|
||||
/// 用户表
|
||||
/// </summary>
|
||||
[SugarTable("User")]
|
||||
[SugarIndex($"index_{nameof(UserName)}", nameof(UserName), OrderByType.Asc)]
|
||||
public class UserEntity : Entity<Guid>, ISoftDelete, IAuditedObject, IOrderNum, IState
|
||||
{
|
||||
public UserEntity()
|
||||
|
||||
Reference in New Issue
Block a user