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.

228 lines
8.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Op.Interface;
using DS.WMS.Core.System.Entity;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Op.Method
{
public class OpCommonService : IOpCommonService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public OpCommonService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
}
#region 获取单据编码
/// <summary>
/// 获取最新业务单据编码
/// </summary>
/// <returns></returns>
public DataResult<string> GetSequenceNext<T>()
{
//生成编号
string sequenceNewNo = "";
#region 获取序号生成器属性
// if (string.IsNullOrWhiteSpace(sequenceName))
// {
// result.ErrMsg = "参数错误:业务编码编号";
// return result.ErrMsg;
// }
var moduleName = typeof(T).Name.ToLower();
//获取序号生成器属性
var sequence = db.Queryable<SysSequence>()
.First(u => u.PermissionEntity.ToLower() == moduleName && u.Status == StatusEnum.Enable);
if (sequence != null)
{
var ruleList = db.Queryable<SysSequenceRule>()
.Where(u => u.SequenceId == sequence.Id && u.Status == StatusEnum.Enable).ToList();
if (ruleList.Any())
{
int delimiterNum = 0;
foreach (var item in ruleList)
{
delimiterNum++;
switch (item.RuleType)
{
case "const": //常量方式
sequenceNewNo += item.RuleValue;
break;
case "shortdate": //短日期 年2位月2位日期2位
sequenceNewNo += DateTime.Now.ToString("yyyyMMdd").Substring(2);
break;
case "date": //日期年4位
sequenceNewNo += DateTime.Now.ToString("yyyyMMdd");
break;
case "ydate": //年月年4位月2位
sequenceNewNo += DateTime.Now.ToString("yyyyMMdd").Substring(0, 6);
break;
case "sydate": //年月年2位月2位
sequenceNewNo += DateTime.Now.ToString("yyyyMMdd").Substring(2, 4);
break;
case "timestamp": //日期时间精确到毫秒
sequenceNewNo += DateTime.Now.ToString("yyyyMMddHHmmssffff");
break;
case "number": //计数,流水号
int num = CurrentReset(sequence, item);
//计数拼接
sequenceNewNo += NumberingSeqRule(item, num).ToString();
//更新当前序号,
sequence.CurrentNo = num;
break;
case "guid": //Guid
sequenceNewNo += GuidHelper.NewGuidFormatN();
break;
case "random": //随机数
Random random = new Random();
string strMax = "9".ToString().PadLeft(item.RuleValue.Length, '9');
string strRandom = random.Next(item.RuleValue.ToInt(), strMax.ToInt()).ToString(); //生成随机编号
sequenceNewNo += strRandom;
break;
}
if (!string.IsNullOrEmpty(sequence.SequenceDelimiter) && delimiterNum != ruleList.Count())
{
sequenceNewNo += sequence.SequenceDelimiter;
}
}
//当前编号
sequence.CurrentCode = sequenceNewNo;
sequence.CurrentReset = DateTime.Now.ToString("yyyyMMdd");
db.Updateable(sequence).ExecuteCommand();
return DataResult<string>.Success(sequenceNewNo);
}
else
{
return DataResult<string>.Failed("未查询到业务编码对应的编码规则配置, 请检查编码规则配置!");
}
}
else
{
return DataResult<string>.Failed("请定义" + moduleName + "的单据编码!");
}
#endregion
}
/// <summary>
/// 计数 方式 重置规则
/// </summary>
/// <param name="seq"></param>
/// <param name="seqRule"></param>
/// <returns></returns>
private static int CurrentReset(SysSequence seq, SysSequenceRule seqRule)
{
int newNo = 0, ruleNo = 0;
try
{
ruleNo = seqRule.RuleValue.ToInt();
}
catch (Exception ex)
{
newNo = 1;
// Log4NetHelper.Error(ex.Message, ex);
}
switch (seq.SequenceReset)
{
case "D": //每天重置
if (!string.IsNullOrEmpty(seq.CurrentReset) && seq.CurrentReset != DateTime.Now.ToString("yyyyMMdd"))
{
newNo = 1;
}
break;
case "M": //每月重置
if (!string.IsNullOrWhiteSpace(seq.CurrentReset))
{
if (!seq.CurrentReset.Contains(DateTime.Now.ToString("yyyyMM")))
{
newNo = ruleNo;
}
}
else
{
newNo = 1;
}
break;
case "Y": //每年重置
if (!string.IsNullOrWhiteSpace(seq.CurrentReset))
{
if (!seq.CurrentReset.Contains(DateTime.Now.ToString("yyyy")))
{
newNo = ruleNo;
}
}
else
{
newNo = 1;
}
break;
}
if (newNo == 0)
{
if (seq.CurrentNo == 0)
{
newNo = ruleNo;
}
else
{
//当前序号+步长
newNo = seq.CurrentNo + seq.Step;
}
}
return newNo;
}
/// <summary>
/// 计数规则
/// </summary>
/// <param name="seqRule"></param>
/// <param name="code"></param>
/// <returns></returns>
private static string NumberingSeqRule(SysSequenceRule seqRule, int code)
{
string str = "";
if (seqRule.PaddingSide == "Left")
{
str += code.ToString().PadLeft(seqRule.PaddingWidth, seqRule.PaddingChar.ToChar());
}
if (seqRule.PaddingSide == "Right")
{
str += code.ToString().PadRight(seqRule.PaddingWidth, seqRule.PaddingChar.ToChar());
}
return str;
}
#endregion
}
}