yz_server/Server/Timers/OldUserActivationTimer.cs

82 lines
3.3 KiB
C#
Raw Normal View History

2022-04-16 07:48:12 +00:00
using Common.DbExtends.Extends;
using Common.DbExtends.Others;
using Common.Models.JsonModels;
using Common.Models.SubCountTables;
using Common.Models.SubTables;
using Common.Models.UnqTables;
using Common.Utils;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Server.Timers
{
public class OldUserActivationTimer : MyTimer
{
private Client client = Client.SingleClient;
SqlSugar.SqlSugarClient Db { get { return client.Db; } }
private DateTime UpdateTime = DateTime.MinValue;
protected override void Run(object state, bool timedOut)
{
var now = DateTime.Now;
if (now.Hour == 4 || now.Hour == 5)
{
if (UpdateTime == DateTime.MinValue || UpdateTime.Day != now.Day)
{
try
{
UpdateTime = DateTime.Now;
var robots = Db.Queryable<Robot>().ToList();
var dbTables = Db.DbMaintenance.GetTableInfoList().Where(f => f.Name.StartsWith("userdata_")).ToList();
if (dbTables.Count == 0) return;
foreach (var robot in robots)
{
var rebateConfig = Db.GetRebateConfigById(robot.ConfigRebateId);
if (rebateConfig != null && rebateConfig.IsOldChange)
{
//排除掉设置的groupid和不自动归类的分组
var groupIds = Db.Queryable<UserGroup>()
.Where(f => f.Id != rebateConfig.GroupId && f.AotuSort == false)
.Select(f => f.Id)
.ToList();
var day = rebateConfig.UnuseDays;
var lastTime = DateTime.Now.AddDays(-day);
foreach (var item in dbTables)
{
var tabName = item.Name;
var db = Db;
db.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService = new MySplitService(MySplitService.MySplitType.);
db.Updateable<UserData>()
.SetColumns(f => f.GroupId == rebateConfig.GroupId)
//.Where(f => f.LastBuyTime.AddDays(day) < DateTime.Now && f.CreateTime.AddDays(day) < DateTime.Now && !groupIds.Contains(f.GroupId))
.Where(f => f.LastBuyTime < lastTime && f.CreateTime < lastTime && !groupIds.Contains(f.GroupId))
.SplitTable(tab => tab.InTableNames(tabName))
.ExecuteCommand();
}
}
}
}
catch (Exception ex)
{
Db.OnLog("更改用户组", ex);
throw;
}
}
}
}
}
}