Exemplo n.º 1
0
        public object GetValue(SqlConnection conn, Dictionary<string, object> fieldValuesOfCurrentRow)
        {
            if (this == null)
            {
                return null;
            }
            Random rd = new Random();
            object value = "";
            switch (this.ValueCategroy)
            {
                case JValueCategroy.List:

                    #region List不管是DateTime、Numeric、String处理方式都一样

                    object[] values = this.Values.ToArray();
                    int listCount = this.Values.Count();
                    int randNum = rd.Next(0, listCount);
                    value = values[randNum];
                    break;

                    #endregion

                case JValueCategroy.Range:

                    #region Range

                    switch (this.ValueType)
                    {
                        case JValueType.DateTime:

                            #region Range

                            DateTime minDate = DateTime.Parse(this.MinValue.ToJString());
                            DateTime maxDate = DateTime.Parse(this.MaxValue.ToJString());
                            int minutesDiff = (int)(maxDate - minDate).TotalMinutes;
                            int randMinutes = rd.Next(0, minutesDiff);
                            value = minDate.AddMinutes(randMinutes);

                            break;

                            #endregion

                        case JValueType.Numeric:

                            #region Range

                            decimal maxSeed = decimal.Parse(this.MaxValue.ToJString());
                            decimal minSeed = decimal.Parse(this.MinValue.ToJString());
                            if (maxSeed <= 1)
                            {
                                maxSeed = maxSeed * 10000;
                                minSeed = minSeed * 10000;
                                value = (decimal)rd.Next((int)minSeed, (int)maxSeed) / 10000;
                            }
                            else
                            {
                                value = rd.Next((int)minSeed, (int)maxSeed);
                            }

                            break;

                            #endregion

                        case JValueType.String:

                            value = String.Format(this.Format, rd.Next((Int32)this.MinValue, (Int32)this.MaxValue));

                            break;

                    }
                    break;

                    #endregion

                case JValueCategroy.Sequence:

                    #region Sequence

                    switch (this.ValueType)
                    {
                        case JValueType.DateTime:
                            #region Sequence

                            throw new FieldValueTypeNotSupportValueCategroyException(this.FieldName, this.ValueType, this.ValueCategroy);

                            #endregion

                        case JValueType.Numeric:
                            #region Sequence

                            value = this.MinValue;
                            this.MinValue = int.Parse(this.MinValue.ToJString("0")) + int.Parse(this.Step.ToJString("0"));
                            break;

                            #endregion

                        case JValueType.String:

                            value = String.Format(this.Format, this.MinValue);
                            this.MinValue = int.Parse(this.MinValue.ToJString("0")) + int.Parse(this.Step.ToJString("0"));
                            break;

                    }
                    break;

                    #endregion

                case JValueCategroy.FromTable:

                    #region FromTable

                    string refTableName = this.ReferenceTableName;
                    string refColumnName = this.ReferenceColumnName;
                    List<string> parameters = new List<string>();
                    string filterFormat = this.RefFilter.ToParameters(parameters);
                    List<object> parameterValues = new List<object>();
                    foreach (var item in parameters)
                    {
                        parameterValues.Add(fieldValuesOfCurrentRow[item]);
                    }
                    string refFilter = string.Format(filterFormat, parameterValues.ToArray());

                    SourceFieldData tempData = new SourceFieldData()
                        {
                            fieldName = refColumnName,
                            TableName = refTableName,
                            Values = CommonDAL.GetValues(conn, refTableName, refColumnName, refFilter).ToArray()
                        };

                    value = tempData.Values[rd.Next(0, tempData.Values.Count())];
                    break;

                    #endregion

                case JValueCategroy.OtherField:

                    #region OtherField 数据准备

                    if (!fieldValuesOfCurrentRow.ContainsKey(this.OtherFiledName))
                    {
                        throw new FieldValueTypeNotSupportValueCategroyException(this.FieldName, this.ValueType, this.ValueCategroy);
                    }
                    value = fieldValuesOfCurrentRow[this.OtherFiledName];

                    #endregion

                    break;
            }

            return value;
        }
