You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Application/Helper/BusinessIDGenHelper.cs

76 lines
2.4 KiB
C#

using Furion;
using Furion.FriendlyException;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using Org.BouncyCastle.Asn1.Tsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Helper
{
public static class BusinessIDGenHelper
{
/// <summary>
/// 生成流水号
/// </summary>
/// <param name="tenantId">租户ID</param>
/// <returns></returns>
public static string GenNext(long tenantId)
{
if (tenantId <= 0)
throw Oops.Oh($"租户ID不能为空");
var _djyTenantParamService = App.GetService<IDjyTenantParamService>();
var paramInfo = _djyTenantParamService
.GetParaCodeWithValue(new[] { "BOOKING_MSG_REFERNO_STARTCODE" }).GetAwaiter().GetResult().FirstOrDefault();
string startCode = string.Empty;
if (paramInfo != null)
{
startCode = paramInfo.ParaValue?.Trim();
}
if (string.IsNullOrWhiteSpace(startCode))
throw Oops.Oh($"当前租户未配置“订舱申请号生成开头代码”租户参数");
string flowno = string.Empty;
var cacheService = App.GetService<ISysCacheService>();
//生成KEY 代码类型-BOOKING_TENANTID_
string cacheKey = $"BOOKING_{tenantId}";
string cacheCalcKey = $"BOOKING_{tenantId}_CALC";
DateTime nowDate = DateTime.Now;
DateTime expireDateTime = new DateTime(nowDate.Year, nowDate.Month, nowDate.Day, 23, 59, 59);
var expireTimeSpan = expireDateTime.Subtract(DateTime.Now).Duration();
var autoIncrementKey = RedisHelper.IncrBy(cacheCalcKey);
if (cacheService.Exists(cacheKey))
{
flowno = $"{startCode}{nowDate.ToString("yyyyMMdd")}{autoIncrementKey.ToString().PadLeft(4, '0')}";
}
else
{
if (autoIncrementKey > 0)
RedisHelper.IncrBy(cacheCalcKey, -autoIncrementKey);
//重新计数
autoIncrementKey = RedisHelper.IncrBy(cacheCalcKey);
cacheService.SetTimeoutAsync(cacheKey, autoIncrementKey, expireTimeSpan).GetAwaiter().GetResult();
}
return flowno;
}
}
}