添加 ExpressLogs 表及相关功能实现
在 `Model.xml` 中新增 `ExpressLogs` 表,包含 `Id`、`Code`、`CreateUser` 等列,并设置唯一索引。 在 `TimeShardTests.cs` 中添加测试类 `TimeShardTests`,验证 `ExpressLogs` 表的初始记录数为 0。 在 `快递单号记录.Biz.cs` 和 `快递单号记录.cs` 中实现 `ExpressLogs` 类,支持数据的增删改查操作,并提供字段名的快捷方式。
This commit is contained in:
parent
ab2966ec25
commit
58937fda71
|
@ -175,5 +175,22 @@
|
|||
<Column Name="Temp" DataType="Single" Description="温度" />
|
||||
</Columns>
|
||||
</Table>
|
||||
<Table Name="ExpressLogs" TableName="DH_ExpressLogs" Description="快递单号记录" IgnoreNameCase="False">
|
||||
<Columns>
|
||||
<Column Name="Id" DataType="Int64" PrimaryKey="True" DataScale="timeShard:yyyy" Description="编号" />
|
||||
<Column Name="Code" DataType="String" Nullable="False" Description="条形码" />
|
||||
<Column Name="CreateUser" DataType="String" Description="创建者" />
|
||||
<Column Name="CreateUserID" DataType="Int32" Description="创建者" />
|
||||
<Column Name="CreateTime" DataType="DateTime" Description="创建时间" />
|
||||
<Column Name="CreateIP" DataType="String" Description="创建地址" />
|
||||
<Column Name="UpdateUser" DataType="String" Description="更新者" />
|
||||
<Column Name="UpdateUserID" DataType="Int32" Description="更新者" />
|
||||
<Column Name="UpdateTime" DataType="DateTime" Description="更新时间" />
|
||||
<Column Name="UpdateIP" DataType="String" Description="更新地址" />
|
||||
</Columns>
|
||||
<Indexes>
|
||||
<Index Columns="Code" Unique="True" />
|
||||
</Indexes>
|
||||
</Table>
|
||||
</Tables>
|
||||
</EntityModel>
|
|
@ -0,0 +1,156 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Script.Serialization;
|
||||
using System.Xml.Serialization;
|
||||
using NewLife;
|
||||
using NewLife.Data;
|
||||
using NewLife.Log;
|
||||
using NewLife.Model;
|
||||
using NewLife.Reflection;
|
||||
using NewLife.Threading;
|
||||
using NewLife.Web;
|
||||
using XCode;
|
||||
using XCode.Cache;
|
||||
using XCode.Configuration;
|
||||
using XCode.DataAccessLayer;
|
||||
using XCode.Membership;
|
||||
using XCode.Shards;
|
||||
|
||||
namespace XUnitTest.XCode.TestEntity;
|
||||
|
||||
public partial class ExpressLogs : Entity<ExpressLogs>
|
||||
{
|
||||
#region 对象操作
|
||||
static ExpressLogs()
|
||||
{
|
||||
// 累加字段,生成 Update xx Set Count=Count+1234 Where xxx
|
||||
//var df = Meta.Factory.AdditionalFields;
|
||||
//df.Add(nameof(CreateUserID));
|
||||
|
||||
// 按年分表
|
||||
Meta.ShardPolicy = new TimeShardPolicy(nameof(Id), Meta.Factory)
|
||||
{
|
||||
TablePolicy = "{0}_{1:yyyy}",
|
||||
Step = TimeSpan.FromDays(365),
|
||||
};
|
||||
|
||||
// 过滤器 UserModule、TimeModule、IPModule
|
||||
Meta.Modules.Add(new UserModule { AllowEmpty = false });
|
||||
Meta.Modules.Add<TimeModule>();
|
||||
Meta.Modules.Add(new IPModule { AllowEmpty = false });
|
||||
|
||||
// 实体缓存
|
||||
// var ec = Meta.Cache;
|
||||
// ec.Expire = 60;
|
||||
}
|
||||
|
||||
/// <summary>验证并修补数据,返回验证结果,或者通过抛出异常的方式提示验证失败。</summary>
|
||||
/// <param name="method">添删改方法</param>
|
||||
public override Boolean Valid(DataMethod method)
|
||||
{
|
||||
//if (method == DataMethod.Delete) return true;
|
||||
// 如果没有脏数据,则不需要进行任何处理
|
||||
if (!HasDirty) return true;
|
||||
|
||||
// 这里验证参数范围,建议抛出参数异常,指定参数名,前端用户界面可以捕获参数异常并聚焦到对应的参数输入框
|
||||
if (Code.IsNullOrEmpty()) throw new ArgumentNullException(nameof(Code), "条形码不能为空!");
|
||||
|
||||
// 建议先调用基类方法,基类方法会做一些统一处理
|
||||
if (!base.Valid(method)) return false;
|
||||
|
||||
// 在新插入数据或者修改了指定字段时进行修正
|
||||
|
||||
// 处理当前已登录用户信息,可以由UserModule过滤器代劳
|
||||
/*var user = ManageProvider.User;
|
||||
if (user != null)
|
||||
{
|
||||
if (method == DataMethod.Insert && !Dirtys[nameof(CreateUserID)]) CreateUserID = user.ID;
|
||||
if (!Dirtys[nameof(UpdateUserID)]) UpdateUserID = user.ID;
|
||||
}*/
|
||||
//if (method == DataMethod.Insert && !Dirtys[nameof(CreateTime)]) CreateTime = DateTime.Now;
|
||||
//if (!Dirtys[nameof(UpdateTime)]) UpdateTime = DateTime.Now;
|
||||
//if (method == DataMethod.Insert && !Dirtys[nameof(CreateIP)]) CreateIP = ManageProvider.UserHost;
|
||||
//if (!Dirtys[nameof(UpdateIP)]) UpdateIP = ManageProvider.UserHost;
|
||||
|
||||
// 检查唯一索引
|
||||
// CheckExist(method == DataMethod.Insert, nameof(Code));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
///// <summary>首次连接数据库时初始化数据,仅用于实体类重载,用户不应该调用该方法</summary>
|
||||
//[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
//protected override void InitData()
|
||||
//{
|
||||
// // InitData一般用于当数据表没有数据时添加一些默认数据,该实体类的任何第一次数据库操作都会触发该方法,默认异步调用
|
||||
// if (Meta.Session.Count > 0) return;
|
||||
|
||||
// if (XTrace.Debug) XTrace.WriteLine("开始初始化ExpressLogs[快递单号记录]数据……");
|
||||
|
||||
// var entity = new ExpressLogs();
|
||||
// entity.Id = 0;
|
||||
// entity.Code = "abc";
|
||||
// entity.Insert();
|
||||
|
||||
// if (XTrace.Debug) XTrace.WriteLine("完成初始化ExpressLogs[快递单号记录]数据!");
|
||||
//}
|
||||
|
||||
///// <summary>已重载。基类先调用Valid(true)验证数据,然后在事务保护内调用OnInsert</summary>
|
||||
///// <returns></returns>
|
||||
//public override Int32 Insert()
|
||||
//{
|
||||
// return base.Insert();
|
||||
//}
|
||||
|
||||
///// <summary>已重载。在事务保护范围内处理业务,位于Valid之后</summary>
|
||||
///// <returns></returns>
|
||||
//protected override Int32 OnDelete()
|
||||
//{
|
||||
// return base.OnDelete();
|
||||
//}
|
||||
#endregion
|
||||
|
||||
#region 扩展属性
|
||||
#endregion
|
||||
|
||||
#region 高级查询
|
||||
/// <summary>高级查询</summary>
|
||||
/// <param name="code">条形码</param>
|
||||
/// <param name="start">编号开始</param>
|
||||
/// <param name="end">编号结束</param>
|
||||
/// <param name="key">关键字</param>
|
||||
/// <param name="page">分页参数信息。可携带统计和数据权限扩展查询等信息</param>
|
||||
/// <returns>实体列表</returns>
|
||||
public static IList<ExpressLogs> Search(String code, DateTime start, DateTime end, String key, PageParameter page)
|
||||
{
|
||||
var exp = new WhereExpression();
|
||||
|
||||
if (!code.IsNullOrEmpty()) exp &= _.Code == code;
|
||||
exp &= _.Id.Between(start, end, Meta.Factory.Snow);
|
||||
if (!key.IsNullOrEmpty()) exp &= SearchWhereByKeys(key);
|
||||
|
||||
return FindAll(exp, page);
|
||||
}
|
||||
|
||||
// Select Count(Id) as Id,Category From DH_ExpressLogs Where CreateTime>'2020-01-24 00:00:00' Group By Category Order By Id Desc limit 20
|
||||
//static readonly FieldCache<ExpressLogs> _CategoryCache = new(nameof(Category))
|
||||
//{
|
||||
//Where = _.CreateTime > DateTime.Today.AddDays(-30) & Expression.Empty
|
||||
//};
|
||||
|
||||
///// <summary>获取类别列表,字段缓存10分钟,分组统计数据最多的前20种,用于魔方前台下拉选择</summary>
|
||||
///// <returns></returns>
|
||||
//public static IDictionary<String, String> GetCategoryList() => _CategoryCache.FindAllName();
|
||||
#endregion
|
||||
|
||||
#region 业务操作
|
||||
#endregion
|
||||
}
|
|
@ -0,0 +1,276 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Web.Script.Serialization;
|
||||
using System.Xml.Serialization;
|
||||
using NewLife;
|
||||
using NewLife.Data;
|
||||
using NewLife.Log;
|
||||
using XCode;
|
||||
using XCode.Cache;
|
||||
using XCode.Configuration;
|
||||
using XCode.DataAccessLayer;
|
||||
|
||||
namespace XUnitTest.XCode.TestEntity;
|
||||
|
||||
/// <summary>快递单号记录</summary>
|
||||
[Serializable]
|
||||
[DataObject]
|
||||
[Description("快递单号记录")]
|
||||
[BindIndex("IU_DH_ExpressLogs_Code", true, "Code")]
|
||||
[BindTable("DH_ExpressLogs", Description = "快递单号记录", ConnName = "test", DbType = DatabaseType.None)]
|
||||
public partial class ExpressLogs
|
||||
{
|
||||
#region 属性
|
||||
private Int64 _Id;
|
||||
/// <summary>编号</summary>
|
||||
[DisplayName("编号")]
|
||||
[Description("编号")]
|
||||
[DataObjectField(true, false, false, 0)]
|
||||
[BindColumn("Id", "编号", "", DataScale = "timeShard:yyyy")]
|
||||
public Int64 Id { get => _Id; set { if (OnPropertyChanging("Id", value)) { _Id = value; OnPropertyChanged("Id"); } } }
|
||||
|
||||
private String _Code;
|
||||
/// <summary>条形码</summary>
|
||||
[DisplayName("条形码")]
|
||||
[Description("条形码")]
|
||||
[DataObjectField(false, false, false, 50)]
|
||||
[BindColumn("Code", "条形码", "")]
|
||||
public String Code { get => _Code; set { if (OnPropertyChanging("Code", value)) { _Code = value; OnPropertyChanged("Code"); } } }
|
||||
|
||||
private String _CreateUser;
|
||||
/// <summary>创建者</summary>
|
||||
[DisplayName("创建者")]
|
||||
[Description("创建者")]
|
||||
[DataObjectField(false, false, true, 50)]
|
||||
[BindColumn("CreateUser", "创建者", "")]
|
||||
public String CreateUser { get => _CreateUser; set { if (OnPropertyChanging("CreateUser", value)) { _CreateUser = value; OnPropertyChanged("CreateUser"); } } }
|
||||
|
||||
private Int32 _CreateUserID;
|
||||
/// <summary>创建者</summary>
|
||||
[DisplayName("创建者")]
|
||||
[Description("创建者")]
|
||||
[DataObjectField(false, false, false, 0)]
|
||||
[BindColumn("CreateUserID", "创建者", "")]
|
||||
public Int32 CreateUserID { get => _CreateUserID; set { if (OnPropertyChanging("CreateUserID", value)) { _CreateUserID = value; OnPropertyChanged("CreateUserID"); } } }
|
||||
|
||||
private DateTime _CreateTime;
|
||||
/// <summary>创建时间</summary>
|
||||
[DisplayName("创建时间")]
|
||||
[Description("创建时间")]
|
||||
[DataObjectField(false, false, true, 0)]
|
||||
[BindColumn("CreateTime", "创建时间", "")]
|
||||
public DateTime CreateTime { get => _CreateTime; set { if (OnPropertyChanging("CreateTime", value)) { _CreateTime = value; OnPropertyChanged("CreateTime"); } } }
|
||||
|
||||
private String _CreateIP;
|
||||
/// <summary>创建地址</summary>
|
||||
[DisplayName("创建地址")]
|
||||
[Description("创建地址")]
|
||||
[DataObjectField(false, false, true, 50)]
|
||||
[BindColumn("CreateIP", "创建地址", "")]
|
||||
public String CreateIP { get => _CreateIP; set { if (OnPropertyChanging("CreateIP", value)) { _CreateIP = value; OnPropertyChanged("CreateIP"); } } }
|
||||
|
||||
private String _UpdateUser;
|
||||
/// <summary>更新者</summary>
|
||||
[DisplayName("更新者")]
|
||||
[Description("更新者")]
|
||||
[DataObjectField(false, false, true, 50)]
|
||||
[BindColumn("UpdateUser", "更新者", "")]
|
||||
public String UpdateUser { get => _UpdateUser; set { if (OnPropertyChanging("UpdateUser", value)) { _UpdateUser = value; OnPropertyChanged("UpdateUser"); } } }
|
||||
|
||||
private Int32 _UpdateUserID;
|
||||
/// <summary>更新者</summary>
|
||||
[DisplayName("更新者")]
|
||||
[Description("更新者")]
|
||||
[DataObjectField(false, false, false, 0)]
|
||||
[BindColumn("UpdateUserID", "更新者", "")]
|
||||
public Int32 UpdateUserID { get => _UpdateUserID; set { if (OnPropertyChanging("UpdateUserID", value)) { _UpdateUserID = value; OnPropertyChanged("UpdateUserID"); } } }
|
||||
|
||||
private DateTime _UpdateTime;
|
||||
/// <summary>更新时间</summary>
|
||||
[DisplayName("更新时间")]
|
||||
[Description("更新时间")]
|
||||
[DataObjectField(false, false, true, 0)]
|
||||
[BindColumn("UpdateTime", "更新时间", "")]
|
||||
public DateTime UpdateTime { get => _UpdateTime; set { if (OnPropertyChanging("UpdateTime", value)) { _UpdateTime = value; OnPropertyChanged("UpdateTime"); } } }
|
||||
|
||||
private String _UpdateIP;
|
||||
/// <summary>更新地址</summary>
|
||||
[DisplayName("更新地址")]
|
||||
[Description("更新地址")]
|
||||
[DataObjectField(false, false, true, 50)]
|
||||
[BindColumn("UpdateIP", "更新地址", "")]
|
||||
public String UpdateIP { get => _UpdateIP; set { if (OnPropertyChanging("UpdateIP", value)) { _UpdateIP = value; OnPropertyChanged("UpdateIP"); } } }
|
||||
#endregion
|
||||
|
||||
#region 获取/设置 字段值
|
||||
/// <summary>获取/设置 字段值</summary>
|
||||
/// <param name="name">字段名</param>
|
||||
/// <returns></returns>
|
||||
public override Object this[String name]
|
||||
{
|
||||
get => name switch
|
||||
{
|
||||
"Id" => _Id,
|
||||
"Code" => _Code,
|
||||
"CreateUser" => _CreateUser,
|
||||
"CreateUserID" => _CreateUserID,
|
||||
"CreateTime" => _CreateTime,
|
||||
"CreateIP" => _CreateIP,
|
||||
"UpdateUser" => _UpdateUser,
|
||||
"UpdateUserID" => _UpdateUserID,
|
||||
"UpdateTime" => _UpdateTime,
|
||||
"UpdateIP" => _UpdateIP,
|
||||
_ => base[name]
|
||||
};
|
||||
set
|
||||
{
|
||||
switch (name)
|
||||
{
|
||||
case "Id": _Id = value.ToLong(); break;
|
||||
case "Code": _Code = Convert.ToString(value); break;
|
||||
case "CreateUser": _CreateUser = Convert.ToString(value); break;
|
||||
case "CreateUserID": _CreateUserID = value.ToInt(); break;
|
||||
case "CreateTime": _CreateTime = value.ToDateTime(); break;
|
||||
case "CreateIP": _CreateIP = Convert.ToString(value); break;
|
||||
case "UpdateUser": _UpdateUser = Convert.ToString(value); break;
|
||||
case "UpdateUserID": _UpdateUserID = value.ToInt(); break;
|
||||
case "UpdateTime": _UpdateTime = value.ToDateTime(); break;
|
||||
case "UpdateIP": _UpdateIP = Convert.ToString(value); break;
|
||||
default: base[name] = value; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 关联映射
|
||||
#endregion
|
||||
|
||||
#region 扩展查询
|
||||
/// <summary>根据编号查找</summary>
|
||||
/// <param name="id">编号</param>
|
||||
/// <returns>实体对象</returns>
|
||||
public static ExpressLogs FindById(Int64 id)
|
||||
{
|
||||
if (id < 0) return null;
|
||||
|
||||
return Find(_.Id == id);
|
||||
}
|
||||
|
||||
/// <summary>根据条形码查找</summary>
|
||||
/// <param name="code">条形码</param>
|
||||
/// <returns>实体对象</returns>
|
||||
public static ExpressLogs FindByCode(String code)
|
||||
{
|
||||
if (code.IsNullOrEmpty()) return null;
|
||||
|
||||
return Find(_.Code == code);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 数据清理
|
||||
/// <summary>清理指定时间段内的数据</summary>
|
||||
/// <param name="start">开始时间。未指定时清理小于指定时间的所有数据</param>
|
||||
/// <param name="end">结束时间</param>
|
||||
/// <returns>清理行数</returns>
|
||||
public static Int32 DeleteWith(DateTime start, DateTime end)
|
||||
{
|
||||
return Delete(_.Id.Between(start, end, Meta.Factory.Snow));
|
||||
}
|
||||
|
||||
/// <summary>删除指定时间段内的数据表</summary>
|
||||
/// <param name="start">开始时间</param>
|
||||
/// <param name="end">结束时间</param>
|
||||
/// <returns>清理行数</returns>
|
||||
public static Int32 DropWith(DateTime start, DateTime end)
|
||||
{
|
||||
return Meta.AutoShard(start, end, session =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return session.Execute($"Drop Table {session.FormatedTableName}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
XTrace.WriteException(ex);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
).Sum();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 字段名
|
||||
/// <summary>取得快递单号记录字段信息的快捷方式</summary>
|
||||
public partial class _
|
||||
{
|
||||
/// <summary>编号</summary>
|
||||
public static readonly Field Id = FindByName("Id");
|
||||
|
||||
/// <summary>条形码</summary>
|
||||
public static readonly Field Code = FindByName("Code");
|
||||
|
||||
/// <summary>创建者</summary>
|
||||
public static readonly Field CreateUser = FindByName("CreateUser");
|
||||
|
||||
/// <summary>创建者</summary>
|
||||
public static readonly Field CreateUserID = FindByName("CreateUserID");
|
||||
|
||||
/// <summary>创建时间</summary>
|
||||
public static readonly Field CreateTime = FindByName("CreateTime");
|
||||
|
||||
/// <summary>创建地址</summary>
|
||||
public static readonly Field CreateIP = FindByName("CreateIP");
|
||||
|
||||
/// <summary>更新者</summary>
|
||||
public static readonly Field UpdateUser = FindByName("UpdateUser");
|
||||
|
||||
/// <summary>更新者</summary>
|
||||
public static readonly Field UpdateUserID = FindByName("UpdateUserID");
|
||||
|
||||
/// <summary>更新时间</summary>
|
||||
public static readonly Field UpdateTime = FindByName("UpdateTime");
|
||||
|
||||
/// <summary>更新地址</summary>
|
||||
public static readonly Field UpdateIP = FindByName("UpdateIP");
|
||||
|
||||
static Field FindByName(String name) => Meta.Table.FindByName(name);
|
||||
}
|
||||
|
||||
/// <summary>取得快递单号记录字段名称的快捷方式</summary>
|
||||
public partial class __
|
||||
{
|
||||
/// <summary>编号</summary>
|
||||
public const String Id = "Id";
|
||||
|
||||
/// <summary>条形码</summary>
|
||||
public const String Code = "Code";
|
||||
|
||||
/// <summary>创建者</summary>
|
||||
public const String CreateUser = "CreateUser";
|
||||
|
||||
/// <summary>创建者</summary>
|
||||
public const String CreateUserID = "CreateUserID";
|
||||
|
||||
/// <summary>创建时间</summary>
|
||||
public const String CreateTime = "CreateTime";
|
||||
|
||||
/// <summary>创建地址</summary>
|
||||
public const String CreateIP = "CreateIP";
|
||||
|
||||
/// <summary>更新者</summary>
|
||||
public const String UpdateUser = "UpdateUser";
|
||||
|
||||
/// <summary>更新者</summary>
|
||||
public const String UpdateUserID = "UpdateUserID";
|
||||
|
||||
/// <summary>更新时间</summary>
|
||||
public const String UpdateTime = "UpdateTime";
|
||||
|
||||
/// <summary>更新地址</summary>
|
||||
public const String UpdateIP = "UpdateIP";
|
||||
}
|
||||
#endregion
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using Xunit;
|
||||
|
||||
using XUnitTest.XCode.TestEntity;
|
||||
|
||||
namespace XUnitTest.XCode;
|
||||
|
||||
public class TimeShardTests
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
var n = ExpressLogs.Meta.Count;
|
||||
|
||||
Assert.Equal(0, n);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue