QueryBuilderParemeter扩展

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

QueryBuilderParemeter扩展

原生QueryBuilderParemeter使用方法

QueryBuilderParemeter queryBuilder = new QueryBuilderParemeter() { FormId = "BD_Supplier" };
ExtJoinTableDescription joinMaterialSupplier = new ExtJoinTableDescription()
{
	JoinOption = QueryBuilderParemeter.JoinOption.InnerJoin,
	TableName = "RH_MaterialSupplier",
	TableNameAs = "ms",
	FieldName = "FSupplierID",
	ScourceKey = "FSupplierID",
};
ExtJoinTableDescription joinMaterialSupplierEntry = new ExtJoinTableDescription()
{
	JoinOption = QueryBuilderParemeter.JoinOption.InnerJoin,
	TableName = "RH_MaterialSupplierEntry",
	TableNameAs = "mse",
	FieldName = "FId",
	ScourceKey = "ms.FId"
};
queryBuilder.ExtJoinTables.Add(joinMaterialSupplier);
queryBuilder.ExtJoinTables.Add(joinMaterialSupplierEntry);
queryBuilder.SelectItems = SelectorItemInfo.CreateItems("FSupplierId");
queryBuilder.FilterClauseWihtKey = "FDocumentStatus=@Status AND FEnableSL=1 AND ms.FDocumentStatus=@Status AND mse.FFirstCategoryId=@FirstCategoryId AND mse.FSecondCategoryId=@SecondCategoryId";
queryBuilder.SqlParams.Add(new SqlParam("Status", KDDbType.String, "C"));
queryBuilder.SqlParams.Add(new SqlParam("FirstCategoryId", KDDbType.Int64, firstCategoryId));
queryBuilder.SqlParams.Add(new SqlParam("SecondCategoryId", KDDbType.Int64, secondCategoryId));

DynamicObjectType supplierType = FormMetaDataCache.GetCachedFormMetaData(this.Context, "BD_Supplier").BusinessInfo.GetDynamicObjectType();
var suppliers = BusinessDataServiceHelper.Load(this.Context, supplierType, queryBuilder);


扩展后将写重复代码封装到扩展方法中,大大减少代码量,基于扩展的使用方法:

QueryBuilderParemeter queryBuilder = QueryBuilderHelper.Create("BD_Supplier")
	.Join("RH_MaterialSupplier", "ms", "FSupplierID", "FSupplierID")
	.Join("RH_MaterialSupplierEntry", "mse", "FId", "ms.FId")
	.SetSelectFields("FSupplierId")
	.SetFilter("FDocumentStatus=@Status AND FEnableSL=1 AND ms.FDocumentStatus=@Status AND mse.FFirstCategoryId=@FirstCategoryId AND mse.FSecondCategoryId=@SecondCategoryId")
	.AddParameter("Status", "C")
	.AddParameter("FirstCategoryId", firstCategoryId, KDDbType.Int32)
	.AddParameter("SecondCategoryId", secondCategoryId, KDDbType.Int32);

DynamicObjectType supplierType = FormMetaDataCache.GetCachedFormMetaData(this.Context, "BD_Supplier").BusinessInfo.GetDynamicObjectType();
var suppliers = BusinessDataServiceHelper.Load(this.Context, supplierType, queryBuilder);


QueryBuilderParemeterExtension.cs源码

using Kingdee.BOS;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.SqlBuilder;

namespace System
{
    /// <summary>
    /// QueryBuilderParemeter工具类
    /// </summary>
    public static class QueryBuilderHelper
    {
        /// <summary>
        /// 创建QueryBuilderParemeter实例
        /// </summary>
        /// <param name="formId">元数据实体标识</param>
        /// <returns></returns>
        public static QueryBuilderParemeter Create(string formId)
        {
            return new QueryBuilderParemeter() { FormId = formId };
        }
    }

