116 lines
3.5 KiB
C#
116 lines
3.5 KiB
C#
using System.Text.RegularExpressions;
|
|
using WcsMain.Common;
|
|
using WcsMain.DataBase.Dao;
|
|
using WcsMain.DataBase.TableEntity;
|
|
using WcsMain.WcsAttribute.AutoFacAttribute;
|
|
|
|
namespace WcsMain.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();
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |