Exemplo n.º 1
0
        /// <summary>
        /// 根据实体创建查询条件
        /// </summary>
        /// <param name="query"></param>
        /// <param name="command"></param>
        private void CreateQueryProductCommand(object query, DataCommand command)
        {
            if (query == null || command == null)
            {
                return;
            }
            var targetProArray = query.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            if (targetProArray.Length <= 0)
            {
                return;
            }
            foreach (var pro in targetProArray)
            {
                var tempType = pro.PropertyType;
                var value    = pro.GetValue(query, null);
                if (!tempType.IsGenericType && tempType.IsClass &&
                    !tempType.Name.ToUpper().Contains("STRING") &&
                    !tempType.Name.ToUpper().Contains("PAGINGINFO")
                    )
                {
                    if (value != null)
                    {
                        CreateQueryProductCommand(value, command);
                    }
                }
                else if (tempType.IsGenericType && tempType.Name.ToLower().Contains("comparison") &&
                         tempType.GetGenericArguments().Length == 2)
                {
                    var targe = GetGenericType(null, tempType.GetGenericArguments()[0]);

                    var otherTarge = tempType.GetGenericArguments()[1];
                    if (targe.Name.ToLower().Contains("decimal"))
                    {
                        var tempValue = (QueryFilter.IM.Comparison <decimal?, OperatorType>)value;
                        CreateComparisonCommand <decimal?, OperatorType>(tempValue, pro.Name, command);
                    }
                    else if (targe.Name.ToLower().Contains("int") &&
                             otherTarge.Name.ToLower().Contains("operatortype"))
                    {
                        var tempValue = (QueryFilter.IM.Comparison <int?, OperatorType>)value;
                        CreateComparisonCommand <int?, OperatorType>(tempValue, pro.Name, command);
                    }
                    else if (targe.Name.ToLower().Contains("int") &&
                             otherTarge.Name.ToLower().Contains("comparison"))
                    {
                        var tempValue = (QueryFilter.IM.Comparison <int?, Comparison>)value;
                        CreateComparisonCommand <int?, Comparison>(tempValue, pro.Name, command);
                    }
                }
                else if (tempType.IsGenericType &&
                         tempType.GetGenericTypeDefinition() == typeof(Nullable <>) &&
                         tempType.GetGenericArguments().Length == 1)
                {
                    var baseType = GetGenericType(value, tempType);
                    if (baseType.IsEnum)
                    {
                        if (value == null)
                        {
                            if (EnumCodeMapper.IsExistMap(baseType))
                            {
                                CreateCommand(EmptyValueStr, pro.Name, command);
                            }
                            else
                            {
                                CreateCommand(EmptyValue, pro.Name, command);
                            }
                        }
                        else
                        {
                            if (EnumCodeMapper.IsExistMap(baseType))
                            {
                                var code      = Convert.ToInt32(value);
                                var codeValue = (char)code;
                                CreateCommand(Convert.ToString(codeValue), pro.Name, command);
                            }
                            else
                            {
                                CreateCommand(Convert.ToInt32(value), pro.Name, command);
                            }
                        }
                    }
                    else if (baseType.Name.ToLower().Contains("datetime"))
                    {
                        var tempValue = value == null?DateTime.Parse(MinTime) : DateTime.Parse(value.ToString());

                        CreateCommand(tempValue, pro.Name, command);
                    }
                    else if (baseType.IsValueType)
                    {
                        var tempValue = value == null ? EmptyValue : Convert.ToInt32(value);
                        CreateCommand(tempValue, pro.Name, command);
                    }
                }
                else if (tempType.IsValueType || tempType.Name.ToLower().Contains("string"))
                {
                    if (tempType.Name.ToLower().Contains("string") && value == null)
                    {
                        CreateCommand("", pro.Name, command);
                    }
                    else
                    {
                        CreateCommand(value, pro.Name, command);
                    }
                }
            }
        }