using CommonLib.DBUtils.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCSPro.Model;
namespace WCSPro.IDAL
{
public interface IUserDAL : IBaseDAL<UserInfo>
{
UserInfo GetUserByLoginKey(string loginKey);
bool Exists(string gid);
bool ExistsUserCode(string userCode);
bool AddUser(UserInfo user);
bool UpdateUser(UserInfo user);
UserInfo GetUserById(string gid);
List<UserInfo> Filter(string filterKey);
bool Remove(string gid);
}
}
using CommonLib.DBUtils.SqlSugar;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCSPro.IDAL;
using WCSPro.Model;
namespace WCSPro.DAL
{
public class UserDAL : BaseDAL<UserInfo>, IUserDAL
{
public UserDAL(ISqlSugarClient db) : base(db)
{
Context.DbMaintenance.CreateDatabase();
Context.CodeFirst.SetStringDefaultLength(200).InitTables<UserInfo>();
if (!IsAny(t => t.GID == "Admin"))
{
Insert(new UserInfo()
{
GID = "Admin",
Code = "Admin",
PetName = "Administrator",
RealName = "Administrator",
CreateTime = Context.GetDate(),
});
}
}
public bool AddUser(UserInfo user)
{
return base.AddObj(user);
}
public bool Exists(string gid)
{
return Context.Queryable<UserInfo>().Any(t => t.GID == gid);
}
public bool ExistsUserCode(string userCode)
{
return Context.Queryable<UserInfo>().Any(t => t.Code.ToUpper() == userCode.ToUpper());
}
public List<UserInfo> Filter(string filterKey)
{
return Context.Queryable<UserInfo>()
.WhereIF
(
!string.IsNullOrWhiteSpace(filterKey),
t => t.Code.ToUpper().Contains(filterKey) ||
t.Email.ToUpper().Contains(filterKey) ||
t.PetName.ToUpper().Contains(filterKey) ||
t.RealName.ToUpper().Contains(filterKey) ||
t.Phone.ToUpper().Contains(filterKey)
).ToList();
}
public UserInfo GetUserById(string gid)
{
return Context.Queryable<UserInfo>().Single(t => t.GID == gid);
}
public UserInfo GetUserByLoginKey(string loginKey)
{
string key = loginKey.ToUpper();
return Context.Queryable<UserInfo>().Single(t => t.Code.ToUpper() == key ||
(!SqlFunc.IsNullOrEmpty(t.Email) && t.Email.ToUpper() == key) ||
(!string.IsNullOrWhiteSpace(t.Email) && t.Phone.ToUpper() == key));
}
public bool Remove(string gid)
{
return Context.Deleteable<UserInfo>(gid).ExecuteCommand() == 1;
}
public bool UpdateUser(UserInfo user)
{
return Context.Updateable(user).ExecuteCommand() == 1;
}
}
}
using PropertyChanged;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace WCSPro.Model
{
[AddINotifyPropertyChangedInterface]
public class UserInfo
{
[Display(Name = "GID", AutoGenerateField = false)]
[SugarColumn(IsPrimaryKey = true, ColumnDataType = "varchar(36)")]
public string GID { get; set; } = Guid.NewGuid().ToString();
[SugarColumn(IsNullable = true, IsJson = true)]
public Role Role { get; set; }
[Display(Name = "账号")]
[SugarColumn(ColumnDataType = "nvarchar(32)")]
public string Code { get; set; } = string.Empty;
[Display(Name = "邮箱")]
[SugarColumn(ColumnDataType = "nvarchar(64)")]
public string Email { get; set; } = string.Empty;
[Display(Name = "电话")]
[SugarColumn(ColumnDataType = "nvarchar(64)")]
public string Phone { get; set; } = string.Empty;
[Display(Name = "昵称")]
[SugarColumn(ColumnDataType = "nvarchar(16)")]
public string PetName { get; set; } = string.Empty;
[Display(Name = "密码")]
[SugarColumn(ColumnDataType = "nvarchar(64)")]
public string Password { get; set; } = string.Empty;
[Display(Name = "真实姓名")]
[SugarColumn(ColumnDataType = "nvarchar(16)")]
public string RealName { get; set; } = string.Empty;
[Display(Name = "年龄")]
[SugarColumn(ColumnDataType = "int")]
public int Age { get; set; }
[Display(Name = "性别")]
[SugarColumn(ColumnDataType = "nvarchar(8)")]
public string Sex { get; set; } = "女";
[Display(Name = "生日")]
[SugarColumn(ColumnDataType = "datetime", IsNullable = true)]
public DateTime? Birthday { get; set; }
[Display(Name = "用户状态")]
[SugarColumn(ColumnDataType = "int")]
public int StateId { get; set; } = 1;
[Display(Name = "备注")]
[SugarColumn(ColumnDataType = "nvarchar(512)")]
public string Note { get; set; } = string.Empty;
[Display(Name = "创建时间")]
[SugarColumn(ColumnDataType = "datetime", IsNullable = true)]
public DateTime? CreateTime { get; set; }
}
}