public void ReturnCodingNo(string progId, string prefix, string codingNo) { string key = string.Format("{0}/t{1}", progId, prefix); object lockItem = lockObjDic.GetOrAdd(key, new object()); lock (lockItem) { CodingNoValue codingNoValue = this.Get <CodingNoValue>(key); if (codingNoValue != null) { codingNoValue.Unused.Enqueue(codingNo); } //增加了新的ReturnCode时,需要将值Set进Redis缓存。 原先使用MemoryCahche不需要此操作 Zhangkj 20170303 this.Set <CodingNoValue>(key, codingNoValue, new TimeSpan(0, 180, 0)); } }
public string GetCodingNo(string progId, string fieldName, string prefix, int serialLen, LibDataAccess dataAccess) { string maxNo = string.Empty; string key = string.Format("{0}/t{1}", progId, prefix); object lockItem = lockObjDic.GetOrAdd(key, new object()); lock (lockItem) { CodingNoValue codingNoValue = this.Get <CodingNoValue>(key); if (codingNoValue == null) { string curSerial = string.Empty; int len = prefix.Length + serialLen; SqlBuilder sqlBuilder = new SqlBuilder(progId); string sql = sqlBuilder.GetQuerySql(0, string.Format("A.{0}", fieldName), string.Format("A.{0} Like '{1}%'", fieldName, prefix), string.Format("A.{0} DESC", fieldName)); if (dataAccess == null) //在单据的保存中,有可能产生新的单据,dataAccess必须是原始事务的dataAccess,否则可能出现死锁 { dataAccess = new LibDataAccess(); } int serial = 0; using (IDataReader reader = dataAccess.ExecuteDataReader(sql)) { while (reader.Read()) { string temp = reader.GetString(0); if (temp.Length != len) { continue; } if (!int.TryParse(temp.Substring(prefix.Length, serialLen), out serial)) { continue; } curSerial = temp; break; } } int value = 0; if (!string.IsNullOrEmpty(curSerial)) { int.TryParse(curSerial.Substring(prefix.Length, serialLen), out value); } codingNoValue = new CodingNoValue() { MaxValue = value }; } if (codingNoValue.Unused.Count > 0) { maxNo = codingNoValue.Unused.Dequeue(); this.Set(key, codingNoValue, new TimeSpan(0, 180, 0));//Zhangkj 20170303 修改Unused值后需要Set回Redis缓存 } else { codingNoValue.MaxValue++; this.Set(key, codingNoValue, new TimeSpan(0, 180, 0)); maxNo = string.Format("{0}{1}", prefix, codingNoValue.MaxValue.ToString().PadLeft(serialLen, '0')); } } return(maxNo); }