using LeaRun.Data;
using LeaRun.Data.Repository;
using LeaRun.Util;
using LeaRun.Util.WebControl;
using LeaRun.Util.Extension;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Linq;
using LeaRun.Application.Entity.BaseManage;
using LeaRun.Application.IService.BaseManage;
using LeaRun.Application.IService.AuthorizeManage;
using LeaRun.Application.Service.AuthorizeManage;
using System;
using LeaRun.Application.Code;
namespace LeaRun.Application.Service.BaseManage
{
///
/// 版 本
/// Copyright (c) 2013-2016 上海力软信息技术有限公司
/// 创建人:佘赐雄
/// 日 期:2015.11.03 10:58
/// 描 述:用户管理
///
public class UserService : RepositoryFactory, IUserService
{
private IAuthorizeService iauthorizeservice = new AuthorizeService();
#region 获取数据
///
/// 用户列表
///
///
public DataTable GetTable()
{
var strSql = new StringBuilder();
strSql.Append(@"SELECT u.*,
d.FullName AS DepartmentName
FROM Base_User u
LEFT JOIN Base_Department d ON d.DepartmentId = u.DepartmentId
WHERE 1=1");
strSql.Append(" AND u.EnabledMark = 1 AND u.DeleteMark=0");
return this.BaseRepository().FindTable(strSql.ToString());
}
///
/// 用户列表
///
///
public IEnumerable GetList()
{
var expression = LinqExtensions.True();
expression = expression.And(t => t.UserId != "System1").And(t => t.EnabledMark == 1).And(t => t.DeleteMark == 0);
return this.BaseRepository().IQueryable(expression).OrderByDescending(t => t.CreateDate).ToList();
}
///
/// 用户列表
///
/// 分页
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
var expression = LinqExtensions.True();
var queryParam = queryJson.ToJObject();
//公司主键
if (!queryParam["organizeId"].IsEmpty())
{
string organizeId = queryParam["organizeId"].ToString();
expression = expression.And(t => t.OrganizeId.Equals(organizeId));
}
//部门主键
if (!queryParam["departmentId"].IsEmpty())
{
string departmentId = queryParam["departmentId"].ToString();
expression = expression.And(t => t.DepartmentId.Equals(departmentId));
}
//查询条件
if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty())
{
string condition = queryParam["condition"].ToString();
string keyord = queryParam["keyword"].ToString();
switch (condition)
{
case "Account": //账户
expression = expression.And(t => t.Account.Contains(keyord));
break;
case "RealName": //姓名
expression = expression.And(t => t.RealName.Contains(keyord));
break;
case "Mobile": //手机
expression = expression.And(t => t.Mobile.Contains(keyord));
break;
default:
break;
}
}
//expression = expression.And(t => t.UserId != "System");
return iauthorizeservice.FindList(expression, pagination);
}
///
/// 用户列表(ALL)
///
///
public DataTable GetAllTable()
{
var strSql = new StringBuilder();
strSql.Append(@"SELECT u.UserId ,
u.EnCode ,
u.Account ,
u.RealName ,
u.Gender ,
u.Birthday ,
u.Mobile ,
u.Manager ,
u.OrganizeId,
u.DepartmentId,
o.FullName AS OrganizeName ,
d.FullName AS DepartmentName ,
u.RoleId ,
u.DutyName ,
u.PostName ,
u.EnabledMark ,
u.CreateDate,
u.Description
FROM Base_User u
LEFT JOIN Base_Organize o ON o.OrganizeId = u.OrganizeId
LEFT JOIN Base_Department d ON d.DepartmentId = u.DepartmentId
WHERE 1=1");
strSql.Append(" AND u.EnabledMark = 1 AND u.DeleteMark=0 order by o.FullName,d.FullName,u.RealName");
return this.BaseRepository().FindTable(strSql.ToString());
}
///
/// 用户列表(导出Excel)
///
///
public DataTable GetExportList()
{
var strSql = new StringBuilder();
strSql.Append(@"SELECT [Account]
,[RealName]
,CASE WHEN Gender=1 THEN '男' ELSE '女' END AS Gender
,[Birthday]
,[Mobile]
,[Telephone]
,u.[Email]
,[WeChat]
,[MSN]
,u.[Manager]
,o.FullName AS Organize
,d.FullName AS Department
,u.[Description]
,u.[CreateDate]
,u.[CreateUserName]
FROM Base_User u
INNER JOIN Base_Department d ON u.DepartmentId=d.DepartmentId
INNER JOIN Base_Organize o ON u.OrganizeId=o.OrganizeId");
return this.BaseRepository().FindTable(strSql.ToString());
}
///
/// 用户实体
///
/// 主键值
///
public UserEntity GetEntity(string keyValue)
{
return this.BaseRepository().FindEntity(keyValue);
}
///
/// 登录验证
///
/// 用户名
///
public UserEntity CheckLogin(string username)
{
var expression = LinqExtensions.True();
expression = expression.And(t => t.Account == username);
expression = expression.Or(t => t.Mobile == username);
expression = expression.Or(t => t.Email == username);
return this.BaseRepository().FindEntity(expression);
}
#endregion
#region 验证数据
///
/// 账户不能重复
///
/// 账户值
/// 主键
///
public bool ExistAccount(string account, string keyValue)
{
var expression = LinqExtensions.True();
expression = expression.And(t => t.Account == account);
if (!string.IsNullOrEmpty(keyValue))
{
expression = expression.And(t => t.UserId != keyValue);
}
return this.BaseRepository().IQueryable(expression).Count() == 0 ? true : false;
}
#endregion
#region 提交数据
///
/// 删除用户
///
/// 主键
public void RemoveForm(string keyValue)
{
this.BaseRepository().Delete(keyValue);
}
///
/// 保存用户表单(新增、修改)
///
/// 主键值
/// 用户实体
///
public string SaveForm(string keyValue, UserEntity userEntity)
{
IRepository db = new RepositoryFactory().BaseRepository().BeginTrans();
try
{
#region 基本信息
if (!string.IsNullOrEmpty(keyValue))
{
userEntity.Modify(keyValue);
userEntity.Password = null;
db.Update(userEntity);
}
else
{
userEntity.Create();
keyValue = userEntity.UserId;
userEntity.Secretkey = Md5Helper.MD5(CommonHelper.CreateNo(), 16).ToLower();
userEntity.Password = Md5Helper.MD5(DESEncrypt.Encrypt(Md5Helper.MD5(userEntity.Password, 32).ToLower(), userEntity.Secretkey).ToLower(), 32).ToLower();
db.Insert(userEntity);
}
#endregion
#region 默认添加 角色、岗位、职位
db.Delete(t => t.IsDefault == 1 && t.UserId == userEntity.UserId);
List userRelationEntitys = new List();
//角色
if (!string.IsNullOrEmpty(userEntity.RoleId))
{
userRelationEntitys.Add(new UserRelationEntity
{
Category = 2,
UserRelationId = Guid.NewGuid().ToString(),
UserId = userEntity.UserId,
ObjectId = userEntity.RoleId,
CreateDate = DateTime.Now,
CreateUserId = OperatorProvider.Provider.Current().UserId,
CreateUserName = OperatorProvider.Provider.Current().UserName,
IsDefault = 1,
});
}
//岗位
if (!string.IsNullOrEmpty(userEntity.DutyId))
{
userRelationEntitys.Add(new UserRelationEntity
{
Category = 3,
UserRelationId = Guid.NewGuid().ToString(),
UserId = userEntity.UserId,
ObjectId = userEntity.DutyId,
CreateDate = DateTime.Now,
CreateUserId = OperatorProvider.Provider.Current().UserId,
CreateUserName = OperatorProvider.Provider.Current().UserName,
IsDefault = 1,
});
}
//职位
if (!string.IsNullOrEmpty(userEntity.PostId))
{
userRelationEntitys.Add(new UserRelationEntity
{
Category = 4,
UserRelationId = Guid.NewGuid().ToString(),
UserId = userEntity.UserId,
ObjectId = userEntity.PostId,
CreateDate = DateTime.Now,
CreateUserId = OperatorProvider.Provider.Current().UserId,
CreateUserName = OperatorProvider.Provider.Current().UserName,
IsDefault = 1,
});
}
db.Insert(userRelationEntitys);
#endregion
db.Commit();
return keyValue;
}
catch (Exception)
{
db.Rollback();
throw;
}
}
///
/// 修改用户登录密码
///
/// 主键值
/// 新密码(MD5 小写)
public void RevisePassword(string keyValue, string Password)
{
UserEntity userEntity = new UserEntity();
userEntity.UserId = keyValue;
userEntity.Secretkey = Md5Helper.MD5(CommonHelper.CreateNo(), 16).ToLower();
userEntity.Password = Md5Helper.MD5(DESEncrypt.Encrypt(Password, userEntity.Secretkey).ToLower(), 32).ToLower();
this.BaseRepository().Update(userEntity);
}
///
/// 修改用户状态
///
/// 主键值
/// 状态:1-启动;0-禁用
public void UpdateState(string keyValue, int State)
{
UserEntity userEntity = new UserEntity();
userEntity.Modify(keyValue);
userEntity.EnabledMark = State;
this.BaseRepository().Update(userEntity);
}
///
/// 修改用户信息
///
/// 实体对象
public void UpdateEntity(UserEntity userEntity)
{
this.BaseRepository().Update(userEntity);
}
#endregion
}
}