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
{
///
/// 生成流水号
///
/// 租户ID
///
public static string GenNext(long tenantId)
{
if (tenantId <= 0)
throw Oops.Oh($"租户ID不能为空");
var _djyTenantParamService = App.GetService();
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();
//生成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;
}
}
}