调整html页面代码生成

This commit is contained in:
yubaolee@163.com 2017-11-27 16:55:52 +08:00
parent b751d8cce8
commit 40dba92379
5 changed files with 492 additions and 0 deletions

View File

@ -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" %>
<script runat="template">
public String GetModelName()
{
if(NeedViewModel)
return ModuleName +"View";
else
return ModuleName;
}
</script>
<%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();
}
/// <summary>
/// 加载一个节点下面的一个或全部<%=GetModelName()%>s
/// </summary>
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
};
}
/// <summary>
/// 获取当前节点的所有下级节点
/// </summary>
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()%>);
}
}
}
}

View File

@ -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" %>
<script runat="template">
public String GetModelName()
{
if(NeedViewModel)
return ModuleName +"View";
else
return ModuleName;
}
</script>
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);
}
/// <summary>
/// 加载节点下面的所有<%=ModuleName %>s
/// </summary>
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);
}
}
}

View File

@ -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
{
<link rel="stylesheet" href="/css/treetable.css" />
}
<blockquote class="layui-elem-quote news_search toolList">
<div class="layui-inline">
<div class="layui-input-inline">
<input class="layui-input" placeholder="请输入关键字"
name="key" id="key" autocomplete="off">
</div>
<button class="layui-btn" data-type="search">搜索</button>
</div>
@Html.Action("MenuHeader", "Home")
@*<button class="layui-btn " data-type="refresh">刷新</button>
<button class="layui-btn " data-type="addData">添加用户</button>
<button class="layui-btn layui-btn-danger" data-type="del">批量删除</button>*@
</blockquote>
<div style="display: flex;">
<ul id="tree" class="ztree" style="display: inline-block; width: 180px; padding: 10px; border: 1px solid #ddd; overflow: auto;"></ul>
<table class="layui-table"
lay-data="{height: 'full-80', page:true, id:'mainList'}"
lay-filter="list">
<thead>
<tr>
<th lay-data="{checkbox:true, fixed: true}"></th>
<% foreach (ColumnSchema column in this.SourceTable.Columns) {%>
<th lay-data="{field:'<%=column.Name%>', width:150}"><%=Tools.GetDescription(column)%></th>
<% }%>
<th lay-data="{fixed: 'right', width:160, align:'center', toolbar: '#barList'}"></th>
</tr>
</thead>
</table>
</div>
<script type="text/html" id="barList">
<a class="layui-btn layui-btn-primary layui-btn-mini" lay-event="detail">查看</a>
</script>
<!--用户添加/编辑窗口-->
<div id="divEdit" style="display: none">
<form class="layui-form" action="" id="formEdit">
<% foreach (ColumnSchema column in this.SourceTable.Columns) {
if(column.IsPrimaryKeyMember){%>
<input type="hidden" name="<%=column.Name%>" v-model="<%=column.Name%>" />
<%}else if(CSharpAlias[column.SystemType.FullName] == "bool") {%>
<div class="layui-form-item">
<label class="layui-form-label"><%=Tools.GetDescription(column)%></label>
<div class="layui-input-block">
<input type="checkbox" name="<%=column.Name%>" v-model="<%=column.Name%>" lay-skin="switch" value="1">
</div>
</div>
<%}else if(CSharpAlias[column.SystemType.FullName] == "int" ) {%>
<div class="layui-form-item">
<label class="layui-form-label"><%=Tools.GetDescription(column)%></label>
<div class="layui-input-block">
<input type="radio" name="<%=column.Name%>" value="1" title="value1" checked>
<input type="radio" name="<%=column.Name%>" value="0" title="value2">
</div>
</div>
<%} else {%>
<div class="layui-form-item">
<label class="layui-form-label"><%=Tools.GetDescription(column)%></label>
<div class="layui-input-block">
<input type="text" name="<%=column.Name%>" v-model="<%=column.Name%>" required lay-verify="required"
placeholder="<%=Tools.GetDescription(column)%>" autocomplete="off" class="layui-input">
</div>
</div>
<%} %>
<%} %>
<%if(Tools.NeedCascade(SourceTable)){ %>
<div class="layui-form-item">
<label class="layui-form-label">所属部门</label>
<div class="layui-input-block">
<input id="Organizations" name="Organizations" v-model="Organizations" required lay-verify="required" class="layui-input" />
<input id="OrganizationIds" name="OrganizationIds" v-model="OrganizationIds" required lay-verify="required" type="hidden" />
<div id="menuContent" class="menuContent" style="display: none;">
<ul id="org" class="ztree"></ul>
</div>
</div>
</div>
<%} %>
</form>
</div>
<script type="text/javascript" src="/layui/layui.js"></script>
<script type="text/javascript" src="/js/<%=ModuleName%>.js"></script>

View File

@ -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") ;
}
}
}

View File

@ -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(); %>
<script runat="template">
public void Generate()
{
Stopwatch watch = Stopwatch.StartNew();
string outputDirectory = Path.GetFullPath(directory);
if (!Directory.Exists(directory)) //根目录
Directory.CreateDirectory(directory);
if (!Directory.Exists(directory +"/views")) //视图根文件夹
Directory.CreateDirectory(directory +"/views");
if (!Directory.Exists(directory +"/views/"+ModuleName)) //视图文件夹
Directory.CreateDirectory(directory +"/views/"+ModuleName);
CreateApplicationClass();
CreateHtmlClass();
this.RegisterReference("System.Configuration");
this.RegisterReference("System.Data");
this.RegisterReference("System.Data.Entity");
this.RegisterReference("System.Runtime.Serialization");
this.RegisterReference("EntityFramework");
watch.Stop();
Response.WriteLine("Generate Time: " + watch.ElapsedMilliseconds + " ms");
}
//创建APP层
public void CreateApplicationClass()
{
ApplicationGenerateClass generatedClass = this.Create<ApplicationGenerateClass>();
this.CopyPropertiesTo(generatedClass);
string rootDirectory = Path.GetFullPath(directory);
string generatedFile = ModuleName + "ManagerApp.cs";
generatedFile = Path.Combine(rootDirectory, generatedFile);
generatedClass.ModuleName = ModuleName;
generatedClass.NeedViewModel = NeedViewModel;
Response.WriteLine(generatedFile);
generatedClass.RenderToFile(generatedFile, generatedFile, true);
}
//创建视图
public void CreateHtmlClass()
{
HtmlGenerateClass generatedClass = this.Create<HtmlGenerateClass>();
this.CopyPropertiesTo(generatedClass);
generatedFile = Path.GetFullPath(directory) + "/views/"+ModuleName+"/" + "index.cshtml";
generatedClass.ModuleName = ModuleName;
generatedClass.SourceTable = SourceTable;
Response.WriteLine(generatedFile);
generatedClass.RenderToFile(generatedFile, generatedFile, true);
}
</script>