170 lines
5.1 KiB
C#
170 lines
5.1 KiB
C#
using System.Text.RegularExpressions;
|
||
using WcsMain.DataBase.Dao;
|
||
using WcsMain.DataBase.TableEntity;
|
||
using WcsMain.Constant.WcsAttribute.AutoFacAttribute;
|
||
|
||
namespace WcsMain.Common.DataService;
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
[Component]
|
||
public class DataBaseData(AppSettingsDao settingsDao)
|
||
{
|
||
private AppSettingsDao _settingsDao = settingsDao;
|
||
|
||
/// <summary>
|
||
/// 获取一个设置值
|
||
/// </summary>
|
||
/// <param name="settingKey"></param>
|
||
/// <returns></returns>
|
||
public string? GetSetting(string settingKey)
|
||
{
|
||
List<AppSettings>? settings = _settingsDao.GetSetting(settingKey);
|
||
if (settings == default || settings.Count < 1)
|
||
{
|
||
return string.Empty;
|
||
}
|
||
return settings[0].SettingValue;
|
||
}
|
||
|
||
|
||
private static readonly object getNewPlcTaskIdLock = new();
|
||
/// <summary>
|
||
/// 获取新的 PLC 任务号,发生异常则返回默认值
|
||
/// </summary>
|
||
/// <param name="count"></param>
|
||
/// <returns></returns>
|
||
public int[]? GetNewPlcTaskId(int count)
|
||
{
|
||
lock (getNewPlcTaskIdLock)
|
||
{
|
||
string? oldPlcTaskIdstr = GetSetting("plc_task_id");
|
||
if (string.IsNullOrEmpty(oldPlcTaskIdstr)) { return default; }
|
||
if (!Regex.IsMatch(oldPlcTaskIdstr, "^\\d+$")) { return default; }
|
||
int oldPlcTaskId = Convert.ToInt32(oldPlcTaskIdstr);
|
||
if (oldPlcTaskId > 2000000000)
|
||
{
|
||
oldPlcTaskId = 100000;
|
||
}
|
||
oldPlcTaskId++;
|
||
int[] ints = new int[count];
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
ints[i] = oldPlcTaskId + i;
|
||
}
|
||
int updateNewTaskPlcId = _settingsDao.Update(new AppSettings() { SettingKey = "plc_task_id", SettingValue = ints.Last().ToString() });
|
||
if (updateNewTaskPlcId < 0) { return default; }
|
||
return ints;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 获取一个PldId
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int? GetNewPlcTaskId()
|
||
{
|
||
var ints = GetNewPlcTaskId(1);
|
||
if (ints == default || ints.Length < 1) { return default; }
|
||
return ints.First();
|
||
}
|
||
|
||
#region 返回一个 UUID,以时间 yyyyMMdd 形式
|
||
|
||
private static readonly object getNewUUIDLock = new();
|
||
private string lastUUID = string.Empty;
|
||
private string lasTimeTick = DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
||
private ushort sortUUID = 0;
|
||
/// <summary>
|
||
/// 返回一个唯一识别号
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string GetNewUUID()
|
||
{
|
||
lock (getNewUUIDLock)
|
||
{
|
||
while (true)
|
||
{
|
||
string? wcsId = CommonData.AppConfig.WcsId;
|
||
if (string.IsNullOrEmpty(wcsId))
|
||
{
|
||
wcsId = "0";
|
||
}
|
||
string timeTick = DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
||
if (timeTick != lasTimeTick)
|
||
{
|
||
lasTimeTick = timeTick;
|
||
sortUUID = 0;
|
||
}
|
||
string idNo = wcsId.PadLeft(4, '0');
|
||
string sort = sortUUID.ToString().PadLeft(5, '0');
|
||
string newUUID = $"{timeTick}{idNo}{sort}";
|
||
sortUUID++;
|
||
if (sortUUID > 60000)
|
||
{
|
||
sortUUID = 0;
|
||
}
|
||
if (newUUID != lastUUID)
|
||
{
|
||
lastUUID = newUUID;
|
||
return newUUID;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
#region 返回一个 UUID,以时间戳形式
|
||
|
||
private static readonly object getNewUUIDLock2 = new();
|
||
private string lastUUID2 = string.Empty;
|
||
private string lasTimeTick2 = DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
||
private ushort sortUUID2 = 0;
|
||
/// <summary>
|
||
/// 返回一个唯一识别号 以时间戳为基础
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
/// <remarks>
|
||
/// 这方法产生的ID会短一点,但是单位时间内产生的数量较少
|
||
/// </remarks>
|
||
public string GetNewUUID2()
|
||
{
|
||
lock (getNewUUIDLock2)
|
||
{
|
||
while (true)
|
||
{
|
||
string? wcsId = CommonData.AppConfig.WcsId;
|
||
if (string.IsNullOrEmpty(wcsId))
|
||
{
|
||
wcsId = "0";
|
||
}
|
||
string timeTick = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
|
||
if (timeTick != lasTimeTick2)
|
||
{
|
||
lasTimeTick2 = timeTick;
|
||
sortUUID2 = 0;
|
||
}
|
||
string idNo = wcsId.PadLeft(3, '0');
|
||
string sort = sortUUID2.ToString().PadLeft(3, '0');
|
||
string newUUID = $"{timeTick}{idNo}{sort}";
|
||
sortUUID2++;
|
||
if (sortUUID2 > 900)
|
||
{
|
||
sortUUID2 = 0;
|
||
}
|
||
if (newUUID != lastUUID2)
|
||
{
|
||
lastUUID2 = newUUID;
|
||
return newUUID;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
} |