using iWareModel; using System; using System.Collections.Generic; using System.Reflection; namespace iWareCommon.Utils { /// /// 验证数据通用类 /// public static class ValidateDataHelper { /// /// 验证是否为空 /// /// /// /// /// public static ValidateResModel ValidateNullOrEmpty(List columnsList, T model) { ValidateResModel res = new ValidateResModel() { IsValidate = true };//默认验证通过 ColumnsModel columnsModel = null; PropertyInfo property = null;//属性对象 Object property_value = null;//属性值 string str = ""; for (int j = 0; j < columnsList.Count; j++) { columnsModel = columnsList[j]; property = typeof(T).GetProperty(columnsModel.PropertyName.Trim()); if (property == null) throw new Exception("列名'" + columnsModel.PropertyName + "'不存在类中"); property_value = property.GetValue(model, null); if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?)) { try { Convert.ToDateTime(property_value); } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "时间格式不正确"; break; } } else if (property.PropertyType == typeof(string)) { str = property_value == null ? "" : Convert.ToString(property_value); if (!columnsModel.IsNullable) {//必输 if (string.IsNullOrEmpty(str.Trim())) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "不能为空"; break; } } #region 验证日期类型的字符串格式(因为json这边接收的日期类型都是字符串) if (columnsModel.DataType == typeof(DateTime) && !string.IsNullOrEmpty(str)) {//如果是日期类型,并且传入的值不为空,则需要验证日期格式是否正确 try { Convert.ToDateTime(property_value); } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "时间格式不正确"; break; } } #endregion #region 验证格林威治日期类型的字符串格式(因为json这边接收的日期类型都是字符串) /* if (columnsModel.DataType == typeof(GreenwichTimeStamp) && !string.IsNullOrEmpty(str)) {//如果是日期类型,并且传入的值不为空,则需要验证日期格式是否正确 try { DateTime? dt = WIPCommon.ForamtGreenwichTimeStampToDateTime(str); if (dt == null) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "格林威治时间格式不正确"; } } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "格林威治时间格式不正确"; break; } } //*/ #endregion } else if (property.PropertyType == typeof(int) || property.PropertyType == typeof(int?)) { try { int int_temp = 0; var str_temp = property_value == null ? "" : Convert.ToString(property_value); if (!int.TryParse(str_temp, out int_temp)) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "不是数字类型"; break; } if (int_temp == 0) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "不能为0"; break; } switch (columnsModel.ValidateIntEnum) { case ValidateIntEnum.必须大于0: if (int_temp <= 0) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "必须大于0"; break; } break; case ValidateIntEnum.必须大于等于0: if (int_temp < 0) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "必须大于等于0"; break; } break; default: break; } } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "int格式不正确"; break; } } else if (property.PropertyType == typeof(float) || property.PropertyType == typeof(float?)) { try { float int_temp = 0; var str_temp = property_value == null ? "" : Convert.ToString(property_value); if (!float.TryParse(str_temp, out int_temp)) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "不是数字类型"; break; } switch (columnsModel.ValidateIntEnum) { case ValidateIntEnum.必须大于0: if (int_temp <= 0) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "必须大于0"; break; } break; case ValidateIntEnum.必须大于等于0: if (int_temp < 0) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "必须大于等于0"; break; } break; default: break; } } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "float格式不正确"; break; } } /* else if (property.PropertyType == typeof(long)) { if (columnsModel.DataType == typeof(UnixTimeStamp)) {//验证long类型的unix时间戳 try { long timestamp = Convert.ToInt64(property_value); if (timestamp == 0) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "unix时间戳不能为0"; break; } } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "unix时间戳格式不正确"; break; } } } //*/ else if (property.PropertyType == typeof(decimal) || property.PropertyType == typeof(decimal?)) { } } return res; } /// /// 验证是否为空 /// /// /// /// /// public static ValidateResModel ValidateNullOrEmpty(List columnsList) { ValidateResModel res = new ValidateResModel() { IsValidate = true };//默认验证通过 ValidateModel columnsModel = null; Object property_value = null;//属性值 string str = ""; for (int j = 0; j < columnsList.Count; j++) { columnsModel = columnsList[j]; property_value = columnsModel.DataValue; #region 验证日期类型的字符串格式(因为json这边接收的日期类型都是字符串) if (columnsModel.DataType == typeof(DateTime)) {//如果是日期类型,并且传入的值不为空,则需要验证日期格式是否正确 str = property_value == null ? "" : Convert.ToString(property_value); try { if (!columnsModel.IsNullable) {//必输 if (string.IsNullOrEmpty(str.Trim())) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "不能为空"; break; } } if (!string.IsNullOrEmpty(str.Trim())) { Convert.ToDateTime(property_value); } } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "时间格式不正确"; break; } } #endregion #region 验证格林威治日期类型的字符串格式(因为json这边接收的日期类型都是字符串) /* else if (columnsModel.DataType == typeof(GreenwichTimeStamp)) {//如果是日期类型,并且传入的值不为空,则需要验证日期格式是否正确 str = property_value == null ? "" : Convert.ToString(property_value); try { if (!columnsModel.IsNullable) {//必输 if (string.IsNullOrEmpty(str.Trim())) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "不能为空"; break; } } if (!string.IsNullOrEmpty(str.Trim())) { DateTime? dt = WIPCommon.ForamtGreenwichTimeStampToDateTime(str); if (dt == null) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "格林威治时间格式不正确"; } } } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "格林威治时间格式不正确"; break; } } //*/ #endregion else if (columnsModel.DataType == typeof(string)) { str = property_value == null ? "" : Convert.ToString(property_value); if (!columnsModel.IsNullable) {//必输 if (string.IsNullOrEmpty(str.Trim())) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "不能为空"; break; } } } else if (columnsModel.DataType == typeof(int)) { try { int timestamp = Convert.ToInt32(property_value); if (timestamp == 0) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "不能为0"; break; } } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "int格式不正确"; break; } } /* else if (columnsModel.DataType == typeof(UnixTimeStamp)) {//验证long类型的unix时间戳 try { long timestamp = Convert.ToInt64(property_value); if (timestamp == 0) { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "unix时间戳不能为0"; break; } } catch { res.IsValidate = false; res.ValidateMsg = columnsModel.ChinaName + "unix时间戳格式不正确"; break; } } //*/ else if (columnsModel.DataType == typeof(decimal) || columnsModel.DataType == typeof(decimal?)) { } } return res; } } }