This commit is contained in:
yubao 2017-12-15 00:03:06 +08:00
parent f212043917
commit 48db85f686
8 changed files with 159 additions and 213 deletions

View File

@ -9,130 +9,77 @@ Description: Create a list of properties from a database table
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %> <%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %> <%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.App.SSO;
using OpenAuth.Repository.Domain;
namespace OpenAuth.App namespace OpenAuth.App
{ {
public class <%=ModuleName%>ManagerApp public class <%=ModuleName%>App : BaseApp<<%=ModuleName%>>
{ {
private I<%=ModuleName%>Repository _repository; public RevelanceManagerApp ReleManagerApp { get; set; }
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> /// <summary>
/// 加载一个节点下面的一个或全部<%=GetModelName()%>s /// 加载列表
/// </summary> /// </summary>
public dynamic Load(int orgId, int pageindex, int pagesize) public TableData Load(Query<%=ModuleName%>ListReq request)
{ {
IEnumerable<<%=ModuleName%>> <%=ModuleName%>s; var loginUser = AuthUtil.GetCurrentUser();
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 string cascadeId = ".0.";
if (!string.IsNullOrEmpty(request.orgId))
{ {
total = total, var org = loginUser.Orgs.SingleOrDefault(u => u.Id == request.orgId);
list = <%=GetModelName()%>s, cascadeId = org.CascadeId;
pageCurrent = pageindex }
var ids = loginUser.Orgs.Where(u => u.CascadeId.Contains(cascadeId)).Select(u => u.Id).ToArray();
var roleIds = ReleManagerApp.Get(Define.ROLEORG, false, ids);
var roles = UnitWork.Find<<%=ModuleName%>>(u => roleIds.Contains(u.Id))
.OrderBy(u => u.Name)
.Skip((request.page - 1) * request.limit)
.Take(request.limit);
var records = Repository.GetCount(u => roleIds.Contains(u.Id));
var roleViews = new List<<%=ModuleName%>View>();
foreach (var role in roles)
{
RoleView uv = role;
var orgs = LoadByRole(role.Id);
uv.Organizations = string.Join(",", orgs.Select(u => u.Name).ToList());
uv.OrganizationIds = string.Join(",", orgs.Select(u => u.Id).ToList());
roleViews.Add(uv);
}
return new TableData
{
count = records,
data = roleViews,
}; };
} }
/// <summary> public void Add(<%=ModuleName%> obj)
/// 获取当前节点的所有下级节点
/// </summary>
private int[] GetSubOrgIds(int orgId)
{ {
var org = _orgRepository.FindSingle(u => u.Id == orgId); Repository.Add(obj);
var orgs = _orgRepository.Find(u => u.CascadeId.Contains(org.CascadeId)).Select(u => u.Id).ToArray();
return orgs;
} }
public <%=GetModelName()%> Find(int id) public void Update(<%=ModuleName%> obj)
{ {
var <%=ModuleName.ToLower()%> = _repository.FindSingle(u => u.Id == id); UnitWork.Update<User>(u => u.Id == obj.Id, u => new User
if (<%=ModuleName.ToLower()%> == null) return new <%=GetModelName()%>();
return <%=ModuleName.ToLower() %>;
}
public void Delete(int id)
{ {
_repository.Delete(id); Name = role.Name,
} Status = role.Status
});
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

@ -5,106 +5,89 @@ Description: Create a list of properties from a database table
--%> --%>
<%@ CodeTemplate Language="C#" Encoding="utf-8" TargetLanguage="C#" Debug="False" Description="控制器" %> <%@ CodeTemplate Language="C#" Encoding="utf-8" TargetLanguage="C#" Debug="False" Description="控制器" %>
<%@ Property Name="ModuleName" Type="String" Category="Context" 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" %> <%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %> <%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %>
<script runat="template">
public String GetModelName()
{
if(NeedViewModel)
return ModuleName +"View";
else
return ModuleName;
}
</script>
using System; using System;
using System.Web.Http;
using System.Web.Mvc; using System.Web.Mvc;
using Infrastructure; using Infrastructure;
using OpenAuth.App; using OpenAuth.App;
<%if(NeedViewModel){ %> using OpenAuth.App.Request;
using OpenAuth.App.ViewModel; using OpenAuth.App.Response;
<%} %> using OpenAuth.Mvc.Models;
using OpenAuth.Domain;
namespace OpenAuth.Mvc.Controllers namespace OpenAuth.Mvc.Controllers
{ {
public class <%=ModuleName%>ManagerController : BaseController public class <%=ModuleName%>ManagerController : BaseController
{ {
private <%=ModuleName%>ManagerApp _app; public <%=ModuleName%>App App { get; set; }
public <%=ModuleName%>ManagerController()
{
_app = AutofacExt.GetFromFac<<%=ModuleName%>ManagerApp>();
}
// //
// GET: /UserManager/ // GET: /UserManager/
[Authenticate]
public ActionResult Index() public ActionResult Index()
{ {
return View(); return View();
} }
public ActionResult Add(int id = 0) //添加或修改组织
{ [System.Web.Mvc.HttpPost]
return View(_app.Find(id)); public string Add(<%=ModuleName%> obj)
}
//添加或修改<%=ModuleName %>
[HttpPost]
public string Add(<%=GetModelName()%> model)
{ {
try try
{ {
_app.AddOrUpdate(model); App.Add(obj);
} }
catch (Exception ex) catch (Exception ex)
{ {
BjuiResponse.statusCode = "300"; Result.Code = 500;
BjuiResponse.message = ex.Message; Result.Message = ex.Message;
} }
return JsonHelper.Instance.Serialize(BjuiResponse); return JsonHelper.Instance.Serialize(Result);
} }
/// <summary> //添加或修改组织
/// 加载节点下面的所有<%=ModuleName %>s [System.Web.Mvc.HttpPost]
/// </summary> public string Update(<%=ModuleName%> obj)
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 try
{ {
_app.Delete(Id); App.Update(obj);
}
catch (Exception ex)
{
Result.Code = 500;
Result.Message = ex.Message;
}
return JsonHelper.Instance.Serialize(Result);
}
/// <summary>
/// 加载组织下面的所有用户
/// </summary>
public string Load([FromUri]Query<%=ModuleName%>ListReq request)
{
return JsonHelper.Instance.Serialize(App.Load(request));
}
[System.Web.Mvc.HttpPost]
public string Delete(string[] ids)
{
try
{
App.Delete(ids);
} }
catch (Exception e) catch (Exception e)
{ {
BjuiResponse.statusCode = "300"; Result.Code = 500;
BjuiResponse.message = e.Message; Result.Message = e.Message;
} }
return JsonHelper.Instance.Serialize(BjuiResponse); return JsonHelper.Instance.Serialize(Result);
} }
} }
} }

View File

@ -36,7 +36,7 @@ Description="连接的数据库" %>
<ul id="tree" class="ztree" style="display: inline-block; width: 180px; padding: 10px; border: 1px solid #ddd; overflow: auto;"></ul> <ul id="tree" class="ztree" style="display: inline-block; width: 180px; padding: 10px; border: 1px solid #ddd; overflow: auto;"></ul>
<table class="layui-table" <table class="layui-table"
lay-data="{height: 'full-80', page:true, id:'mainList'}" lay-data="{height: 'full-80', page:true, id:'mainList'}"
lay-filter="list"> lay-filter="list" lay-size="sm">
<thead> <thead>
<tr> <tr>
<th lay-data="{checkbox:true, fixed: true}"></th> <th lay-data="{checkbox:true, fixed: true}"></th>

View File

@ -36,13 +36,6 @@ OnChanged="OnSourceDatabaseChanged"%>
Description="代码生成路径" Description="代码生成路径"
Editor="System.Windows.Forms.Design.FolderNameEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %> Editor="System.Windows.Forms.Design.FolderNameEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
<%@ Property Name="NeedViewModel"
Type="Boolean"
Default="False"
Optional="True"
Description="是否需要ViewModel" %>
<%@ Register Name="ApplicationGenerateClass" <%@ Register Name="ApplicationGenerateClass"
Template="Web\Application.cst" Template="Web\Application.cst"
MergeProperties="False" %> MergeProperties="False" %>

View File

@ -38,52 +38,23 @@ namespace OpenAuth.App
var records = Repository.GetCount(u => roleIds.Contains(u.Id)); var records = Repository.GetCount(u => roleIds.Contains(u.Id));
var userviews = new List<RoleView>(); var roleViews = new List<RoleView>();
foreach (var role in roles) foreach (var role in roles)
{ {
RoleView uv = role; RoleView uv = role;
var orgs = LoadByRole(role.Id); var orgs = LoadByRole(role.Id);
uv.Organizations = string.Join(",", orgs.Select(u => u.Name).ToList()); uv.Organizations = string.Join(",", orgs.Select(u => u.Name).ToList());
uv.OrganizationIds = string.Join(",", orgs.Select(u => u.Id).ToList()); uv.OrganizationIds = string.Join(",", orgs.Select(u => u.Id).ToList());
userviews.Add(uv); roleViews.Add(uv);
} }
return new TableData return new TableData
{ {
count = records, count = records,
data = userviews, data = roleViews,
}; };
} }
public void AddOrUpdate(RoleView view)
{
if (string.IsNullOrEmpty(view.OrganizationIds))
throw new Exception("请为用户分配机构");
Role role = view;
if (string.IsNullOrEmpty(view.Id))
{
if (UnitWork.IsExist<User>(u => u.Account == view.Name))
{
throw new Exception("用户账号已存在");
}
role.CreateTime = DateTime.Now;
Repository.Add(role);
view.Id = role.Id; //要把保存后的ID存入view
}
else
{
UnitWork.Update<User>(u => u.Id == view.Id, u => new User
{
Name = role.Name,
Status = role.Status
});
}
string[] orgIds = view.OrganizationIds.Split(',').ToArray();
ReleManagerApp.DeleteBy(Define.ROLEORG, role.Id);
ReleManagerApp.AddRelevance(Define.ROLEORG, orgIds.ToLookup(u => role.Id));
}
/// <summary> /// <summary>
/// 加载角色的所有机构 /// 加载角色的所有机构
/// </summary> /// </summary>
@ -97,5 +68,42 @@ namespace OpenAuth.App
} }
public void Add(RoleView obj)
{
if (string.IsNullOrEmpty(obj.OrganizationIds))
throw new Exception("请为角色分配机构");
Role role = obj;
role.CreateTime = DateTime.Now;
Repository.Add(role);
obj.Id = role.Id; //要把保存后的ID存入view
UpdateRele(obj);
}
public void Update(RoleView obj)
{
if (string.IsNullOrEmpty(obj.OrganizationIds))
throw new Exception("请为角色分配机构");
Role role = obj;
UnitWork.Update<User>(u => u.Id == obj.Id, u => new User
{
Name = role.Name,
Status = role.Status
});
UpdateRele(obj);
}
/// <summary>
/// 更新相应的多对多关系
/// </summary>
/// <param name="view"></param>
private void UpdateRele(RoleView view)
{
string[] orgIds = view.OrganizationIds.Split(',').ToArray();
ReleManagerApp.DeleteBy(Define.ROLEORG, view.Id);
ReleManagerApp.AddRelevance(Define.ROLEORG, orgIds.ToLookup(u => view.Id));
}
} }
} }

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Web.Http; using System.Web.Http;
using System.Web.Mvc; using System.Web.Mvc;
using Infrastructure; using Infrastructure;
@ -24,11 +23,28 @@ namespace OpenAuth.Mvc.Controllers
//添加或修改组织 //添加或修改组织
[System.Web.Mvc.HttpPost] [System.Web.Mvc.HttpPost]
public string AddOrUpdate(RoleView view) public string Add(RoleView obj)
{ {
try try
{ {
App.AddOrUpdate(view); App.Add(obj);
}
catch (Exception ex)
{
Result.Code = 500;
Result.Message = ex.Message;
}
return JsonHelper.Instance.Serialize(Result);
}
//添加或修改组织
[System.Web.Mvc.HttpPost]
public string Update(RoleView obj)
{
try
{
App.Update(obj);
} }
catch (Exception ex) catch (Exception ex)

View File

@ -15,8 +15,7 @@
<tr> <tr>
<th lay-data="{checkbox:true, fixed: true}"></th> <th lay-data="{checkbox:true, fixed: true}"></th>
<th lay-data="{field:'Name', width:150}">角色名称</th> <th lay-data="{field:'Name', width:150}">角色名称</th>
<th lay-data="{field:'Organizations', width:135}">所属部门</th> <th lay-data="{field:'Organizations', width:250}">所属部门</th>
<th lay-data="{field:'CreateTime', width:180}">创建时间</th>
<th lay-data="{fixed: 'right', width:160, align:'center', toolbar: '#barList'}"></th> <th lay-data="{fixed: 'right', width:160, align:'center', toolbar: '#barList'}"></th>
</tr> </tr>
</thead> </thead>

View File

@ -78,9 +78,9 @@ layui.config({
}, },
end: mainList end: mainList
}); });
var url = "/RoleManager/AddOrUpdate"; var url = "/RoleManager/Add";
if (update) { if (update) {
url = "/RoleManager/AddOrUpdate"; //暂时和添加一个地址 url = "/RoleManager/Update";
} }
//提交数据 //提交数据
form.on('submit(formSubmit)', form.on('submit(formSubmit)',