/// <summary> /// 保存序列种子 /// </summary> /// <param name="serialSeed">序列种子</param> /// <returns>受影响的行数</returns> public void Save(SerialSeed serialSeed) { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append("UPDATE dbo.SerialSeeds SET "); sqlBuilder.Append(" [Name] = @Name, "); sqlBuilder.Append(" [Prefix] = @Prefix, "); sqlBuilder.Append(" [Timestamp] = @Timestamp, "); sqlBuilder.Append(" [SerialLength] = @SerialLength, "); sqlBuilder.Append(" [TodayCount] = @TodayCount, "); sqlBuilder.Append(" [Description] = @Description "); sqlBuilder.Append("WHERE Id = @Id "); IDbDataParameter[] parameters = { new SqlParameter("@Id", serialSeed.Id), new SqlParameter("@Name", serialSeed.Name.ToDbValue()), new SqlParameter("@Prefix", serialSeed.Prefix.ToDbValue()), new SqlParameter("@Timestamp", serialSeed.Timestamp.ToDbValue()), new SqlParameter("@SerialLength", serialSeed.SerialLength.ToDbValue()), new SqlParameter("@TodayCount", serialSeed.TodayCount.ToDbValue()), new SqlParameter("@Description", serialSeed.Description.ToDbValue()) }; _SqlHelper.ExecuteNonQuery(sqlBuilder.ToString(), parameters); }
/// <summary> /// 创建序列种子 /// </summary> /// <param name="serialSeed">序列种子</param> public void Create(SerialSeed serialSeed) { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append("SET @NEWID = UUID(); "); sqlBuilder.Append("INSERT INTO SerialSeeds "); sqlBuilder.Append(" (Id, Name, Prefix, Timestamp, SerialLength, TodayCount, Description) "); sqlBuilder.Append("VALUES "); sqlBuilder.Append(" (@NEWID, @Name, @Prefix, @Timestamp, @SerialLength, @TodayCount, @Description); "); sqlBuilder.Append("SELECT @NEWID; "); IDbDataParameter[] parameters = { new MySqlParameter("@Name", serialSeed.Name.ToDbValue()), new MySqlParameter("@Prefix", serialSeed.Prefix.ToDbValue()), new MySqlParameter("@Timestamp", serialSeed.Timestamp.ToDbValue()), new MySqlParameter("@SerialLength", serialSeed.SerialLength.ToDbValue()), new MySqlParameter("@TodayCount", serialSeed.TodayCount.ToDbValue()), new MySqlParameter("@Description", serialSeed.Description.ToDbValue()) }; object result = _SqlHelper.ExecuteScalar(sqlBuilder.ToString(), parameters); Guid serialSeedId = Guid.Parse(result.ToString()); serialSeed.Id = serialSeedId; }
/// <summary> /// 创建序列种子 /// </summary> /// <param name="serialSeed">序列种子</param> public void Create(SerialSeed serialSeed) { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append("INSERT INTO dbo.SerialSeeds "); sqlBuilder.Append(" ([Id], [Name], [Prefix], [Timestamp], [SerialLength], [TodayCount], [Description]) "); sqlBuilder.Append("OUTPUT Inserted.Id "); sqlBuilder.Append("VALUES "); sqlBuilder.Append(" (NEWID(), @Name , @Prefix, @Timestamp, @SerialLength, @TodayCount, @Description) "); IDbDataParameter[] parameters = { new SqlParameter("@Name", serialSeed.Name.ToDbValue()), new SqlParameter("@Prefix", serialSeed.Prefix.ToDbValue()), new SqlParameter("@Timestamp", serialSeed.Timestamp.ToDbValue()), new SqlParameter("@SerialLength", serialSeed.SerialLength.ToDbValue()), new SqlParameter("@TodayCount", serialSeed.TodayCount.ToDbValue()), new SqlParameter("@Description", serialSeed.Description.ToDbValue()) }; object result = _SqlHelper.ExecuteScalar(sqlBuilder.ToString(), parameters); Guid serialSeedId = Guid.Parse(result.ToString()); serialSeed.Id = serialSeedId; }
/// <summary> /// 保存序列种子 /// </summary> /// <param name="serialSeed">序列种子</param> /// <returns>受影响的行数</returns> public void Save(SerialSeed serialSeed) { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append("UPDATE SerialSeeds SET "); sqlBuilder.Append(" \"Name\" = @Name, "); sqlBuilder.Append(" \"Prefix\" = @Prefix, "); sqlBuilder.Append(" \"Timestamp\" = @Timestamp, "); sqlBuilder.Append(" \"SerialLength\" = @SerialLength, "); sqlBuilder.Append(" \"TodayCount\" = @TodayCount, "); sqlBuilder.Append(" \"Description\" = @Description "); sqlBuilder.Append("WHERE \"Id\" = @Id "); IDbDataParameter[] parameters = { new NpgsqlParameter("@Id", serialSeed.Id), new NpgsqlParameter("@Name", serialSeed.Name.ToDbValue()), new NpgsqlParameter("@Prefix", serialSeed.Prefix.ToDbValue()), new NpgsqlParameter("@Timestamp", serialSeed.Timestamp.ToDbValue()), new NpgsqlParameter("@SerialLength", serialSeed.SerialLength.ToDbValue()), new NpgsqlParameter("@TodayCount", serialSeed.TodayCount.ToDbValue()), new NpgsqlParameter("@Description", serialSeed.Description.ToDbValue()) }; _SqlHelper.ExecuteNonQuery(sqlBuilder.ToString(), parameters); }
/// <summary> /// 创建序列种子 /// </summary> /// <param name="serialSeed">序列种子</param> public void Create(SerialSeed serialSeed) { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append("INSERT INTO SerialSeeds "); sqlBuilder.Append(" (\"Id\", \"Name\", \"Prefix\", \"Timestamp\", \"SerialLength\", \"TodayCount\", \"Description\") "); sqlBuilder.Append("VALUES "); sqlBuilder.Append(" (uuid_generate_v4(), @Name, @Prefix, @Timestamp, @SerialLength, @TodayCount, @Description) "); sqlBuilder.Append("RETURNING \"Id\"; "); IDbDataParameter[] parameters = { new NpgsqlParameter("@Name", serialSeed.Name.ToDbValue()), new NpgsqlParameter("@Prefix", serialSeed.Prefix.ToDbValue()), new NpgsqlParameter("@Timestamp", serialSeed.Timestamp.ToDbValue()), new NpgsqlParameter("@SerialLength", serialSeed.SerialLength.ToDbValue()), new NpgsqlParameter("@TodayCount", serialSeed.TodayCount.ToDbValue()), new NpgsqlParameter("@Description", serialSeed.Description.ToDbValue()) }; object result = _SqlHelper.ExecuteScalar(sqlBuilder.ToString(), parameters); Guid serialSeedId = Guid.Parse(result.ToString()); serialSeed.Id = serialSeedId; }
/// <summary> /// 创建序列种子 /// </summary> /// <param name="serialSeed">序列种子</param> public void Create(SerialSeed serialSeed) { string generateIdSql = "SELECT GEN_UUID() FROM rdb$database;"; object result = _SqlHelper.ExecuteScalar(generateIdSql); Guid serialSeedId = Guid.Parse(result.ToString()); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append("INSERT INTO \"SerialSeeds\" "); sqlBuilder.Append(" (\"Id\", \"Name\", \"Prefix\", \"Timestamp\", \"SerialLength\", \"TodayCount\", \"Description\") "); sqlBuilder.Append("VALUES "); sqlBuilder.Append(" (@Id, @Name, @Prefix, @Timestamp, @SerialLength, @TodayCount, @Description); "); IDbDataParameter[] parameters = { new FbParameter("@Id", serialSeedId.ToDbValue()), new FbParameter("@Name", serialSeed.Name.ToDbValue()), new FbParameter("@Prefix", serialSeed.Prefix.ToDbValue()), new FbParameter("@Timestamp", serialSeed.Timestamp.ToDbValue()), new FbParameter("@SerialLength", serialSeed.SerialLength.ToDbValue()), new FbParameter("@TodayCount", serialSeed.TodayCount.ToDbValue()), new FbParameter("@Description", serialSeed.Description.ToDbValue()) }; _SqlHelper.ExecuteNonQuery(sqlBuilder.ToString(), parameters); serialSeed.Id = serialSeedId; }
/// <summary> /// 批量生成序列号 /// </summary> /// <param name="seedName">种子名称</param> /// <param name="prefix">前缀</param> /// <param name="timeFormat">时间格式</param> /// <param name="serialLength">流水长度</param> /// <param name="description">描述</param> /// <param name="count">数量</param> /// <returns>序列号集</returns> public string[] GenerateRange(string seedName, string prefix, string timeFormat, int serialLength, string description, int count) { lock (_SyncLock) { #region # 验证参数 seedName = string.IsNullOrWhiteSpace(seedName) ? string.Empty : seedName; prefix = string.IsNullOrWhiteSpace(prefix) ? string.Empty : prefix; timeFormat = string.IsNullOrWhiteSpace(timeFormat) ? string.Empty : timeFormat; serialLength = serialLength < 1 ? 1 : serialLength; if (count < 1) { throw new ArgumentOutOfRangeException(nameof(count), "数量不可小于1!"); } #endregion string timestamp = string.IsNullOrWhiteSpace(timeFormat) ? string.Empty : DateTime.Now.ToString(timeFormat); SerialSeed serialSeed = this._serialSeedProvider.SingleOrDefault(seedName, prefix, timestamp, serialLength); int initialIndex = serialSeed?.TodayCount ?? 0; if (serialSeed == null) { serialSeed = new SerialSeed(seedName, prefix, timestamp, serialLength, description); serialSeed.UpdateInfo(count); this._serialSeedProvider.Create(serialSeed); } else { serialSeed.UpdateInfo(serialSeed.TodayCount + count); this._serialSeedProvider.Save(serialSeed); } ICollection <string> keys = new HashSet <string>(); for (int index = 1; index <= count; index++) { int serial = initialIndex + index; string serialText = serial.ToString($"D{serialLength}"); StringBuilder keyBuilder = new StringBuilder(); keyBuilder.Append(serialSeed.Prefix); keyBuilder.Append(serialSeed.Timestamp); keyBuilder.Append(serialText); keys.Add(keyBuilder.ToString()); } return(keys.ToArray()); } }
/// <summary> /// 根据SqlDataReader返回对象 /// </summary> /// <param name="reader">SqlDataReader对象</param> /// <returns>实体对象</returns> private SerialSeed ToModel(IDataReader reader) { SerialSeed serialSeed = new SerialSeed { Id = (Guid)reader.ToClsValue("Id"), Name = (string)reader.ToClsValue("Name"), Prefix = (string)reader.ToClsValue("Prefix"), Timestamp = (string)reader.ToClsValue("Timestamp"), SerialLength = (int)reader.ToClsValue("SerialLength"), TodayCount = (int)reader.ToClsValue("TodayCount"), Description = (string)reader.ToClsValue("Description") }; return(serialSeed); }
/// <summary> /// 生成序列号 /// </summary> /// <param name="seedName">种子名称</param> /// <param name="prefix">前缀</param> /// <param name="timeFormat">时间格式</param> /// <param name="serialLength">流水长度</param> /// <param name="description">描述</param> /// <returns>序列号</returns> public string Generate(string seedName, string prefix, string timeFormat, int serialLength, string description) { lock (_SyncLock) { #region # 验证参数 seedName = string.IsNullOrWhiteSpace(seedName) ? string.Empty : seedName; prefix = string.IsNullOrWhiteSpace(prefix) ? string.Empty : prefix; timeFormat = string.IsNullOrWhiteSpace(timeFormat) ? string.Empty : timeFormat; serialLength = serialLength < 1 ? 1 : serialLength; #endregion string timestamp = string.IsNullOrWhiteSpace(timeFormat) ? string.Empty : DateTime.Now.ToString(timeFormat); SerialSeed serialSeed = this._serialSeedProvider.SingleOrDefault(seedName, prefix, timestamp, serialLength); if (serialSeed == null) { serialSeed = new SerialSeed(seedName, prefix, timestamp, serialLength, description); this._serialSeedProvider.Create(serialSeed); } else { serialSeed.UpdateInfo(serialSeed.TodayCount + 1); this._serialSeedProvider.Save(serialSeed); } int serial = serialSeed.TodayCount; string serialText = serial.ToString($"D{serialLength}"); StringBuilder keyBuilder = new StringBuilder(); keyBuilder.Append(serialSeed.Prefix); keyBuilder.Append(serialSeed.Timestamp); keyBuilder.Append(serialText); return(keyBuilder.ToString()); } }