yz_server/Server/Timers/StoreBlackTimer.cs

132 lines
5.0 KiB
C#
Raw Normal View History

2022-04-16 07:48:12 +00:00
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.Controllers.Help;
using Server.Utils;
namespace Server.Timers
{
/// <summary>
/// 商店黑名单同步
/// </summary>
public class StoreBlackTimer : 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 platform = GetPlatform(item);
if (platform == LianmengType.)
{
continue;
}
var info = db.Queryable<StoreBlacklist>().Where(w => w.Platform == platform && w.StoreId == item.StoreId).First();
if (info == null)
{
info = new StoreBlacklist();
info.Platform = platform;
info.StoreName = item.StoreName;
info.Remark = item.Remark;
info.UpdateTime = DateTime.Now;
info.IsDel = item.IsRemove;
info.IsCloud = true;
info.StoreId = item.StoreId;
db.Insertable(info).ExecuteCommand();
}
else
{
if (info.IsDel)
{
//如果本地已经删除,则忽略。
continue;
}
info.Remark = item.Remark;
info.UpdateTime = DateTime.Now;
info.IsDel = item.IsRemove;
info.IsCloud = true;
db.Updateable(info).ExecuteCommand();
}
}
}
}, ex => db.OnLog("同步商店黑名单", ex));
if (issyncAdd)
{
Client.Config.RuntimeCache.SetPublicValue("玩家商店黑名单同步时间", items.LastOrDefault().UpdateDateTime);
}
return false;
});
}
private LianmengType GetPlatform(CloudStoreBlacklistResult item)
{
return LianmengTypeHelper.GetPlatformType(item.Platform);
}
/// <summary>
/// 读取
/// </summary>
/// <param name="isAsc"></param>
/// <param name="callback"></param>
public static void ReadBlacklist(DateTime updateDateTime, bool isAsc, Func<List<CloudStoreBlacklistResult>, int, int, bool> callback)
{
var size = 1000;
var index = 1;
while (true)
{
var res = YZCloudApiHelper.GetStoreBlacklist(new StoreBlacklistSearchPagingInput()
{
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++;
}
}
}
}