Skip to content

Commit

Permalink
优化代码生成对枚举字段的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 30, 2024
1 parent 83e0f86 commit 0044878
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 76 deletions.
52 changes: 12 additions & 40 deletions XCode/Code/EntityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,11 +1658,11 @@ protected virtual Boolean BuildExtendFind(IDataColumn[] columns, Int32 index)
var methodName = columns.Select(e => e.Name).Join("And");
methodName = $"FindBy{methodName}";

var ps = GetParameters(columns);
var args = ps.Join(", ", e => $"{e.Value} {e.Key}");
var builder = new SearchBuilder(Table) { Nullable = Option.Nullable };
var ps = builder.GetParameters(columns);

// 如果方法名已存在,则不生成
var key = $"{methodName}({ps.Join(",", e => e.Value)})";
var key = $"{methodName}({ps.Join(",", e => e.TypeName)})";
if (Members.Contains(key)) return false;
Members.Add(key);

Expand All @@ -1675,6 +1675,7 @@ protected virtual Boolean BuildExtendFind(IDataColumn[] columns, Int32 index)

var nullable = EntityOption.Nullable;

var args = ps.Join(", ", e => $"{e.TypeFullName} {e.Name}");
WriteLine("/// <returns>实体对象</returns>");
WriteLine("public static {2} {0}({1})", methodName, args, ClassName + (nullable ? "?" : ""));
WriteLine("{");
Expand Down Expand Up @@ -1761,11 +1762,11 @@ protected virtual Boolean BuildExtendFindAll(IDataColumn[] columns, Int32 index)
var methodName = columns.Select(e => e.Name).Join("And");
methodName = $"FindAllBy{methodName}";

var ps = GetParameters(columns);
var args = ps.Join(", ", e => $"{e.Value} {e.Key}");
var builder = new SearchBuilder(Table) { Nullable = Option.Nullable };
var ps = builder.GetParameters(columns);

// 如果方法名已存在,则不生成
var key = $"{methodName}({ps.Join(",", e => e.Value)})";
var key = $"{methodName}({ps.Join(",", e => e.TypeName)})";
if (Members.Contains(key)) return false;
Members.Add(key);

Expand All @@ -1776,6 +1777,7 @@ protected virtual Boolean BuildExtendFindAll(IDataColumn[] columns, Int32 index)
WriteLine("/// <param name=\"{0}\">{1}</param>", dc.CamelName(), dc.DisplayName);
}

var args = ps.Join(", ", e => $"{e.TypeFullName} {e.Name}");
WriteLine("/// <returns>实体列表</returns>");
WriteLine("public static IList<{2}> {0}({1})", methodName, args, ClassName);
WriteLine("{");
Expand Down Expand Up @@ -1827,36 +1829,6 @@ protected virtual Boolean BuildExtendFindAll(IDataColumn[] columns, Int32 index)
return true;
}

private IDictionary<String, String> GetParameters(IList<IDataColumn> columns)
{
var ps = new Dictionary<String, String>();
var ps2 = new Dictionary<String, String>();
foreach (var dc in columns)
{
var type = dc.Properties["Type"];
if (type.IsNullOrEmpty()) type = dc.DataType?.Name + "";

if (dc.DataType == typeof(Boolean))
type += "?";
else if (dc.DataType == typeof(String))
{
if (Option.Nullable && dc.Nullable)
{
type += "?";
}
}

ps[dc.CamelName()] = type;

var p = type.LastIndexOf('.');
if (p > 0) type = type[(p + 1)..];
ps2[dc.CamelName()] = type;
}

//return (ps, ps2);
return ps2;
}

