ServiceMap.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 ServiceMap : IServiceMap
  12. {
  13. private List<string> _userDefinedComplexReturnTypeList;
  14. private string _serviceName;
  15. private List<MethodMap> _methodMapList;
  16. public List<ComplexModelType> UsedComplexModelTypeList { get; }
  17. public ServiceMap(Type type)
  18. {
  19. UsedComplexModelTypeList = new List<ComplexModelType>();
  20. GenerateDataCache.Instance.SetCurrentServiceMap(this);
  21. _methodMapList = new List<MethodMap>();
  22. var methodsList = type.GetMethods();
  23. _serviceName = type.Name[1..];
  24. foreach (var method in methodsList)
  25. {
  26. var methodMap = new MethodMap(method);
  27. _methodMapList.Add(methodMap);
  28. }
  29. }
  30. public string GetServiceModelDartString()
  31. {
  32. var dartString = new StringBuilder();
  33. var importServiceList = new List<string>();
  34. var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
  35. foreach (var modelType in UsedComplexModelTypeList)
  36. {
  37. if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  38. {
  39. alreadyGeneratedList.Add(modelType, _serviceName);
  40. dartString.AppendLine(modelType.GetDartString());
  41. }
  42. else
  43. {
  44. var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  45. if (importService == null)
  46. {
  47. throw new Exception("Import Service is null");
  48. }
  49. if (importService != _serviceName && !importServiceList.Contains(importService))
  50. {
  51. importServiceList.Add(importService);
  52. }
  53. }
  54. }
  55. if (string.IsNullOrWhiteSpace(dartString.ToString()))
  56. {
  57. return null;
  58. }
  59. var serviceModelDartString = new StringBuilder();
  60. foreach (var importService in importServiceList)
  61. {
  62. serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  63. }
  64. if (importServiceList.Count > 0)
  65. {
  66. serviceModelDartString.AppendLine();
  67. }
  68. var dartStringInfo = dartString.ToString();
  69. if (dartStringInfo.Contains("JsonRpcUtils"))
  70. {
  71. serviceModelDartString.AppendLine(CodeGenerator.StringUtils);
  72. serviceModelDartString.AppendLine();
  73. }
  74. if (dartStringInfo.Contains("FJsonConvert"))
  75. {
  76. serviceModelDartString.AppendLine(CodeGenerator.StringJsonConvert);
  77. serviceModelDartString.AppendLine();
  78. }
  79. serviceModelDartString.AppendLine(dartStringInfo);
  80. return serviceModelDartString.ToString();
  81. }
  82. public string GetServiceDartString()
  83. {
  84. var serviceDartString = new StringBuilder();
  85. var importServiceList = new List<string>();
  86. var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
  87. foreach (var methodMap in _methodMapList)
  88. {
  89. string importService = null;
  90. foreach (var modelType in methodMap.ParameterModelTypes)
  91. {
  92. if (modelType is ComplexModelType)
  93. {
  94. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  95. if (importService == null)
  96. {
  97. throw new Exception("Import Service is null");
  98. }
  99. if (importService != _serviceName && !importServiceList.Contains(importService))
  100. {
  101. importServiceList.Add(importService);
  102. }
  103. }
  104. }
  105. var returnModelType = methodMap.ReturnParameterModelType;
  106. if (returnModelType is ComplexModelType)
  107. {
  108. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == returnModelType.ParameterType.Name && x.Key.ParameterType.Namespace == returnModelType.ParameterType.Namespace).Value;
  109. if (importService == null)
  110. {
  111. throw new Exception("Import Service is null");
  112. }
  113. if (importService != _serviceName && !importServiceList.Contains(importService))
  114. {
  115. importServiceList.Add(importService);
  116. }
  117. }
  118. else if (returnModelType is ListModelType listModelType)
  119. {
  120. var subReturnType = listModelType.GenericArgumentModelType;
  121. if (subReturnType is ComplexModelType)
  122. {
  123. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  124. if (importService == null)
  125. {
  126. throw new Exception("Import Service is null");
  127. }
  128. if (importService != _serviceName && !importServiceList.Contains(importService))
  129. {
  130. importServiceList.Add(importService);
  131. }
  132. }
  133. }
  134. else if (returnModelType is ArrayModelType arrayModelType)
  135. {
  136. var subReturnType = arrayModelType.Child;
  137. if (subReturnType is ComplexModelType)
  138. {
  139. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  140. if (importService == null)
  141. {
  142. throw new Exception("Import Service is null");
  143. }
  144. if (importService != _serviceName && !importServiceList.Contains(importService))
  145. {
  146. importServiceList.Add(importService);
  147. }
  148. }
  149. }
  150. else if (returnModelType is DictionaryModelType dictionaryModelType)
  151. {
  152. foreach (var subReturnType in dictionaryModelType.GenericArgumentModelTypeList)
  153. {
  154. if (subReturnType is ComplexModelType)
  155. {
  156. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  157. if (importService == null)
  158. {
  159. throw new Exception("Import Service is null");
  160. }
  161. if (importService != _serviceName && !importServiceList.Contains(importService))
  162. {
  163. importServiceList.Add(importService);
  164. }
  165. }
  166. }
  167. }
  168. }
  169. var complexReturnTypeNameList = GetUserDefinedComplexReturnTypeList();
  170. foreach (var complexReturnTypeName in complexReturnTypeNameList)
  171. {
  172. string importService;
  173. if (complexReturnTypeName.Contains("<"))
  174. {
  175. var returnTypeName = complexReturnTypeName.Substring(0, complexReturnTypeName.IndexOf("<"));
  176. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name[0..^2] == returnTypeName).Value;
  177. }
  178. else
  179. {
  180. importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == complexReturnTypeName).Value;
  181. }
  182. if (importService == null)
  183. {
  184. throw new Exception("Import Service is null");
  185. }
  186. if (importService != _serviceName && !importServiceList.Contains(importService))
  187. {
  188. importServiceList.Add(importService);
  189. }
  190. }
  191. foreach (var importService in importServiceList)
  192. {
  193. serviceDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  194. }
  195. if (importServiceList.Count > 0)
  196. {
  197. serviceDartString.AppendLine();
  198. }
  199. serviceDartString.AppendLine();
  200. serviceDartString.AppendLine($"class {_serviceName} extends JsonRpcClientBase {{");
  201. serviceDartString.AppendLine($"\t{_serviceName}(");
  202. serviceDartString.AppendLine("\t\tString host, {");
  203. serviceDartString.AppendLine($"\t\tString serviceName = \"I{_serviceName}\",");
  204. serviceDartString.AppendLine("\t\tMap<String, String>? headers,");
  205. serviceDartString.AppendLine("\t\tint? timeout,");
  206. serviceDartString.AppendLine("\t}) : super(");
  207. serviceDartString.AppendLine("\t\t\t\t\t\thost,");
  208. serviceDartString.AppendLine("\t\t\t\t\t\tserviceName,");
  209. serviceDartString.AppendLine("\t\t\t\t\t\theaders: headers,");
  210. serviceDartString.AppendLine("\t\t\t\t\t\ttimeout: timeout,");
  211. if (complexReturnTypeNameList.Count() == 0)
  212. {
  213. serviceDartString.AppendLine("\t\t\t\t);");
  214. }
  215. else
  216. {
  217. serviceDartString.AppendLine("\t\t\t\t) {");
  218. serviceDartString.AppendLine("\t\t/// 注册响应实体反序列化处理器");
  219. foreach (var complexRetrunTypeName in complexReturnTypeNameList)
  220. {
  221. serviceDartString.AppendLine($"\t\tFJsonConvert.setDecoder((map) => {complexRetrunTypeName}.fromJson(map));");
  222. }
  223. serviceDartString.AppendLine("\t}");
  224. }
  225. serviceDartString.AppendLine();
  226. foreach (var methodMap in _methodMapList)
  227. {
  228. serviceDartString.AppendLine(methodMap.GetMethodDartString());
  229. }
  230. serviceDartString.AppendLine("}");
  231. return serviceDartString.ToString();
  232. }
  233. public List<string> GetUserDefinedComplexReturnTypeList()
  234. {
  235. _userDefinedComplexReturnTypeList = new List<string>();
  236. foreach (var method in _methodMapList)
  237. {
  238. var veturnParameterModelType = method.ReturnParameterModelType;
  239. AddUserDefinedComplexReturnType(veturnParameterModelType);
  240. }
  241. return _userDefinedComplexReturnTypeList;
  242. }
  243. private void AddUserDefinedComplexReturnType(ModelType modelType)
  244. {
  245. if (modelType is UserDefinedModelType)
  246. {
  247. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  248. {
  249. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  250. }
  251. }
  252. else if (modelType is UserDefinedDerivedModelType)
  253. {
  254. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  255. {
  256. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  257. }
  258. }
  259. else if (modelType is UserDefinedGenericModelType userDefinedGenericComplexModelType)
  260. {
  261. if (!_userDefinedComplexReturnTypeList.Contains(userDefinedGenericComplexModelType.GetFlutterTypeName()))
  262. {
  263. _userDefinedComplexReturnTypeList.Add(userDefinedGenericComplexModelType.GetFlutterTypeName());
  264. }
  265. var argumentModelType = userDefinedGenericComplexModelType.GenericArgumentModelType;
  266. AddUserDefinedComplexReturnType(argumentModelType);
  267. }
  268. else if (modelType is ListModelType listModelType)
  269. {
  270. var argumentModelType = listModelType.GenericArgumentModelType;
  271. AddUserDefinedComplexReturnType(argumentModelType);
  272. }
  273. else if (modelType is DictionaryModelType dictionaryModelType)
  274. {
  275. foreach (var argumentModelType in dictionaryModelType.GenericArgumentModelTypeList)
  276. {
  277. AddUserDefinedComplexReturnType(argumentModelType);
  278. }
  279. }
  280. }
  281. }
  282. }