using learun.iapplication; using learun.util; using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Threading.Tasks; using Dapper; using Newtonsoft.Json.Linq; namespace learun.application { /// /// 版 本 EasyCode EC管理后台 /// Copyright (c) 2019-present EC管理有限公司 /// 创建人:tobin /// 日 期:2019.09.12 /// 描 述:用户(数据库操作) /// public class UserService : ServiceBase { #region 属性 构造函数 private readonly string fieldSql; /// /// /// public UserService() { fieldSql = @" t.F_UserId, t.F_EnCode, t.F_Account, t.F_Password, t.F_Secretkey, t.F_RealName, t.F_NickName, t.F_HeadIcon, t.F_QuickQuery, t.F_SimpleSpelling, t.F_Gender, t.F_Birthday, t.F_Mobile, t.F_Telephone, t.F_Email, t.F_OICQ, t.F_WeChat, t.F_MSN, t.F_CompanyId, t.F_CompanyName, t.F_DepartmentId, t.F_SecurityLevel, t.F_OpenId, t.F_Question, t.F_AnswerQuestion, t.F_CheckOnLine, t.F_AllowStartTime, t.F_AllowEndTime, t.F_LockStartDate, t.F_LockEndDate, t.F_SortCode, t.F_DeleteMark, t.F_EnabledMark, t.F_Description, t.F_CreateDate, t.F_CreateUserId, t.F_CreateUserName, t.F_ModifyDate, t.F_ModifyUserId, t.F_ModifyUserName "; } #endregion 属性 构造函数 #region 获取数据 /// /// 获取用户信息通过账号 /// /// 用户账号 /// public Task GetEntityByAccount(string account) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(fieldSql); strSql.Append(" FROM LR_Base_User t "); strSql.Append(" WHERE t.F_Account = @account AND t.F_DeleteMark = 0 "); return this.BaseRepository().FindEntity(strSql.ToString(), new { account }); } /// /// 用户列表(根据用户主键集合) /// /// 用户主键集合主键 /// public Task> GetListByKeyValues(string keyValues) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(fieldSql.Replace("t.F_Password,", "").Replace("t.F_Secretkey,", "")); strSql.Append(" FROM LR_Base_User t WHERE t.F_DeleteMark = 0 "); strSql.Append(" AND F_UserId in ('" + keyValues.Replace(",", "','") + "')"); return this.BaseRepository().FindList(strSql.ToString()); } /// /// 用户列表(根据公司主键) /// /// 公司主键 /// 部门主键 /// 关键词 /// public Task> GetList(string companyId, string departmentId, string keyword) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(fieldSql.Replace("t.F_Password,", "").Replace("t.F_Secretkey,", "")); strSql.Append(" FROM LR_Base_User t WHERE t.F_DeleteMark = 0 "); if (!string.IsNullOrEmpty(companyId)) { strSql.Append(" AND t.F_CompanyId = @companyId "); } if (!string.IsNullOrEmpty(departmentId)) { strSql.Append(" AND t.F_DepartmentId = @departmentId "); } if (!string.IsNullOrEmpty(keyword)) { keyword = "%" + keyword + "%"; strSql.Append(" AND( t.F_Account like @keyword or t.F_RealName like @keyword or t.F_Mobile like @keyword ) "); } return this.BaseRepository().FindList(strSql.ToString(), new { companyId, departmentId, keyword }); } /// /// 用户列表(根据公司主键)(分页) /// /// 公司主键 /// 部门主键 /// 分页参数 /// 关键词 /// public Task> GetPageList(string companyId, string departmentId, Pagination pagination, string keyword) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(fieldSql.Replace("t.F_Password,", "").Replace("t.F_Secretkey,", "")); strSql.Append(" FROM LR_Base_User t WHERE t.F_DeleteMark = 0 "); if (!string.IsNullOrEmpty(companyId)) { strSql.Append(" AND t.F_CompanyId = @companyId "); } if (!string.IsNullOrEmpty(departmentId)) { strSql.Append(" AND t.F_DepartmentId = @departmentId "); } if (!string.IsNullOrEmpty(keyword)) { keyword = "%" + keyword + "%"; strSql.Append(" AND( t.F_Account like @keyword or t.F_RealName like @keyword or t.F_Mobile like @keyword ) "); } return this.BaseRepository().FindList(strSql.ToString(), new { companyId, departmentId, keyword }, pagination); } /// /// 用户列表,全部 /// /// 关键词 /// public Task> GetAllList(string keyword) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(fieldSql.Replace("t.F_Password,", "").Replace("t.F_Secretkey,", "")); strSql.Append(" FROM LR_Base_User t WHERE t.F_DeleteMark = 0 AND t.F_EnabledMark = 1 "); if (!string.IsNullOrEmpty(keyword)) { keyword = "%" + keyword + "%"; strSql.Append(" AND( t.F_Account like @keyword or t.F_RealName like @keyword or t.F_Mobile like @keyword ) "); } strSql.Append(" ORDER BY t.F_CompanyId,t.F_DepartmentId,t.F_RealName "); return this.BaseRepository().FindList(strSql.ToString(), new { keyword }); } /// /// 用户列表(导出Excel) /// /// public Task GetExportList() { var strSql = new StringBuilder(); strSql.Append(@"SELECT u.F_Account ,u.F_RealName ,CASE WHEN u.F_Gender=1 THEN '男' ELSE '女' END AS F_Gender ,u.F_Birthday ,u.F_Mobile ,u.F_Telephone ,u.F_Email ,u.F_WeChat ,u.F_MSN ,o.F_FullName AS F_Company ,d.F_FullName AS F_Department ,u.F_Description ,u.F_CreateDate ,u.F_CreateUserName FROM LR_Base_User u INNER JOIN LR_Base_Department d ON u.F_DepartmentId=d.F_DepartmentId INNER JOIN LR_Base_Company o ON u.F_CompanyId=o.F_CompanyId WHERE u.F_DeleteMark = 0 "); return this.BaseRepository().FindTable(strSql.ToString()); } /// /// 用户实体 /// /// 主键值 /// public Task GetEntity(string keyValue) { return this.BaseRepository().FindEntityByKey(keyValue); } /// /// /// 参数 /// public Task GetEntity(object param) { return BaseRepository().FindEntity(param); } /// /// 获取超级管理员用户列表 /// /// public Task> GetAdminList() { return this.BaseRepository().FindList(" select * from LR_Base_User where F_SecurityLevel = 1 "); } #endregion 获取数据 #region 2 /// /// 用户列表(根据公司主键) /// /// 查询参数,json字串 /// public Task> GetList(JObject queryParam) { var strSql = new StringBuilder(); var dp = new DynamicParameters(new { }); strSql.Append("SELECT "); strSql.Append(fieldSql.Replace("t.F_Password,", "").Replace("t.F_Secretkey,", "")); strSql.Append(" FROM LR_Base_User t WHERE t.F_DeleteMark = 0 "); // dp.Add("CompanyId", queryParam["CompanyId"], DbType.String); strSql.Append(" AND F_CompanyId = @CompanyId"); // 关键字 if (!queryParam["Keyword"].IsEmpty()) { dp.Add("Keyword", $"%{queryParam["Keyword"]}%", DbType.String); strSql.Append(" AND (F_Account like @Keyword or F_RealName like @Keyword or F_Mobile like @Keyword)"); } // 操作时间 if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty()) { dp.Add("startTime", queryParam["StartTime"].ToDate(), DbType.DateTime); dp.Add("endTime", queryParam["EndTime"].ToDate(), DbType.DateTime); strSql.Append(" AND (F_CreateDate >= @startTime AND F_CreateDate <= @endTime)"); } return this.BaseRepository().FindList(strSql.ToString(), dp); } /// /// 用户列表(根据公司主键)(分页) /// /// 分页参数 /// 查询参数,json字串 /// public Task> GetPageList(Pagination pagination, JObject queryParam) { var strSql = new StringBuilder(); var dp = new DynamicParameters(new { }); strSql.Append("SELECT "); strSql.Append(fieldSql.Replace("t.F_Password,", "").Replace("t.F_Secretkey,", "")); strSql.Append(" FROM LR_Base_User t WHERE t.F_DeleteMark = 0 "); // dp.Add("CompanyId", queryParam["CompanyId"], DbType.String); strSql.Append(" AND F_CompanyId = @CompanyId"); // 关键字 if (!queryParam["Keyword"].IsEmpty()) { dp.Add("Keyword", $"%{queryParam["Keyword"]}%", DbType.String); strSql.Append(" AND (F_Account like @Keyword or F_EnCode like @Keyword or F_RealName like @Keyword)"); } // 操作时间 if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty()) { dp.Add("startTime", queryParam["StartTime"].ToDate(), DbType.DateTime); dp.Add("endTime", queryParam["EndTime"].ToDate(), DbType.DateTime); strSql.Append(" AND (F_CreateDate >= @startTime AND F_CreateDate <= @endTime)"); } return BaseRepository().FindList(strSql.ToString(), dp, pagination); } #endregion 2 #region 提交数据 /// /// 虚拟删除 /// /// 主键 public async Task Delete(string keyValue) { await this.BaseRepository().DeleteAny(new { F_UserId = keyValue }); } /// /// 保存用户表单(新增、修改) /// /// 主键值 /// 用户实体 /// public async Task SaveEntity(string keyValue, UserEntity entity) { var userId = GetUserId(); var userAccount = GetUserAccount(); if (entity == null) return; var temp = !string.IsNullOrEmpty(entity.F_UserId) ? await GetEntity(new { F_UserId = entity.F_UserId }) : null; if (temp == null) { entity.F_UserId = Guid.NewGuid().ToString(); entity.F_DeleteMark = 0; entity.F_EnabledMark = 1; entity.F_Secretkey = Md5Helper.Encrypt(CommonHelper.CreateNo(), 16).ToLower(); entity.F_Password = Md5Helper.Encrypt(DESEncrypt.Encrypt(entity.F_Password, entity.F_Secretkey).ToLower(), 32).ToLower(); entity.F_CreateUserId = userId; entity.F_CreateUserName = userAccount; entity.F_CreateDate = DateTime.Now; entity.F_ModifyUserId = userId; entity.F_ModifyUserName = userAccount; entity.F_ModifyDate = entity.F_CreateDate; await this.BaseRepository().Insert(entity); } else { entity.F_Account = null;// 账号不允许改动 entity.F_Secretkey = null; entity.F_Password = null; entity.F_ModifyUserId = userId; entity.F_ModifyUserName = userAccount; entity.F_ModifyDate = DateTime.Now; await this.BaseRepository().Update(entity); } } /// /// 修改用户登录密码 /// /// 主键值 /// 新密码(MD5 小写) /// 当前用户信息 public async Task RevisePassword(string keyValue, string password, UserEntity userInfo) { UserEntity userEntity = new UserEntity(); userEntity.F_ModifyUserId = userInfo.F_UserId; userEntity.F_ModifyUserName = userInfo.F_RealName; userEntity.F_UserId = keyValue; userEntity.F_ModifyDate = DateTime.Now; userEntity.F_Secretkey = Md5Helper.Encrypt(CommonHelper.CreateNo(), 16).ToLower(); userEntity.F_Password = Md5Helper.Encrypt(DESEncrypt.Encrypt(password, userEntity.F_Secretkey).ToLower(), 32).ToLower(); await this.BaseRepository().Update(userEntity); } /// /// 修改用户状态 /// /// 主键值 /// 状态:1-启动;0-禁用 /// 当前用户信息 public async Task UpdateState(string keyValue, int state, UserEntity userInfo) { UserEntity userEntity = new UserEntity(); userEntity.F_ModifyUserId = userInfo.F_UserId; userEntity.F_ModifyUserName = userInfo.F_RealName; userEntity.F_UserId = keyValue; userEntity.F_ModifyDate = DateTime.Now; userEntity.F_EnabledMark = state; await this.BaseRepository().Update(userEntity); } /// /// 修改用户信息 /// /// 实体对象 public async Task UpdateEntity(UserEntity userEntity) { await this.BaseRepository().Update(userEntity); } #endregion 提交数据 } }