/// <summary>
        /// 验证内容
        /// </summary>
        public override void ValidationValue()
        {
            //调用基类实体验证
            base.ValidationValue();

            //动态列验证
            DynamicColumn <TEntity> .ValidationValue(ExcelGlobalDTO);

            //验证值后
            this.ValidationValueAfter();
        }
Beispiel #2
0
        /// <summary>
        /// 验证内容
        /// </summary>
        public virtual void ValidationValue()
        {
            //遍历Sheet实体集合
            foreach (var sheet in ExcelGlobalDTO.Sheets)
            {
                //获取Sheet头部实体集合
                var headDtoList = sheet.SheetHeadList;

                //为空判断
                if (sheet.SheetEntityList == null)
                {
                    continue;
                }

                var pairs           = new ConcurrentDictionary <int, List <ValidationResult> >();
                var parallelOptions = MultiThreadingHelper.GetParallelOptions();
                Parallel.ForEach(sheet.SheetEntityList, parallelOptions, entity =>
                {
                    pairs.TryAdd(entity.RowNumber, ValidationHelper.Exec <TEntity>(entity));
                });

                foreach (var item in sheet.SheetEntityList)
                {
                    #region  项值校验
                    //校验文本框的值是否在下拉框选项中
                    foreach (var headDto in headDtoList)
                    {
                        #region 基础判断
                        //判断是否为选项列
                        if (headDto.ColumnType != Attribute.Enum.ColumnTypeEnum.Option)
                        {
                            continue;
                        }
                        //判断选项是否存在
                        if (sheet.ColumnOptions == null)
                        {
                            continue;
                        }

                        //判断属性是否存在
                        if (string.IsNullOrEmpty(headDto.PropertyName))
                        {
                            continue;
                        }
                        #endregion

                        //其他列的键值
                        string key = null;

                        #region 获其他列的键值
                        if (sheet.ColumnOptions.Keys.Contains(headDto.HeadName))
                        {
                            key = headDto.HeadName;
                        }
                        if (sheet.ColumnOptions.Keys.Contains(headDto.PropertyName))
                        {
                            key = headDto.PropertyName;
                        }
                        //判断键是否为空
                        if (key == null)
                        {
                            continue;
                        }
                        #endregion

                        //变量设置
                        PropertyInfo propertyInfo = item.GetType().GetProperty(headDto.PropertyName);
                        string       value        = propertyInfo.GetValue(item).ToString();

                        #region 校验
                        //类型判断decimal
                        if (propertyInfo.PropertyType == typeof(decimal) || propertyInfo.PropertyType == typeof(decimal?))
                        {
                            List <decimal> options = sheet.ColumnOptions[key].Select(n => Convert.ToDecimal(n)).ToList();
                            if (options.Contains(Convert.ToDecimal(value)) == true)
                            {
                                continue;
                            }
                        }

                        //类型判断double
                        if (propertyInfo.PropertyType == typeof(double) || propertyInfo.PropertyType == typeof(double?))
                        {
                            List <double> options = sheet.ColumnOptions[key].Select(n => Convert.ToDouble(n)).ToList();
                            if (options.Contains(Convert.ToDouble(value)) == true)
                            {
                                continue;
                            }
                        }

                        //判断输入的值是否在选项值中
                        if (sheet.ColumnOptions[key].Contains(value) == true)
                        {
                            continue;
                        }

                        #endregion

                        #region  通过则提示异常
                        //异常信息
                        ColumnErrorMessage errorMsg = new ColumnErrorMessage
                        {
                            PropertyName = headDto.PropertyName,
                            ColumnName   = headDto.HeadName,
                            ErrorMessage = ExcelGlobalDTO.ExcelValidationMessage.Clgyl_Common_Import_NotExistOptions
                        };
                        item.ColumnErrorMessage.Add(errorMsg);
                        #endregion
                    }
                    #endregion

                    #region  实体特性验证
                    List <ValidationResult> result = pairs[item.RowNumber];
                    if (result == null)
                    {
                        continue;
                    }
                    foreach (var msg in result)
                    {
                        //异常信息
                        ColumnErrorMessage errorMsg = new ColumnErrorMessage
                        {
                            PropertyName = msg.PropertyName,
                            ErrorMessage = msg.ErrorMessage
                        };

                        //设置列信息
                        var headDto = headDtoList.Where(w => w.PropertyName == msg.PropertyName).FirstOrDefault();
                        if (headDto != null)
                        {
                            errorMsg.ColumnName = headDto.HeadName;
                        }

                        //添加至集合
                        item.ColumnErrorMessage.Add(errorMsg);
                    }
                    #endregion
                }
            }

            //动态列验证
            DynamicColumn <TEntity> .ValidationValue(ExcelGlobalDTO, ValidationModelEnum.DynamicColumn);

            //验证值后
            this.ValidationValueAfter();
        }