OpenAuth.Net/OpenAuth.App/UserManagerApp.cs

140 lines
4.9 KiB
C#
Raw Normal View History

using OpenAuth.App.ViewModel;
2015-11-16 23:18:51 +08:00
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
2017-10-12 16:38:46 +08:00
using OpenAuth.App.Request;
2015-11-16 23:18:51 +08:00
2015-11-16 23:18:51 +08:00
namespace OpenAuth.App
{
public class UserManagerApp
{
public IUnitWork _unitWork { get; set; }
public RevelanceManagerApp ReleManagerApp { get; set; }
2015-11-16 23:18:51 +08:00
2016-07-11 18:21:26 +08:00
public User Get(string account)
{
return _unitWork.FindSingle<User>(u => u.Account == account);
2016-07-11 18:21:26 +08:00
}
2015-11-16 23:18:51 +08:00
/// <summary>
/// 加载一个部门及子部门全部用户
/// </summary>
2017-10-12 16:38:46 +08:00
public GridData Load(QueryUserListReq request)
2015-11-16 23:18:51 +08:00
{
2017-10-12 16:38:46 +08:00
if (request.page < 1) request.page = 1; //TODO:如果列表为空新增加一个用户后前端会传一个0过来奇怪
2015-11-16 23:18:51 +08:00
IEnumerable<User> users;
2016-10-15 01:27:39 +08:00
int records = 0;
2017-10-12 16:38:46 +08:00
if (request.orgId ==string.Empty)
2015-11-16 23:18:51 +08:00
{
users = _unitWork.Find<User>(null).OrderBy(u => u.Id).Skip((request.page - 1) * request.limit).Take(request.limit);
records = _unitWork.GetCount<User>();
2015-11-16 23:18:51 +08:00
}
else
{
2017-10-12 16:38:46 +08:00
var ids = GetSubOrgIds(request.orgId);
users = _unitWork.Find<User>(u =>ids.Contains(u.Id)).OrderBy(u => u.Id).Skip((request.page - 1) * request.limit).Take(request.limit);
records = _unitWork.GetCount<User>();
2015-11-16 23:18:51 +08:00
}
var userviews = new List<UserView>();
foreach (var user in users)
{
UserView uv = user;
var orgs = LoadByUser(user.Id);
2016-04-16 23:25:00 +08:00
uv.Organizations = string.Join(",", orgs.Select(u => u.Name).ToList());
uv.OrganizationIds = string.Join(",", orgs.Select(u => u.Id).ToList());
2015-11-16 23:18:51 +08:00
userviews.Add(uv);
}
return new GridData
2015-11-16 23:18:51 +08:00
{
2017-08-31 19:54:12 +08:00
count = records,
data = userviews,
2015-11-16 23:18:51 +08:00
};
}
/// <summary>
/// 获取当前组织的所有下级组织
/// </summary>
2017-10-11 16:19:34 +08:00
private string[] GetSubOrgIds(string orgId)
2015-11-16 23:18:51 +08:00
{
var org = _unitWork.FindSingle<Org>(u => u.Id == orgId);
var orgs = _unitWork.Find<Org>(u => u.CascadeId.Contains(org.CascadeId)).Select(u => u.Id).ToArray();
2015-11-16 23:18:51 +08:00
return orgs;
}
2017-10-11 16:19:34 +08:00
public UserView Find(string id)
2015-11-16 23:18:51 +08:00
{
var user = _unitWork.FindSingle<User>(u => u.Id == id);
2015-11-16 23:18:51 +08:00
if (user == null) return new UserView();
UserView view = user;
foreach (var org in LoadByUser(id))
2015-11-16 23:18:51 +08:00
{
view.Organizations += "," + org.Name;
view.OrganizationIds += "," + org.Id;
}
view.OrganizationIds = view.OrganizationIds.TrimStart(',');
view.Organizations = view.Organizations.TrimStart(',');
return view;
}
2017-10-11 16:19:34 +08:00
public void Delete(string[] ids)
2015-11-16 23:18:51 +08:00
{
_unitWork.Delete<User>(u => ids.Contains(u.Id));
2015-11-16 23:18:51 +08:00
}
public void AddOrUpdate(UserView view)
{
2017-04-15 23:31:18 +08:00
if (string.IsNullOrEmpty(view.OrganizationIds))
throw new Exception("请为用户分配机构");
2015-11-16 23:18:51 +08:00
User user = view;
2017-10-12 16:38:46 +08:00
if (string.IsNullOrEmpty(view.Id))
2015-11-16 23:18:51 +08:00
{
if (_unitWork.IsExist<User>(u => u.Account == view.Account))
2016-08-22 16:14:55 +08:00
{
throw new Exception("用户账号已存在");
}
2016-05-26 10:30:15 +08:00
user.CreateTime = DateTime.Now;
2015-12-01 22:25:36 +08:00
user.Password = user.Account; //初始密码与账号相同
_unitWork.Add(user);
_unitWork.Save();
2015-12-05 21:24:01 +08:00
view.Id = user.Id; //要把保存后的ID存入view
2015-11-16 23:18:51 +08:00
}
else
{
_unitWork.Update<User>(u => u.Id == view.Id, u => new User
2016-05-26 10:30:15 +08:00
{
Account = user.Account,
BizCode = user.BizCode,
Name = user.Name,
Sex = user.Sex,
Status = user.Status,
Type = user.Type
});
2015-11-16 23:18:51 +08:00
}
2017-10-11 16:19:34 +08:00
string[] orgIds = view.OrganizationIds.Split(',').ToArray();
2015-11-16 23:18:51 +08:00
ReleManagerApp.DeleteBy("UserOrg", user.Id);
ReleManagerApp.AddRelevance("UserOrg", orgIds.ToLookup(u => user.Id));
2015-11-16 23:18:51 +08:00
}
/// <summary>
/// 加载用户的所有机构
/// </summary>
public IEnumerable<Org> LoadByUser(string userId)
2016-09-07 11:11:34 +08:00
{
var result = from userorg in _unitWork.Find<Relevance>(null)
join org in _unitWork.Find<Org>(null) on userorg.SecondId equals org.Id
where userorg.FirstId == userId && userorg.Key == "UserOrg"
select org;
return result;
2016-09-07 11:11:34 +08:00
}
2015-11-16 23:18:51 +08:00
}
}