Beispiel #1
0
        /// <summary>
        /// Gets the conditions via the attribute of the modeltype and the data from page.
        /// </summary>
        /// <param name="modelType">the model type.</param>
        /// <param name="dataAdapter">the data from page.</param>
        /// <returns>the condition.</returns>
        protected virtual SqlCriteria GetQueryCondition(Type modelType, BeeDataAdapter dataAdapter)
        {
            IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxyFromType(modelType);
            SqlCriteria  result      = new SqlCriteria();

            dataAdapter = new BeeDataAdapter(dataAdapter);
            dataAdapter.RemoveEmptyOrNull();

            ModelAttribute modelAttribute = entityProxy.GetCustomerAttribute <ModelAttribute>();

            foreach (PropertySchema propertySchema in entityProxy.GetPropertyList())
            {
                string propertyName = propertySchema.Name;
                ModelPropertyAttribute modelPropertyAttribute
                    = propertySchema.GetCustomerAttribute <ModelPropertyAttribute>();
                if (modelPropertyAttribute != null)
                {
                    if (dataAdapter.ContainsKey(propertyName))
                    {
                        if (modelPropertyAttribute.Queryable)
                        {
                            object conditionValue = dataAdapter[propertyName];

                            if (propertySchema.PropertyType == typeof(string))
                            {
                                conditionValue = HttpUtility.HtmlDecode(conditionValue.ToString());
                            }

                            if (modelPropertyAttribute.QueryType == ModelQueryType.Equal &&
                                propertySchema.PropertyType == typeof(bool))
                            {
                                bool value = false;
                                bool.TryParse(conditionValue.ToString(), out value);
                                if (value)
                                {
                                    conditionValue = 1;
                                }
                                else
                                {
                                    conditionValue = 0;
                                }
                            }

                            if (propertySchema.PropertyType == typeof(DateTime))
                            {
                                DateTime propertyValue = dataAdapter.TryGetValue <DateTime>(propertyName, DateTime.MinValue);
                                if (propertyValue != DateTime.MinValue)
                                {
                                    conditionValue = propertyValue;
                                }
                                else
                                {
                                    // 若是时间,而又没有赋值, 则略过
                                    continue;
                                }
                            }

                            AddCondition(result, modelPropertyAttribute.QueryType, propertyName, conditionValue);
                        }
                    }
                    else
                    {
                        if (modelPropertyAttribute.Queryable &&
                            modelPropertyAttribute.QueryType == ModelQueryType.Between)
                        {
                            if (dataAdapter.ContainsKey(propertyName + "begin"))
                            {
                                if (propertySchema.PropertyType == typeof(DateTime))
                                {
                                    DateTime beginTime = dataAdapter.TryGetValue <DateTime>(propertyName + "begin", DateTime.MinValue);
                                    result.GreaterThanOrEqual(propertyName, beginTime);
                                }
                                else
                                {
                                    result.GreaterThanOrEqual(propertyName, dataAdapter[propertyName + "begin"]);
                                }
                            }

                            if (dataAdapter.ContainsKey(propertyName + "end"))
                            {
                                DateTime endTime = dataAdapter.TryGetValue <DateTime>(propertyName + "end", DateTime.MinValue);
                                if (propertySchema.PropertyType == typeof(DateTime))
                                {
                                    if (endTime == endTime.Date)
                                    {
                                        endTime = endTime.AddDays(1);
                                    }

                                    //result.LessThan(propertyName, endTime.ToString("yyyy-MM-dd"));
                                    result.LessThan(propertyName, endTime);
                                }
                                else
                                {
                                    result.LessThanOrEqual(propertyName, dataAdapter[propertyName + "end"]);
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }