平安校园
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

155 lines
6.2 KiB

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