public static string GetUniqueOrderId(string branchId, IDistributedCache cacheProvider) { int seq = -1; int length = 0; lock (orderSequenceCache) { if (!orderSequenceCache.ContainsKey(branchId)) { orderSequenceCache.Add(branchId, new BranchOrderSequence() { Date = DateTime.Now }); } var seqEntity = orderSequenceCache[branchId]; var cached = cacheProvider.GetInt32(string.Format(BRANCH_ORDER_Sequence_CACHEKEY, branchId)); if (cached != null) { seqEntity.Current = cached.Value; } if (seqEntity.Current >= seqEntity.Max) { seqEntity.Current = 0; seqEntity.Date = DateTime.Now; } seq = seqEntity.Current += 1; length = seqEntity.Max.ToString().Length; cacheProvider.SetInt32(string.Format(BRANCH_ORDER_Sequence_CACHEKEY, branchId), seq); } return(string.Format("{0}{1}", branchId.Substring(2, 4), seq.ToString().PadLeft(length, '0'))); }
/// <summary> /// 为收款记录和付款记录添加序列编号 /// </summary> /// <param name="type"></param> /// <param name="branchId"></param> /// <returns></returns> public static string GenerateFinanceLogSequenceNo(int type, string branchId, IDistributedCache cacheProvider) { int seq = -1; int length = 0; var key = $"{type}_{branchId}"; lock (financeLogSequenceCache) { if (!financeLogSequenceCache.ContainsKey(key)) { financeLogSequenceCache.Add(key, new FinanceLogSequence() { Date = DateTime.Now }); } var seqEntity = financeLogSequenceCache[key]; var cached = cacheProvider.GetInt32(string.Format(BRANCH_FINANCELOG_SEQUENCE_CACHEKEY, type, branchId)); if (cached != null) { seqEntity.Current = cached.Value; } if (seqEntity.Current >= seqEntity.Max) { seqEntity.Current = 0; seqEntity.Date = DateTime.Now; } seq = seqEntity.Current += 1; length = seqEntity.Max.ToString().Length; cacheProvider.SetInt32(string.Format(BRANCH_FINANCELOG_SEQUENCE_CACHEKEY, type, branchId), seq); } return(seq.ToString().PadLeft(length, '0')); }