SingleTableTemplate.cs 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Data;
  8. using System.Web;
  9. using Newtonsoft.Json.Linq;
  10. using YiSha.Data.Repository;
  11. using YiSha.CodeGenerator.Model;
  12. using YiSha.Util;
  13. using YiSha.Util.Extension;
  14. using YiSha.Util.Model;
  15. using YiSha.Entity.SystemManage;
  16. using YiSha.Enum.SystemManage;
  17. using YiSha.Business.Cache;
  18. using YiSha.Entity;
  19. using YiSha.Business.Cache.IBusiness;
  20. using YiSha.Enum;
  21. namespace YiSha.CodeGenerator.Template
  22. {
  23. public class SingleTableTemplate
  24. {
  25. #region GetBaseConfig
  26. public BaseConfigModel GetBaseConfig(string path, string userName, string tableName, string tableDescription, List<string> tableFieldList)
  27. {
  28. path = GetProjectRootPath(path);
  29. int defaultField = 2; // 默认显示2个字段
  30. BaseConfigModel baseConfigModel = new BaseConfigModel();
  31. baseConfigModel.TableName = tableName;
  32. baseConfigModel.TableNameUpper = tableName;
  33. #region FileConfigModel
  34. baseConfigModel.FileConfig = new FileConfigModel();
  35. baseConfigModel.FileConfig.ClassPrefix = TableMappingHelper.GetClassNamePrefix(tableName);
  36. baseConfigModel.FileConfig.ClassDescription = tableDescription;
  37. baseConfigModel.FileConfig.CreateName = userName;
  38. baseConfigModel.FileConfig.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
  39. baseConfigModel.FileConfig.EntityName = string.Format("{0}Entity", baseConfigModel.FileConfig.ClassPrefix);
  40. baseConfigModel.FileConfig.EntityMapName = string.Format("{0}Map", baseConfigModel.FileConfig.ClassPrefix);
  41. baseConfigModel.FileConfig.EntityParamName = string.Format("{0}Param", baseConfigModel.FileConfig.ClassPrefix);
  42. baseConfigModel.FileConfig.BusinessName = string.Format("{0}BLL", baseConfigModel.FileConfig.ClassPrefix);
  43. baseConfigModel.FileConfig.IBusinessName = string.Format("I{0}BLL", baseConfigModel.FileConfig.ClassPrefix);
  44. baseConfigModel.FileConfig.ServiceName = string.Format("{0}Service", baseConfigModel.FileConfig.ClassPrefix);
  45. baseConfigModel.FileConfig.IServiceName = string.Format("I{0}Service", baseConfigModel.FileConfig.ClassPrefix);
  46. baseConfigModel.FileConfig.ControllerName = string.Format("{0}Controller", baseConfigModel.FileConfig.ClassPrefix);
  47. baseConfigModel.FileConfig.PageIndexName = string.Format("{0}Index", baseConfigModel.FileConfig.ClassPrefix);
  48. baseConfigModel.FileConfig.PageFormName = string.Format("{0}Form", baseConfigModel.FileConfig.ClassPrefix);
  49. #endregion
  50. #region OutputConfigModel
  51. baseConfigModel.OutputConfig = new OutputConfigModel();
  52. baseConfigModel.OutputConfig.OutputModule = string.Empty;
  53. baseConfigModel.OutputConfig.OutputEntity = Path.Combine(path, "YiSha.Entity");
  54. baseConfigModel.OutputConfig.OutputBusiness = Path.Combine(path, "YiSha.Business");
  55. baseConfigModel.OutputConfig.OutputWeb = Path.Combine(path, "YiSha.Web", "YiSha.Admin.Web");
  56. string areasModule = Path.Combine(baseConfigModel.OutputConfig.OutputWeb, "Areas");
  57. if (Directory.Exists(areasModule))
  58. {
  59. baseConfigModel.OutputConfig.ModuleList = Directory.GetDirectories(areasModule).Select(p => Path.GetFileName(p)).Where(p => p != "DemoManage").ToList();
  60. }
  61. else
  62. {
  63. baseConfigModel.OutputConfig.ModuleList = new List<string> { "TestManage" };
  64. }
  65. #endregion
  66. #region PageIndexModel
  67. baseConfigModel.PageIndex = new PageIndexModel();
  68. baseConfigModel.PageIndex.IsSearch = 1;
  69. baseConfigModel.PageIndex.IsPagination = 1;
  70. baseConfigModel.PageIndex.ButtonList = new List<string>();
  71. baseConfigModel.PageIndex.ColumnList = new List<string>();
  72. baseConfigModel.PageIndex.ColumnList.AddRange(tableFieldList.Take(defaultField));
  73. #endregion
  74. #region PageFormModel
  75. baseConfigModel.PageForm = new PageFormModel();
  76. baseConfigModel.PageForm.ShowMode = 1;
  77. baseConfigModel.PageForm.FieldList = new List<string>();
  78. baseConfigModel.PageForm.FieldList.AddRange(tableFieldList.Take(defaultField));
  79. #endregion
  80. return baseConfigModel;
  81. }
  82. #endregion
  83. #region BuildEntity
  84. public string BuildEntity(BaseConfigModel baseConfigModel, DataTable dt)
  85. {
  86. string baseEntity = GetBaseEntity(dt);
  87. StringBuilder sb = new StringBuilder();
  88. sb.AppendLine("using System;");
  89. sb.AppendLine("using Newtonsoft.Json;");
  90. sb.AppendLine("using System.ComponentModel.DataAnnotations.Schema;");
  91. sb.AppendLine("using YiSha.Util;");
  92. sb.AppendLine();
  93. sb.AppendLine("namespace YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule);
  94. sb.AppendLine("{");
  95. SetClassDescription("实体类", baseConfigModel, sb);
  96. sb.AppendLine(" [Table(\"" + baseConfigModel.TableName + "\")]");
  97. sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.EntityName + " : " + baseEntity);
  98. sb.AppendLine(" {");
  99. string column = string.Empty;
  100. string remark = string.Empty;
  101. string datatype = string.Empty;
  102. foreach (DataRow dr in dt.Rows)
  103. {
  104. column = dr["TableColumn"].ToString();
  105. if (BaseField.BaseFieldList.Where(p => p.ToLower() == column.ToLower()).Any())
  106. {
  107. // 基础字段不需要生成,继承合适的BaseEntity即可。
  108. continue;
  109. }
  110. remark = dr["Remark"].ToString();
  111. datatype = dr["Datatype"].ToString();
  112. datatype = TableMappingHelper.GetPropertyDatatype(datatype);
  113. sb.AppendLine(" /// <summary>");
  114. sb.AppendLine(" /// " + remark);
  115. sb.AppendLine(" /// </summary>");
  116. sb.AppendLine(" /// <returns></returns>");
  117. switch (datatype)
  118. {
  119. case "long?":
  120. sb.AppendLine(" [JsonConverter(typeof(StringJsonConverter))]");
  121. break;
  122. case "DateTime?":
  123. sb.AppendLine(" [JsonConverter(typeof(DateTimeJsonConverter))]");
  124. break;
  125. }
  126. sb.AppendLine(" public " + datatype + " " + column + " { get; set; }");
  127. }
  128. sb.AppendLine(" }");
  129. sb.AppendLine("}");
  130. return sb.ToString();
  131. }
  132. #endregion
  133. #region BuildEntityParam
  134. public string BuildEntityParam(BaseConfigModel baseConfigModel)
  135. {
  136. StringBuilder sb = new StringBuilder();
  137. sb.AppendLine("using System;");
  138. sb.AppendLine("using System.Collections.Generic;");
  139. sb.AppendLine("using Newtonsoft.Json;");
  140. sb.AppendLine("using YiSha.Util;");
  141. sb.AppendLine();
  142. sb.AppendLine("namespace YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule);
  143. sb.AppendLine("{");
  144. SetClassDescription("实体查询类", baseConfigModel, sb);
  145. sb.AppendLine(" public class " + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam"));
  146. sb.AppendLine(" {");
  147. sb.AppendLine(" }");
  148. sb.AppendLine("}");
  149. return sb.ToString();
  150. }
  151. #endregion
  152. #region BuildService
  153. public string BuildService(BaseConfigModel baseConfigModel, DataTable dt)
  154. {
  155. string baseEntity = GetBaseEntity(dt);
  156. StringBuilder sb = new StringBuilder();
  157. string method = string.Empty;
  158. sb.AppendLine("using System;");
  159. sb.AppendLine("using System.Linq;");
  160. sb.AppendLine("using System.Text;");
  161. sb.AppendLine("using System.Data.Common;");
  162. sb.AppendLine("using System.Linq.Expressions;");
  163. sb.AppendLine("using System.Collections.Generic;");
  164. sb.AppendLine("using System.Threading.Tasks;");
  165. sb.AppendLine("using YiSha.Util;");
  166. sb.AppendLine("using YiSha.Util.Extension;");
  167. sb.AppendLine("using YiSha.Util.Model;");
  168. sb.AppendLine("using YiSha.Data;");
  169. sb.AppendLine("using YiSha.Data.Repository;");
  170. sb.AppendLine("using YiSha.Enum;");
  171. sb.AppendLine("using System.ComponentModel.DataAnnotations.Schema;");
  172. sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";");
  173. sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";");
  174. sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";");
  175. sb.AppendLine();
  176. sb.AppendLine("namespace YiSha.Service." + baseConfigModel.OutputConfig.OutputModule);
  177. sb.AppendLine("{");
  178. SetClassDescription("服务实现类", baseConfigModel, sb);
  179. sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.ServiceName + " : I" + baseConfigModel.FileConfig.IServiceName + "");
  180. sb.AppendLine(" {");
  181. sb.AppendLine(" private DBConnectTypeEnum dbConnectType = " + GetDBConnectTypeEnum(baseConfigModel.DBConnectType) + ";");
  182. sb.AppendLine(" #region 获取数据");
  183. sb.AppendLine(" /// <summary>");
  184. sb.AppendLine(" /// 获取列表数据");
  185. sb.AppendLine(" /// </summary>");
  186. sb.AppendLine(" /// <param name=\"param\">请求参数</param>");
  187. sb.AppendLine(" /// <returns></returns>");
  188. sb.AppendLine(" public async Task<List<" + baseConfigModel.FileConfig.EntityName + ">> GetList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)");
  189. sb.AppendLine(" {");
  190. sb.AppendLine(" var expression = ListFilterPartial(param);");
  191. sb.AppendLine(" var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression);");
  192. sb.AppendLine(" return list.ToList();");
  193. sb.AppendLine(" }");
  194. sb.AppendLine();
  195. sb.AppendLine(" /// <summary>");
  196. sb.AppendLine(" /// 获取分页数据");
  197. sb.AppendLine(" /// </summary>");
  198. sb.AppendLine(" /// <param name=\"param\">请求参数</param>");
  199. sb.AppendLine(" /// <param name=\"pagination\">分页条件</param>");
  200. sb.AppendLine(" /// <returns></returns>");
  201. sb.AppendLine(" public async Task<List<" + baseConfigModel.FileConfig.EntityName + ">> GetPageList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination)");
  202. sb.AppendLine(" {");
  203. sb.AppendLine(" var expression = ListFilterPartial(param);");
  204. sb.AppendLine(" var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression, pagination);");
  205. sb.AppendLine(" return list.ToList();");
  206. sb.AppendLine(" }");
  207. sb.AppendLine();
  208. sb.AppendLine(" /// <summary>");
  209. sb.AppendLine(" /// 获取指定列数据");
  210. sb.AppendLine(" /// </summary>");
  211. sb.AppendLine(" /// <param name=\"column\">表字段列</param>");
  212. sb.AppendLine(" /// <param name=\"where\">条件</param>");
  213. sb.AppendLine(" /// <param name=\"parameters\">条件参数</param>");
  214. sb.AppendLine(" /// <param name=\"sort\">排序列</param>");
  215. sb.AppendLine(" /// <param name=\"sortType\">排序类型</param>");
  216. sb.AppendLine(" /// <returns></returns>");
  217. sb.AppendLine(" public async Task<List<" + baseConfigModel.FileConfig.EntityName + ">> GetListColumn(string column, string where, List<DbParameter> parameters, string groupBy = \"\", string sort = \"basecreatetime\", string sortType = \"desc\")");
  218. sb.AppendLine(" {");
  219. sb.AppendLine(" if (string.IsNullOrEmpty(column) || string.IsNullOrEmpty(where))");
  220. sb.AppendLine(" return new List<" + baseConfigModel.FileConfig.EntityName + ">();");
  221. sb.AppendLine(" if (!string.IsNullOrEmpty(groupBy))");
  222. sb.AppendLine(" groupBy = $\" group by {groupBy} \";");
  223. sb.AppendLine(" string tableName = typeof(" + baseConfigModel.FileConfig.EntityName + ").GetAttributeValue((TableAttribute ta) => ta.Name);");
  224. if (baseEntity.Equals("BaseExtensionEntity"))
  225. {
  226. sb.AppendLine(" string selectSql = $\"select {column} from {tableName} where baseisdelete = @baseisdelete and {where} {groupBy} order by {sort} {sortType}\";");
  227. sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@baseisdelete\", (int)IsDeleteEnum.No, dbConnectType));");
  228. }
  229. else
  230. {
  231. sb.AppendLine(" string selectSql = $\"select {column} from {tableName} where 1 = 1 and {where} {groupBy} order by {sort} {sortType}\";");
  232. }
  233. sb.AppendLine(" var result = await _baseRepository.BaseRepository(dbConnectType).FindList<" + baseConfigModel.FileConfig.EntityName + ">(selectSql, DbParameterExtension.ToDbParameter(parameters.ToArray()));");
  234. sb.AppendLine(" return result.ToList();");
  235. sb.AppendLine(" }");
  236. sb.AppendLine();
  237. sb.AppendLine(" /// <summary>");
  238. sb.AppendLine(" /// 根据主键Id获取数据");
  239. sb.AppendLine(" /// </summary>");
  240. sb.AppendLine(" /// <param name=\"id\">主键id</param>");
  241. sb.AppendLine(" /// <returns></returns>");
  242. sb.AppendLine(" public async Task<" + baseConfigModel.FileConfig.EntityName + "> GetEntity(int id)");
  243. sb.AppendLine(" {");
  244. sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).FindEntity<" + baseConfigModel.FileConfig.EntityName + ">(id);");
  245. sb.AppendLine(" }");
  246. sb.AppendLine(" #endregion");
  247. sb.AppendLine();
  248. sb.AppendLine(" #region 提交数据");
  249. sb.AppendLine(" /// <summary>");
  250. sb.AppendLine(" /// 保存数据");
  251. sb.AppendLine(" /// </summary>");
  252. sb.AppendLine(" /// <param name=\"entity\">实体参数</param>");
  253. sb.AppendLine(" /// <returns></returns>");
  254. sb.AppendLine(" public async Task SaveForm(" + baseConfigModel.FileConfig.EntityName + " entity)");
  255. sb.AppendLine(" {");
  256. sb.AppendLine(" if (entity.Id.IsNullOrZero())");
  257. sb.AppendLine(" {");
  258. sb.AppendLine(" " + GetSaveFormCreate(baseEntity));
  259. sb.AppendLine(" await _baseRepository.BaseRepository(dbConnectType).Insert(entity);");
  260. sb.AppendLine(" }");
  261. sb.AppendLine(" else");
  262. sb.AppendLine(" {");
  263. sb.AppendLine(" " + GetSaveFormModify(baseEntity));
  264. sb.AppendLine(" await _baseRepository.BaseRepository(dbConnectType).Update(entity);");
  265. sb.AppendLine(" }");
  266. sb.AppendLine(" }");
  267. sb.AppendLine();
  268. sb.AppendLine(" /// <summary>");
  269. sb.AppendLine(" /// 保存多条数据");
  270. sb.AppendLine(" /// </summary>");
  271. sb.AppendLine(" /// <param name=\"list\">数据列表</param>");
  272. sb.AppendLine(" /// <returns></returns>");
  273. sb.AppendLine(" public async Task<int> InsertMany(List<" + baseConfigModel.FileConfig.EntityName + "> list)");
  274. sb.AppendLine(" {");
  275. sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).Insert(list);");
  276. sb.AppendLine(" }");
  277. sb.AppendLine();
  278. sb.AppendLine(" /// <summary>");
  279. sb.AppendLine(" /// 根据主键Id删除数据");
  280. sb.AppendLine(" /// </summary>");
  281. sb.AppendLine(" /// <param name=\"ids\">主键Id</param>");
  282. sb.AppendLine(" /// <param name=\"del\">是否真删除 true真删除 false软删除 默认false</param>");
  283. sb.AppendLine(" /// <returns></returns>");
  284. sb.AppendLine(" public async Task<int> DeleteFormById(string ids, bool del = false)");
  285. sb.AppendLine(" {");
  286. sb.AppendLine(" if (string.IsNullOrWhiteSpace(ids) || !ids.Split(\", \").Any())");
  287. sb.AppendLine(" {");
  288. sb.AppendLine(" return await Task.FromResult(-1);");
  289. sb.AppendLine(" }");
  290. sb.AppendLine(" ids = $\"{ string.Join(\",\", ids.Split(\",\"))}\";");
  291. sb.AppendLine(" string tableName = typeof(" + baseConfigModel.FileConfig.EntityName + ").GetAttributeValue((TableAttribute ta) => ta.Name);");
  292. if (baseEntity.Equals("BaseExtensionEntity"))
  293. {
  294. sb.AppendLine(" string deleteSql = $\"update {tableName} set baseisdelete = @baseisdelete where FIND_IN_SET (id,@id)\";");
  295. sb.AppendLine(" if (del)");
  296. sb.AppendLine(" {");
  297. sb.AppendLine(" deleteSql = $\"delete from {tableName} where FIND_IN_SET (id,@id)\";");
  298. sb.AppendLine(" }");
  299. sb.AppendLine(" List<DbParameter> parameters = new List<DbParameter>();");
  300. sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@baseisdelete\", (int)IsDeleteEnum.Yes, dbConnectType));");
  301. sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@id\", ids, dbConnectType));");
  302. }
  303. else
  304. {
  305. var where = "";
  306. DBConnectHelper.GetInstance.GetDBConnectInfo(baseConfigModel.DBConnectType, out string dbType, out string dbConnectionString);
  307. sb.AppendLine(" List<DbParameter> parameters = new List<DbParameter>();");
  308. if (dbType.ToLower() == "mysql")
  309. {
  310. where = "where FIND_IN_SET (id,@id)";
  311. sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@id\", ids, dbConnectType));");
  312. }
  313. else
  314. where = "where id in ({ids})";
  315. sb.AppendLine(" string deleteSql = $\"delete from {tableName} " + where + "\";");
  316. }
  317. sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).ExecuteBySql(deleteSql, DbParameterExtension.ToDbParameter(parameters.ToArray()));");
  318. sb.AppendLine(" }");
  319. sb.AppendLine();
  320. sb.AppendLine(" /// <summary>");
  321. sb.AppendLine(" /// 根据条件删除数据");
  322. sb.AppendLine(" /// </summary>");
  323. sb.AppendLine(" /// <param name=\"where\">where条件</param>");
  324. sb.AppendLine(" /// <param name=\"parameters\">参数</param>");
  325. sb.AppendLine(" /// <param name=\"del\">是否真删除 true真删除 false软删除 默认false</param>");
  326. sb.AppendLine(" /// <returns></returns>");
  327. sb.AppendLine(" public async Task<int> DeleteFormByWhere(string where, List<DbParameter> parameters, bool del = false)");
  328. sb.AppendLine(" {");
  329. sb.AppendLine(" if (string.IsNullOrWhiteSpace(where))");
  330. sb.AppendLine(" {");
  331. sb.AppendLine(" return await Task.FromResult(-1);");
  332. sb.AppendLine(" }");
  333. sb.AppendLine(" string tableName = typeof(" + baseConfigModel.FileConfig.EntityName + ").GetAttributeValue((TableAttribute ta) => ta.Name);");
  334. if (baseEntity.Equals("BaseExtensionEntity"))
  335. {
  336. sb.AppendLine(" string deleteSql = $\"update {tableName} set baseisdelete = @baseisdelete where {where}\";");
  337. sb.AppendLine(" if (del)");
  338. sb.AppendLine(" {");
  339. sb.AppendLine(" deleteSql = $\"delete from {tableName} where {where}\";");
  340. sb.AppendLine(" }");
  341. sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@baseisdelete\", (int)IsDeleteEnum.Yes, dbConnectType));");
  342. }
  343. else
  344. {
  345. sb.AppendLine(" string deleteSql = $\"delete from {tableName} where {where}\";");
  346. }
  347. sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).ExecuteBySql(deleteSql, DbParameterExtension.ToDbParameter(parameters.ToArray()));");
  348. sb.AppendLine(" }");
  349. sb.AppendLine();
  350. sb.AppendLine(" /// <summary>");
  351. sb.AppendLine(" /// 修改多条数据");
  352. sb.AppendLine(" /// </summary>");
  353. sb.AppendLine(" /// <param name=\"list\">数据列表</param>");
  354. sb.AppendLine(" /// <returns></returns>");
  355. sb.AppendLine(" public async Task<int> UpdateMany(List<" + baseConfigModel.FileConfig.EntityName + "> list)");
  356. sb.AppendLine(" {");
  357. sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).Update(list);");
  358. sb.AppendLine(" }");
  359. sb.AppendLine();
  360. sb.AppendLine(" /// <summary>");
  361. sb.AppendLine(" /// 根据条件修改数据");
  362. sb.AppendLine(" /// </summary>");
  363. sb.AppendLine(" /// <param name=\"setField\">修改字段</param>");
  364. sb.AppendLine(" /// <param name=\"where\">where条件</param>");
  365. sb.AppendLine(" /// <param name=\"parameters\">参数</param>");
  366. sb.AppendLine(" /// <returns></returns>");
  367. sb.AppendLine(" public async Task<int> UpdateFormByWhere(string setField, string where, List<DbParameter> parameters)");
  368. sb.AppendLine(" {");
  369. sb.AppendLine(" if (string.IsNullOrWhiteSpace(setField) || string.IsNullOrWhiteSpace(where))");
  370. sb.AppendLine(" {");
  371. sb.AppendLine(" return await Task.FromResult(-1);");
  372. sb.AppendLine(" }");
  373. sb.AppendLine(" string tableName = typeof(" + baseConfigModel.FileConfig.EntityName + ").GetAttributeValue((TableAttribute ta) => ta.Name);");
  374. sb.AppendLine(" string updateSql = $\"update {tableName} set {setField} where {where}\";");
  375. sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).ExecuteBySql(updateSql, DbParameterExtension.ToDbParameter(parameters.ToArray()));");
  376. sb.AppendLine(" }");
  377. sb.AppendLine(" #endregion");
  378. sb.AppendLine();
  379. sb.AppendLine(" #region 私有方法");
  380. sb.AppendLine(" ///// <summary>");
  381. sb.AppendLine(" ///// 列表条件过滤");
  382. sb.AppendLine(" ///// 建议该方法放在Partial部分类中,因为代码生成时当前类会被覆盖(该方法在生成时将会被注释)");
  383. sb.AppendLine(" ///// </summary>");
  384. sb.AppendLine(" ///// <param name=\"param\"></param>");
  385. sb.AppendLine(" ///// <returns></returns>");
  386. sb.AppendLine(" //private Expression<Func<" + baseConfigModel.FileConfig.EntityName + ", bool>> ListFilter(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)");
  387. sb.AppendLine(" //{");
  388. sb.AppendLine(" // var expression = LinqExtensions.True<" + baseConfigModel.FileConfig.EntityName + ">();");
  389. sb.AppendLine(" // expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);");
  390. sb.AppendLine(" // if (param != null)");
  391. sb.AppendLine(" // {");
  392. sb.AppendLine(" // }");
  393. sb.AppendLine(" // return expression;");
  394. sb.AppendLine(" //}");
  395. sb.AppendLine(" #endregion");
  396. sb.AppendLine(" }");
  397. sb.AppendLine("}");
  398. return sb.ToString();
  399. }
  400. #endregion
  401. #region BuildService_Partial
  402. public string BuildService_Partial(BaseConfigModel baseConfigModel, DataTable dt)
  403. {
  404. string baseEntity = GetBaseEntity(dt);
  405. StringBuilder sb = new StringBuilder();
  406. string method = string.Empty;
  407. sb.AppendLine("using System;");
  408. sb.AppendLine("using System.Linq;");
  409. sb.AppendLine("using System.Text;");
  410. sb.AppendLine("using System.Data.Common;");
  411. sb.AppendLine("using System.Linq.Expressions;");
  412. sb.AppendLine("using System.Collections.Generic;");
  413. sb.AppendLine("using System.Threading.Tasks;");
  414. sb.AppendLine("using YiSha.Util;");
  415. sb.AppendLine("using YiSha.Util.Extension;");
  416. sb.AppendLine("using YiSha.Util.Model;");
  417. sb.AppendLine("using YiSha.Data;");
  418. sb.AppendLine("using YiSha.Data.Repository;");
  419. sb.AppendLine("using YiSha.Enum;");
  420. sb.AppendLine("using System.ComponentModel.DataAnnotations.Schema;");
  421. sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";");
  422. sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";");
  423. sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";");
  424. sb.AppendLine();
  425. sb.AppendLine("namespace YiSha.Service." + baseConfigModel.OutputConfig.OutputModule);
  426. sb.AppendLine("{");
  427. SetClassDescription("服务实现类(部分类)", baseConfigModel, sb);
  428. sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.ServiceName + "");
  429. sb.AppendLine(" {");
  430. sb.AppendLine(" private IRepositoryFactory _baseRepository;");
  431. sb.AppendLine(" public " + baseConfigModel.FileConfig.ServiceName + "(IRepositoryFactory baseRepository)");
  432. sb.AppendLine(" {");
  433. sb.AppendLine(" _baseRepository = baseRepository;");
  434. sb.AppendLine(" }");
  435. sb.AppendLine();
  436. sb.AppendLine(" #region 私有方法");
  437. sb.AppendLine(" /// <summary>");
  438. sb.AppendLine(" /// 列表条件过滤");
  439. sb.AppendLine(" /// 建议该方法放在Partial部分类中,因为代码生成时当前类会被覆盖(该方法在生成时将会被注释)");
  440. sb.AppendLine(" /// </summary>");
  441. sb.AppendLine(" /// <param name=\"param\"></param>");
  442. sb.AppendLine(" /// <returns></returns>");
  443. sb.AppendLine(" private Expression<Func<" + baseConfigModel.FileConfig.EntityName + ", bool>> ListFilterPartial(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)");
  444. sb.AppendLine(" {");
  445. sb.AppendLine(" var expression = LinqExtensions.True<" + baseConfigModel.FileConfig.EntityName + ">();");
  446. //只有BaseExtensionEntity实体才会存在BaseIsDelete字段
  447. if (baseEntity.Equals("BaseExtensionEntity"))
  448. sb.AppendLine(" expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);");
  449. sb.AppendLine(" if (param != null)");
  450. sb.AppendLine(" {");
  451. sb.AppendLine(" }");
  452. sb.AppendLine(" return expression;");
  453. sb.AppendLine(" }");
  454. sb.AppendLine(" #endregion");
  455. sb.AppendLine(" }");
  456. sb.AppendLine("}");
  457. return sb.ToString();
  458. }
  459. #endregion
  460. #region BuildIService
  461. public string BuildIService(BaseConfigModel baseConfigModel)
  462. {
  463. StringBuilder sb = new StringBuilder();
  464. string method = string.Empty;
  465. sb.AppendLine("using YiSha.Util.Model;");
  466. sb.AppendLine("using System.Data.Common;");
  467. sb.AppendLine("using System.Threading.Tasks;");
  468. sb.AppendLine("using System.Collections.Generic;");
  469. sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";");
  470. sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";");
  471. sb.AppendLine();
  472. sb.AppendLine("namespace YiSha.IService." + baseConfigModel.OutputConfig.OutputModule);
  473. sb.AppendLine("{");
  474. SetClassDescription("服务接口类", baseConfigModel, sb);
  475. sb.AppendLine(" public partial interface I" + baseConfigModel.FileConfig.ServiceName + "");
  476. sb.AppendLine(" {");
  477. sb.AppendLine(" #region 获取数据");
  478. sb.AppendLine(" /// <summary>");
  479. sb.AppendLine(" /// 获取列表数据");
  480. sb.AppendLine(" /// </summary>");
  481. sb.AppendLine(" /// <param name=\"param\">请求参数</param>");
  482. sb.AppendLine(" /// <returns></returns>");
  483. sb.AppendLine(" Task<List<" + baseConfigModel.FileConfig.EntityName + ">> GetList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param);");
  484. sb.AppendLine();
  485. sb.AppendLine(" /// <summary>");
  486. sb.AppendLine(" /// 获取分页数据");
  487. sb.AppendLine(" /// </summary>");
  488. sb.AppendLine(" /// <param name=\"param\">请求参数</param>");
  489. sb.AppendLine(" /// <param name=\"pagination\">分页条件</param>");
  490. sb.AppendLine(" /// <returns></returns>");
  491. sb.AppendLine(" Task<List<" + baseConfigModel.FileConfig.EntityName + ">> GetPageList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination);");
  492. sb.AppendLine();
  493. sb.AppendLine(" /// <summary>");
  494. sb.AppendLine(" /// 获取指定列数据");
  495. sb.AppendLine(" /// </summary>");
  496. sb.AppendLine(" /// <param name=\"column\">表字段列</param>");
  497. sb.AppendLine(" /// <param name=\"where\">条件</param>");
  498. sb.AppendLine(" /// <param name=\"parameters\">条件参数</param>");
  499. sb.AppendLine(" /// <param name=\"sort\">排序列</param>");
  500. sb.AppendLine(" /// <param name=\"sortType\">排序类型</param>");
  501. sb.AppendLine(" /// <returns></returns>");
  502. sb.AppendLine(" Task<List<" + baseConfigModel.FileConfig.EntityName + ">> GetListColumn(string column, string where, List<DbParameter> parameters, string groupBy = \"\", string sort = \"basecreatetime\", string sortType = \"desc\");");
  503. sb.AppendLine();
  504. sb.AppendLine(" /// <summary>");
  505. sb.AppendLine(" /// 根据主键Id获取数据");
  506. sb.AppendLine(" /// </summary>");
  507. sb.AppendLine(" /// <param name=\"id\">主键id</param>");
  508. sb.AppendLine(" /// <returns></returns>");
  509. sb.AppendLine(" Task<" + baseConfigModel.FileConfig.EntityName + "> GetEntity(int id);");
  510. sb.AppendLine(" #endregion");
  511. sb.AppendLine();
  512. sb.AppendLine(" #region 提交数据");
  513. sb.AppendLine(" /// <summary>");
  514. sb.AppendLine(" /// 保存数据");
  515. sb.AppendLine(" /// </summary>");
  516. sb.AppendLine(" /// <param name=\"entity\">实体参数</param>");
  517. sb.AppendLine(" /// <returns></returns>");
  518. sb.AppendLine(" Task SaveForm(" + baseConfigModel.FileConfig.EntityName + " entity);");
  519. sb.AppendLine();
  520. sb.AppendLine(" /// <summary>");
  521. sb.AppendLine(" /// 保存多条数据");
  522. sb.AppendLine(" /// </summary>");
  523. sb.AppendLine(" /// <param name=\"list\">数据列表</param>");
  524. sb.AppendLine(" /// <returns></returns>");
  525. sb.AppendLine(" Task<int> InsertMany(List<" + baseConfigModel.FileConfig.EntityName + "> list);");
  526. sb.AppendLine();
  527. sb.AppendLine(" /// <summary>");
  528. sb.AppendLine(" /// 根据主键Id删除数据");
  529. sb.AppendLine(" /// </summary>");
  530. sb.AppendLine(" /// <param name=\"ids\">主键Id</param>");
  531. sb.AppendLine(" /// <param name=\"del\">是否真删除 true真删除 false软删除 默认false</param>");
  532. sb.AppendLine(" /// <returns></returns>");
  533. sb.AppendLine(" Task<int> DeleteFormById(string ids, bool del = false);");
  534. sb.AppendLine();
  535. sb.AppendLine(" /// <summary>");
  536. sb.AppendLine(" /// 根据条件删除数据");
  537. sb.AppendLine(" /// </summary>");
  538. sb.AppendLine(" /// <param name=\"where\">where条件</param>");
  539. sb.AppendLine(" /// <param name=\"parameters\">参数</param>");
  540. sb.AppendLine(" /// <param name=\"del\">是否真删除 true真删除 false软删除 默认false</param>");
  541. sb.AppendLine(" /// <returns></returns>");
  542. sb.AppendLine(" Task<int> DeleteFormByWhere(string where, List<DbParameter> parameters, bool del = false);");
  543. sb.AppendLine();
  544. sb.AppendLine(" /// <summary>");
  545. sb.AppendLine(" /// 修改多条数据");
  546. sb.AppendLine(" /// </summary>");
  547. sb.AppendLine(" /// <param name=\"list\">数据列表</param>");
  548. sb.AppendLine(" /// <returns></returns>");
  549. sb.AppendLine(" Task<int> UpdateMany(List<" + baseConfigModel.FileConfig.EntityName + "> list);");
  550. sb.AppendLine();
  551. sb.AppendLine(" /// <summary>");
  552. sb.AppendLine(" /// 根据条件修改数据");
  553. sb.AppendLine(" /// </summary>");
  554. sb.AppendLine(" /// <param name=\"setField\">修改字段</param>");
  555. sb.AppendLine(" /// <param name=\"where\">where条件</param>");
  556. sb.AppendLine(" /// <param name=\"parameters\">参数</param>");
  557. sb.AppendLine(" /// <returns></returns>");
  558. sb.AppendLine(" Task<int> UpdateFormByWhere(string setField, string where, List<DbParameter> parameters);");
  559. sb.AppendLine(" #endregion");
  560. sb.AppendLine();
  561. sb.AppendLine(" #region 私有方法");
  562. sb.AppendLine(" ///// <summary>");
  563. sb.AppendLine(" ///// 列表条件过滤");
  564. sb.AppendLine(" ///// 建议该方法放在Partial部分类中,因为代码生成时当前类会被覆盖(该方法在生成时将会被注释)");
  565. sb.AppendLine(" ///// </summary>");
  566. sb.AppendLine(" ///// <param name=\"param\"></param>");
  567. sb.AppendLine(" ///// <returns></returns>");
  568. sb.AppendLine(" //Expression<Func<" + baseConfigModel.FileConfig.EntityName + ", bool>> ListFilter(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param);");
  569. sb.AppendLine(" #endregion");
  570. sb.AppendLine(" }");
  571. sb.AppendLine("}");
  572. return sb.ToString();
  573. }
  574. #endregion
  575. #region BuildIService_Partial
  576. public string BuildIService_Partial(BaseConfigModel baseConfigModel)
  577. {
  578. StringBuilder sb = new StringBuilder();
  579. string method = string.Empty;
  580. sb.AppendLine("using YiSha.Util.Model;");
  581. sb.AppendLine("using System.Data.Common;");
  582. sb.AppendLine("using System.Threading.Tasks;");
  583. sb.AppendLine("using System.Collections.Generic;");
  584. sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";");
  585. sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";");
  586. sb.AppendLine();
  587. sb.AppendLine("namespace YiSha.IService." + baseConfigModel.OutputConfig.OutputModule);
  588. sb.AppendLine("{");
  589. SetClassDescription("服务接口类(部分类)", baseConfigModel, sb);
  590. sb.AppendLine(" public partial interface I" + baseConfigModel.FileConfig.ServiceName + "");
  591. sb.AppendLine(" {");
  592. sb.AppendLine(" }");
  593. sb.AppendLine("}");
  594. return sb.ToString();
  595. }
  596. #endregion
  597. #region BuildBusiness
  598. public string BuildBusiness(BaseConfigModel baseConfigModel)
  599. {
  600. StringBuilder sb = new StringBuilder();
  601. sb.AppendLine("using System.Collections.Generic;");
  602. sb.AppendLine("using System.Threading.Tasks;");
  603. sb.AppendLine("using YiSha.Util.Extension;");
  604. sb.AppendLine("using YiSha.Util.Model;");
  605. sb.AppendLine("using System.Data.Common;");
  606. sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";");
  607. sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";");
  608. sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";");
  609. sb.AppendLine("using YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule + ";");
  610. sb.AppendLine();
  611. sb.AppendLine("namespace YiSha.Business." + baseConfigModel.OutputConfig.OutputModule);
  612. sb.AppendLine("{");
  613. SetClassDescription("业务实现类", baseConfigModel, sb);
  614. sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.BusinessName + " : I" + baseConfigModel.FileConfig.IBusinessName + "");
  615. sb.AppendLine(" {");
  616. sb.AppendLine(" #region 获取数据");
  617. sb.AppendLine(" /// <summary>");
  618. sb.AppendLine(" /// 获取列表数据");
  619. sb.AppendLine(" /// </summary>");
  620. sb.AppendLine(" /// <param name=\"param\">请求参数</param>");
  621. sb.AppendLine(" /// <returns></returns>");
  622. sb.AppendLine(" public async Task<TData<List<" + baseConfigModel.FileConfig.EntityName + ">>> GetList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)");
  623. sb.AppendLine(" {");
  624. sb.AppendLine(" TData<List<" + baseConfigModel.FileConfig.EntityName + ">> obj = new TData<List<" + baseConfigModel.FileConfig.EntityName + ">>();");
  625. sb.AppendLine(" obj.Data = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".GetList(param);");
  626. sb.AppendLine(" obj.Total = obj.Data.Count;");
  627. sb.AppendLine(" obj.Tag = 1;");
  628. sb.AppendLine(" return obj;");
  629. sb.AppendLine(" }");
  630. sb.AppendLine();
  631. sb.AppendLine(" /// <summary>");
  632. sb.AppendLine(" /// 获取分页数据");
  633. sb.AppendLine(" /// </summary>");
  634. sb.AppendLine(" /// <param name=\"param\">请求参数</param>");
  635. sb.AppendLine(" /// <param name=\"pagination\">分页条件</param>");
  636. sb.AppendLine(" /// <returns></returns>");
  637. sb.AppendLine(" public async Task<TData<List<" + baseConfigModel.FileConfig.EntityName + ">>> GetPageList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination)");
  638. sb.AppendLine(" {");
  639. sb.AppendLine(" TData<List<" + baseConfigModel.FileConfig.EntityName + ">> obj = new TData<List<" + baseConfigModel.FileConfig.EntityName + ">>();");
  640. sb.AppendLine(" obj.Data = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".GetPageList(param, pagination);");
  641. sb.AppendLine(" obj.Total = pagination.TotalCount;");
  642. sb.AppendLine(" obj.Tag = 1;");
  643. sb.AppendLine(" return obj;");
  644. sb.AppendLine(" }");
  645. sb.AppendLine();
  646. sb.AppendLine(" /// <summary>");
  647. sb.AppendLine(" /// 获取指定列数据");
  648. sb.AppendLine(" /// </summary>");
  649. sb.AppendLine(" /// <param name=\"column\">表字段列</param>");
  650. sb.AppendLine(" /// <param name=\"where\">条件</param>");
  651. sb.AppendLine(" /// <param name=\"parameters\">条件参数</param>");
  652. sb.AppendLine(" /// <param name=\"sort\">排序列</param>");
  653. sb.AppendLine(" /// <param name=\"sortType\">排序类型</param>");
  654. sb.AppendLine(" /// <returns></returns>");
  655. sb.AppendLine(" public async Task<TData<List<" + baseConfigModel.FileConfig.EntityName + ">>> GetListColumn(string column, string where, List<DbParameter> parameters, string groupBy = \"\", string sort = \"basecreatetime\", string sortType = \"desc\")");
  656. sb.AppendLine(" {");
  657. sb.AppendLine(" TData<List<" + baseConfigModel.FileConfig.EntityName + ">> obj = new TData<List<" + baseConfigModel.FileConfig.EntityName + ">>();");
  658. sb.AppendLine(" obj.Data = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".GetListColumn(column, where, parameters, groupBy, sort, sortType);");
  659. sb.AppendLine(" obj.Total = obj.Data.Count;");
  660. sb.AppendLine(" obj.Tag = 1;");
  661. sb.AppendLine(" return obj;");
  662. sb.AppendLine(" }");
  663. sb.AppendLine();
  664. sb.AppendLine(" /// <summary>");
  665. sb.AppendLine(" /// 根据主键Id获取数据");
  666. sb.AppendLine(" /// </summary>");
  667. sb.AppendLine(" /// <param name=\"id\">主键id</param>");
  668. sb.AppendLine(" /// <returns></returns>");
  669. sb.AppendLine(" public async Task<TData<" + baseConfigModel.FileConfig.EntityName + ">> GetEntity(int id)");
  670. sb.AppendLine(" {");
  671. sb.AppendLine(" TData<" + baseConfigModel.FileConfig.EntityName + "> obj = new TData<" + baseConfigModel.FileConfig.EntityName + ">();");
  672. sb.AppendLine(" obj.Data = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".GetEntity(id);");
  673. sb.AppendLine(" if (obj.Data != null)");
  674. sb.AppendLine(" {");
  675. sb.AppendLine(" obj.Tag = 1;");
  676. sb.AppendLine(" }");
  677. sb.AppendLine(" return obj;");
  678. sb.AppendLine(" }");
  679. sb.AppendLine(" #endregion");
  680. sb.AppendLine();
  681. sb.AppendLine(" #region 提交数据");
  682. sb.AppendLine(" /// <summary>");
  683. sb.AppendLine(" /// 保存数据");
  684. sb.AppendLine(" /// </summary>");
  685. sb.AppendLine(" /// <param name=\"entity\">实体参数</param>");
  686. sb.AppendLine(" /// <returns></returns>");
  687. sb.AppendLine(" public async Task<TData<int>> SaveForm(" + baseConfigModel.FileConfig.EntityName + " entity)");
  688. sb.AppendLine(" {");
  689. sb.AppendLine(" TData<int> obj = new TData<int>();");
  690. sb.AppendLine(" await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".SaveForm(entity);");
  691. sb.AppendLine(" obj.Data = entity.Id;");
  692. sb.AppendLine(" obj.Tag = 1;");
  693. sb.AppendLine(" return obj;");
  694. sb.AppendLine(" }");
  695. sb.AppendLine();
  696. sb.AppendLine(" /// <summary>");
  697. sb.AppendLine(" /// 根据主键Id删除数据");
  698. sb.AppendLine(" /// </summary>");
  699. sb.AppendLine(" /// <param name=\"ids\">主键Id</param>");
  700. sb.AppendLine(" /// <param name=\"del\">是否真删除 true真删除 false软删除 默认false</param>");
  701. sb.AppendLine(" /// <returns></returns>");
  702. sb.AppendLine(" public async Task<TData> DeleteFormById(string ids, bool del = false)");
  703. sb.AppendLine(" {");
  704. sb.AppendLine(" TData obj = new TData();");
  705. sb.AppendLine(" obj.Tag = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".DeleteFormById(ids, del);");
  706. sb.AppendLine(" return obj;");
  707. sb.AppendLine(" }");
  708. sb.AppendLine();
  709. sb.AppendLine(" /// <summary>");
  710. sb.AppendLine(" /// 根据条件删除数据");
  711. sb.AppendLine(" /// </summary>");
  712. sb.AppendLine(" /// <param name=\"where\">where条件</param>");
  713. sb.AppendLine(" /// <param name=\"parameters\">参数</param>");
  714. sb.AppendLine(" /// <param name=\"del\">是否真删除 true真删除 false软删除 默认false</param>");
  715. sb.AppendLine(" /// <returns></returns>");
  716. sb.AppendLine(" public async Task<TData> DeleteFormByWhere(string where, List<DbParameter> parameters, bool del = false)");
  717. sb.AppendLine(" {");
  718. sb.AppendLine(" TData obj = new TData();");
  719. sb.AppendLine(" obj.Tag = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".DeleteFormByWhere(where, parameters, del);");
  720. sb.AppendLine(" return obj;");
  721. sb.AppendLine(" }");
  722. sb.AppendLine();
  723. sb.AppendLine(" /// <summary>");
  724. sb.AppendLine(" /// 根据条件修改数据");
  725. sb.AppendLine(" /// </summary>");
  726. sb.AppendLine(" /// <param name=\"setField\">修改字段</param>");
  727. sb.AppendLine(" /// <param name=\"where\">where条件</param>");
  728. sb.AppendLine(" /// <param name=\"parameters\">参数</param>");
  729. sb.AppendLine(" /// <returns></returns>");
  730. sb.AppendLine(" public async Task<TData> UpdateFormByWhere(string setField, string where, List<DbParameter> parameters)");
  731. sb.AppendLine(" {");
  732. sb.AppendLine(" TData obj = new TData();");
  733. sb.AppendLine(" obj.Tag = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".UpdateFormByWhere(setField, where, parameters);");
  734. sb.AppendLine(" return obj;");
  735. sb.AppendLine(" }");
  736. sb.AppendLine(" #endregion");
  737. sb.AppendLine();
  738. sb.AppendLine(" #region 私有方法");
  739. sb.AppendLine(" #endregion");
  740. sb.AppendLine(" }");
  741. sb.AppendLine("}");
  742. return sb.ToString();
  743. }
  744. #endregion
  745. #region BuildBusiness_Partial
  746. public string BuildBusiness_Partial(BaseConfigModel baseConfigModel)
  747. {
  748. StringBuilder sb = new StringBuilder();
  749. sb.AppendLine("using System.Collections.Generic;");
  750. sb.AppendLine("using System.Threading.Tasks;");
  751. sb.AppendLine("using YiSha.Util.Extension;");
  752. sb.AppendLine("using YiSha.Util.Model;");
  753. sb.AppendLine("using System.Data.Common;");
  754. sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";");
  755. sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";");
  756. sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";");
  757. sb.AppendLine("using YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule + ";");
  758. sb.AppendLine();
  759. sb.AppendLine("namespace YiSha.Business." + baseConfigModel.OutputConfig.OutputModule);
  760. sb.AppendLine("{");
  761. SetClassDescription("业务实现类(部分类)", baseConfigModel, sb);
  762. sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.BusinessName + "");
  763. sb.AppendLine(" {");
  764. sb.AppendLine(" private I" + baseConfigModel.FileConfig.ServiceName + " _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ";");
  765. sb.AppendLine(" public " + baseConfigModel.FileConfig.BusinessName + " (I" + baseConfigModel.FileConfig.ServiceName + " " + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ")");
  766. sb.AppendLine(" {");
  767. sb.AppendLine(" _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + " = " + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ";");
  768. sb.AppendLine(" }");
  769. sb.AppendLine(" }");
  770. sb.AppendLine("}");
  771. return sb.ToString();
  772. }
  773. #endregion
  774. #region BuildIBusiness
  775. public string BuildIBusiness(BaseConfigModel baseConfigModel)
  776. {
  777. StringBuilder sb = new StringBuilder();
  778. sb.AppendLine("using YiSha.Util.Model;");
  779. sb.AppendLine("using System.Data.Common;");
  780. sb.AppendLine("using System.Threading.Tasks;");
  781. sb.AppendLine("using System.Collections.Generic;");
  782. sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";");
  783. sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";");
  784. sb.AppendLine("namespace YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule);
  785. sb.AppendLine("{");
  786. SetClassDescription("业务接口类", baseConfigModel, sb);
  787. sb.AppendLine(" public partial interface I" + baseConfigModel.FileConfig.BusinessName);
  788. sb.AppendLine(" {");
  789. sb.AppendLine(" #region 获取数据");
  790. sb.AppendLine(" /// <summary>");
  791. sb.AppendLine(" /// 获取列表数据");
  792. sb.AppendLine(" /// </summary>");
  793. sb.AppendLine(" /// <param name=\"param\">请求参数</param>");
  794. sb.AppendLine(" /// <returns></returns>");
  795. sb.AppendLine(" Task<TData<List<" + baseConfigModel.FileConfig.EntityName + ">>> GetList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param);");
  796. sb.AppendLine();
  797. sb.AppendLine(" /// <summary>");
  798. sb.AppendLine(" /// 获取分页数据");
  799. sb.AppendLine(" /// </summary>");
  800. sb.AppendLine(" /// <param name=\"param\">请求参数</param>");
  801. sb.AppendLine(" /// <param name=\"pagination\">分页条件</param>");
  802. sb.AppendLine(" /// <returns></returns>");
  803. sb.AppendLine(" Task<TData<List<" + baseConfigModel.FileConfig.EntityName + ">>> GetPageList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination);");
  804. sb.AppendLine();
  805. sb.AppendLine(" /// <summary>");
  806. sb.AppendLine(" /// 获取指定列数据");
  807. sb.AppendLine(" /// </summary>");
  808. sb.AppendLine(" /// <param name=\"column\">表字段列</param>");
  809. sb.AppendLine(" /// <param name=\"where\">条件</param>");
  810. sb.AppendLine(" /// <param name=\"parameters\">条件参数</param>");
  811. sb.AppendLine(" /// <param name=\"sort\">排序列</param>");
  812. sb.AppendLine(" /// <param name=\"sortType\">排序类型</param>");
  813. sb.AppendLine(" /// <returns></returns>");
  814. sb.AppendLine(" Task<TData<List<" + baseConfigModel.FileConfig.EntityName + ">>> GetListColumn(string column, string where, List<DbParameter> parameters, string groupBy = \"\", string sort = \"basecreatetime\", string sortType = \"desc\");");
  815. sb.AppendLine();
  816. sb.AppendLine(" /// <summary>");
  817. sb.AppendLine(" /// 根据主键Id获取数据");
  818. sb.AppendLine(" /// </summary>");
  819. sb.AppendLine(" /// <param name=\"id\">主键id</param>");
  820. sb.AppendLine(" /// <returns></returns>");
  821. sb.AppendLine(" Task<TData<" + baseConfigModel.FileConfig.EntityName + ">> GetEntity(int id);");
  822. sb.AppendLine(" #endregion");
  823. sb.AppendLine();
  824. sb.AppendLine(" #region 提交数据");
  825. sb.AppendLine(" /// <summary>");
  826. sb.AppendLine(" /// 保存数据");
  827. sb.AppendLine(" /// </summary>");
  828. sb.AppendLine(" /// <param name=\"entity\">实体参数</param>");
  829. sb.AppendLine(" /// <returns></returns>");
  830. sb.AppendLine(" Task<TData<int>> SaveForm(" + baseConfigModel.FileConfig.EntityName + " entity);");
  831. sb.AppendLine();
  832. sb.AppendLine(" /// <summary>");
  833. sb.AppendLine(" /// 根据主键Id删除数据");
  834. sb.AppendLine(" /// </summary>");
  835. sb.AppendLine(" /// <param name=\"ids\">主键Id</param>");
  836. sb.AppendLine(" /// <param name=\"del\">是否真删除 true真删除 false软删除 默认false</param>");
  837. sb.AppendLine(" /// <returns></returns>");
  838. sb.AppendLine(" Task<TData> DeleteFormById(string ids, bool del = false);");
  839. sb.AppendLine();
  840. sb.AppendLine(" /// <summary>");
  841. sb.AppendLine(" /// 根据条件删除数据");
  842. sb.AppendLine(" /// </summary>");
  843. sb.AppendLine(" /// <param name=\"where\">where条件</param>");
  844. sb.AppendLine(" /// <param name=\"parameters\">参数</param>");
  845. sb.AppendLine(" /// <param name=\"del\">是否真删除 true真删除 false软删除 默认false</param>");
  846. sb.AppendLine(" /// <returns></returns>");
  847. sb.AppendLine(" Task<TData> DeleteFormByWhere(string where, List<DbParameter> parameters, bool del = false);");
  848. sb.AppendLine();
  849. sb.AppendLine(" /// <summary>");
  850. sb.AppendLine(" /// 根据条件修改数据");
  851. sb.AppendLine(" /// </summary>");
  852. sb.AppendLine(" /// <param name=\"setField\">修改字段</param>");
  853. sb.AppendLine(" /// <param name=\"where\">where条件</param>");
  854. sb.AppendLine(" /// <param name=\"parameters\">参数</param>");
  855. sb.AppendLine(" /// <returns></returns>");
  856. sb.AppendLine(" Task<TData> UpdateFormByWhere(string setField, string where, List<DbParameter> parameters);");
  857. sb.AppendLine(" #endregion");
  858. sb.AppendLine();
  859. sb.AppendLine(" #region 私有方法");
  860. sb.AppendLine(" #endregion");
  861. sb.AppendLine(" }");
  862. sb.AppendLine("}");
  863. return sb.ToString();
  864. }
  865. #endregion
  866. #region BuildIBusiness_Partial
  867. public string BuildIBusiness_Partial(BaseConfigModel baseConfigModel)
  868. {
  869. StringBuilder sb = new StringBuilder();
  870. sb.AppendLine("using YiSha.Util.Model;");
  871. sb.AppendLine("using System.Data.Common;");
  872. sb.AppendLine("using System.Threading.Tasks;");
  873. sb.AppendLine("using System.Collections.Generic;");
  874. sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";");
  875. sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";");
  876. sb.AppendLine("namespace YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule);
  877. sb.AppendLine("{");
  878. SetClassDescription("业务接口类(部分类)", baseConfigModel, sb);
  879. sb.AppendLine(" public partial interface I" + baseConfigModel.FileConfig.BusinessName);
  880. sb.AppendLine(" {");
  881. sb.AppendLine(" }");
  882. sb.AppendLine("}");
  883. return sb.ToString();
  884. }
  885. #endregion
  886. #region BuildController
  887. public string BuildController(BaseConfigModel baseConfigModel)
  888. {
  889. string modulePrefix = GetModulePrefix(baseConfigModel);
  890. string classPrefix = baseConfigModel.FileConfig.ClassPrefix.ToLower();
  891. StringBuilder sb = new StringBuilder();
  892. sb.AppendLine("using System;");
  893. sb.AppendLine("using System.Collections.Generic;");
  894. sb.AppendLine("using System.Threading.Tasks;");
  895. sb.AppendLine("using System.Linq;");
  896. sb.AppendLine("using System.Web;");
  897. sb.AppendLine("using Microsoft.AspNetCore.Mvc;");
  898. sb.AppendLine("using YiSha.Util;");
  899. sb.AppendLine("using YiSha.Util.Model;");
  900. sb.AppendLine("using YiSha.Entity;");
  901. sb.AppendLine("using YiSha.Model;");
  902. sb.AppendLine("using YiSha.Admin.Web.Controllers;");
  903. sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";");
  904. sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";");
  905. sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";");
  906. sb.AppendLine("using YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule + ";");
  907. sb.AppendLine();
  908. sb.AppendLine("namespace YiSha.Admin.Web.Areas." + baseConfigModel.OutputConfig.OutputModule + ".Controllers");
  909. sb.AppendLine("{");
  910. SetClassDescription("控制器类", baseConfigModel, sb);
  911. sb.AppendLine(" [Area(\"" + baseConfigModel.OutputConfig.OutputModule + "\")]");
  912. sb.AppendLine(" public class " + baseConfigModel.FileConfig.ControllerName + " : BaseController");
  913. sb.AppendLine(" {");
  914. sb.AppendLine(" private I" + baseConfigModel.FileConfig.IBusinessName + " _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ";");
  915. sb.AppendLine();
  916. sb.AppendLine(" public " + baseConfigModel.FileConfig.ControllerName + "(I" + baseConfigModel.FileConfig.IBusinessName + " " + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ")");
  917. sb.AppendLine(" {");
  918. sb.AppendLine(" _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + " = " + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ";");
  919. sb.AppendLine(" }");
  920. sb.AppendLine();
  921. sb.AppendLine(" #region 视图功能");
  922. sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "view") + "\")]");
  923. sb.AppendLine(" public ActionResult " + baseConfigModel.FileConfig.PageIndexName + "()");
  924. sb.AppendLine(" {");
  925. sb.AppendLine(" return View();");
  926. sb.AppendLine(" }");
  927. sb.AppendLine();
  928. sb.AppendLine(" public ActionResult " + baseConfigModel.FileConfig.PageFormName + "()");
  929. sb.AppendLine(" {");
  930. sb.AppendLine(" return View();");
  931. sb.AppendLine(" }");
  932. sb.AppendLine(" #endregion");
  933. sb.AppendLine();
  934. sb.AppendLine(" #region 获取数据");
  935. sb.AppendLine(" [HttpGet]");
  936. sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "search") + "\")]");
  937. sb.AppendLine(" public async Task<ActionResult> GetListJson(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)");
  938. sb.AppendLine(" {");
  939. sb.AppendLine(" TData<List<" + baseConfigModel.FileConfig.EntityName + ">> obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".GetList(param);");
  940. sb.AppendLine(" return Json(obj);");
  941. sb.AppendLine(" }");
  942. sb.AppendLine();
  943. sb.AppendLine(" [HttpGet]");
  944. sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "search") + "\")]");
  945. sb.AppendLine(" public async Task<ActionResult> GetPageListJson(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination)");
  946. sb.AppendLine(" {");
  947. sb.AppendLine(" TData<List<" + baseConfigModel.FileConfig.EntityName + ">> obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".GetPageList(param, pagination);");
  948. sb.AppendLine(" return Json(obj);");
  949. sb.AppendLine(" }");
  950. sb.AppendLine();
  951. sb.AppendLine(" [HttpGet]");
  952. sb.AppendLine(" public async Task<ActionResult> GetFormJson(int id)");
  953. sb.AppendLine(" {");
  954. sb.AppendLine(" TData<" + baseConfigModel.FileConfig.EntityName + "> obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".GetEntity(id);");
  955. sb.AppendLine(" return Json(obj);");
  956. sb.AppendLine(" }");
  957. sb.AppendLine(" #endregion");
  958. sb.AppendLine();
  959. sb.AppendLine(" #region 提交数据");
  960. sb.AppendLine(" [HttpPost]");
  961. sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "add") + "," + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "edit") + "\")]");
  962. sb.AppendLine(" public async Task<ActionResult> SaveFormJson(" + baseConfigModel.FileConfig.EntityName + " entity)");
  963. sb.AppendLine(" {");
  964. sb.AppendLine(" TData<int> obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".SaveForm(entity);");
  965. sb.AppendLine(" return Json(obj);");
  966. sb.AppendLine(" }");
  967. sb.AppendLine();
  968. sb.AppendLine(" [HttpPost]");
  969. sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "delete") + "\")]");
  970. sb.AppendLine(" public async Task<ActionResult> DeleteFormJson(string ids)");
  971. sb.AppendLine(" {");
  972. sb.AppendLine(" TData obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".DeleteFormById(ids);");
  973. sb.AppendLine(" return Json(obj);");
  974. sb.AppendLine(" }");
  975. sb.AppendLine(" #endregion");
  976. sb.AppendLine(" }");
  977. sb.AppendLine("}");
  978. return sb.ToString();
  979. }
  980. #endregion
  981. #region BuildIndex
  982. public string BuildIndex(BaseConfigModel baseConfigModel)
  983. {
  984. #region 初始化集合
  985. if (baseConfigModel.PageIndex.ButtonList == null)
  986. {
  987. baseConfigModel.PageIndex.ButtonList = new List<string>();
  988. }
  989. if (baseConfigModel.PageIndex.ColumnList == null)
  990. {
  991. baseConfigModel.PageIndex.ColumnList = new List<string>();
  992. }
  993. #endregion
  994. StringBuilder sb = new StringBuilder();
  995. sb.AppendLine("@{");
  996. sb.AppendLine(" Layout = \"~/Views/Shared/_Index.cshtml\";");
  997. sb.AppendLine(" }");
  998. sb.AppendLine("<div class=\"container-div\">");
  999. sb.AppendLine(" <div class=\"row\">");
  1000. #region 是否显示搜索
  1001. if (baseConfigModel.PageIndex.IsSearch == 1)
  1002. {
  1003. string fieldName = TextHelper.GetCustomValue(baseConfigModel.PageIndex.ColumnList.FirstOrDefault(), "fieldName");
  1004. string fieldNameLower = TableMappingHelper.FirstLetterLowercase(fieldName);
  1005. sb.AppendLine(" <div id=\"searchDiv\" class=\"col-sm-12 search-collapse\">");
  1006. sb.AppendLine(" <div class=\"select-list\">");
  1007. sb.AppendLine(" <ul>");
  1008. sb.AppendLine(" <li>");
  1009. sb.AppendLine(" " + fieldName + ":<input id=\"" + fieldNameLower + "\" col=\"" + fieldName + "\" type=\"text\" autocomplete=\"off\" />");
  1010. sb.AppendLine(" </li>");
  1011. sb.AppendLine(" <li>");
  1012. sb.AppendLine(" <a id=\"btnSearch\" class=\"btn btn-primary btn-sm\" onclick=\"searchGrid()\"><i class=\"fa fa-search\"></i>&nbsp;搜索</a>");
  1013. sb.AppendLine(" </li>");
  1014. sb.AppendLine(" </ul>");
  1015. sb.AppendLine(" </div>");
  1016. sb.AppendLine(" </div>");
  1017. }
  1018. #endregion
  1019. #region 是否显示工具栏
  1020. if (baseConfigModel.PageIndex.ButtonList.Where(p => p != "btnSearch").Any())
  1021. {
  1022. sb.AppendLine(" <div class=\"btn-group-sm hidden-xs\" id=\"toolbar\">");
  1023. if (baseConfigModel.PageIndex.ButtonList.Where(p => p == "btnAdd").Any())
  1024. {
  1025. sb.AppendLine(" <a id=\"btnAdd\" class=\"btn btn-success\" onclick=\"showSaveForm(true)\"><i class=\"fa fa-plus\"></i> 新增</a>");
  1026. }
  1027. if (baseConfigModel.PageIndex.ButtonList.Where(p => p == "btnEdit").Any())
  1028. {
  1029. sb.AppendLine(" <a id=\"btnEdit\" class=\"btn btn-primary disabled\" onclick=\"showSaveForm(false)\"><i class=\"fa fa-edit\"></i> 修改</a>");
  1030. }
  1031. if (baseConfigModel.PageIndex.ButtonList.Where(p => p == "btnDelete").Any())
  1032. {
  1033. sb.AppendLine(" <a id=\"btnDelete\" class=\"btn btn-danger disabled\" onclick=\"deleteForm()\"><i class=\"fa fa-remove\"></i> 删除</a>");
  1034. }
  1035. sb.AppendLine(" </div>");
  1036. }
  1037. #endregion
  1038. sb.AppendLine(" <div class=\"col-sm-12 select-table table-striped\">");
  1039. sb.AppendLine(" <table id=\"gridTable\" data-mobile-responsive=\"true\"></table>");
  1040. sb.AppendLine(" </div>");
  1041. sb.AppendLine(" </div>");
  1042. sb.AppendLine("</div>");
  1043. sb.AppendLine("");
  1044. sb.AppendLine("<script type=\"text/javascript\">");
  1045. sb.AppendLine(" $(function () {");
  1046. sb.AppendLine(" initGrid();");
  1047. sb.AppendLine(" });");
  1048. sb.AppendLine("");
  1049. sb.AppendLine(" function initGrid() {");
  1050. sb.AppendLine(" var queryUrl = '@Url.Content(\"~/" + baseConfigModel.OutputConfig.OutputModule + "/" + baseConfigModel.FileConfig.ClassPrefix + "/GetPageListJson\")';");
  1051. sb.AppendLine(" $('#gridTable').ysTable({");
  1052. sb.AppendLine(" url: queryUrl,");
  1053. sb.AppendLine(" columns: [");
  1054. sb.AppendLine(" { checkbox: true, visible: true },");
  1055. sb.AppendLine(" { field: 'Id', title: 'Id', visible: false },");
  1056. foreach (string column in baseConfigModel.PageIndex.ColumnList)
  1057. {
  1058. sb.AppendLine(" { field: '" + column + "', title: '" + column + "' },");
  1059. }
  1060. sb.AppendLine(" ],");
  1061. if (baseConfigModel.PageIndex.IsPagination == 0)
  1062. {
  1063. sb.AppendLine(" pagination: false,");
  1064. }
  1065. sb.AppendLine(" queryParams: function (params) {");
  1066. sb.AppendLine(" var pagination = $('#gridTable').ysTable('getPagination', params);");
  1067. sb.AppendLine(" var queryString = $('#searchDiv').getWebControls(pagination);");
  1068. sb.AppendLine(" return queryString;");
  1069. sb.AppendLine(" }");
  1070. sb.AppendLine(" });");
  1071. sb.AppendLine(" }");
  1072. sb.AppendLine("");
  1073. sb.AppendLine(" function searchGrid() {");
  1074. sb.AppendLine(" $('#gridTable').ysTable('search');");
  1075. sb.AppendLine(" resetToolbarStatus();");
  1076. sb.AppendLine(" }");
  1077. sb.AppendLine("");
  1078. #region 新增和修改方法
  1079. if (baseConfigModel.PageIndex.ButtonList.Where(p => p == "btnAdd" || p == "btnEdit").Any())
  1080. {
  1081. sb.AppendLine(" function showSaveForm(bAdd) {");
  1082. sb.AppendLine(" var id = '';");
  1083. sb.AppendLine(" if (!bAdd) {");
  1084. sb.AppendLine(" var selectedRow = $('#gridTable').bootstrapTable('getSelections');");
  1085. sb.AppendLine(" if (!ys.checkRowEdit(selectedRow)) {");
  1086. sb.AppendLine(" return;");
  1087. sb.AppendLine(" }");
  1088. sb.AppendLine(" else {");
  1089. sb.AppendLine(" id = selectedRow[0].Id;");
  1090. sb.AppendLine(" }");
  1091. sb.AppendLine(" }");
  1092. sb.AppendLine(" ys.openDialog({");
  1093. sb.AppendLine(" title: !ys.isNullOrEmpty(id) ? '编辑' : '添加',");
  1094. sb.AppendLine(" content: '@Url.Content(\"~/" + baseConfigModel.OutputConfig.OutputModule + "/" + baseConfigModel.FileConfig.ClassPrefix + "/" + baseConfigModel.FileConfig.ClassPrefix + "Form\")' + '?id=\' + id,");
  1095. sb.AppendLine(" width: '768px',");
  1096. sb.AppendLine(" height: '550px',");
  1097. sb.AppendLine(" callback: function (index, layero) {");
  1098. sb.AppendLine(" var iframeWin = window[layero.find('iframe')[0]['name']];");
  1099. sb.AppendLine(" iframeWin.saveForm(index);");
  1100. sb.AppendLine(" }");
  1101. sb.AppendLine(" });");
  1102. sb.AppendLine(" }");
  1103. }
  1104. #endregion
  1105. sb.AppendLine("");
  1106. #region 删除方法
  1107. if (baseConfigModel.PageIndex.ButtonList.Where(p => p == "btnDelete").Any())
  1108. {
  1109. sb.AppendLine(" function deleteForm() {");
  1110. sb.AppendLine(" var selectedRow = $('#gridTable').bootstrapTable('getSelections');");
  1111. sb.AppendLine(" if (ys.checkRowDelete(selectedRow)) {");
  1112. sb.AppendLine(" ys.confirm('确认要删除选中的' + selectedRow.length + '条数据吗?', function () {");
  1113. sb.AppendLine(" var ids = ys.getIds(selectedRow);");
  1114. sb.AppendLine(" ys.ajax({");
  1115. sb.AppendLine(" url: '@Url.Content(\"~/" + baseConfigModel.OutputConfig.OutputModule + "/" + baseConfigModel.FileConfig.ClassPrefix + "/DeleteFormJson\")' + '?ids=' + ids,");
  1116. sb.AppendLine(" type: 'post',");
  1117. sb.AppendLine(" success: function (obj) {");
  1118. sb.AppendLine(" if (obj.Success) {");
  1119. sb.AppendLine(" ys.msgSuccess(obj.Message);");
  1120. sb.AppendLine(" searchGrid();");
  1121. sb.AppendLine(" }");
  1122. sb.AppendLine(" else {");
  1123. sb.AppendLine(" ys.msgError(obj.Message);");
  1124. sb.AppendLine(" }");
  1125. sb.AppendLine(" }");
  1126. sb.AppendLine(" });");
  1127. sb.AppendLine(" });");
  1128. sb.AppendLine(" }");
  1129. sb.AppendLine(" }");
  1130. }
  1131. #endregion
  1132. sb.AppendLine("</script>");
  1133. return sb.ToString();
  1134. }
  1135. #endregion
  1136. #region BuildForm
  1137. public string BuildForm(BaseConfigModel baseConfigModel)
  1138. {
  1139. #region 初始化集合
  1140. if (baseConfigModel.PageForm.FieldList == null)
  1141. {
  1142. baseConfigModel.PageForm.FieldList = new List<string>();
  1143. }
  1144. #endregion
  1145. StringBuilder sb = new StringBuilder();
  1146. sb.AppendLine("@{");
  1147. sb.AppendLine(" Layout = \"~/Views/Shared/_FormWhite.cshtml\";");
  1148. sb.AppendLine("}");
  1149. sb.AppendLine("");
  1150. sb.AppendLine("<div class=\"wrapper animated fadeInRight\">");
  1151. sb.AppendLine(" <form id=\"form\" class=\"form-horizontal m\">");
  1152. #region 表单控件
  1153. if (baseConfigModel.PageForm.FieldList.Count > 0)
  1154. {
  1155. string field = string.Empty;
  1156. string fieldLower = string.Empty;
  1157. switch (baseConfigModel.PageForm.ShowMode)
  1158. {
  1159. case 1:
  1160. for (int i = 0; i < baseConfigModel.PageForm.FieldList.Count; i++)
  1161. {
  1162. field = baseConfigModel.PageForm.FieldList[i];
  1163. fieldLower = TableMappingHelper.FirstLetterLowercase(field);
  1164. sb.AppendLine(" <div class=\"form-group\">");
  1165. sb.AppendLine(" <label class=\"col-sm-3 control-label \">" + field + (i == 0 ? "<font class=\"red\"> *</font>" : string.Empty) + "</label>");
  1166. sb.AppendLine(" <div class=\"col-sm-8\">");
  1167. sb.AppendLine(" <input id=\"" + fieldLower + "\" col=\"" + field + "\" type=\"text\" class=\"form-control\" autocomplete=\"off\" />");
  1168. sb.AppendLine(" </div>");
  1169. sb.AppendLine(" </div>");
  1170. }
  1171. break;
  1172. case 2:
  1173. for (int i = 0; i < baseConfigModel.PageForm.FieldList.Count; i++)
  1174. {
  1175. field = baseConfigModel.PageForm.FieldList[i];
  1176. fieldLower = TableMappingHelper.FirstLetterLowercase(field);
  1177. if (i % 2 == 0)
  1178. {
  1179. sb.AppendLine(" <div class=\"form-group\">");
  1180. }
  1181. sb.AppendLine(" <label class=\"col-sm-2 control-label \">" + field + "<font class=\"red\"> *</font></label>");
  1182. sb.AppendLine(" <div class=\"col-sm-4\">");
  1183. sb.AppendLine(" <input id=\"" + fieldLower + "\" col=\"" + field + "\" type=\"text\" class=\"form-control\" autocomplete=\"off\" />");
  1184. sb.AppendLine(" </div>");
  1185. if (i % 2 == 1)
  1186. {
  1187. sb.AppendLine(" </div>");
  1188. }
  1189. }
  1190. break;
  1191. }
  1192. }
  1193. #endregion
  1194. sb.AppendLine(" </form>");
  1195. sb.AppendLine("</div>");
  1196. sb.AppendLine("");
  1197. sb.AppendLine("<script type=\"text/javascript\">");
  1198. sb.AppendLine(" var id = ys.request(\"id\");");
  1199. sb.AppendLine(" $(function () {");
  1200. sb.AppendLine(" getForm();");
  1201. sb.AppendLine("");
  1202. sb.AppendLine(" $('#form').validate({");
  1203. sb.AppendLine(" rules: {");
  1204. sb.AppendLine(" " + TextHelper.GetCustomValue(TableMappingHelper.FirstLetterLowercase(baseConfigModel.PageForm.FieldList.FirstOrDefault()), "fieldName") + ": { required: true }");
  1205. sb.AppendLine(" }");
  1206. sb.AppendLine(" });");
  1207. sb.AppendLine(" });");
  1208. sb.AppendLine("");
  1209. sb.AppendLine(" function getForm() {");
  1210. sb.AppendLine(" if (!ys.isNullOrEmpty(id)) {");
  1211. sb.AppendLine(" ys.ajax({");
  1212. sb.AppendLine(" url: '@Url.Content(\"~/" + baseConfigModel.OutputConfig.OutputModule + "/" + baseConfigModel.FileConfig.ClassPrefix + "/GetFormJson\")' + '?id=' + id,");
  1213. sb.AppendLine(" type: 'get',");
  1214. sb.AppendLine(" success: function (obj) {");
  1215. sb.AppendLine(" if (obj.Success) {");
  1216. sb.AppendLine(" $('#form').setWebControls(obj.Data);");
  1217. sb.AppendLine(" }");
  1218. sb.AppendLine(" }");
  1219. sb.AppendLine(" });");
  1220. sb.AppendLine(" }");
  1221. sb.AppendLine(" else {");
  1222. sb.AppendLine(" var defaultData = {};");
  1223. sb.AppendLine(" $('#form').setWebControls(defaultData);");
  1224. sb.AppendLine(" }");
  1225. sb.AppendLine(" }");
  1226. sb.AppendLine("");
  1227. sb.AppendLine(" function saveForm(index) {");
  1228. sb.AppendLine(" if ($('#form').validate().form()) {");
  1229. sb.AppendLine(" var postData = $('#form').getWebControls({ Id: id });");
  1230. sb.AppendLine(" ys.ajax({");
  1231. sb.AppendLine(" url: '@Url.Content(\"~/" + baseConfigModel.OutputConfig.OutputModule + "/" + baseConfigModel.FileConfig.ClassPrefix + "/SaveFormJson\")',");
  1232. sb.AppendLine(" type: 'post',");
  1233. sb.AppendLine(" data: postData,");
  1234. sb.AppendLine(" success: function (obj) {");
  1235. sb.AppendLine(" if (obj.Success) {");
  1236. sb.AppendLine(" ys.msgSuccess(obj.Message);");
  1237. sb.AppendLine(" parent.searchGrid();");
  1238. sb.AppendLine(" parent.layer.close(index);");
  1239. sb.AppendLine(" }");
  1240. sb.AppendLine(" else {");
  1241. sb.AppendLine(" ys.msgError(obj.Message);");
  1242. sb.AppendLine(" }");
  1243. sb.AppendLine(" }");
  1244. sb.AppendLine(" });");
  1245. sb.AppendLine(" }");
  1246. sb.AppendLine(" }");
  1247. sb.AppendLine("</script>");
  1248. sb.AppendLine("");
  1249. return sb.ToString();
  1250. }
  1251. #endregion
  1252. #region BuildMenu
  1253. public string BuildMenu(BaseConfigModel baseConfigModel)
  1254. {
  1255. StringBuilder sb = new StringBuilder();
  1256. sb.AppendLine();
  1257. sb.AppendLine(" 菜单路径:" + baseConfigModel.OutputConfig.OutputModule + "/" + baseConfigModel.FileConfig.ClassPrefix + "/" + baseConfigModel.FileConfig.PageIndexName);
  1258. sb.AppendLine();
  1259. string modulePrefix = GetModulePrefix(baseConfigModel);
  1260. string classPrefix = baseConfigModel.FileConfig.ClassPrefix.ToLower();
  1261. sb.AppendLine(" 页面显示权限:" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "view"));
  1262. sb.AppendLine();
  1263. List<KeyValue> list = GetButtonAuthorizeList();
  1264. if (baseConfigModel.PageIndex.IsSearch == 1)
  1265. {
  1266. KeyValue button = list.Where(p => p.Key == "btnSearch").FirstOrDefault();
  1267. sb.AppendLine(" 按钮" + button.Description + "权限:" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, button.Value));
  1268. }
  1269. foreach (string btn in baseConfigModel.PageIndex.ButtonList)
  1270. {
  1271. KeyValue button = list.Where(p => p.Key == btn).FirstOrDefault();
  1272. sb.AppendLine(" 按钮" + button.Description + "权限:" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, button.Value));
  1273. }
  1274. sb.AppendLine();
  1275. return sb.ToString();
  1276. }
  1277. #endregion
  1278. #region CreateCode
  1279. public async Task<List<KeyValue>> CreateCode(BaseConfigModel baseConfigModel, string code, IMenuCacheBusiness menuCacheBusiness)
  1280. {
  1281. List<KeyValue> result = new List<KeyValue>();
  1282. JObject param = code.ToJObject();
  1283. #region 实体类
  1284. if (!string.IsNullOrEmpty(param["CodeEntity"].ParseToString()))
  1285. {
  1286. string codeEntity = HttpUtility.HtmlDecode(param["CodeEntity"].ToString());
  1287. string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputEntity, "YiSha.Entity", baseConfigModel.OutputConfig.OutputModule, baseConfigModel.FileConfig.EntityName + ".cs");
  1288. CreateCodeFile(codePath, codeEntity, out bool isAdd, true);
  1289. result.Add(new KeyValue { Key = "实体类", Value = codePath });
  1290. }
  1291. #endregion
  1292. #region 实体查询类
  1293. if (!param["CodeEntityParam"].IsEmpty())
  1294. {
  1295. string codeListEntity = HttpUtility.HtmlDecode(param["CodeEntityParam"].ToString());
  1296. string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputEntity, "YiSha.Model", "Param", baseConfigModel.OutputConfig.OutputModule, baseConfigModel.FileConfig.EntityParamName + ".cs");
  1297. CreateCodeFile(codePath, codeListEntity, out bool isAdd); //查询实体不需要生成
  1298. result.Add(new KeyValue { Key = "实体查询类", Value = codePath });
  1299. }
  1300. #endregion
  1301. #region 服务接口类
  1302. if (!param["CodeIService"].IsEmpty())
  1303. {
  1304. string codeService = HttpUtility.HtmlDecode(param["CodeIService"].ToString());
  1305. string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.IService", baseConfigModel.OutputConfig.OutputModule, "I" + baseConfigModel.FileConfig.IServiceName + ".cs");
  1306. CreateCodeFile(codePath, codeService, out bool isAdd, true);
  1307. result.Add(new KeyValue { Key = "服务接口类", Value = codePath });
  1308. }
  1309. #endregion
  1310. #region 服务接口类(部分类)
  1311. if (!param["CodeIServicePartial"].IsEmpty())
  1312. {
  1313. string codeService = HttpUtility.HtmlDecode(param["CodeIServicePartial"].ToString());
  1314. string partialPath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.IService", baseConfigModel.OutputConfig.OutputModule, "Partial");
  1315. CreatePartialFilePath(partialPath);
  1316. string codePath = Path.Combine(partialPath, "I" + baseConfigModel.FileConfig.IServiceName + ".cs");
  1317. CreateCodeFile(codePath, codeService, out bool isAdd);
  1318. result.Add(new KeyValue { Key = "服务接口类(部分类)", Value = codePath });
  1319. }
  1320. #endregion
  1321. #region 服务实现类
  1322. if (!param["CodeService"].IsEmpty())
  1323. {
  1324. string codeService = HttpUtility.HtmlDecode(param["CodeService"].ToString());
  1325. string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.Service", baseConfigModel.OutputConfig.OutputModule, baseConfigModel.FileConfig.ServiceName + ".cs");
  1326. CreateCodeFile(codePath, codeService, out bool isAdd, true);
  1327. result.Add(new KeyValue { Key = "服务实现类", Value = codePath });
  1328. }
  1329. #endregion
  1330. #region 服务实现类(部分类)
  1331. if (!param["CodeServicePartial"].IsEmpty())
  1332. {
  1333. string codeService = HttpUtility.HtmlDecode(param["CodeServicePartial"].ToString());
  1334. string partialPath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.Service", baseConfigModel.OutputConfig.OutputModule, "Partial");
  1335. CreatePartialFilePath(partialPath);
  1336. string codePath = Path.Combine(partialPath, baseConfigModel.FileConfig.ServiceName + ".cs");
  1337. CreateCodeFile(codePath, codeService, out bool isAdd);
  1338. result.Add(new KeyValue { Key = "服务实现类(部分类)", Value = codePath });
  1339. }
  1340. #endregion
  1341. #region 业务接口类
  1342. if (!param["CodeIBusiness"].IsEmpty())
  1343. {
  1344. string codeBusiness = HttpUtility.HtmlDecode(param["CodeIBusiness"].ToString());
  1345. string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.IBusiness", baseConfigModel.OutputConfig.OutputModule, "I" + baseConfigModel.FileConfig.IBusinessName + ".cs");
  1346. CreateCodeFile(codePath, codeBusiness, out bool isAdd, true);
  1347. result.Add(new KeyValue { Key = "业务接口类", Value = codePath });
  1348. }
  1349. #endregion
  1350. #region 业务接口类(部分类)
  1351. if (!param["CodeIBusinessPartial"].IsEmpty())
  1352. {
  1353. string codeBusiness = HttpUtility.HtmlDecode(param["CodeIBusinessPartial"].ToString());
  1354. string partialPath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.IBusiness", baseConfigModel.OutputConfig.OutputModule, "Partial");
  1355. CreatePartialFilePath(partialPath);
  1356. string codePath = Path.Combine(partialPath, "I" + baseConfigModel.FileConfig.IBusinessName + ".cs");
  1357. CreateCodeFile(codePath, codeBusiness, out bool isAdd);
  1358. result.Add(new KeyValue { Key = "业务接口类(部分类)", Value = codePath });
  1359. }
  1360. #endregion
  1361. #region 业务实现类
  1362. if (!param["CodeBusiness"].IsEmpty())
  1363. {
  1364. string codeBusiness = HttpUtility.HtmlDecode(param["CodeBusiness"].ToString());
  1365. string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.Business", baseConfigModel.OutputConfig.OutputModule, baseConfigModel.FileConfig.BusinessName + ".cs");
  1366. CreateCodeFile(codePath, codeBusiness, out bool isAdd, true);
  1367. result.Add(new KeyValue { Key = "业务实现类", Value = codePath });
  1368. }
  1369. #endregion
  1370. #region 业务实现类(部分类)
  1371. if (!param["CodeBusinessPartial"].IsEmpty())
  1372. {
  1373. string codeBusiness = HttpUtility.HtmlDecode(param["CodeBusinessPartial"].ToString());
  1374. string partialPath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.Business", baseConfigModel.OutputConfig.OutputModule, "Partial");
  1375. CreatePartialFilePath(partialPath);
  1376. string codePath = Path.Combine(partialPath, baseConfigModel.FileConfig.BusinessName + ".cs");
  1377. CreateCodeFile(codePath, codeBusiness, out bool isAdd);
  1378. result.Add(new KeyValue { Key = "业务实现类(部分类)", Value = codePath });
  1379. }
  1380. #endregion
  1381. #region 控制器
  1382. if (!param["CodeController"].IsEmpty())
  1383. {
  1384. string codeController = HttpUtility.HtmlDecode(param["CodeController"].ToString());
  1385. string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputWeb, "Areas", baseConfigModel.OutputConfig.OutputModule, "Controllers", baseConfigModel.FileConfig.ControllerName + ".cs");
  1386. CreateCodeFile(codePath, codeController, out bool isAdd);
  1387. result.Add(new KeyValue { Key = "控制器", Value = codePath });
  1388. }
  1389. #endregion
  1390. #region 列表页
  1391. if (!param["CodeIndex"].IsEmpty())
  1392. {
  1393. //代码生成器,生成菜单按钮权限默认获取系统数据库数据
  1394. DBConnectTypeEnum dbConnectType = DBConnectTypeEnum.SystemDB;
  1395. string codeIndex = HttpUtility.HtmlDecode(param["CodeIndex"].ToString());
  1396. string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputWeb, "Areas", baseConfigModel.OutputConfig.OutputModule, "Views", baseConfigModel.FileConfig.ClassPrefix, baseConfigModel.FileConfig.PageIndexName + ".cshtml");
  1397. CreateCodeFile(codePath, codeIndex, out bool isAdd);
  1398. result.Add(new KeyValue { Key = "列表页", Value = codePath });
  1399. if (isAdd)
  1400. {
  1401. //列表页面添加成功后,才生成菜单权限
  1402. // 生成菜单
  1403. RepositoryFactory repositoryFactory = new RepositoryFactory();
  1404. List<KeyValue> buttonAuthorizeList = GetButtonAuthorizeList();
  1405. string menuUrl = baseConfigModel.OutputConfig.OutputModule + "/" + baseConfigModel.FileConfig.ClassPrefix + "/" + baseConfigModel.FileConfig.PageIndexName;
  1406. string modulePrefix = GetModulePrefix(baseConfigModel);
  1407. string classPrefix = baseConfigModel.FileConfig.ClassPrefix.ToLower();
  1408. MenuEntity menuEntity = new MenuEntity
  1409. {
  1410. MenuName = baseConfigModel.FileConfig.ClassDescription,
  1411. MenuUrl = menuUrl,
  1412. MenuType = (int)MenuTypeEnum.Menu,
  1413. Authorize = string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "view")
  1414. };
  1415. TData obj = await AddMenu(repositoryFactory, menuEntity, dbConnectType);
  1416. if (obj.Success)
  1417. {
  1418. result.Add(new KeyValue { Key = "菜单(刷新页面可见)", Value = menuUrl });
  1419. if (baseConfigModel.PageIndex.IsSearch == 1)
  1420. {
  1421. // 按钮搜索权限
  1422. KeyValue button = buttonAuthorizeList.Where(p => p.Key == "btnSearch").FirstOrDefault();
  1423. MenuEntity buttonEntity = new MenuEntity
  1424. {
  1425. ParentId = menuEntity.Id,
  1426. MenuName = baseConfigModel.FileConfig.ClassDescription + button.Description,
  1427. MenuType = (int)MenuTypeEnum.Button,
  1428. Authorize = string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, button.Value)
  1429. };
  1430. await AddMenu(repositoryFactory, buttonEntity, dbConnectType);
  1431. }
  1432. foreach (string btn in baseConfigModel.PageIndex.ButtonList)
  1433. {
  1434. KeyValue button = buttonAuthorizeList.Where(p => p.Key == btn).FirstOrDefault();
  1435. MenuEntity buttonEntity = new MenuEntity
  1436. {
  1437. ParentId = menuEntity.Id,
  1438. MenuName = baseConfigModel.FileConfig.ClassDescription + button.Description,
  1439. MenuType = (int)MenuTypeEnum.Button,
  1440. Authorize = string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, button.Value)
  1441. };
  1442. await AddMenu(repositoryFactory, buttonEntity, dbConnectType);
  1443. }
  1444. menuCacheBusiness.Remove();
  1445. }
  1446. }
  1447. }
  1448. #endregion
  1449. #region 表单页
  1450. if (!param["CodeForm"].IsEmpty())
  1451. {
  1452. string codeSave = HttpUtility.HtmlDecode(param["CodeForm"].ToString());
  1453. string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputWeb, "Areas", baseConfigModel.OutputConfig.OutputModule, "Views", baseConfigModel.FileConfig.ClassPrefix, baseConfigModel.FileConfig.PageFormName + ".cshtml");
  1454. CreateCodeFile(codePath, codeSave, out bool isAdd);
  1455. result.Add(new KeyValue { Key = "表单页", Value = codePath });
  1456. }
  1457. #endregion
  1458. return result;
  1459. }
  1460. private async Task<TData> AddMenu(RepositoryFactory repositoryFactory, MenuEntity menuEntity, DBConnectTypeEnum dbConnectType)
  1461. {
  1462. TData obj = new TData();
  1463. IEnumerable<MenuEntity> menuList = await repositoryFactory.BaseRepository(dbConnectType).FindList<MenuEntity>();
  1464. if (!menuList.Where(p => p.MenuName == menuEntity.MenuName && p.Authorize == menuEntity.Authorize).Any())
  1465. {
  1466. menuEntity.MenuSort = menuList.Max(p => p.MenuSort) + 1;
  1467. menuEntity.MenuStatus = 1;
  1468. await menuEntity.Create();
  1469. await repositoryFactory.BaseRepository(dbConnectType).Insert(menuEntity);
  1470. obj.Tag = 1;
  1471. }
  1472. return obj;
  1473. }
  1474. #endregion
  1475. #region 私有方法
  1476. #region GetProjectRootPath
  1477. private string GetProjectRootPath(string path)
  1478. {
  1479. path = path.ParseToString();
  1480. path = path.Trim('\\');
  1481. if (GlobalContext.SystemConfig.Debug)
  1482. {
  1483. // 向上找二级
  1484. path = Directory.GetParent(path).FullName;
  1485. path = Directory.GetParent(path).FullName;
  1486. }
  1487. return path;
  1488. }
  1489. #endregion
  1490. #region SetClassDescription
  1491. private void SetClassDescription(string type, BaseConfigModel baseConfigModel, StringBuilder sb)
  1492. {
  1493. sb.AppendLine(" /// <summary>");
  1494. sb.AppendLine(" /// 创 建:" + baseConfigModel.FileConfig.CreateName);
  1495. sb.AppendLine(" /// 日 期:" + baseConfigModel.FileConfig.CreateDate);
  1496. sb.AppendLine(" /// 描 述:" + baseConfigModel.FileConfig.ClassDescription + type);
  1497. sb.AppendLine(" /// </summary>");
  1498. }
  1499. #endregion
  1500. #region GetButtonAuthorizeList
  1501. private List<KeyValue> GetButtonAuthorizeList()
  1502. {
  1503. var list = new List<KeyValue>();
  1504. list.Add(new KeyValue { Key = "btnSearch", Value = "search", Description = "搜索" });
  1505. list.Add(new KeyValue { Key = "btnAdd", Value = "add", Description = "新增" });
  1506. list.Add(new KeyValue { Key = "btnEdit", Value = "edit", Description = "修改" });
  1507. list.Add(new KeyValue { Key = "btnDelete", Value = "delete", Description = "删除" });
  1508. return list;
  1509. }
  1510. #endregion
  1511. private string GetModulePrefix(BaseConfigModel baseConfigModel)
  1512. {
  1513. return baseConfigModel.OutputConfig.OutputModule.Replace("Manage", string.Empty).ToLower();
  1514. }
  1515. private string GetBaseEntity(DataTable dt)
  1516. {
  1517. string entity = string.Empty;
  1518. var columnList = dt.AsEnumerable().Select(p => p["TableColumn"].ParseToString()).ToList();
  1519. bool id = columnList.Where(p => p.ToLower().Equals("Id".ToLower())).Any();
  1520. bool baseIsDelete = columnList.Where(p => p.ToLower().Equals("BaseIsDelete".ToLower())).Any();
  1521. bool baseVersion = columnList.Where(p => p.ToLower().Equals("BaseVersion".ToLower())).Any();
  1522. bool baseModifyTime = columnList.Where(p => p.ToLower().Equals("BaseModifyTime".ToLower())).Any();
  1523. bool baseModifierId = columnList.Where(p => p.ToLower().Equals("BaseModifierId".ToLower())).Any();
  1524. bool baseCreateTime = columnList.Where(p => p.ToLower().Equals("BaseCreateTime".ToLower())).Any();
  1525. bool baseCreatorId = columnList.Where(p => p.ToLower().Equals("BaseCreatorId".ToLower())).Any();
  1526. if (!id)
  1527. {
  1528. throw new Exception("数据库表必须有主键Id字段");
  1529. }
  1530. if (baseIsDelete && baseVersion && baseModifyTime && baseModifierId && baseCreateTime && baseCreatorId)
  1531. {
  1532. entity = "BaseExtensionEntity";
  1533. }
  1534. else if (baseVersion && baseModifyTime && baseModifierId && baseCreateTime && baseCreatorId)
  1535. {
  1536. entity = "BaseModifyEntity";
  1537. }
  1538. else if (baseCreateTime && baseCreatorId)
  1539. {
  1540. entity = "BaseCreateEntity";
  1541. }
  1542. else
  1543. {
  1544. entity = "BaseEntity";
  1545. }
  1546. return entity;
  1547. }
  1548. private string GetSaveFormCreate(string entity)
  1549. {
  1550. string line = string.Empty;
  1551. switch (entity)
  1552. {
  1553. case "BaseEntity":
  1554. line = "entity.Create();";
  1555. break;
  1556. case "BaseCreateEntity":
  1557. line = "await entity.Create();";
  1558. break;
  1559. case "BaseModifyEntity":
  1560. line = "await entity.Create();";
  1561. break;
  1562. case "BaseExtensionEntity":
  1563. line = "await entity.Create();";
  1564. break;
  1565. }
  1566. return line;
  1567. }
  1568. private string GetSaveFormModify(string entity)
  1569. {
  1570. string line = string.Empty;
  1571. switch (entity)
  1572. {
  1573. case "BaseEntity":
  1574. line = string.Empty;
  1575. break;
  1576. case "BaseCreateEntity":
  1577. line = string.Empty;
  1578. break;
  1579. case "BaseModifyEntity":
  1580. line = "await entity.Modify();";
  1581. break;
  1582. case "BaseExtensionEntity":
  1583. line = "await entity.Modify();";
  1584. break;
  1585. }
  1586. return line;
  1587. }
  1588. /// <summary>
  1589. /// 创建部分类文件路径
  1590. /// </summary>
  1591. /// <param name="partialPath"></param>
  1592. private void CreatePartialFilePath(string partialPath)
  1593. {
  1594. if (!Directory.Exists(partialPath))
  1595. Directory.CreateDirectory(partialPath);
  1596. }
  1597. /// <summary>
  1598. /// 创建代码文件
  1599. /// </summary>
  1600. /// <param name="codePath">地址</param>
  1601. /// <param name="codeContent">内容</param>
  1602. /// <param name="isAdd">是否添加成功 true成功,false未添加</param>
  1603. /// <param name="isCovered">是否覆盖 true覆盖,false不覆盖 默认不覆盖</param>
  1604. private void CreateCodeFile(string codePath, string codeContent, out bool isAdd, bool isCovered = false)
  1605. {
  1606. isAdd = false;
  1607. if (!File.Exists(codePath))
  1608. {
  1609. isAdd = true;
  1610. //代码不存在直接创建
  1611. FileHelper.CreateFile(codePath, codeContent);
  1612. }
  1613. else
  1614. {
  1615. //代码已存在,判断是否需要覆盖
  1616. if (isCovered)
  1617. {
  1618. isAdd = true;
  1619. File.Delete(codePath);
  1620. FileHelper.CreateFile(codePath, codeContent);
  1621. }
  1622. }
  1623. }
  1624. /// <summary>
  1625. /// 获取数据库操作类型
  1626. /// </summary>
  1627. /// <param name="dbConnectType"></param>
  1628. /// <returns></returns>
  1629. private string GetDBConnectTypeEnum(DBConnectTypeEnum dbConnectType)
  1630. {
  1631. //默认空,如果数据库操作类型不存在,生成的代码则会报错
  1632. var result = "";
  1633. switch (dbConnectType)
  1634. {
  1635. case DBConnectTypeEnum.KjhDB:
  1636. result = "DBConnectTypeEnum.KjhDB";
  1637. break;
  1638. case DBConnectTypeEnum.SystemDB:
  1639. result = "DBConnectTypeEnum.SystemDB";
  1640. break;
  1641. case DBConnectTypeEnum.TkDB:
  1642. result = "DBConnectTypeEnum.TkDB";
  1643. break;
  1644. case DBConnectTypeEnum.ZxDB:
  1645. result = "DBConnectTypeEnum.ZxDB";
  1646. break;
  1647. }
  1648. return result;
  1649. }
  1650. #endregion
  1651. }
  1652. }