using System.Globalization;
|
using System.Text;
|
using Admin.NET.Application.Entity;
|
|
namespace Admin.NET.Application
|
{
|
/// <summary>
|
/// 单据号生成帮助类
|
/// </summary>
|
public class SerialUtil
|
{
|
private readonly static object _lockTest = new();
|
private readonly static object _lockPublic = new();
|
|
|
/// <summary>
|
/// 生成编号/单据号
|
/// </summary>
|
/// <param name="SerialType"></param>
|
/// <returns></returns>
|
public static async Task<string> GetSerial(SerialTypeEnum SerialType, SqlSugarRepository<WmsConfigSerialRule> _repRuleDetailRep, SqlSugarRepository<WmsConfigSerialSN> _repSNRep)
|
{
|
string _result = string.Empty;
|
switch (SerialType)
|
{
|
case SerialTypeEnum.测试编号:
|
lock (_lockTest)
|
{
|
_result= GetSerialDetails(SerialType, _repRuleDetailRep, _repSNRep).Result;
|
break;
|
};
|
|
default:
|
lock (_lockPublic)
|
{//为了避免多线程出现编号一致的情况,加上锁,此处为公共锁,只用于不频繁生成编号的,若需要频繁生成,请参考上面Test设置单独锁
|
_result= GetSerialDetails(SerialType, _repRuleDetailRep, _repSNRep).Result;
|
break;
|
}
|
}
|
|
await Task.CompletedTask;
|
return _result;
|
}
|
/// <summary>
|
/// 生成编号/单据号
|
/// </summary>
|
/// <param name="SerialType"></param>
|
/// <returns></returns>
|
private static async Task<string> GetSerialDetails(SerialTypeEnum SerialType, SqlSugarRepository<WmsConfigSerialRule> _repRuleDetailRep, SqlSugarRepository<WmsConfigSerialSN> _repSNRep)
|
{
|
|
var lst =await _repRuleDetailRep.AsQueryable().Where(u => u.SerialType == (int)SerialType).OrderBy(x => x.ItemNo).ToListAsync();
|
|
string[] strs = new string[lst.Count];
|
|
WmsConfigSerialSN sl = new WmsConfigSerialSN();
|
for (int i = 0; i < lst.Count; i++)
|
{
|
if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.UD.ToString())
|
{
|
strs[i] = lst[i].UserDefine ?? string.Empty;//如果数据源是自定义,则取自定义字段
|
}
|
|
if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.BT.ToString())
|
{
|
strs[i] = lst[i].UserDefine ?? string.Empty;//如果数据源是自定义,则取自定义字段
|
}
|
|
if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.Y4.ToString())
|
{
|
strs[i] = DateTime.Now.Year.ToString();
|
}
|
if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.Y2.ToString())
|
{
|
strs[i] = DateTime.Now.Year.ToString().Substring(2, 2);
|
}
|
if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.M2.ToString())
|
{
|
strs[i] = DateTime.Now.Month.ToString("00");
|
}
|
if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.D2.ToString())
|
{
|
strs[i] = DateTime.Now.Day.ToString("00");
|
|
}
|
if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.W2.ToString())
|
{
|
GregorianCalendar gc = new GregorianCalendar();
|
int week = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
|
strs[i] = "W" + week.ToString();
|
|
}
|
if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.SN.ToString())
|
{
|
int cout = lst[i].SerialLength;
|
string c = string.Empty;
|
for (int j = 0; j < cout; j++)
|
{
|
c += "0";
|
}
|
try
|
{
|
if (lst[i].GetData.HasValue && lst[i].GetData == 1)
|
{
|
var sn =await _repSNRep.AsQueryable().Where(x => x.SerialType == lst[i].SerialType && x.CurrentDate == DateTime.Now.Date)
|
.OrderByDescending(x => x.Sn).FirstAsync();
|
if (sn == null)
|
{
|
sl.Sn = 1;
|
}
|
else
|
{
|
sl.Sn = sn.Sn + 1;
|
}
|
|
}
|
else
|
{
|
|
var sn =await _repSNRep.AsQueryable().Where(x => x.SerialType == lst[i].SerialType).OrderByDescending(x => x.Sn).FirstAsync();
|
sl.Sn = sn.Sn + 1;
|
}
|
|
strs[i] = sl.Sn.ToString(c);
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
sl.CurrentDate = DateTime.Now.Date;
|
sl.SerialType = lst[i].SerialType;
|
}
|
}
|
|
StringBuilder WmsConfigSerialSN = new StringBuilder();
|
foreach (var item in strs)
|
{
|
WmsConfigSerialSN.Append(item.ToString());
|
}
|
// await sl.InsertAsync();
|
await _repSNRep.InsertAsync(sl);
|
|
//await repositoryBaseSerialSN.Add(sl);
|
//edm.SaveChanges();
|
|
if (string.IsNullOrEmpty(WmsConfigSerialSN.ToString()))
|
{
|
throw Oops.Oh("生成的编号为空");
|
}
|
|
return WmsConfigSerialSN.ToString();
|
}
|
}
|
}
|