OtherServiceMap.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using FlutterCodeGenerator.Helper;
  2. using FlutterCodeGenerator.Map.Interface;
  3. using FlutterCodeGenerator.Model;
  4. using FlutterCodeGenerator.ModelTypes;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. namespace FlutterCodeGenerator.Map
  10. {
  11. internal class OtherServiceMap : IServiceMap
  12. {
  13. private List<Type> _typeList;
  14. public string ServiceName { get; private set; }
  15. public List<ComplexModelType> UsedComplexModelTypeList { get; }
  16. public OtherServiceMap(List<Type> types)
  17. {
  18. ServiceName = "OtherService";
  19. _typeList = types;
  20. UsedComplexModelTypeList = new List<ComplexModelType>();
  21. GenerateDataCache.Instance.SetCurrentServiceMap(this);
  22. foreach (var type in _typeList)
  23. {
  24. try
  25. {
  26. var parameterModelType = ModelTypeGenerator.Create(type, type.Name, true);
  27. }
  28. catch (Exception ex)
  29. {
  30. Console.WriteLine($"Type is {type.FullName},Error:{ex}");
  31. throw;
  32. }
  33. }
  34. }
  35. public string GetServiceDartString()
  36. {
  37. return null;
  38. }
  39. public string GetServiceModelDartString()
  40. {
  41. var dartString = new StringBuilder();
  42. var importServiceList = new List<string>();
  43. var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
  44. foreach (var modelType in UsedComplexModelTypeList)
  45. {
  46. if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  47. {
  48. if (modelType is EnumModelType enumModelType)
  49. {
  50. if (enumModelType.UserDefinedEnumDictionary.Any(x => x.Key >= 1000))
  51. {
  52. continue;
  53. }
  54. else
  55. {
  56. alreadyGeneratedList.Add(modelType, ServiceName);
  57. dartString.AppendLine(modelType.GetDartString());
  58. }
  59. }
  60. else
  61. {
  62. alreadyGeneratedList.Add(modelType, ServiceName);
  63. dartString.AppendLine(modelType.GetDartString());
  64. }
  65. }
  66. else
  67. {
  68. var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  69. if (importService == null)
  70. {
  71. throw new Exception("Import Service is null");
  72. }
  73. if (importService != ServiceName && !importServiceList.Contains(importService))
  74. {
  75. importServiceList.Add(importService);
  76. }
  77. }
  78. }
  79. if (string.IsNullOrWhiteSpace(dartString.ToString()))
  80. {
  81. return null;
  82. }
  83. var serviceModelDartString = new StringBuilder();
  84. foreach (var importService in importServiceList)
  85. {
  86. serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  87. }
  88. if (importServiceList.Count > 0)
  89. {
  90. serviceModelDartString.AppendLine();
  91. }
  92. var dartStringInfo = dartString.ToString();
  93. if (dartStringInfo.Contains("JsonRpcUtils"))
  94. {
  95. serviceModelDartString.AppendLine(CommonParameters.StringUtils);
  96. serviceModelDartString.AppendLine();
  97. }
  98. if (dartStringInfo.Contains("FJsonConvert"))
  99. {
  100. serviceModelDartString.AppendLine(CommonParameters.StringJsonConvert);
  101. serviceModelDartString.AppendLine();
  102. }
  103. serviceModelDartString.AppendLine(dartStringInfo);
  104. return serviceModelDartString.ToString();
  105. }
  106. }
  107. }