using Rhea.Common;
|
using System;
|
using System.Collections.Generic;
|
using System.Data.Entity;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using Tiger.IBusiness;
|
using Tiger.Model;
|
|
namespace Tiger.Business.DbCache
|
{
|
/// <summary>
|
/// 不良代码数据库数据缓存
|
/// </summary>
|
public class MesDefectCache : IMesDefectCache
|
{
|
#region Variables
|
private WhileThread AutoUpdateThread;
|
private DateTime LastUpdateTime = DateTime.MinValue;
|
#endregion
|
|
#region Propertys
|
public string Id { get; set; } = Guid.NewGuid().ToString("N");
|
public string Tag { get; set; } = "MesDefectCache";
|
public string Name { get; set; } = "MesDefectCache";
|
public bool IsRunning { get; set; }
|
private List<BAS_DEFECT_GRP> _groups = new();
|
public List<BAS_DEFECT_GRP> Groups { get { Update(); return _groups; } set => _groups = value; }
|
private List<BAS_DEFECT> _defects = new();
|
public List<BAS_DEFECT> Defects { get { Update(); return _defects; } set => _defects = value; }
|
public BAS_DEFECT this[string code, string group = null] { get { Update(); return _defects.FirstOrDefault(q => q.DFT_CODE == code && (group.IsNullOrEmpty() || q.DFTG_CODE == group)); } }
|
#endregion
|
|
#region Functions
|
/// <summary>
|
/// 启动自动更新缓存
|
/// </summary>
|
public void Start()
|
{
|
try
|
{
|
AutoUpdateThread = new(AutoUpdate);
|
AutoUpdateThread.Start();
|
Logger.Default.Info("Start MesDefect Cache Auto Update Thread");
|
}
|
catch (System.Exception ex)
|
{
|
Logger.Default.Fatal(ex, "Start MesDefect Cache Auto Update Thread Exception");
|
}
|
}
|
|
/// <summary>
|
/// 关闭自动更新缓存
|
/// </summary>
|
public void Stop()
|
{
|
try
|
{
|
AutoUpdateThread?.Stop();
|
Logger.Console.Info("Stop MesDefect Cache Auto Update Thread");
|
}
|
catch (System.Exception ex)
|
{
|
Logger.Console.Fatal(ex, "Stop MesDefect Cache Auto Update Thread Exception");
|
}
|
}
|
|
/// <summary>
|
/// 更新数据缓存
|
/// </summary>
|
public void Update()
|
{
|
var lastUpdate = Biz.Db.Queryable<BAS_DEFECT>().Max(q => q.UPDATE_TIME);
|
if (LastUpdateTime < lastUpdate)
|
{
|
_groups = Biz.Db.Queryable<BAS_DEFECT_GRP>().IncludesAllFirstLayer().ToList();
|
var defectList = new List<BAS_DEFECT>();
|
foreach (var group in _groups)
|
{
|
defectList.AddRange(group.Defects);
|
}
|
_defects = defectList;
|
LastUpdateTime = defectList.Max(q => q.UPDATE_TIME);
|
Logger.Console.Info($"Get MesDefect successful, total {Defects.Count} params in {Groups.Count} groups, last update time is {LastUpdateTime:yyyy-MM-dd HH:mm:ss}");
|
}
|
}
|
|
/// <summary>
|
/// 自动更新
|
/// </summary>
|
private void AutoUpdate()
|
{
|
try
|
{
|
Update();
|
}
|
catch (System.Exception ex)
|
{
|
Logger.Console.Fatal(ex, "MesDefect Cache Auto Update Exception");
|
LastUpdateTime = DateTime.MinValue;
|
}
|
|
//休眠30分钟
|
Thread.Sleep(30 * 60 * 1000);//
|
}
|
|
|
#endregion
|
}
|
}
|