using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Bro.Process.DataBase
{
public class BaseModel
{
///
/// 编号,唯一值,使用GUID
///
[Key]
[Required(ErrorMessage = "编号不可为空!")]
[StringLength(64)]
public string ID { get; set; } = Guid.NewGuid().ToString().ToUpper();
///
/// 禁用状态 0 未禁用 1 已禁用
///
[Required]
public int IS_DISABLED { get; set; } = 0;
///
/// 删除状态 0 未删除 1 已删除
///
[Required]
public int IS_DELETED { get; set; } = 0;
///
/// 创建人信息
///
//[Required]
[StringLength(64)]
public string CREATE_USER { get; set; }
///
/// 创建时间
///
//[Required]
public DateTime? CREATE_TIME { get; set; } = DateTime.Now;
///
/// 更新人信息
///
[StringLength(64)]
public string UPDATE_USER { get; set; }
///
/// 更新时间
///
public DateTime? UPDATE_TIME { get; set; }
}
public static class BaseModelHelper
{
public static void SetNew(this T t, string userId) where T : BaseModel
{
t.CREATE_USER = userId;
t.CREATE_TIME = DateTime.Now;
}
public static void SetUpdate(this T t, string userId) where T : BaseModel
{
t.UPDATE_USER = userId;
t.UPDATE_TIME = DateTime.Now;
}
static List NoTransferProps = new List() { "ID", "CREATE_USER", "CREATE_TIME", "UPDATE_USER", "UPDATE_TIME", "IS_DELETED" };
public static void DataTransfer(this T destT, T sourceT) where T : BaseModel
{
destT.DataFrom(sourceT, NoTransferProps);
}
public static void DataFrom(this T1 destT, T2 sourceT, List exceptionProps = null) where T1 : class where T2 : class
{
PropertyInfo[] propDest = destT.GetType().GetProperties().Where(p => !(p.GetMethod.IsVirtual && !p.GetMethod.IsFinal)).ToArray();
PropertyInfo[] propSource = sourceT.GetType().GetProperties();
Array.ForEach(propDest, prop =>
{
if (exceptionProps == null || !exceptionProps.Contains(prop.Name))
{
if (prop.CanWrite)
{
PropertyInfo propS = propSource.FirstOrDefault(p => p.Name == prop.Name);
if (propS != null && propS.CanRead)
{
prop.SetValue(destT, propS.GetValue(sourceT));
}
}
}
});
}
public static List ToPagedList(this IQueryable orderQuery, BaseRequest request) where T : class
{
return orderQuery.Skip((request.PageNum - 1) * request.PageSize).Take(request.PageSize).ToList();
}
}
public class BaseRequest : INotifyPropertyChanged
{
private int pageNum = 1;
///
/// 查询结果页数
///
public int PageNum
{
get => pageNum;
set
{
if (pageNum != value)
{
pageNum = value;
PropertyChanged?.BeginInvoke(this, new PropertyChangedEventArgs("PageNum"), null, null);
}
}
}
private int pageSize = 100;
///
/// 每页的查询结果条数
///
public int PageSize
{
get => pageSize;
set
{
if (pageSize != value)
{
pageSize = value;
PropertyChanged?.BeginInvoke(this, new PropertyChangedEventArgs("PageSize"), null, null);
}
}
}
///
/// 查询起始时间
///
public DateTime? StartTime { get; set; }
///
/// 查询结束时间
///
public DateTime? EndTime { get; set; }
///
/// 启用状态过滤器 1:仅未启用 0:仅启用 -1:全部
///
public int DisableFilter { get; set; } = 0;
///
/// 查询字符串
///
public string SearchTxt { get; set; }
private int totalNum = 0;
///
/// 数据总数
///
public int TotalNum
{
get => totalNum;
set
{
if (totalNum != value)
{
totalNum = value;
PropertyChanged?.BeginInvoke(this, new PropertyChangedEventArgs("TotalNum"), null, null);
}
TotalPage = (int)Math.Ceiling((double)TotalNum / PageSize);
}
}
private int totalPage = 0;
///
/// 总页数
///
public int TotalPage
{
get => totalPage;
set
{
if (totalPage != value)
{
totalPage = value;
PropertyChanged?.BeginInvoke(this, new PropertyChangedEventArgs("TotalPage"), null, null);
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}