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 { /// /// 黑名单同步 /// public class BlackTimer : MyTimer { readonly Client Client = Client.SingleClient; private SqlSugar.SqlSugarClient Db => Client.Db; /// /// 运行 /// /// /// 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().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.微信用户; } /// /// 处理分组类型 /// /// /// 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; } } /// /// 读取黑名单 /// /// /// public static void ReadBlacklist(DateTime updateDateTime, bool isAsc, Func, 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++; } } } }