Browse Source

加入任务接取频繁限制

master
feng 3 years ago
parent
commit
45da610e8a
  1. 12
      src/Coldairarrow.Api/.config/dotnet-tools.json
  2. 2
      src/Coldairarrow.Api/Program.cs
  3. 8
      src/Coldairarrow.Api/Properties/PublishProfiles/FolderProfile.pubxml
  4. 76
      src/Coldairarrow.Business/HuiYan/pricetasklogBusiness.cs
  5. 23
      src/Coldairarrow.Entity/DTO/TaskCountDto.cs
  6. 4
      src/Coldairarrow.Util/01.Coldairarrow.Util.csproj

12
src/Coldairarrow.Api/.config/dotnet-tools.json

@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "6.0.9",
"commands": [
"dotnet-ef"
]
}
}
}

2
src/Coldairarrow.Api/Program.cs

@ -22,7 +22,7 @@ namespace Coldairarrow.Api
services.AddAutoMapper();
services.AddEFCoreSharding(config =>
{
config.SetEntityAssemblies(GlobalAssemblies.AllAssemblies);
//config.SetEntityAssemblies(GlobalAssemblies.AllAssemblies);
var dbOptions = hostContext.Configuration.GetSection("Database:BaseDb").Get<DatabaseOptions>();
var dbmdsOptions = hostContext.Configuration.GetSection("MsdDatabase:BaseDb").Get<DatabaseOptions>();

8
src/Coldairarrow.Api/Properties/PublishProfiles/FolderProfile.pubxml

@ -10,10 +10,12 @@
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
<ExcludeApp_Data>false</ExcludeApp_Data>
<ProjectGuid>bea1bf0d-b063-4931-89c7-22f92973143a</ProjectGuid>
<publishUrl>bin\Release\netcoreapp3.1\publish\</publishUrl>
<DeleteExistingFiles>False</DeleteExistingFiles>
<DeleteExistingFiles>true</DeleteExistingFiles>
<TargetFramework>net5.0</TargetFramework>
<SelfContained>false</SelfContained>
</PropertyGroup>
</Project>

76
src/Coldairarrow.Business/HuiYan/pricetasklogBusiness.cs

@ -8,6 +8,7 @@ using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
@ -24,14 +25,16 @@ namespace Coldairarrow.Business.HuiYan
IteamitemsBusiness _iteamitemsBusiness;
readonly IOperator _operator;
readonly ILogger _logger;
readonly IDistributedCache _cache;
public pricetasklogBusiness(IDbAccessor db, IuserBusiness iuserBusiness, IteamitemsBusiness iteamitemsBusiness, ILogger<pricetasklogBusiness> logger,
IOperator @operator)
IOperator @operator, IDistributedCache cache)
: base(db)
{
_operator = @operator;
_iuserBusiness = iuserBusiness;
_iteamitemsBusiness = iteamitemsBusiness;
_logger = logger;
_cache = cache;
}
#region 外部接口
@ -275,41 +278,64 @@ namespace Coldairarrow.Business.HuiYan
bool CheckIsMaxTaskCount()
{
Expression<Func<teamitems, items, pricetasklog, TeamitemDto>> select = (a, b, c) => new TeamitemDto
{
GoodsId = b.GoodsId,
HasFilter = b.HasFilter,
Platform = b.Platform,
GoodsUrl = b.GoodsUrl,
PriceTaskId = c.Id,
PriceTaskState = (int)c.State,
Extensions = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TeamItemExtension>>(a.ExtensionJson)
};
select = select.BuildExtendSelectExpre();
var q_titem = Db.GetIQueryable<teamitems>();
var q = from a in q_titem.AsExpandable()
join b in Db.GetIQueryable<items>() on a.ItemId equals b.Id into ab
from ba in ab.DefaultIfEmpty()
join c in Db.GetIQueryable<pricetasklog>() on a.Id equals c.TeamItemId into ac
from bb in ac.DefaultIfEmpty()
select @select.Invoke(a, ba, bb);
var list = Db.GetListBySql<TaskCountDto>(@"SELECT
CAST( `p`.`State` AS signed ) AS State,
COUNT(*) AS count
FROM
`teamitems` AS `t`
LEFT JOIN `items` AS `i` ON `t`.`ItemId` = `i`.`Id`
LEFT JOIN `pricetasklog` AS `p` ON `t`.`Id` = `p`.`TeamItemId`
WHERE
( `t`.`PriceTaskUserId` = @uid )
AND `p`.`State` IS NOT NULL
GROUP BY
State",("uid",_operator.UserId));
//查询对应状态
var where = LinqHelper.True<TeamitemDto>().And(c => c.PriceTaskUserId == _operator.UserId);
where = where.And(c => c.PriceTaskState != null);
int? hasTaskCount = list.Select(c => new { Id = c.State, Count = c.Count }).Where(c => c.Id == 0).FirstOrDefault()?.Count;
var list = q.Where(where).Select(c => c.PriceTaskState).ToList();
int? hasTaskCount = list.GroupBy(c => c).Select(c => new { Id = c.Key, Count = c.Count() }).Where(c => c.Id == 0).FirstOrDefault()?.Count;
return hasTaskCount > 10;
}
public AjaxResult AcceptTask(string teamItemId)
{
string cacheKey = $"acceptTask:{_operator.UserId}";
string keyTime = _cache.GetString(cacheKey);
AcceptTime lastAccept = null;
//判断是否没有请求过
if (!string.IsNullOrEmpty(keyTime))
{
var time=Newtonsoft.Json.JsonConvert.DeserializeObject<AcceptTime>(keyTime);
//如果是新一轮请求
if (time.Date < DateTime.Now)
{
time.Date= DateTime.Now.AddMinutes(1);
time.Count = 1;
}
if (time.Count >= 3)
{
return Error("请求过于频繁!");
}
time.Count = time.Count + 1;
lastAccept = time;
}
else {
lastAccept = new AcceptTime() { Date = DateTime.Now.AddMinutes(1), Count = 1 };
}
_cache.SetString(cacheKey, Newtonsoft.Json.JsonConvert.SerializeObject(lastAccept));
var teamItem = Db.GetIQueryable<teamitems>().FirstOrDefault(c => c.Id == teamItemId);
if (teamItem == null)
@ -317,11 +343,13 @@ namespace Coldairarrow.Business.HuiYan
return Error("任务不存在!");
}
if (CheckIsMaxTaskCount())
{
return Error("任务接取已达上限!");
}
var result = Db.RunTransaction(() =>
{
string orderId = DateTime.Now.ToString("yyyyMMddHHmmssff");

23
src/Coldairarrow.Entity/DTO/TaskCountDto.cs

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coldairarrow.Entity.DTO
{
public class TaskCountDto
{
public int State { get; set; }
public int Count { get; set; }
}
public class AcceptTime
{
public DateTime Date { get; set; }
public int Count { get; set; }
}
}

4
src/Coldairarrow.Util/01.Coldairarrow.Util.csproj

@ -13,8 +13,8 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Castle.Core.AsyncInterceptor" Version="1.7.0" />
<PackageReference Include="EFCore.Sharding.MySql" Version="5.0.17" />
<PackageReference Include="EFCore.Sharding.SqlServer" Version="5.0.11" />
<PackageReference Include="EFCore.Sharding.MySql" Version="5.0.0" />
<PackageReference Include="EFCore.Sharding.SqlServer" Version="5.0.0" />
<PackageReference Include="IdHelper.Zookeeper" Version="1.5.1" />
<PackageReference Include="MySqlConnector" Version="1.3.8" />
<PackageReference Include="Npgsql" Version="5.0.4" />

Loading…
Cancel
Save