yz_server/Server/Timers/BlackTimer.cs

167 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Common.DbExtends.Extends;
using Common.Models.Enums;
using Common.Models.UnqTables;
using Common.Utils;
using Newtonsoft.Json.Linq;
using Server.Utils;
namespace Server.Timers
{
/// <summary>
/// 黑名单同步
/// </summary>
public class BlackTimer : MyTimer
{
readonly Client Client = Client.SingleClient;
private SqlSugar.SqlSugarClient Db => Client.Db;
/// <summary>
/// 运行
/// </summary>
/// <param name="state"></param>
/// <param name="timedOut"></param>
protected override void Run(object state, bool timedOut)
{
//YZCloudApiHelper.GetBlacklist()
ReadBlacklist(Client.Config.RuntimeCache.GetPublicValue("玩家黑名单同步时间", () => DateTime.MinValue), false, (items, index, total) =>
{
//Console.WriteLine("输出:" + index + "|" + total + "|" + items.Count);
var db = Db;
var syncTime = Client.Config.RuntimeCache.GetPublicValue("玩家黑名单同步时间", () => DateTime.MinValue);
if (syncTime != DateTime.MinValue)
{
syncTime = syncTime.AddMinutes(-1);
}
bool issyncAdd = false;
db.UseTran(() =>
{
foreach (var item in items)
{
if (item.UpdateDateTime >= syncTime)
{
issyncAdd = true;
var type = SetUserType(item.Type);
var info = db.Queryable<UserBlack>().Where(w => w.Username == item.UserName && w.UserType == type).First();
if (info == null)
{
info = new UserBlack();
info.UserType = SetUserType(item.Type);
info.Username = item.UserName;
SetBlackType(info, item.GroupType);
info.NickName = item.UserNick;
info.Remark = item.Remark;
info.Avatar = item.Avatar;
info.UpdateTime = DateTime.Now;
info.IsDel = item.IsRemove;
info.IsCloud = true;
db.Insertable(info).ExecuteCommand();
}
else
{
if (info.IsDel)
{
//如果本地已经删除,则忽略。
continue;
}
SetBlackType(info, item.GroupType);
info.Avatar = item.Avatar;
info.IsDel = item.IsRemove;
info.UpdateTime = DateTime.Now;
info.Remark = item.Remark;
info.IsCloud = true;
db.Updateable(info).ExecuteCommand();
}
}
}
}, ex => db.OnLog("同步黑名单", ex));
if (issyncAdd)
{
Client.Config.RuntimeCache.SetPublicValue("玩家黑名单同步时间", items.LastOrDefault().UpdateDateTime);
}
return false;
});
}
private UserType SetUserType(int itemType)
{
switch (itemType)
{
case 0:
return UserType.;
case 1:
return UserType.QQ用户;
case 2:
return UserType.;
}
return UserType.;
}
/// <summary>
/// 处理分组类型
/// </summary>
/// <param name="info"></param>
/// <param name="itemGroupType"></param>
private void SetBlackType(UserBlack info, BlacklistGroupType itemGroupType)
{
switch (itemGroupType)
{
case BlacklistGroupType.:
info.BlackType = BlackType.;
break;
case BlacklistGroupType.:
info.BlackType = BlackType.;
break;
case BlacklistGroupType.:
info.BlackType = BlackType.;
break;
default:
info.BlackType = BlackType.;
break;
}
}
/// <summary>
/// 读取黑名单
/// </summary>
/// <param name="isAsc"></param>
/// <param name="callback"></param>
public static void ReadBlacklist(DateTime updateDateTime, bool isAsc, Func<List<CloudBlacklistResult>, int, int, bool> callback)
{
var size = 1000;
var index = 1;
while (true)
{
var res = YZCloudApiHelper.GetBlacklist(new BlacklistSearchPagingInput()
{
PageIndex = index,
PageSize = size,
UserToken = "",
IsAsc = isAsc,
UpdateDateTime = updateDateTime,
AuditState = AuditStateEmun.Succee
});
if (res == null || res.Datas.Count <= 0)
{
break;
}
if (callback?.Invoke(res.Datas, index, res.PageNumber) == true)
{
break;
}
if (index <= 0)
{
break;
}
index++;
}
}
}
}