平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SeedDataUtil.cs 6.2 KiB

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 
  2. //
  3. using System.Text.RegularExpressions;
  4. namespace SafeCampus.SqlSugar;
  5. /// <summary>
  6. /// 种子数据工具类
  7. /// </summary>
  8. public class SeedDataUtil
  9. {
  10. public static List<T> GetSeedData<T>(string jsonName)
  11. {
  12. var seedData = new List<T>();//种子数据结果
  13. var basePath = AppContext.BaseDirectory;//获取项目目录
  14. var json = basePath.CombinePath("SeedData", "Json", jsonName);//获取文件路径
  15. var dataString = FileHelper.ReadFile(json);//读取文件
  16. if (!string.IsNullOrEmpty(dataString))//如果有内容
  17. {
  18. //字段没有数据的替换成null
  19. dataString = dataString.Replace("\"\"", "null");
  20. #region 针对导出的json字符串嵌套json字符串如 "DefaultDataScope": "{\"Level\":5,\"ScopeCategory\":\"SCOPE_ALL\",\"ScopeDefineOrgIdList\":[]}"
  21. //正则匹配"ConfigValue": "[{开头的字符串以]"结尾
  22. var matcheDefaultDataScope = Regex.Matches(dataString, "\"DefaultDataScope\": \"\\{.*?\\}\"");
  23. foreach (Match match in matcheDefaultDataScope)
  24. {
  25. //获取匹配的值
  26. var value = match.Value;
  27. //将匹配的值替换成"ConfigValue": "{XXX}"
  28. //字符串是\"的替换成"
  29. var newValue = value.Replace("\\\"", "\"");
  30. //字符串是\{替换成{
  31. newValue = newValue.Replace("\"{", "{");
  32. //字符串是}"的替换成}
  33. newValue = newValue.Replace("}\"", "}");
  34. dataString = dataString.Replace(value, newValue);
  35. }
  36. #endregion 针对导出的json字符串嵌套json字符串如 "DefaultDataScope": "{\"Level\":5,\"ScopeCategory\":\"SCOPE_ALL\",\"ScopeDefineOrgIdList\":[]}"
  37. #region Sys_Org
  38. //如果T是Sys_Org
  39. var nameofT = typeof(T).Name;//获取类型名称
  40. if (nameofT == "SysOrg")
  41. {
  42. //字段是"[的替换成[
  43. dataString = dataString.Replace("\"[", "[");
  44. //字段是]"的替换成]
  45. dataString = dataString.Replace("]\"", "]");
  46. }
  47. #endregion
  48. //将json字符串转为实体,这里ExtJson可以正常转换为字符串
  49. var seedDataRecord1 = dataString.ToJsonEntity<SeedDataRecords<T>>();
  50. //正则匹配"ConfigValue": "[{开头的字符串以]"结尾
  51. var matches = Regex.Matches(dataString, "\"ConfigValue\": \"\\[\\{.*?\\}\\]\"");
  52. foreach (Match match in matches)
  53. {
  54. //获取匹配的值
  55. var value = match.Value;
  56. //将匹配的值替换成"ConfigValue": "null"
  57. dataString = dataString.Replace(value, "\"ConfigValue\": \"null\"");
  58. }
  59. #region 针对导出的json字符串嵌套json字符串如 "DefaultDataScope": "{\"Level\":5,\"ScopeCategory\":\"SCOPE_ALL\",\"ScopeDefineOrgIdList\":[]}"
  60. //字符串是\"的替换成"
  61. dataString = dataString.Replace("\\\"", "\"");
  62. //字符串是\{替换成{
  63. dataString = dataString.Replace("\"{", "{");
  64. //字符串是}"的替换成}
  65. dataString = dataString.Replace("}\"", "}");
  66. //将json字符串转为实体,这里ExtJson会转为null,替换字符串把ExtJson值变为实体类型而实体类是string类型
  67. var seedDataRecord2 = dataString.ToJsonEntity<SeedDataRecords<T>>();
  68. #endregion 针对导出的json字符串嵌套json字符串如 "DefaultDataScope": "{\"Level\":5,\"ScopeCategory\":\"SCOPE_ALL\",\"ScopeDefineOrgIdList\":[]}"
  69. //遍历seedDataRecord2
  70. for (var i = 0; i < seedDataRecord2.Records.Count; i++)
  71. {
  72. #region 处理ExtJosn
  73. //获取ExtJson属性
  74. var propertyExtJson = typeof(T).GetProperty(nameof(PrimaryKeyEntity.ExtJson));
  75. if (propertyExtJson != null)
  76. {
  77. //获取ExtJson的值
  78. var extJson = propertyExtJson.GetValue(seedDataRecord2.Records[i])?.ToString();
  79. // 如果ExtJson不为空并且包含NullableDictionary表示序列化失败了
  80. if (!string.IsNullOrEmpty(extJson) && extJson.Contains("NullableDictionary"))
  81. {
  82. //设置ExtJson为seedDataRecord1对应的值
  83. extJson = propertyExtJson.GetValue(seedDataRecord1.Records[i])?.ToString();
  84. //seedDataRecord2赋值seedDataRecord1的ExtJson
  85. propertyExtJson.SetValue(seedDataRecord2.Records[i], extJson);
  86. }
  87. }
  88. #endregion 处理ExtJosn
  89. #region 处理ConfigValue
  90. //获取ExtJson属性
  91. var propertyConfigValue = typeof(T).GetProperty("ConfigValue");
  92. if (propertyConfigValue != null)
  93. {
  94. //获取configValue的值
  95. var configValue = propertyConfigValue.GetValue(seedDataRecord2.Records[i])?.ToString();
  96. // 如果configValue不为空并且包含NullableDictionary表示序列化失败了
  97. if (!string.IsNullOrEmpty(configValue) && (configValue.Contains("NullableDictionary") || configValue == "null"))
  98. {
  99. //设置ExtJson为seedDataRecord1对应的值
  100. configValue = propertyConfigValue.GetValue(seedDataRecord1.Records[i])?.ToString();
  101. //seedDataRecord2赋值seedDataRecord1的ExtJson
  102. propertyConfigValue.SetValue(seedDataRecord2.Records[i], configValue);
  103. }
  104. }
  105. #endregion 处理ConfigValue
  106. }
  107. //种子数据赋值
  108. seedData = seedDataRecord2.Records;
  109. }
  110. return seedData;
  111. }
  112. }
  113. /// <summary>
  114. /// 种子数据格式实体类,遵循Navicat导出json格式
  115. /// </summary>
  116. /// <typeparam name="T"></typeparam>
  117. public class SeedDataRecords<T>
  118. {
  119. /// <summary>
  120. /// 数据
  121. /// </summary>
  122. public List<T> Records { get; set; }
  123. }