    /// <summary>
    /// QueryBuilderParemeter 扩展方法
    /// </summary>
    public static class QueryBuilderParemeterExtension
    {
        /// <summary>
        /// 内联
        /// </summary>
        /// <param name="queryBuilder">QueryBuilderParemeter对象</param>
        /// <param name="tableName">关联表名(数据库表名,非元数据实体标识)</param>
        /// <param name="tableAliasName">关联表别名</param>
        /// <param name="selfField">关联字段名(表体字段名)</param>
        /// <param name="joinField">外联目标字段名(关联主元数据实体时为ORM对象标识,否则为表体字段名)</param>
        /// <returns></returns>
        public static QueryBuilderParemeter Join(this QueryBuilderParemeter queryBuilder, string tableName, string tableAliasName, string selfField, string joinField)
        {
            ExtJoinTableDescription join = new ExtJoinTableDescription()
            {
                JoinOption = QueryBuilderParemeter.JoinOption.InnerJoin,
                TableName = tableName,
                TableNameAs = tableAliasName,
                FieldName = selfField,
                ScourceKey = joinField,
            };
            queryBuilder.ExtJoinTables.Add(join);
            return queryBuilder;
        }

        /// <summary>
        /// 左联
        /// </summary>
        /// <param name="queryBuilder">QueryBuilderParemeter对象</param>
        /// <param name="tableName">关联表名(数据库表名,非元数据实体标识)</param>
        /// <param name="tableAliasName">关联表别名</param>
        /// <param name="selfField">关联字段名(表体字段名)</param>
        /// <param name="joinField">外联目标字段名(关联主元数据实体时为ORM对象标识,否则为表体字段名)</param>
        /// <returns></returns>
        public static QueryBuilderParemeter JoinLeft(this QueryBuilderParemeter queryBuilder, string tableName, string tableAliasName, string selfField, string joinField)
        {
            ExtJoinTableDescription join = new ExtJoinTableDescription()
            {
                JoinOption = QueryBuilderParemeter.JoinOption.LeftJoin,
                TableName = tableName,
                TableNameAs = tableAliasName,
                FieldName = selfField,
                ScourceKey = joinField,
            };
            queryBuilder.ExtJoinTables.Add(join);
            return queryBuilder;
        }

        /// <summary>
        /// 设置查询字段
        /// </summary>
        /// <param name="queryBuilder">QueryBuilderParemeter对象</param>
        /// <param name="fields">查询字段集合(其中字段为元数据实体字段时为ORM对象标识,否则为【别名.表体字段名】)</param>
        /// <returns></returns>
        public static QueryBuilderParemeter SetSelectFields(this QueryBuilderParemeter queryBuilder, params string[] fields)
        {
            queryBuilder.SelectItems = SelectorItemInfo.CreateItems(fields);
            return queryBuilder;
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="queryBuilder">QueryBuilderParemeter对象</param>
        /// <param name="fields">查询条件字符串(其中字段为元数据实体字段时为ORM对象标识,否则为【别名.表体字段名】;包含参数时参数名前加@符号,并通过AddParameter方法添加参数信息)</param>
        /// <returns></returns>
        public static QueryBuilderParemeter SetFilter(this QueryBuilderParemeter queryBuilder, string where)
        {
            queryBuilder.FilterClauseWihtKey = where;
            return queryBuilder;
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="queryBuilder">QueryBuilderParemeter对象</param>
        /// <param name="fields">查询条件字符串(其中字段为元数据实体字段时为ORM对象标识,否则为【别名.表体字段名】;包含参数时参数名前加@符号,并通过AddParameter方法添加参数信息)</param>
        /// <returns></returns>
        public static QueryBuilderParemeter SetOrder(this QueryBuilderParemeter queryBuilder, string order)
        {
            queryBuilder.OrderByClauseWihtKey = order;
            return queryBuilder;
        }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="queryBuilder">QueryBuilderParemeter对象</param>
        /// <param name="name">参数名(不包含@符号)</param>
        /// <param name="value">参数值</param>
        /// <param name="valueType">参数值类型</param>
        /// <returns></returns>
        public static QueryBuilderParemeter AddParameter(this QueryBuilderParemeter queryBuilder, string name, object value, KDDbType valueType = KDDbType.String)
        {
            queryBuilder.SqlParams.Add(new SqlParam(name, valueType, value));
            return queryBuilder;
        }
    }
}


磁贴仅做抛砖引玉,更多方法小伙伴可自行扩展~~~


QueryBuilderParemeter扩展

原生QueryBuilderParemeter使用方法:QueryBuilderParemeter queryBuilder = new QueryBuilderParemeter() { FormId = "BD_Supplier" };Ext...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息