예제 #1
0
        /// <summary>
        ///     获取时间方式的表达式
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="timeType"></param>
        /// <param name="dateTime"></param>
        /// <param name="predicate"></param>
        public static Expression <Func <TEntity, bool> > GetPredicate <TEntity, TKey>(this TimeType timeType,
                                                                                      DateTime dateTime, Expression <Func <TEntity, bool> > predicate)
            where TEntity : class, IAggregateRoot <TEntity, TKey>
        {
            var query = new ExpressionQuery <TEntity>();

            if (predicate != null)
            {
                query.And(predicate);
            }

            query.OrderByAscending(e => e.Id);
            switch (timeType)
            {
            case TimeType.Day:
                query.And(r =>
                          r.CreateTime.Day == dateTime.Day && r.CreateTime.Month == dateTime.Month &&
                          r.CreateTime.Year == dateTime.Year);
                break;

            case TimeType.Month:
                query.And(r => r.CreateTime.Month == dateTime.Month && r.CreateTime.Year == dateTime.Year);
                break;

            case TimeType.Year:
                query.And(r => r.CreateTime.Year == dateTime.Year);
                break;
            }

            var result = query.BuildExpression();

            return(result);
        }
예제 #2
0
        public List <ArticleItem> GetArticleList(ArticleInput articleInput)
        {
            var query = new ExpressionQuery <Article>();

            query.OrderByAscending(e => e.CreateTime);
            if (!articleInput.ChannelId.IsNullOrEmpty())
            {
                query.And(r => r.ChannelId == articleInput.ChannelId.ToObjectId());
            }

            if (articleInput.OrderType == 0)
            {
                query.OrderByAscending(a => a.ViewCount);
            }
            else
            {
                query.OrderByDescending(a => a.ViewCount);
            }

            var list   = GetList(query);
            var result = new List <ArticleItem>();

            foreach (var item in list)
            {
                if (articleInput.TotalCount > 0 && result.Count >= articleInput.TotalCount)
                {
                    break;
                }

                var articleItem = AutoMapping.SetValue <ArticleItem>(item);
                articleItem.RelationId = item.RelationId;
                result.Add(articleItem);
            }

            return(result);
        }
예제 #3
0
        /// <summary>
        ///     获取单条记录
        /// </summary>
        /// <param name="guid"></param>
        public Category GetSingle(Guid guid)
        {
            var category = GetSingle(r => r.Id == guid);

            if (category != null)
            {
                //读取所有类目属性
                var propertys = Resolve <ICategoryPropertyService>().GetList(r => r.CategoryId == category.Id)
                                ?.OrderBy(r => r.SortOrder).ToList();
                var propertyIds = propertys.Select(r => r.Id).ToList();

                // 读取所有类目属性值
                var query = new ExpressionQuery <CategoryPropertyValue>();
                query.And(r => propertyIds.Contains(r.PropertyId));
                query.OrderByAscending(e => e.SortOrder);
                var propertyValues = Resolve <ICategoryPropertyValueService>().GetList(query).ToList();

                // 规格,销售用
                var salePropertys = propertys.Where(r => r.IsSale).ToList();
                category.SalePropertys = new List <CategoryProperty>();
                foreach (var item in salePropertys)
                {
                    item.PropertyValues = propertyValues.Where(r => r.PropertyId == item.Id).ToList();
                    category.SalePropertys.Add(item);
                }

                // 商品参数,显示用
                var displayPropertys = propertys.Where(r => r.IsSale == false).ToList(); //规格,销售用
                category.DisplayPropertys = new List <CategoryProperty>();
                foreach (var item in displayPropertys)
                {
                    item.PropertyValues = propertyValues.Where(r => r.PropertyId == item.Id).ToList();
                    category.DisplayPropertys.Add(item);
                }
            }

            return(category);
        }
