diff --git a/CodeSmith/EF/CSharp/Web/Application.cst b/CodeSmith/EF/CSharp/Web/Application.cst new file mode 100644 index 00000000..8425cd7b --- /dev/null +++ b/CodeSmith/EF/CSharp/Web/Application.cst @@ -0,0 +1,138 @@ +<%-- +Name: Database Table Properties +Author: yubaolee +Description: Create a list of properties from a database table +--%> +<%@ CodeTemplate Language="C#" Encoding="utf-8" TargetLanguage="C#" Debug="False" Description="应用层" %> +<%@ Property Name="ModuleName" Type="String" Category="Context" Description="模块名称" %> +<%@ Property Name="NeedViewModel" Type="Boolean" Category="Context" Default="False" Description="是否需要ViewModel" %> +<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %> +<%@ Assembly Name="SchemaExplorer" %> +<%@ Import Namespace="SchemaExplorer" %> + + +<%if(NeedViewModel){ %> +using OpenAuth.App.ViewModel; +<%} %> +using OpenAuth.Domain; +using OpenAuth.Domain.Interface; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace OpenAuth.App +{ + public class <%=ModuleName%>ManagerApp + { + private I<%=ModuleName%>Repository _repository; + private IOrgRepository _orgRepository; + + public <%=ModuleName%>ManagerApp(I<%=ModuleName%>Repository repository, + IOrgRepository orgRepository) + { + _repository = repository; + _orgRepository = orgRepository; + } + + public int Get<%=GetModelName()%>CntInOrg(int orgId) + { + if (orgId == 0) + { + return _repository.Find(null).Count(); + } + else + { + return _repository.Get<%=GetModelName()%>CntInOrgs(GetSubOrgIds(orgId)); + } + } + + public List<<%=GetModelName()%>> LoadAll() + { + return _repository.Find(null).ToList(); + } + + /// + /// 加载一个节点下面的一个或全部<%=GetModelName()%>s + /// + public dynamic Load(int orgId, int pageindex, int pagesize) + { + IEnumerable<<%=ModuleName%>> <%=ModuleName%>s; + int total = 0; + if (orgId == 0) + { + <%=ModuleName%>s = _repository.Load<%=ModuleName%>s(pageindex, pagesize); + total = _repository.GetCount(); + } + else + { + <%=ModuleName%>s = _repository.LoadInOrgs(pageindex, pagesize,GetSubOrgIds(orgId)); + total = _repository.Get<%=ModuleName%>CntInOrgs(orgId); + } + <%if(NeedViewModel){ %> + var <%=ModuleName%>views = new List<<%=ModuleName%>View>(); + foreach (var <%=ModuleName%> in <%=ModuleName%>s) + { + <%=ModuleName%>View uv = <%=ModuleName%>; + uv.Organizations = string.Join(",", _orgRepository.LoadBy<%=ModuleName%>(<%=ModuleName%>.Id).Select(u => u.Name).ToList()); + <%=ModuleName%>views.Add(uv); + } + <%} %> + + return new + { + total = total, + list = <%=GetModelName()%>s, + pageCurrent = pageindex + }; + } + + /// + /// 获取当前节点的所有下级节点 + /// + private int[] GetSubOrgIds(int orgId) + { + var org = _orgRepository.FindSingle(u => u.Id == orgId); + var orgs = _orgRepository.Find(u => u.CascadeId.Contains(org.CascadeId)).Select(u => u.Id).ToArray(); + return orgs; + } + + public <%=GetModelName()%> Find(int id) + { + var <%=ModuleName.ToLower()%> = _repository.FindSingle(u => u.Id == id); + if (<%=ModuleName.ToLower()%> == null) return new <%=GetModelName()%>(); + + return <%=ModuleName.ToLower() %>; + } + + public void Delete(int id) + { + _repository.Delete(id); + } + + public void AddOrUpdate(<%=GetModelName()%> model) + { + <%=ModuleName%> <%=ModuleName.ToLower()%> = new <%=ModuleName%>(); + model.CopyTo(<%=ModuleName.ToLower()%>); + + if (<%=ModuleName.ToLower()%>.Id == 0) + { + _repository.Add(<%=ModuleName.ToLower()%>); + } + else + { + _repository.Update(<%=ModuleName.ToLower()%>); + } + + } + + + } +} \ No newline at end of file diff --git a/CodeSmith/EF/CSharp/Web/Controller.cst b/CodeSmith/EF/CSharp/Web/Controller.cst new file mode 100644 index 00000000..c4352691 --- /dev/null +++ b/CodeSmith/EF/CSharp/Web/Controller.cst @@ -0,0 +1,110 @@ +<%-- +Name: Database Table Properties +Author: yubaolee +Description: Create a list of properties from a database table +--%> +<%@ CodeTemplate Language="C#" Encoding="utf-8" TargetLanguage="C#" Debug="False" Description="控制器" %> +<%@ Property Name="ModuleName" Type="String" Category="Context" Description="模块名称" %> +<%@ Property Name="NeedViewModel" Type="Boolean" Category="Context" Default="False" Description="是否需要ViewModel" %> +<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %> +<%@ Assembly Name="SchemaExplorer" %> +<%@ Import Namespace="SchemaExplorer" %> + + +using System; +using System.Web.Mvc; +using Infrastructure; +using OpenAuth.App; +<%if(NeedViewModel){ %> +using OpenAuth.App.ViewModel; +<%} %> +using OpenAuth.Domain; + +namespace OpenAuth.Mvc.Controllers +{ + public class <%=ModuleName%>ManagerController : BaseController + { + private <%=ModuleName%>ManagerApp _app; + + public <%=ModuleName%>ManagerController() + { + _app = AutofacExt.GetFromFac<<%=ModuleName%>ManagerApp>(); + } + + // + // GET: /UserManager/ + public ActionResult Index() + { + return View(); + } + + public ActionResult Add(int id = 0) + { + return View(_app.Find(id)); + } + + //添加或修改<%=ModuleName %> + [HttpPost] + public string Add(<%=GetModelName()%> model) + { + try + { + _app.AddOrUpdate(model); + + } + catch (Exception ex) + { + BjuiResponse.statusCode = "300"; + BjuiResponse.message = ex.Message; + } + return JsonHelper.Instance.Serialize(BjuiResponse); + } + + /// + /// 加载节点下面的所有<%=ModuleName %>s + /// + public string Load(int parentId, int pageCurrent = 1, int pageSize = 30) + { + return JsonHelper.Instance.Serialize(_app.Load(parentId, pageCurrent, pageSize)); + } + + public string LoadForTree() + { + var models = _app.LoadAll(); + //添加根节点 + models.Add(new <%=ModuleName %> + { + Id = 0, + ParentId = -1, + Name = "根结点", + CascadeId = "0" + }); + return JsonHelper.Instance.Serialize(models); + } + + public string Delete(int Id) + { + try + { + _app.Delete(Id); + } + catch (Exception e) + { + BjuiResponse.statusCode = "300"; + BjuiResponse.message = e.Message; + } + + return JsonHelper.Instance.Serialize(BjuiResponse); + } + + + } +} \ No newline at end of file diff --git a/CodeSmith/EF/CSharp/Web/Index.cshtml.cst b/CodeSmith/EF/CSharp/Web/Index.cshtml.cst new file mode 100644 index 00000000..99e232d5 --- /dev/null +++ b/CodeSmith/EF/CSharp/Web/Index.cshtml.cst @@ -0,0 +1,105 @@ +<%-- +Name: 列表页面 +Author: yubaolee +Description: 列表页面 +--%> +<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Encoding="utf-8" Description="添加模块" %> +<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" +Description="连接的数据库" %> +<%@ Property Name="ModuleName" Type="String" Category="Context" Description="模块名称" %> + +<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %> +<%@ Assembly Name="SchemaExplorer" %> +<%@ Import Namespace="SchemaExplorer" %> +<%@ Assembly Src="Util.cs" %> +<%@ Import Namespace="Util" %> + +@section header +{ + +} + + +
+ + + + + + <% foreach (ColumnSchema column in this.SourceTable.Columns) {%> + + <% }%> + + + +
<%=Tools.GetDescription(column)%>
+
+ + + + + + + + + diff --git a/CodeSmith/EF/CSharp/Web/Util.cs b/CodeSmith/EF/CSharp/Web/Util.cs new file mode 100644 index 00000000..0ebb0882 --- /dev/null +++ b/CodeSmith/EF/CSharp/Web/Util.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using CodeSmith.Engine; +using SchemaExplorer; + +namespace Util{ + public class Tools{ + public static String GetDescription(ColumnSchema column) { //得到字段的描述 + if(string.IsNullOrEmpty(column.Description)) + return column.Name; + else + return column.Description; + } + + public static bool NeedCascade(TableSchema SourceTable){ //判断表中是否需要下拉选择树 + return SourceTable.Columns.Contains("ParentId") + || SourceTable.Columns.Contains("CascadeId") ; + } + } +} \ No newline at end of file diff --git a/CodeSmith/EF/CSharp/WebGenerate.cst b/CodeSmith/EF/CSharp/WebGenerate.cst new file mode 100644 index 00000000..d8b00750 --- /dev/null +++ b/CodeSmith/EF/CSharp/WebGenerate.cst @@ -0,0 +1,115 @@ +<%@ Template Language="C#" TargetLanguage="Text" Debug="True" OutputType="None" %> + +<%@ Assembly Name="SchemaExplorer" %> +<%@ Assembly Name="CodeSmith.CustomProperties" %> + +<%@ Assembly Name="Mono.Cecil" Path="..\Common" %> +<%@ Assembly Name="ICSharpCode.NRefactory" Path="..\Common" %> +<%@ Assembly Name="ICSharpCode.NRefactory.CSharp" Path="..\Common" %> + +<%@ Assembly Src="Internal\Model.cs" %> +<%@ Assembly Src="Internal\Extensions.cs" %> +<%@ Assembly Src="Internal\Generator.cs" %> +<%@ Assembly Src="Internal\Parser.cs" %> + +<%@ Import Namespace="System.Collections.Generic" %> +<%@ Import Namespace="System.IO" %> +<%@ Import Namespace="System.Linq" %> +<%@ Import Namespace="System.Text" %> +<%@ Import Namespace="System.Text.RegularExpressions" %> + +<%@ Import Namespace="SchemaMapper" %> + +<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" +Description="连接的数据库" %> +<%@ Property Name="ModuleName" + Type="System.String" + Description="模块名称,如:User"%> +<%@ Property Name="directory" + Type="System.String" + Default=".\" + Optional="True" + Description="代码生成路径" + Editor="System.Windows.Forms.Design.FolderNameEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %> + +<%@ Property Name="NeedViewModel" + Type="Boolean" + Category="5.Customization" + Default="False" + Optional="True" + Description="是否需要ViewModel" %> + + +<%@ Register Name="ApplicationGenerateClass" + Template="Web\Application.cst" + MergeProperties="False" %> +<%@ Register Name="HtmlGenerateClass" + Template="Web\Index.cshtml.cst" + MergeProperties="False" %> +Generating Entities ... +<% Generate(); %> + + \ No newline at end of file