Exemplo n.º 2
0
        public object GetValue(SqlConnection conn, Dictionary <string, object> fieldValuesOfCurrentRow)
        {
            if (this == null)
            {
                return(null);
            }
            Random rd    = new Random();
            object value = "";

            switch (this.ValueCategroy)
            {
            case JValueCategroy.List:

                #region List不管是DateTime、Numeric、String处理方式都一样

                object[] values    = this.Values.ToArray();
                int      listCount = this.Values.Count();
                int      randNum   = rd.Next(0, listCount);
                value = values[randNum];
                break;

                #endregion

            case JValueCategroy.Range:

                #region Range

                switch (this.ValueType)
                {
                case JValueType.DateTime:

                    #region Range

                    DateTime minDate     = DateTime.Parse(this.MinValue.ToJString());
                    DateTime maxDate     = DateTime.Parse(this.MaxValue.ToJString());
                    int      minutesDiff = (int)(maxDate - minDate).TotalMinutes;
                    int      randMinutes = rd.Next(0, minutesDiff);
                    value = minDate.AddMinutes(randMinutes);

                    break;

                    #endregion

                case JValueType.Numeric:

                    #region Range

                    decimal maxSeed = decimal.Parse(this.MaxValue.ToJString());
                    decimal minSeed = decimal.Parse(this.MinValue.ToJString());
                    if (maxSeed <= 1)
                    {
                        maxSeed = maxSeed * 10000;
                        minSeed = minSeed * 10000;
                        value   = (decimal)rd.Next((int)minSeed, (int)maxSeed) / 10000;
                    }
                    else
                    {
                        value = rd.Next((int)minSeed, (int)maxSeed);
                    }

                    break;

                    #endregion

                case JValueType.String:

                    value = String.Format(this.Format, rd.Next((Int32)this.MinValue, (Int32)this.MaxValue));

                    break;
                }
                break;

                #endregion

            case JValueCategroy.Sequence:

                #region Sequence

                switch (this.ValueType)
                {
                case JValueType.DateTime:
                    #region Sequence

                    throw new FieldValueTypeNotSupportValueCategroyException(this.FieldName, this.ValueType, this.ValueCategroy);

                    #endregion

                case JValueType.Numeric:
                    #region Sequence

                    value         = this.MinValue;
                    this.MinValue = int.Parse(this.MinValue.ToJString("0")) + int.Parse(this.Step.ToJString("0"));
                    break;

                    #endregion

                case JValueType.String:

                    value         = String.Format(this.Format, this.MinValue);
                    this.MinValue = int.Parse(this.MinValue.ToJString("0")) + int.Parse(this.Step.ToJString("0"));
                    break;
                }
                break;

                #endregion

            case JValueCategroy.FromTable:

                #region FromTable

                string        refTableName    = this.ReferenceTableName;
                string        refColumnName   = this.ReferenceColumnName;
                List <string> parameters      = new List <string>();
                string        filterFormat    = this.RefFilter.ToParameters(parameters);
                List <object> parameterValues = new List <object>();
                foreach (var item in parameters)
                {
                    parameterValues.Add(fieldValuesOfCurrentRow[item]);
                }
                string refFilter = string.Format(filterFormat, parameterValues.ToArray());

                SourceFieldData tempData = new SourceFieldData()
                {
                    fieldName = refColumnName,
                    TableName = refTableName,
                    Values    = CommonDAL.GetValues(conn, refTableName, refColumnName, refFilter).ToArray()
                };

                value = tempData.Values[rd.Next(0, tempData.Values.Count())];
                break;

                #endregion

            case JValueCategroy.OtherField:

                #region OtherField 数据准备

                if (!fieldValuesOfCurrentRow.ContainsKey(this.OtherFiledName))
                {
                    throw new FieldValueTypeNotSupportValueCategroyException(this.FieldName, this.ValueType, this.ValueCategroy);
                }
                value = fieldValuesOfCurrentRow[this.OtherFiledName];

                #endregion

                break;
            }

            return(value);
        }