523 lines
19 KiB
C#
523 lines
19 KiB
C#
/******************************
|
|
* 说明:表格报表模版
|
|
* 创建人:龚宇超
|
|
* 创建日期:2017-11-20
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
using Lskj.Control;
|
|
using Lskj.Model;
|
|
using Lskj.Control.Model;
|
|
using Lskj.Util;
|
|
using Lskj.Data;
|
|
using Lskj.Business.Impl;
|
|
using Lskj.Business;
|
|
using System.Threading.Tasks;
|
|
using System.Collections;
|
|
using System.Reflection;
|
|
|
|
namespace Lskj.Report
|
|
{
|
|
public partial class FrmMain : BaseForm
|
|
{
|
|
/// <summary>
|
|
/// 报表动态链接库参数
|
|
/// </summary>
|
|
public DynamicReportModel Model;
|
|
|
|
private readonly HashSet<DevExpress.XtraGrid.Views.Base.BaseView>
|
|
mPreexistingGridLocalizerViews = CaptureGridLocalizerViews();
|
|
private readonly HashSet<DevExpress.XtraGrid.Views.Base.BaseView>
|
|
mReportGridLocalizerViews =
|
|
new HashSet<DevExpress.XtraGrid.Views.Base.BaseView>();
|
|
private bool mReportGridResourcesReleased;
|
|
|
|
public FrmMain()
|
|
{
|
|
InitializeComponent();
|
|
this.Disposed += OnFrmMainDisposed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:窗口加载</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-21 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
|
|
protected override void OnLoad(EventArgs e)
|
|
{
|
|
WaitForm.ShowForm();
|
|
try
|
|
{
|
|
Dictionary<object, Hashtable> dataCaches = Model != null ? Model.DataCaches : null;
|
|
this.Model.Privilege = this.Model.Privilege.Equals("3") ? "1" : Model.ModuleId > 0 ? BaseImpl.GetUserPurviewsByMenuId(Model.ModuleId + "") + "" : BaseImpl.GetUserPurviewsByMenuCode(Model.ModuleCode) + "";
|
|
|
|
this.mControl.ModuleGridDetailObj.ModuleGridObj.VisibleOperPanel = false;
|
|
|
|
if (!dataCaches.GetValue(this, "Details", out List<GridDetailModel> details))
|
|
{
|
|
details = GetReportDetails();
|
|
}
|
|
|
|
this.mControl.Details = details;
|
|
this.mControl.InitControls(Model);
|
|
CaptureReportGridLocalizerViews();
|
|
this.FormClosing += new FormClosingEventHandler(OnClose);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
|
|
//MessageUtil.Show(ex.Message + "\r\n" + ex.StackTrace);
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
Lskj.Control.Model.AutoSizeChange.ControllInitializeSize(this);
|
|
this.mControl.Visible = true;
|
|
}
|
|
|
|
WaitForm.HideForm();
|
|
}
|
|
|
|
private void OnClose(object sender, FormClosingEventArgs e)
|
|
{
|
|
this.mControl.CloseModule();
|
|
}
|
|
|
|
private void OnFrmMainDisposed(object sender, EventArgs e)
|
|
{
|
|
DisposeReportGridViews();
|
|
mReportGridLocalizerViews.Clear();
|
|
mPreexistingGridLocalizerViews.Clear();
|
|
this.FormClosing -= OnClose;
|
|
this.Disposed -= OnFrmMainDisposed;
|
|
|
|
DynamicReportModel model = this.Model;
|
|
this.Model = null;
|
|
if (model == null || model.DataCaches == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
model.DataCaches.ClearCaches();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
|
|
}
|
|
}
|
|
|
|
private void DisposeReportGridViews()
|
|
{
|
|
if (mReportGridResourcesReleased)
|
|
{
|
|
return;
|
|
}
|
|
|
|
mReportGridResourcesReleased = true;
|
|
CaptureReportGridLocalizerViews();
|
|
if (this.mControl != null)
|
|
{
|
|
this.mControl.ReleaseResourcesForDispose();
|
|
}
|
|
|
|
HashSet<DevExpress.XtraGrid.Views.Base.BaseView> disposedViews =
|
|
new HashSet<DevExpress.XtraGrid.Views.Base.BaseView>();
|
|
foreach (System.Windows.Forms.Control control in
|
|
EnumerateControls(this.mControl))
|
|
{
|
|
DevExpress.XtraGrid.GridControl gridControl =
|
|
control as DevExpress.XtraGrid.GridControl;
|
|
if (gridControl != null)
|
|
{
|
|
try
|
|
{
|
|
if (gridControl.DataSource != null)
|
|
{
|
|
gridControl.DataSource = null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
|
|
}
|
|
|
|
DevExpress.XtraGrid.Views.Base.BaseView[] views =
|
|
gridControl.ViewCollection
|
|
.Cast<DevExpress.XtraGrid.Views.Base.BaseView>()
|
|
.ToArray();
|
|
foreach (DevExpress.XtraGrid.Views.Base.BaseView view in views)
|
|
{
|
|
if (view == null || !disposedViews.Add(view))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
view.Dispose();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
|
|
}
|
|
}
|
|
}
|
|
|
|
Type controlType = control.GetType();
|
|
if (controlType.FullName != null &&
|
|
controlType.FullName.StartsWith(
|
|
"DevExpress.XtraTreeList.",
|
|
StringComparison.Ordinal))
|
|
{
|
|
try
|
|
{
|
|
PropertyInfo dataSourceProperty =
|
|
controlType.GetProperty(
|
|
"DataSource",
|
|
BindingFlags.Instance |
|
|
BindingFlags.Public);
|
|
if (dataSourceProperty != null &&
|
|
dataSourceProperty.CanRead &&
|
|
dataSourceProperty.CanWrite &&
|
|
dataSourceProperty.GetValue(control, null) != null)
|
|
{
|
|
dataSourceProperty.SetValue(control, null, null);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
|
|
}
|
|
}
|
|
}
|
|
|
|
ReleaseTrackedGridLocalizerViews(disposedViews);
|
|
}
|
|
|
|
private void CaptureReportGridLocalizerViews()
|
|
{
|
|
HashSet<DevExpress.XtraGrid.Views.Base.BaseView> currentViews =
|
|
CaptureGridLocalizerViews();
|
|
foreach (DevExpress.XtraGrid.Views.Base.BaseView view in currentViews)
|
|
{
|
|
if (!mPreexistingGridLocalizerViews.Contains(view))
|
|
{
|
|
mReportGridLocalizerViews.Add(view);
|
|
}
|
|
}
|
|
}
|
|
|
|
private int ReleaseTrackedGridLocalizerViews(
|
|
HashSet<DevExpress.XtraGrid.Views.Base.BaseView> disposedViews)
|
|
{
|
|
foreach (DevExpress.XtraGrid.Views.Base.BaseView view in
|
|
mReportGridLocalizerViews)
|
|
{
|
|
if (view == null ||
|
|
(disposedViews != null && !disposedViews.Add(view)))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
view.Dispose();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogHelper.Instance.WriteError(exception, ResourceKeys.BaseReport);
|
|
}
|
|
}
|
|
|
|
return RemoveGridLocalizerHandlers(mReportGridLocalizerViews);
|
|
}
|
|
|
|
private static HashSet<DevExpress.XtraGrid.Views.Base.BaseView>
|
|
CaptureGridLocalizerViews()
|
|
{
|
|
HashSet<DevExpress.XtraGrid.Views.Base.BaseView> result =
|
|
new HashSet<DevExpress.XtraGrid.Views.Base.BaseView>();
|
|
try
|
|
{
|
|
var activeLocalizer =
|
|
DevExpress.XtraGrid.Localization.GridLocalizer.Active;
|
|
if (activeLocalizer == null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
Type currentType = activeLocalizer.GetType();
|
|
while (currentType != null && currentType != typeof(object))
|
|
{
|
|
FieldInfo[] fields = currentType.GetFields(
|
|
BindingFlags.Instance |
|
|
BindingFlags.Static |
|
|
BindingFlags.Public |
|
|
BindingFlags.NonPublic |
|
|
BindingFlags.DeclaredOnly);
|
|
foreach (FieldInfo field in fields)
|
|
{
|
|
if (!typeof(Delegate).IsAssignableFrom(field.FieldType))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
object owner = field.IsStatic ? null : (object)activeLocalizer;
|
|
Delegate current;
|
|
try
|
|
{
|
|
current = field.GetValue(owner) as Delegate;
|
|
}
|
|
catch
|
|
{
|
|
continue;
|
|
}
|
|
if (current == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
foreach (Delegate handler in current.GetInvocationList())
|
|
{
|
|
if (IsGridLocalizerHandler(handler))
|
|
{
|
|
result.Add(
|
|
(DevExpress.XtraGrid.Views.Base.BaseView)
|
|
handler.Target);
|
|
}
|
|
}
|
|
}
|
|
|
|
currentType = currentType.BaseType;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Diagnostics cleanup must never interfere with form construction.
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private static int RemoveGridLocalizerHandlers(
|
|
HashSet<DevExpress.XtraGrid.Views.Base.BaseView> targetViews)
|
|
{
|
|
if (targetViews == null || targetViews.Count == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int removedCount = 0;
|
|
try
|
|
{
|
|
var activeLocalizer =
|
|
DevExpress.XtraGrid.Localization.GridLocalizer.Active;
|
|
if (activeLocalizer == null)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
Type currentType = activeLocalizer.GetType();
|
|
while (currentType != null && currentType != typeof(object))
|
|
{
|
|
FieldInfo[] fields = currentType.GetFields(
|
|
BindingFlags.Instance |
|
|
BindingFlags.Static |
|
|
BindingFlags.Public |
|
|
BindingFlags.NonPublic |
|
|
BindingFlags.DeclaredOnly);
|
|
foreach (FieldInfo field in fields)
|
|
{
|
|
if (!typeof(Delegate).IsAssignableFrom(field.FieldType))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
object owner = field.IsStatic ? null : (object)activeLocalizer;
|
|
Delegate current;
|
|
try
|
|
{
|
|
current = field.GetValue(owner) as Delegate;
|
|
}
|
|
catch
|
|
{
|
|
continue;
|
|
}
|
|
if (current == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Delegate retained = null;
|
|
int fieldRemovedCount = 0;
|
|
foreach (Delegate handler in current.GetInvocationList())
|
|
{
|
|
DevExpress.XtraGrid.Views.Base.BaseView view =
|
|
handler.Target as DevExpress.XtraGrid.Views.Base.BaseView;
|
|
if (IsGridLocalizerHandler(handler) &&
|
|
targetViews.Contains(view))
|
|
{
|
|
fieldRemovedCount++;
|
|
continue;
|
|
}
|
|
|
|
retained = retained == null
|
|
? handler
|
|
: Delegate.Combine(retained, handler);
|
|
}
|
|
|
|
if (fieldRemovedCount == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
field.SetValue(owner, retained);
|
|
removedCount += fieldRemovedCount;
|
|
}
|
|
catch
|
|
{
|
|
// Some DevExpress versions expose a read-only backing field.
|
|
}
|
|
}
|
|
|
|
currentType = currentType.BaseType;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Diagnostics cleanup must never interfere with form disposal.
|
|
}
|
|
|
|
return removedCount;
|
|
}
|
|
|
|
private static bool IsGridLocalizerHandler(Delegate handler)
|
|
{
|
|
return handler != null &&
|
|
string.Equals(
|
|
handler.Method.Name,
|
|
"OnLocalizer_Changed",
|
|
StringComparison.Ordinal) &&
|
|
handler.Target is DevExpress.XtraGrid.Views.Base.BaseView;
|
|
}
|
|
|
|
private static IEnumerable<System.Windows.Forms.Control> EnumerateControls(
|
|
System.Windows.Forms.Control root)
|
|
{
|
|
if (root == null)
|
|
{
|
|
yield break;
|
|
}
|
|
yield return root;
|
|
foreach (System.Windows.Forms.Control child in root.Controls)
|
|
{
|
|
foreach (System.Windows.Forms.Control nested in EnumerateControls(child))
|
|
{
|
|
yield return nested;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:获取报表明细</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-22 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>List<GridDetailModel>.</returns>
|
|
private List<GridDetailModel> GetReportDetails()
|
|
{
|
|
List<GridDetailModel> details = new List<GridDetailModel>();
|
|
|
|
DataTable table = ReportImpl.GetReportDetailPages(Model.ModuleCode);
|
|
foreach (DataRow item in table.Rows)
|
|
{
|
|
DataTable gridColumns = ReportImpl.GetReportDetailColumns(Model.ModuleCode, item["id"] + "");
|
|
details.Add(new GridDetailModel(item, gridColumns, GridCustomColumnStruct.BaseDetailGridView));
|
|
}
|
|
|
|
return details;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取数据源缓存
|
|
/// </summary>
|
|
/// <param name="cachesDic"></param>
|
|
public void GetDataCaches(Dictionary<object, Hashtable> cachesDic)
|
|
{
|
|
//全局数据
|
|
Task<DynamicModel> dynamicModelTask = cachesDic.AddTask(this, "DynamicModel", new Task<DynamicModel>(() =>
|
|
{
|
|
return Model;//动态链接库对象
|
|
}));
|
|
Task<DataRow> systemDllRowTask = cachesDic.AddTask(this, "SystemDllRow", new Task<DataRow>(() =>
|
|
{
|
|
return MainImpl.GetSystemdllTab(Model.ModuleCode);//获取单个模块信息
|
|
}));
|
|
Task<ModuleModel> sysModelTask = cachesDic.AddTask(this, "SysModel", new Task<ModuleModel>(() =>
|
|
{
|
|
ModuleModel sysModel = new ModuleModel(systemDllRowTask.Result);
|
|
return sysModel;//系统模块实体对象
|
|
}));
|
|
Task<List<GridDetailModel>> detailTask = cachesDic.AddTask(this, "Details", new Task<List<GridDetailModel>>(() =>
|
|
{
|
|
DataTable detailPages = ReportImpl.GetReportDetailPages(Model.ModuleCode);//
|
|
List<GridDetailModel> details = new List<GridDetailModel>();//表格明细对象的集合
|
|
foreach (DataRow item in detailPages.Rows)
|
|
{
|
|
GridDetailModel gridDetailModel = new GridDetailModel(item, null, GridCustomColumnStruct.BaseDetailGridView);
|
|
details.Add(gridDetailModel);
|
|
Task<DataTable> addGridColumnsTask = cachesDic.AddTask(gridDetailModel, "GridColumns", new Task<DataTable>(() =>
|
|
{
|
|
DataTable gridColumns = ReportImpl.GetReportDetailColumns(Model.ModuleCode, item["id"] + "");//获取报表明细列
|
|
gridDetailModel.GridColumns = gridColumns;
|
|
return gridColumns;
|
|
}));
|
|
}
|
|
return details;
|
|
}));
|
|
Task<int> formTypeTask = cachesDic.AddTask(this, "FormType", new Task<int>(() =>
|
|
{
|
|
return BaseImpl.GetBaseType(Model.ModuleCode);
|
|
}));
|
|
Task<string> privilegeTask = cachesDic.AddTask(this, "Privilege", new Task<string>(() =>
|
|
{
|
|
return Model.Privilege.Equals("3") ? "1" : Model.ModuleId > 0 ? BaseImpl.GetUserPurviewsByMenuId(Model.ModuleId + "") + "" : BaseImpl.GetUserPurviewsByMenuCode(Model.ModuleCode) + "";// 权限(1.有操作权限.0.无权限.2.只读权限.3.右键配置权限全部开放)
|
|
}));
|
|
//添加数据到mControl
|
|
cachesDic.AddTask(mControl, "DynamicModel", dynamicModelTask);
|
|
cachesDic.AddTask(mControl, "SystemDllRow", systemDllRowTask);
|
|
cachesDic.AddTask(mControl, "SysModel", sysModelTask);
|
|
cachesDic.AddTask(mControl, "Details", detailTask);
|
|
//获取mControl控件的数据
|
|
mControl.GetDataCaches(cachesDic);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|