/// <summary>自定义查询区域</summary>
protected virtual void BuildSearch()
{
Expand Down Expand Up @@ -1941,10 +1913,10 @@ protected virtual IList<IDataColumn> BuildAdvanceSearch()

var returnName = ClassName;

var ps = builder.GetParameters(cs, true, true, true);
var ps = builder.GetParameters(cs, true);

// 如果方法名已存在,则不生成
var key = $"Search({ps.Join(",", e => e.Value)})";
var key = $"Search({ps.Join(",", e => e.TypeName)})";
if (Members.Contains(key)) return cs;
Members.Add(key);

Expand All @@ -1966,7 +1938,7 @@ protected virtual IList<IDataColumn> BuildAdvanceSearch()
WriteLine("/// <returns>实体列表</returns>");

// 参数部分
var pis = ps.Join(", ", e => $"{e.Value} {e.Key}");
var pis = ps.Join(", ", e => $"{e.TypeFullName} {e.Name}");
WriteLine("public static IList<{0}> Search({1})", returnName, pis);
WriteLine("{");
{
Expand All @@ -1976,7 +1948,7 @@ protected virtual IList<IDataColumn> BuildAdvanceSearch()
WriteLine();
foreach (var dc in cs)
{
if (dc.DataType.IsInt() && dc.DataType.IsEnum)
if (dc.DataType.IsInt() && (dc.DataType.IsEnum || !dc.Properties["Type"].IsNullOrEmpty()))
WriteLine("if ({0} > 0) exp &= _.{1} == {0};", dc.CamelName(), dc.Name);
else if (dc.DataType.IsInt())
WriteLine("if ({0} >= 0) exp &= _.{1} == {0};", dc.CamelName(), dc.Name);
Expand Down
14 changes: 14 additions & 0 deletions XCode/Code/ParameterModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace XCode.Code;

/// <summary>参数模型</summary>
public class ParameterModel
{
/// <summary>参数名</summary>
public String Name { get; set; } = null!;

/// <summary>类型名。简称</summary>
public String TypeName { get; set; } = null!;

/// <summary>类型全名。含命名空间</summary>
public String TypeFullName { get; set; } = null!;
}
101 changes: 73 additions & 28 deletions XCode/Code/SearchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ public IList<IDataColumn> GetColumns()
// 数据时间字段可用于搜索
if (!dc.DataScale.IsNullOrEmpty())
cs.Add(dc);
// 整型枚举
if (dc.DataType.IsInt() && dc.DataType.IsEnum)
cs.Add(dc);
// 整型有映射
else if (dc.DataType.IsInt() && !dc.Map.IsNullOrEmpty())
cs.Add(dc);
// 整型
else if (dc.DataType.IsInt())
{
// 整型枚举
if (dc.DataType.IsEnum)
cs.Add(dc);
else if (!dc.Properties["Type"].IsNullOrEmpty())
cs.Add(dc);
// 整型有映射
else if (!dc.Map.IsNullOrEmpty())
cs.Add(dc);
}
// 布尔型
else if (dc.DataType == typeof(Boolean) && !dc.Name.EqualIgnoreCase("enable", "isDeleted"))
cs.Add(dc);
Expand Down Expand Up @@ -87,44 +93,83 @@ public IList<IDataColumn> GetColumns()
return cs;
}

/// <summary>获取参数列表。名称+类型</summary>
///// <summary>获取参数列表。名称+类型</summary>
///// <param name="columns"></param>
///// <param name="includeTime"></param>
///// <param name="includeKey"></param>
///// <param name="includePage"></param>
///// <returns></returns>
//public IDictionary<String, String> GetParameters(IList<IDataColumn> columns, Boolean includeTime = false, Boolean includeKey = false, Boolean includePage = false)
//{
// var ps = new Dictionary<String, String>();
// foreach (var dc in columns)
// {
// var type = dc.Properties["Type"];
// if (type.IsNullOrEmpty()) type = dc.DataType?.Name + "";

// if (dc.DataType == typeof(Boolean))
// type += "?";
// else if (dc.DataType == typeof(String))
// {
// if (Nullable && dc.Nullable)
// {
// type += "?";
// }
// }

// //var p = type.LastIndexOf('.');
// //if (p > 0) type = type[(p + 1)..];
// ps[dc.CamelName()] = type;
// }

// if (includeTime && DataTime != null)
// {
// ps["start"] = "DateTime";
// ps["end"] = "DateTime";
// }
// if (includeKey)
// ps["key"] = "String";
// if (includePage)
// ps["page"] = "PageParameter";

// return ps;
//}

/// <summary>获取参数列表。名称+类型(全名+简名)</summary>
/// <param name="columns"></param>
/// <param name="includeTime"></param>
/// <param name="includeKey"></param>
/// <param name="includePage"></param>
/// <param name="extend"></param>
/// <returns></returns>
public IDictionary<String, String> GetParameters(IList<IDataColumn> columns, Boolean includeTime = false, Boolean includeKey = false, Boolean includePage = false)
public IList<ParameterModel> GetParameters(IList<IDataColumn> columns, Boolean extend = false)
{
var ps = new Dictionary<String, String>();
var ps = new List<ParameterModel>();
foreach (var dc in columns)
{
var type = dc.Properties["Type"];
if (type.IsNullOrEmpty()) type = dc.DataType?.Name + "";

if (dc.DataType == typeof(Boolean))
type += "?";
else if (dc.DataType == typeof(String))
{
if (Nullable && dc.Nullable)
{
type += "?";
}
}
else if (dc.DataType == typeof(String) && Nullable && dc.Nullable)
type += "?";

var model = new ParameterModel { Name = dc.CamelName(), TypeName = type, TypeFullName = type };

var p = type.LastIndexOf('.');
if (p > 0) type = type[(p + 1)..];
ps[dc.CamelName()] = type;
if (p > 0) model.TypeName = type[(p + 1)..];

ps.Add(model);
}

if (includeTime && DataTime != null)
if (extend)
{
ps["start"] = "DateTime";
ps["end"] = "DateTime";
if (DataTime != null)
{
ps.Add(new ParameterModel { Name = "start", TypeName = "DateTime", TypeFullName = "DateTime" });
ps.Add(new ParameterModel { Name = "end", TypeName = "DateTime", TypeFullName = "DateTime" });
}
ps.Add(new ParameterModel { Name = "key", TypeName = "String", TypeFullName = "String" });
ps.Add(new ParameterModel { Name = "page", TypeName = "PageParameter", TypeFullName = "PageParameter" });
}
if (includeKey)
ps["key"] = "String";
if (includePage)
ps["page"] = "PageParameter";

return ps;
}
Expand Down
Binary file modified XCode/Membership/xcodetool.exe
Binary file not shown.
6 changes: 3 additions & 3 deletions XCodeTool/CubeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,6 @@ private String BuildSearch()
var cs = builder.GetColumns();
if (cs.Count <= 0) return null;

var ps = builder.GetParameters(cs, true, true, true);

var sb = Pool.StringBuilder.Get();

var pis = new List<String>();
Expand All @@ -351,7 +349,9 @@ private String BuildSearch()
if (dc.DataType.IsInt())
{
if (dc.DataType.IsEnum)
sb.AppendLine($" var {name} = ({dc.DataType.Name})p[\"{name}\"].ToInt();");
sb.AppendLine($" var {name} = ({dc.DataType.FullName})p[\"{name}\"].ToInt();");
else if (!dc.Properties["Type"].IsNullOrEmpty())
sb.AppendLine($" var {name} = ({dc.Properties["Type"]})p[\"{name}\"].ToInt();");
else if (dc.DataType == typeof(Int64))
sb.AppendLine($" var {name} = p[\"{name}\"].ToLong(-1);");
else
Expand Down
3 changes: 2 additions & 1 deletion XUnitTest.XCode/Code/Controllers/controller_parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ protected override IEnumerable<Parameter> Search(Pager p)
var userId = p["userId"].ToInt(-1);
var category = p["category"];
var name = p["name"];
var kind = (XCode.Membership.ParameterKinds)p["kind"].ToInt();
var enable = p["enable"]?.ToBoolean();

var start = p["dtStart"].ToDateTime();
var end = p["dtEnd"].ToDateTime();

return Parameter.Search(userId, category, name, enable, start, end, p["Q"], p);
return Parameter.Search(userId, category, name, kind, enable, start, end, p["Q"], p);
}
}
3 changes: 2 additions & 1 deletion XUnitTest.XCode/Code/Controllers/controller_user.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected override IEnumerable<User> Search(Pager p)
var code = p["code"];
var roleId = p["roleId"].ToInt(-1);
var departmentId = p["departmentId"].ToInt(-1);
var sex = (XCode.Membership.SexKinds)p["sex"].ToInt();
var mailVerified = p["mailVerified"]?.ToBoolean();
var mobileVerified = p["mobileVerified"]?.ToBoolean();
var areaId = p["areaId"].ToInt(-1);
Expand All @@ -69,6 +70,6 @@ protected override IEnumerable<User> Search(Pager p)
var start = p["dtStart"].ToDateTime();
var end = p["dtEnd"].ToDateTime();

return User.Search(name, mail, mobile, code, roleId, departmentId, mailVerified, mobileVerified, areaId, online, enable, start, end, p["Q"], p);
return User.Search(name, mail, mobile, code, roleId, departmentId, sex, mailVerified, mobileVerified, areaId, online, enable, start, end, p["Q"], p);
}
}
4 changes: 3 additions & 1 deletion XUnitTest.XCode/Code/Entity/字典参数.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,21 @@ public override Boolean Valid(DataMethod method)
/// <param name="userId">用户。按用户区分参数,用户0表示系统级</param>
/// <param name="category">类别</param>
/// <param name="name">名称</param>
/// <param name="kind">种类。0普通,21列表,22名值</param>
/// <param name="enable">启用</param>
/// <param name="start">更新时间开始</param>
/// <param name="end">更新时间结束</param>
/// <param name="key">关键字</param>
/// <param name="page">分页参数信息。可携带统计和数据权限扩展查询等信息</param>
/// <returns>实体列表</returns>
public static IList<Parameter> Search(Int32 userId, String? category, String? name, Boolean? enable, DateTime start, DateTime end, String key, PageParameter page)
public static IList<Parameter> Search(Int32 userId, String? category, String? name, XCode.Membership.ParameterKinds kind, Boolean? enable, DateTime start, DateTime end, String key, PageParameter page)
{
var exp = new WhereExpression();

if (userId >= 0) exp &= _.UserID == userId;
if (!category.IsNullOrEmpty()) exp &= _.Category == category;
if (!name.IsNullOrEmpty()) exp &= _.Name == name;
if (kind > 0) exp &= _.Kind == kind;
if (enable != null) exp &= _.Enable == enable;
exp &= _.UpdateTime.Between(start, end);
if (!key.IsNullOrEmpty()) exp &= SearchWhereByKeys(key);
Expand Down
4 changes: 3 additions & 1 deletion XUnitTest.XCode/Code/Entity/用户.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public override Boolean Valid(DataMethod method)
/// <param name="mobile">手机</param>
/// <param name="code">代码。身份证、员工编号等</param>
/// <param name="roleId">角色。主要角色</param>
/// <param name="sex">性别。未知、男、女</param>
/// <param name="areaId">地区。省市区</param>
/// <param name="online">在线</param>
/// <param name="enable">启用</param>
Expand All @@ -169,7 +170,7 @@ public override Boolean Valid(DataMethod method)
/// <param name="key">关键字</param>
/// <param name="page">分页参数信息。可携带统计和数据权限扩展查询等信息</param>
/// <returns>实体列表</returns>
public static IList<User> Search(String name, String? mail, String? mobile, String? code, Int32 roleId, Int32 areaId, Boolean? online, Boolean? enable, DateTime start, DateTime end, String key, PageParameter page)
public static IList<User> Search(String name, String? mail, String? mobile, String? code, Int32 roleId, XCode.Membership.SexKinds sex, Int32 areaId, Boolean? online, Boolean? enable, DateTime start, DateTime end, String key, PageParameter page)
{
var exp = new WhereExpression();

Expand All @@ -178,6 +179,7 @@ public static IList<User> Search(String name, String? mail, String? mobile, Stri
if (!mobile.IsNullOrEmpty()) exp &= _.Mobile == mobile;
if (!code.IsNullOrEmpty()) exp &= _.Code == code;
if (roleId >= 0) exp &= _.RoleID == roleId;
if (sex > 0) exp &= _.Sex == sex;
if (areaId >= 0) exp &= _.AreaId == areaId;
if (online != null) exp &= _.Online == online;
if (enable != null) exp &= _.Enable == enable;
Expand Down
4 changes: 3 additions & 1 deletion XUnitTest.XCode/Code/entity_user_normal_biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public override Boolean Valid(DataMethod method)
/// <param name="code">代码。身份证、员工编码等,支持登录</param>
/// <param name="roleId">角色。主要角色</param>
/// <param name="departmentId">部门。组织机构</param>
/// <param name="sex">性别。未知、男、女</param>
/// <param name="mailVerified">邮箱验证。邮箱是否已通过验证</param>
/// <param name="mobileVerified">手机验证。手机是否已通过验证</param>
/// <param name="areaId">地区。省市区</param>
Expand All @@ -167,7 +168,7 @@ public override Boolean Valid(DataMethod method)
/// <param name="key">关键字</param>
/// <param name="page">分页参数信息。可携带统计和数据权限扩展查询等信息</param>
/// <returns>实体列表</returns>
public static IList<User> Search(String name, String? mail, String? mobile, String? code, Int32 roleId, Int32 departmentId, Boolean? mailVerified, Boolean? mobileVerified, Int32 areaId, Boolean? online, Boolean? enable, DateTime start, DateTime end, String key, PageParameter page)
public static IList<User> Search(String name, String? mail, String? mobile, String? code, Int32 roleId, Int32 departmentId, XCode.Membership.SexKinds sex, Boolean? mailVerified, Boolean? mobileVerified, Int32 areaId, Boolean? online, Boolean? enable, DateTime start, DateTime end, String key, PageParameter page)
{
var exp = new WhereExpression();

Expand All @@ -177,6 +178,7 @@ public static IList<User> Search(String name, String? mail, String? mobile, Stri
if (!code.IsNullOrEmpty()) exp &= _.Code == code;
if (roleId >= 0) exp &= _.RoleID == roleId;
if (departmentId >= 0) exp &= _.DepartmentID == departmentId;
if (sex > 0) exp &= _.Sex == sex;
if (mailVerified != null) exp &= _.MailVerified == mailVerified;
if (mobileVerified != null) exp &= _.MobileVerified == mobileVerified;
if (areaId >= 0) exp &= _.AreaId == areaId;
Expand Down

0 comments on commit 0044878

Please sign in to comment.