feat: 😀优化种子数据生成
This commit is contained in:
parent
ce225e9d86
commit
d921db4f83
@ -445,7 +445,32 @@ public class SysDatabaseService : IDynamicApiController, ITransient
|
||||
propertyList[0] = temp;
|
||||
}
|
||||
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
|
||||
var recordList = JsonConvert.SerializeObject(records, Formatting.Indented, timeConverter);
|
||||
// var recordList = JsonConvert.SerializeObject(records, Formatting.Indented, timeConverter);
|
||||
|
||||
// 拼接种子数据
|
||||
var recordList = records.Select(obj => string.Join(", ", propertyList.Select(prop =>
|
||||
{
|
||||
var propType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
|
||||
object value = prop.GetValue(obj);
|
||||
if (value == null) value = "null";
|
||||
else if (propType == typeof(string))
|
||||
{
|
||||
value = $"@\"{value}\"";
|
||||
}
|
||||
else if (propType.IsEnum)
|
||||
{
|
||||
value = $"{propType.Name}.{value}";
|
||||
}
|
||||
else if (propType == typeof(bool))
|
||||
{
|
||||
value = (bool)value ? "true" : "false";
|
||||
}
|
||||
else if (propType == typeof(DateTime))
|
||||
{
|
||||
value = $"DateTime.Parse(\"{((DateTime)value):yyyy-MM-dd HH:mm:ss.fff}\")";
|
||||
}
|
||||
return $"{prop.Name}={value}";
|
||||
}))).ToList();
|
||||
|
||||
var tContent = await File.ReadAllTextAsync(templatePath);
|
||||
var data = new
|
||||
|
||||
@ -20,26 +20,10 @@ public class @(Model.SeedDataName): ISqlSugarEntitySeedData<@(Model.EntityName)>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<@(Model.EntityName)> HasData()
|
||||
{
|
||||
string recordList = @@"
|
||||
@(Model.RecordList.Replace("\"","\"\"").Replace("\n", "\n\t\t\t"))
|
||||
";
|
||||
List<@(Model.EntityName)> records = Newtonsoft.Json.JsonConvert.DeserializeObject<List<@(Model.EntityName)>>(recordList);
|
||||
@if (Model.JsonIgnoreInfo.Count > 0) {
|
||||
@:
|
||||
@:#region 处理 JsonIgnore 的Property
|
||||
@:
|
||||
@foreach (var jii in Model.JsonIgnoreInfo){
|
||||
@foreach (var j in jii){
|
||||
@:records[@j.RecordIndex].@(j.Name) = @(j.Value);
|
||||
}
|
||||
@:
|
||||
return new List<@(Model.EntityName)> {
|
||||
@foreach (var record in Model.RecordList) {
|
||||
@:new() { @record },
|
||||
}
|
||||
@:#endregion
|
||||
}
|
||||
|
||||
// 后处理数据的特殊字段
|
||||
//for (int i = 0; i < records.Count; i++) { }
|
||||
|
||||
return records;
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user