OtherServiceMap.cs 3.5 KB

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