yz_server/Server/Timers/TbSpecialTimer.cs

107 lines
4.2 KiB
C#
Raw Normal View History

2022-04-16 07:48:12 +00:00
using Common.Models.UnqTables;
using Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.DbExtends.Extends;
using Common.Requests.Lianmengs;
using Common.Models.SubValueTables;
using SqlSugar;
using System.Threading;
using Common.Models.SubTables;
using Common.Utils.Extends;
namespace Server.Timers
{
public class TbSpecialTimer : MyTimer
{
private DateTime DeleteTime = DateTime.MinValue;
private Client client = Client.SingleClient;
private int UpdaetCount = 0;
private SqlSugar.SqlSugarClient Db { get { return client.Db; } }
protected override void Run(object state, bool timedOut)
{
try
{
UpdaetCount++;
var time = DateTime.Now.AddMinutes(-60);
//第一步:每天删除一次三天以前的查询记录
if (DeleteTime.Day != DateTime.Now.Day)
{
DeleteTime = DateTime.Now;
Db.Deleteable<TbSpecialCache>().Where(f => f.CreateTime < DateTime.Now.AddDays(-3));
}
//第二步筛选时间优先把最近30分钟筛选出来其他的一天查一次
var exp = Expressionable.Create<TbSpecialCache>();
if (UpdaetCount == 600) //10小时了同步下当天的
{
exp.And(f => f.CreateTime >= DateTime.Now.AddDays(-1));
}
else if (UpdaetCount != 1440) //没到24小时仅同步最近30分钟的
{
exp.And(f => f.CreateTime >= DateTime.Now.AddMinutes(-30));
}
var list = Db.Queryable<TbSpecialCache>().Where(exp.ToExpression()).OrderBy(f => f.Id, SqlSugar.OrderByType.Asc).ToList();
if (list.Count > 0)
{
foreach (var item in list)
{
var lm = Db.GetLianmeng(item.LianmengId);
if (lm != null && lm.ExpirationTime > DateTime.Now)
{
var req = new TaobaoRequest(lm);
var type = item.UserType.ToExternalType();
var data = req.QuerySpecial($"{item.Username}", type);
if (data != null && data["inviter_list"] != null && data["inviter_list"].Count() > 0)
{
foreach (var v in data["inviter_list"])
{
var special_id = (long)data["special_id"];
var tableName = Db.GetTableName<TbSpecial>(req.Lianmeng.Id);
var rst = Db.GetTbSpecial(item.UserId, item.LianmengId);
if (rst == null)
{
rst = new TbSpecial()
{
SpecialId = special_id,
LianmengId = item.LianmengId,
UpdateTime = DateTime.Now,
UserId = item.UserId
};
Db.Save(rst);
}
//删掉
Db.Deleteable(item).ExecuteCommand();
break;
}
}
else
{
item.SyncTime = DateTime.Now;
Db.Updateable(item).ExecuteCommand();
}
}
Thread.Sleep(1000);
}
}
}
catch (Exception ex)
{
Db.OnLog("淘宝会员关系同步", ex);
}
}
}
}