using DS.Module.Core; using DS.Module.Core.Extensions; using DS.Module.UserModule; using DS.WMS.Core.System.Dtos; using DS.WMS.Core.System.Entity; using DS.WMS.Core.System.Interface; using Mapster; using Microsoft.Extensions.DependencyInjection; using SqlSugar; namespace DS.WMS.Core.System.Method; public class SequenceService : ISequenceService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; private readonly IUser user; /// /// /// /// public SequenceService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); } /// /// 列表 /// /// /// public DataResult> GetListByPage(PageRequest request) { //序列化查询条件 var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition); var data = db.Queryable() .LeftJoin((a, b) => a.PermissionId == b.Id) .Where(whereList) .Select().ToQueryPage(request.PageCondition); return data; } /// /// 编辑 /// /// /// public DataResult EditSequence(SequenceReq req) { if (req.Id == 0) { if (db.Queryable().Where(x => x.PermissionId == req.PermissionId && x.OrderNo == req.OrderNo).Any()) { return DataResult.Failed("系统编码已存在!", MultiLanguageConst.SequenceExist); } var data = req.Adapt(); var entity = db.Insertable(data).ExecuteReturnEntity(); return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess); } else { var info = db.Queryable().Where(x => x.Id == req.Id).First(); info = req.Adapt(info); db.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess); } } /// /// 详情 /// /// /// public DataResult GetSequenceInfo(string id) { var data = db.Queryable() .LeftJoin((a, b) => a.PermissionId == b.Id) .Where(a => a.Id == long.Parse(id)) .Select() .First(); return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess); } /// /// 获取最新业务单据编码 /// /// public DataResult GetSequenceNextTest(string id) { //生成编号 string sequenceNewNo = ""; #region 获取序号生成器属性 //获取序号生成器属性 var sequence = db.Queryable() .First(u => u.Id == long.Parse(id)); if (sequence != null) { var ruleList = db.Queryable().OrderBy(x => x.OrderNo) .Where(u => u.SequenceId == sequence.Id && u.Status == StatusEnum.Enable).ToList(); if (ruleList.Any()) { int delimiterNum = 0; for (global::System.Int32 i = 0; i < ruleList.Count; i++) { var item = ruleList[i]; 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; } } return DataResult.Success(sequenceNewNo); } else { return DataResult.Failed("未查询到业务编码对应的编码规则配置, 请检查编码规则配置!"); } } else { return DataResult.Failed("不存在的单据编码!"); } #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; } }