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; /// /// /// /// public OpCommonService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); saasService = _serviceProvider.GetRequiredService(); } #region 获取单据编码 /// /// 获取最新业务单据编码 /// /// public DataResult GetSequenceNext() { //生成编号 string sequenceNewNo = ""; #region 获取序号生成器属性 // if (string.IsNullOrWhiteSpace(sequenceName)) // { // result.ErrMsg = "参数错误:业务编码编号"; // return result.ErrMsg; // } var moduleName = typeof(T).Name.ToLower(); //获取序号生成器属性 var sequence = db.Queryable() .First(u => u.PermissionEntity.ToLower() == moduleName && u.Status == StatusEnum.Enable); if (sequence != null) { var ruleList = db.Queryable() .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.Success(sequenceNewNo); } else { return DataResult.Failed("未查询到业务编码对应的编码规则配置, 请检查编码规则配置!"); } } else { return DataResult.Failed("请定义" + moduleName + "的单据编码!"); } #endregion } /// /// 计数 方式 重置规则 /// /// /// /// 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; } /// /// 计数规则 /// /// /// /// 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 } }