using System;
using System.Collections.Generic;
using System.Linq;
using IoTIntegrationPlatform.Common;
using IoTIntegrationPlatform.Interface;
using IoTIntegrationPlatform.Model.common;
using IoTIntegrationPlatform.Model.Model;
using SqlSugar;
namespace IoTIntegrationPlatform.Services
{
public class AdminUserService : IAdminUserService
{
private ISqlSugarClient db { get; }
public AdminUserService(ISqlSugarClient _db)
{
db = _db;
}
#region 类句柄操作
private IntPtr handle;
private bool disposed = false;
//关闭句柄
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Runtime.InteropServices.DllImport("Kernel32")]
public extern static Boolean CloseHandle(IntPtr handle);
///
/// 释放对象资源
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// 释放对象资源
///
///
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (handle != IntPtr.Zero)
{
CloseHandle(handle);
handle = IntPtr.Zero;
}
}
disposed = true;
}
~AdminUserService()
{
Dispose(false);
}
#endregion
///
/// 获取管理人员信息
///
///
///
///
///
///
public AdminUser GetAdminUserInfo(string uuid, string account, string passWord, string mobile)
{
AdminUser model = new AdminUser();
try
{
model = db.Queryable()
.Where(it => it.IsValid == true)
.WhereIF(!string.IsNullOrEmpty(account), it => it.Account == account)
.WhereIF(!string.IsNullOrEmpty(passWord), it => it.Password == passWord)
.WhereIF(!string.IsNullOrEmpty(mobile), it => it.Mobile == mobile)
.First();
}
catch (Exception ex)
{
Logging.Error(ex, ex.Message);
}
return model;
}
///
/// 获取用户列表
///
///
///
///
public List GetUserList(BaseParm parm, ref int totalPage)
{
List list = new List();
try
{
list = db.CopyNew().Queryable()
.Where(it => it.IsValid == true && it.IsSuper != true)//隐藏到超级管理员
.WhereIF(!string.IsNullOrEmpty(parm.Name), it => it.RealName.Contains(parm.Name) || it.Account.Contains(parm.Name))
.WhereIF(!string.IsNullOrEmpty(parm.Param), it => it.Mobile == parm.Param )
.ToPageList(parm.PageIndex, parm.PageSize, ref totalPage);
}
catch (Exception ex)
{
Logging.Error(ex, ex.Message);
}
return list;
}
///
/// 新增
///
///
///
public bool Add(AdminUser model)
{
bool result = false;
try
{
model.IsSuper = false;
model.CreateTime = DateTime.Now;
model.UpdateTime = DateTime.Now;
model.IsValid = true;
model.Id = db.Insertable(model).ExecuteReturnIdentity();
if (model != null && model.Id > 0)
{
result = true;
}
}
catch (Exception ex)
{
Logging.Error(ex, ex.Message);
}
return result;
}
///
/// 编辑管理员
///
///
///
public bool UpdateUser(AdminUser model)
{
bool result = false;
try
{
model.UpdateTime = DateTime.Now;
int row = db.Updateable(model).ExecuteCommand();
if (row > 0)
{
result = true;
}
}
catch (Exception ex)
{
Logging.Error(ex, ex.Message);
}
return result;
}
///
/// 修改密码
///
///
///
public bool UpdatePassWord(AdminUser model)
{
bool result = false;
try
{
model.UpdateTime = DateTime.Now;
int row = db.Updateable()
.SetColumns(it => new AdminUser { UpdateTime = DateTime.Now, Password = model.Password,SecretKey=model.SecretKey })
.Where(it => it.Uuid == model.Uuid)
.ExecuteCommand();
if (row > 0)
{
result = true;
}
}
catch (Exception ex)
{
Logging.Error(ex, ex.Message);
}
return result;
}
///
/// 删除管理员
///
///
///
public bool DeleteUser(BaseParm parm)
{
bool result = false;
try
{
int row = db.Updateable()
.SetColumns(it => new AdminUser { UpdateTime = DateTime.Now, IsValid = true })
.Where(it => it.Id == parm.Id)
.ExecuteCommand();
if (row > 0)
{
result = true;
}
}
catch (Exception ex)
{
Logging.Error(ex, ex.Message);
}
return result;
}
///
/// 获取用户菜单权限
///
///
///
public List GetUserMenus(int userid)
{
List list = new List();
try
{
var query= db.Queryable()
.LeftJoin((a, b) => a.IsValid == true && a.MenuCode == b.MenuCode)
.LeftJoin((a, b, c) => b.IsValid == true && c.IsValid == true && b.RoleId == c.Id)
.LeftJoin((a, b, c, d) => d.IsValid == true && c.Id== d.RoleId)
.Where((a,b,c,d) => d.UserId == userid)
.Select((a, b, c, d) => new AdminMenu { });
list = query.ToList();
}
catch (Exception ex)
{
Logging.Error(ex, "GetUserMenus");
}
return list;
}
}
}