using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;
using WalkingTec.Mvvm.Core.Extensions;
namespace WalkingTec.Mvvm.Core
{
///
/// 列表列的定义
///
/// 列表的数据源类
public class GridColumn : IGridColumn where T : TopBasePoco
{
public bool? ShowTotal { get; set; }
public GridColumn(Expression> columnExp, int? width)
{
ColumnExp = columnExp;
Width = width;
}
///
/// 表头类型
///
public GridColumnTypeEnum ColumnType { get; set; }
private string _field;
///
/// 设定字段名
///
public string Field
{
get
{
if (_field == null)
{
_field = PI?.Name;
if (_field == null)
{
_field = CompiledCol?.Invoke(null).ToString();
}
}
return _field;
}
set
{
_field = value;
}
}
private string _title;
///
/// 标题名称
///
public string Title
{
get
{
if (_title == null && PI != null)
{
_title = PropertyHelper.GetPropertyDisplayName(PI) ?? string.Empty;
}
return _title;
}
set
{
_title = value;
}
}
///
/// 列宽
///
public int? Width { get; set; }
///
/// //监听单元格事件
///
public string Event { get; set; }
///
/// 是否允许排序
///
public bool? Sort { get; set; }
///
/// 是否固定列
///
public GridColumnFixedEnum? Fixed { get; set; }
///
/// 对齐方式
///
public GridColumnAlignEnum Align { get; set; }
///
/// 是否可改变列宽
///
public bool? UnResize { get; set; }
///
/// 隐藏列
///
public bool? Hide { get; set; }
///
/// 是否禁止导出此列
///
public bool DisableExport { get; set; }
///
/// 子列
///
public IEnumerable> Children { get; set; }
private int? _childrenLen;
///
/// 底层子列数量
///
public int ChildrenLength
{
get
{
if (_childrenLen == null)
{
var len = 0;
if (Children != null && Children.Any())
{
len += Children.Where(x => x.Children == null || !x.Children.Any()).Count();
var tempChildren = Children.Where(x => x.Children != null && x.Children.Any()).ToList();
foreach (var item in tempChildren)
{
len += item.ChildrenLength;
}
}
_childrenLen = len;
}
return _childrenLen.Value;
}
}
public EditTypeEnum? EditType { get; set; }
public List ListItems { get; set; }
#region 只读属性 生成 Excel 及其 表头用
///
/// 递归获取子列数量最大层的子列数量
///
public int MaxChildrenCount
{
get
{
int rv = 1;
if (this.Children != null && Children.Any())
{
rv = 0;
foreach (var child in this.Children)
{
rv += child.MaxChildrenCount;
}
}
return rv;
}
}
///
/// 获取最大层数
///
public int MaxLevel
{
get
{
int rv = 1;
if (this.Children != null && Children.Any())
{
int max = 0;
foreach (var child in this.Children)
{
int temp = child.MaxLevel;
if (max < temp)
{
max = temp;
}
}
rv += max;
}
return rv;
}
}
#endregion
private PropertyInfo _pi;
protected PropertyInfo PI
{
get
{
if (_pi == null && ColumnExp != null)
{
_pi = PropertyHelper.GetPropertyInfo(ColumnExp);
}
return _pi;
}
}
///
/// ColumnExp
///
public Expression> ColumnExp { get; set; }
private int? _maxDepth;
///
/// 最大深度
///
public int MaxDepth
{
get
{
if (_maxDepth == null)
{
_maxDepth = 1;
if (Children?.Count() > 0)
{
_maxDepth += Children.Max(x => x.MaxDepth);
}
}
return _maxDepth.Value;
}
}
#region 暂时没有用
///
///
///
public string Id { get; set; }
private Func _compiledCol;
protected Func CompiledCol
{
get
{
if (_compiledCol == null)
{
if (ColumnExp == null)
{
_compiledCol = (T) => "";
}
else
{
_compiledCol = ColumnExp.Compile();
}
}
return _compiledCol;
}
}
private Type _fieldType;
///
/// 获取值域类型
///
///
public Type FieldType
{
get
{
return _fieldType ?? PI?.PropertyType;
}
set
{
_fieldType = value;
}
}
public string FieldName
{
get
{
return PI?.Name;
}
}
///
/// 本列是否需要分组
///
public bool NeedGroup { get; set; }
public bool IsLocked { get; set; }
public bool Sortable { get; set; }
///
/// 是否允许换行
///
public bool AllowMultiLine { get; set; }
///
/// 设置某列是否应该尽量充满
///
public int? Flex { get; set; }
///
/// 列内容的格式化函数
///
public ColumnFormatCallBack Format { get; set; }
///
/// 本列前景色函数
///
public Func ForeGroundFunc { get; set; }
///
/// 本列背景色函数
///
public Func BackGroundFunc { get; set; }
///
/// 获取最底层的子列
///
public IEnumerable> BottomChildren
{
get
{
List> rv = new List>();
if (Children != null && Children.Any())
{
foreach (var child in Children)
{
rv.AddRange(child.BottomChildren);
}
}
else
{
rv.Add(this);
}
return rv;
}
}
///
/// 根据设置前景色的函数返回前景色
///
/// 源数据
/// 前景色
public string GetForeGroundColor(object source)
{
if (ForeGroundFunc == null)
{
return "";
}
else
{
return ForeGroundFunc.Invoke(source as T);
}
}
///
/// 根据设置背景色的函数返回背景色
///
/// 源数据
/// 背景色
public string GetBackGroundColor(object source)
{
if (BackGroundFunc == null)
{
return "";
}
else
{
return BackGroundFunc.Invoke(source as T);
}
}
public bool HasFormat()
{
if(Format != null)
{
return true;
}
else
{
return false;
}
}
///
/// 获取单元格要输出的内容
///
/// 源数据
/// 是否使用format
/// Html内容
public virtual object GetText(object source, bool needFormat = true)
{
object rv = null;
var col = CompiledCol?.Invoke(source as T);
if(needFormat == false && Format != null)
{
var test = Format.Invoke(source as T, col);
if(test is ColumnFormatInfo == false && test is List == false)
{
return test??"";
}
}
if (Format == null || needFormat == false)
{
if (col == null)
{
rv = "";
}
else if (col is DateTime || col is DateTime?)
{
var datevalue = col as DateTime?;
if(datevalue != null)
{
if (datevalue.Value.Hour == 0 && datevalue.Value.Minute == 0 && datevalue.Value.Second == 0)
{
rv = datevalue.Value.ToString("yyyy-MM-dd");
}
else
{
rv = datevalue.Value.ToString("yyyy-MM-dd HH:mm:ss");
}
}
else
{
rv = "";
}
}
else if (col.GetType().IsEnumOrNullableEnum())
{
rv = col.ToString();
}
else if (col.GetType().Namespace.Equals("System") == false)
{
if (needFormat == false)
{
rv = JsonSerializer.Serialize(col, CoreProgram.DefaultJsonOption);
}
else
{
rv = col.ToString();
}
}
else
{
rv = col.ToString();
}
}
else
{
rv = Format.Invoke(source as T, col);
}
if (rv == null)
{
rv = "";
}
return rv;
}
public virtual object GetObject(object source)
{
object rv = CompiledCol?.Invoke(source as T);
return rv;
}
///
/// 获取列头内容
///
/// Html内容
protected virtual string GetHeader()
{
string rv = PropertyHelper.GetPropertyDisplayName(PI);
return rv ?? "";
}
///
/// 默认构造函数
///
public GridColumn()
{
AllowMultiLine = true;
this.Sortable = true;
}
///
/// 构造函数
///
///
///
///
///
///
///
///
///
///
///
public GridColumn(Expression> ColumnExp, ColumnFormatCallBack Format = null, string Header = null, int? Width = null, int? Flex = null, bool AllowMultiLine = true, bool NeedGroup = false, Func ForeGroundFunc = null, Func BackGroundFunc = null, bool sortable = true)
{
this.ColumnExp = ColumnExp;
this.Format = Format;
this.Title = Header;
this.Width = Width;
this.NeedGroup = NeedGroup;
this.ForeGroundFunc = ForeGroundFunc;
this.BackGroundFunc = BackGroundFunc;
this.AllowMultiLine = AllowMultiLine;
this.Flex = Flex;
this.Sortable = sortable;
}
#endregion
}
}