/// <summary> /// 如果没有查找出相关编号将返回空值 /// 属性类型是string /// </summary> /// <typeparam name="T">参数实体</typeparam> /// <param name="prevCode">编号前缀符</param> /// <param name="property">参数的属性名称</param> /// <param name="length">长度</param> /// <param name="dateTime">时间</param> /// <param name="codeType">Code类型,非NONE情况下以时间格式化</param> /// <returns></returns> public static string GetMaxCode<T>(string prevCode, string property, int length, DateTime dateTime, EnumCodeType codeType) where T : class { if (string.IsNullOrEmpty(property)) throw new DMSFrameException("属性名称不能为空"); var query = DMS.Create<T>(); string strWhere = prevCode; if (codeType != EnumCodeType.None) { strWhere += dateTime.ToString(codeType.ToString()); } PropertyInfo propertyInfo = typeof(T).GetProperty(property, BindingFlags.Public | BindingFlags.Instance); if (propertyInfo == null || propertyInfo.PropertyType != typeof(string)) { throw new DMSFrameException("没有找到属性或属性是不支持编号的类型"); } ParameterExpression param = Expression.Parameter(typeof(T), "q"); Expression left = Expression.Property(param, propertyInfo.Name); if (!string.IsNullOrEmpty(strWhere)) { Expression right = Expression.Constant(strWhere); Expression filter = Expression.Call(typeof(ExpressionExt), "Like", null, new Expression[] { left, right }); Expression<Func<T, bool>> where = Expression.Lambda<Func<T, bool>>(filter, param); query.Where(where); } MethodCallExpression maxExpr = Expression.Call(typeof(ExpressionExt), "Max", new Type[] { propertyInfo.PropertyType }, new Expression[] { left }); UnaryExpression unaryExpr = Expression.MakeUnary(ExpressionType.Convert, maxExpr, typeof(object)); Expression expNewExpr = Expression.NewArrayInit(typeof(object), new Expression[] { unaryExpr }); MethodCallExpression columnExpr = Expression.Call(typeof(ExpressionExt), "Columns", new Type[] { typeof(T) }, new Expression[] { param, expNewExpr }); Expression<Func<T, T>> seletor = Expression.Lambda<Func<T, T>>(columnExpr, param); var result = query.Select(seletor).ToEntity<T>(); if (result != null) { object value = propertyInfo.GetValue(result, null); string num = value.ToString().Substring(strWhere.Length); int newValue = TypeParse.StrToInt(num) + 1; string prev = "000000000000000000000" + newValue; prev = prev.Substring(prev.Length - length); return prev; } return string.Empty; } /// <summary> /// 取某个字段的最大值 /// </summary> /// <typeparam name="TEntity">实体名称</typeparam> /// <typeparam name="T">列类型</typeparam> /// <param name="property">属性名称</param> /// <returns></returns> public static T GetMaxValue<TEntity, T>(string property) where TEntity : class { if (string.IsNullOrEmpty(property)) throw new DMSFrameException("属性名称不能为空"); var query = DMS.Create<TEntity>(); ParameterExpression param = Expression.Parameter(typeof(TEntity), "q"); PropertyInfo propertyInfo = typeof(TEntity).GetProperty(property, BindingFlags.Public | BindingFlags.Instance); if (propertyInfo == null) { throw new DMSFrameException("没有找到属性或属性是不支持编号的类型"); } Expression left = Expression.Property(param, propertyInfo.Name); MethodCallExpression maxExpr = Expression.Call(typeof(ExpressionExt), "Max", new Type[] { propertyInfo.PropertyType }, new Expression[] { left }); UnaryExpression unaryExpr = Expression.MakeUnary(ExpressionType.Convert, maxExpr, typeof(object)); Expression expNewExpr = Expression.NewArrayInit(typeof(object), new Expression[] { unaryExpr }); MethodCallExpression columnExpr = Expression.Call(typeof(ExpressionExt), "Columns", new Type[] { typeof(TEntity) }, new Expression[] { param, expNewExpr }); Expression<Func<TEntity, TEntity>> seletor = Expression.Lambda<Func<TEntity, TEntity>>(columnExpr, param); var result = query.Select(seletor).ToEntity<TEntity>(); if (result != null) { object value = propertyInfo.GetValue(result, null); return (T)value; } return default(T); }