예제 #4
0
        public PagedList <TEntity> GetPagedList(object paramater, Type searchView,
                                                Expression <Func <TEntity, bool> > predicate = null)
        {
            var query = new ExpressionQuery <TEntity> {
                EnablePaging = true
            };

            #region 构建查询页数

            var dictionary = paramater.DeserializeJson <Dictionary <string, string> >();
            if (dictionary != null)
            {
                var pagedInput = AutoMapping.SetValue <PagedInputDto>(dictionary);
                if (pagedInput != null)
                {
                    query.PageIndex = (int)pagedInput.PageIndex;
                    query.PageSize  = (int)pagedInput.PageSize;
                }
            }

            #endregion 构建查询页数

            if (predicate != null)
            {
                query.And(predicate);
            }

            var result           = HanderDictionary(ref dictionary); // 获取搜索范围相关的字段
            var rangList         = result.Item1;
            var interpreter      = new Interpreter();
            var classDescription = new ClassDescription(searchView);
            var propertyResults  = searchView.GetPropertyResultFromCache();
            var propertyInfos    = propertyResults.Select(r => r.PropertyInfo);

            #region 普通字段搜索

            foreach (var item in dictionary)
            {
                var name  = item.Key.Replace("_Start", "").Replace("_End", "");
                var value = item.Value.SafeString();
                if (value.IsNullOrEmpty() || name.IsNullOrEmpty())
                {
                    continue;
                }

                if (name.Equals("UserName", StringComparison.OrdinalIgnoreCase))
                {
                    #region 搜索用户名

                    var property =
                        propertyInfos.FirstOrDefault(r => r.Name.Equals("UserId", StringComparison.OrdinalIgnoreCase));
                    if (property == null)
                    {
                        continue;
                    }

                    var user = EntityDynamicService.GetSingleUser(value); // 动态获取用户名
                    if (user != null)
                    {
                        var expression = Lambda.Equal <TEntity>(property.Name, user.Id);
                        query.And(expression);
                    }

                    #endregion 搜索用户名
                }
                else if (name.Equals("Serial", StringComparison.OrdinalIgnoreCase))
                {
                    #region 搜索序号

                    // 序号处理,适合序号是通过Id生成的方式,比如Bill,Order,Reard表
                    var property =
                        propertyInfos.FirstOrDefault(r => r.Name.Equals("Id", StringComparison.OrdinalIgnoreCase));
                    if (property == null)
                    {
                        continue;
                    }

                    if (value.Length > 7 && value.StartsWith("9"))
                    {
                        value = value.Substring(1, value.Length - 1);
                    }
                    // 去掉前面的0
                    var reg = new Regex(@"^0+");
                    value = reg.Replace(value, "");
                    var idValue = value.ConvertToLong(0);
                    if (idValue > 0)
                    {
                        var expression = Lambda.Equal <TEntity>(property.Name, idValue);
                        query.And(expression);
                    }

                    #endregion 搜索序号
                }
                else
                {
                    var property =
                        propertyInfos.FirstOrDefault(r => r.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

                    if (property == null)
                    {
                        continue;
                    }

                    if (property.PropertyType.IsEnum)
                    {
                        if (value == "-1")
                        {
                            continue;
                        }
                    }

                    var fieldAttribute = propertyResults.FirstOrDefault(r => r.PropertyInfo.Name == property.Name)
                                         .FieldAttribute;
                    var valueList = value.ToSplitList();
                    foreach (var itemValue in valueList)
                    {
                        if (!itemValue.IsNullOrEmpty())
                        {
                            Expression <Func <TEntity, bool> > expression = null;
                            if (fieldAttribute != null)
                            {
                                if (fieldAttribute.Operator == Operator.Contains)
                                {
                                    expression = Lambda.Contains <TEntity>(property.Name, itemValue);
                                }

                                if (fieldAttribute.Operator == Operator.Equal)
                                {
                                    expression = Lambda.Equal <TEntity>(property.Name, itemValue);
                                }
                            }
                            // 字段中包含name和title,一般都可以模糊搜索
                            else if (property.Name.Contains("name", StringComparison.OrdinalIgnoreCase) ||
                                     property.Name.Contains("intro", StringComparison.OrdinalIgnoreCase) ||
                                     property.Name.Contains("desc", StringComparison.OrdinalIgnoreCase) ||
                                     property.Name.Contains("remark", StringComparison.OrdinalIgnoreCase) ||
                                     property.Name.Contains("title", StringComparison.OrdinalIgnoreCase))
                            {
                                expression = Lambda.Contains <TEntity>(property.Name, itemValue);
                            }
                            else
                            {
                                expression = Lambda.Equal <TEntity>(property.Name, itemValue);
                            }

                            if (expression != null)
                            {
                                query.And(expression);
                            }
                        }
                    }
                }
            }

            #endregion 普通字段搜索

            #region 搜索范围字段搜索

            foreach (var item in rangList)
            {
                var property =
                    propertyInfos.FirstOrDefault(r => r.Name.Equals(item.Name, StringComparison.OrdinalIgnoreCase));
                if (property == null || item.StartValue.IsNullOrEmpty() && item.EndValue.IsNullOrEmpty())
                {
                    continue;
                }
                // 大于等于
                if (!item.StartValue.IsNullOrEmpty())
                {
                    var expression = Lambda.GreaterEqual <TEntity>(property.Name, item.StartValue);
                    if (expression != null)
                    {
                        query.And(expression);
                    }
                }

                // 小于等于
                if (!item.EndValue.IsNullOrEmpty())
                {
                    var expression = Lambda.LessEqual <TEntity>(property.Name, item.EndValue);
                    if (expression != null)
                    {
                        query.And(expression);
                    }
                }
            }

            #endregion 搜索范围字段搜索

            #region 排序

            var sortType     = result.Item2;
            var propertySort =
                propertyInfos.FirstOrDefault(r => r.Name.Equals(sortType.Name, StringComparison.OrdinalIgnoreCase));
            if (propertySort != null)
            {
                var selectExpression = $"entity.{propertySort.Name}";
                var expression       = interpreter.ParseAsExpression <Func <TEntity, object> >(selectExpression, "entity");
                if (sortType.OrderType == OrderType.Descending)
                {
                    query.OrderByDescending(expression);
                }
                else
                {
                    query.OrderByAscending(expression);
                }
            }

            #endregion 排序

            var pageList = Store.GetPagedList(query);
            pageList = PagedListStyle(pageList);

            return(pageList);
        }