using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using Lskj.Business.Impl;
using Lskj.Control;
using Lskj.Control.Model;
using Lskj.Model;
using Lskj.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Lskj.PubArtworkDynamic.Control
{
public partial class ModuleGridDetail : UserControl
{
///
/// 模块类
///
public ViewModel MainViewModel;
///
/// 搜索按钮
///
public SimpleButton SearchButton
{
get
{
if (this.SearchMainObj.ButtonObj != null)
{
return this.SearchMainObj.ButtonObj;
}
else
{
return this.plm_top_btnFixSearch;
}
}
}
///
/// 主模块的表格
///
public GridControlEx MainGridEx
{
get
{
if (gc_Top.ContainsFocus)
{
return gc_Top;
}
else
{
return gc_Bottom;
}
}
}
///
/// 上方表格控件
///
public GridControlEx TopGridEx { get { return gc_Top; } }
///
/// 下方表格控件
///
public GridControlEx BottomGridEx { get { return gc_Bottom; } }
///
/// 明细Model
///
public GridDetailModel DetailModel;
///
/// 主条件控件
///
public MyControl SearchMainObj;
///
/// 明细条件控件
///
public MyControl SearchDetailObj;
///
/// 条件参数
///
public string[] OtherParams;
///
/// 拖拽方向(1只能拖拽到主表,2只能拖拽到明细。默认0都可以拖)
///
public int DragDirection;
///
/// 构造函数
///
///
public ModuleGridDetail(GridDetailModel model)
{
DetailModel = model;
InitializeComponent();
Load += OnModuleGridDetailLoad;
}
public ModuleGridDetail(GridDetailModel model, string[] otherParams)
{
DetailModel = model;
this.OtherParams = otherParams;
InitializeComponent();
Load += OnModuleGridDetailLoad;
}
///
/// 加载完成后
///
///
///
private void OnModuleGridDetailLoad(object sender, EventArgs e)
{
try
{
gc_Top.GridView.OptionsSelection.MultiSelect = false;
gc_Top.GridView.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;
gc_Bottom.GridView.OptionsSelection.MultiSelect = false;
gc_Bottom.GridView.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;
gc_Top.GridView.RowClick += OnGridViewRowClick;
gc_Bottom.GridView.RowClick += OnGridViewRowClick;
//gc_Bottom.GridView.FocusedRowObjectChanged += OnGridViewFocusedRowObjectChanged;
if (DetailModel != null)
{
this.DragDirection = DetailModel.SystemModel.DragDirection;
if (DetailModel.IsReadOnly)
{
this.SearchMainObj = new MyControl(DetailModel.DetailSql, gc_Top);//构建主表mycontrol
}
else
{
this.SearchMainObj = new MyControl(DetailModel.SystemModel.MenuSql, gc_Top);//构建主表mycontrol
}
this.SearchMainObj.OtherParams = this.OtherParams;
this.SearchMainObj.OnSearchAfterCallBack += OnSearchAfterCallBack;//搜索完成后
DataTable gridColumns = new DataTable();
if (!string.IsNullOrEmpty(DetailModel.UnionModule))
{
gridColumns = BaseModuleImpl.GetBaseGridColumns(DetailModel.UnionModule);
}
else
{
gridColumns = ReportImpl.GetReportDetailColumns(MainViewModel.SysModel.ModuleCode, DetailModel.Id + "");
}
gc_Top.GridView.OptionsBehavior.Editable = false;
gc_Top.SetReadOnlyColumns(gridColumns, GridCustomColumnStruct.BaseMainGridView + DetailModel.FormKey);//明细
DynamicModel dyncModel = new DynamicModuleModel(new string[] { DetailModel.DetailName, ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, MainViewModel.SysModel.Privilege, DetailModel.UnionModule, "0" });
if (!string.IsNullOrEmpty(DetailModel.SystemModel.ModeCode))
{
gc_Top.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(DetailModel.SystemModel.ModeCode), dyncModel, OnRightMenuCallBack, this.SearchMainObj);
}
else if (!string.IsNullOrEmpty(DetailModel.FormKey))
{
gc_Top.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(DetailModel.FormKey), dyncModel, OnRightMenuCallBack, this.SearchMainObj);
}
DataTable queryTable = BaseModuleImpl.GetCustomQueryFields(DetailModel.SystemModel.CondKey);//获取主模块构建的条件
if (queryTable != null && queryTable.Rows.Count > 0)
{
// 加载自定义查询条件
this.pl_top_cond.Controls.Clear();
this.SearchMainObj.InitSearchControl(queryTable, this.pl_top_cond, null, null, DetailModel.SystemModel.ConditionRefresh);
}
else
{
// 加载固定查询条件
DataTable table = BaseModuleImpl.GetFixedQueryFields(DetailModel.SystemModel.ModeCode);//加载固定查询条件
this.plm_top_cbField.DisplayMember = "fieldText";
this.plm_top_cbField.ValueMember = "fieldName";
this.plm_top_cbField.DataSource = table;
this.SearchMainObj.InitDefaultSearchControl(table, this.pl_top_cond, this.plm_top_cbField.Name, this.plm_top_txtFixKey.Name, this.plm_top_btnFixSearch.Name);
}
//加载明细表格
DataTable moduleDetailTable = BaseModuleImpl.GetBaseDetailPages(DetailModel.UnionModule);//获取关联模块配置明细
DataRow detailRow = moduleDetailTable.Rows.Cast().Where(n => (n["orderId"] + "").Equals("99")).FirstOrDefault();
if (detailRow != null)
{
GridDetailModel bottomDetailModel = new GridDetailModel(detailRow);
bottomDetailModel.SystemModel = new ModuleModel(MainImpl.GetSystemdllTab(bottomDetailModel.UnionModule));//获取主模块构建类
gc_Bottom.Tag = bottomDetailModel;
if (bottomDetailModel.IsReadOnly)
{
this.SearchDetailObj = new MyControl(bottomDetailModel.DetailSql, gc_Bottom);//构建明细表mycontrol
}
else
{
this.SearchDetailObj = new MyControl(bottomDetailModel.SystemModel.MenuSql, gc_Bottom);//构建明细表mycontrol
}
DataTable bottomDetailColumns = new DataTable();
if (!string.IsNullOrEmpty(bottomDetailModel.UnionModule))
{
bottomDetailColumns = BaseModuleImpl.GetBaseGridColumns(bottomDetailModel.UnionModule);
}
else
{
bottomDetailColumns = ReportImpl.GetReportDetailColumns(MainViewModel.SysModel.ModuleCode, bottomDetailModel.Id + "");
}
gc_Bottom.GridView.OptionsBehavior.Editable = false;
gc_Bottom.SetReadOnlyColumns(bottomDetailColumns, GridCustomColumnStruct.BaseMainGridView + bottomDetailModel.FormKey);//明细
DynamicModel dyncBottomDetailModel = new DynamicModuleModel(new string[] { bottomDetailModel.DetailName, ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, MainViewModel.SysModel.Privilege, bottomDetailModel.UnionModule, "0" });
if (!string.IsNullOrEmpty(bottomDetailModel.SystemModel.ModeCode))
{
gc_Bottom.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(bottomDetailModel.SystemModel.ModeCode), dyncBottomDetailModel, OnRightMenuCallBack, SearchDetailObj);
}
else if (!string.IsNullOrEmpty(bottomDetailModel.FormKey))
{
gc_Bottom.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(bottomDetailModel.FormKey), dyncBottomDetailModel, OnRightMenuCallBack, SearchDetailObj);
}
}
else
{
splitMainControl.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
}
}
else
{
ModuleModel MainModuleModel = new ModuleModel(MainImpl.GetSystemdllTab(MainViewModel.SysModel.ModuleCode));//获取主模块构建类
this.DragDirection = MainModuleModel.DragDirection;
this.SearchMainObj = new MyControl(MainModuleModel.MenuSql, gc_Top);//构建主表mycontrol
this.SearchMainObj.OtherParams = this.OtherParams;
this.SearchMainObj.OnSearchAfterCallBack += OnSearchAfterCallBack;//搜索完成后
DataTable gridColumns = BaseModuleImpl.GetBaseGridColumns(MainModuleModel.ModeCode);
gc_Top.GridView.OptionsBehavior.Editable = false;
gc_Top.SetReadOnlyColumns(gridColumns, GridCustomColumnStruct.BaseMainGridView + MainModuleModel.FormKey);//明细
DynamicModel dyncModel = new DynamicModuleModel(new string[] { MainModuleModel.MenuName, ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, MainViewModel.SysModel.Privilege, MainModuleModel.ModeCode, "0" });
if (!string.IsNullOrEmpty(MainModuleModel.ModeCode))
{
gc_Top.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(MainModuleModel.ModeCode), dyncModel, OnRightMenuCallBack, this.SearchMainObj);
}
else if (!string.IsNullOrEmpty(MainModuleModel.FormKey))
{
gc_Top.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(MainModuleModel.FormKey), dyncModel, OnRightMenuCallBack, this.SearchMainObj);
}
DataTable queryTable = BaseModuleImpl.GetCustomQueryFields(MainModuleModel.CondKey);//获取主模块构建的条件
if (queryTable != null && queryTable.Rows.Count > 0)
{
// 加载自定义查询条件
this.pl_top_cond.Controls.Clear();
this.SearchMainObj.InitSearchControl(queryTable, this.pl_top_cond, null, null, MainModuleModel.ConditionRefresh);
}
else
{
// 加载固定查询条件
DataTable table = BaseModuleImpl.GetFixedQueryFields(DetailModel.SystemModel.ModeCode);//加载固定查询条件
this.plm_top_cbField.DisplayMember = "fieldText";
this.plm_top_cbField.ValueMember = "fieldName";
this.plm_top_cbField.DataSource = table;
this.SearchMainObj.InitDefaultSearchControl(table, this.pl_top_cond, this.plm_top_cbField.Name, this.plm_top_txtFixKey.Name, this.plm_top_btnFixSearch.Name);
}
//加载明细表格
DataTable moduleDetailTable = BaseModuleImpl.GetBaseDetailPages(MainModuleModel.ModeCode);//获取关联模块配置明细
DataRow detailRow = moduleDetailTable.Rows.Cast().Where(n => (n["orderId"] + "").Equals("99")).FirstOrDefault();
if (detailRow != null)
{
GridDetailModel bottomDetailModel = new GridDetailModel(detailRow);
bottomDetailModel.SystemModel = new ModuleModel(MainImpl.GetSystemdllTab(bottomDetailModel.UnionModule));//获取主模块构建类
gc_Bottom.Tag = bottomDetailModel;
if (bottomDetailModel.IsReadOnly)
{
this.SearchDetailObj = new MyControl(bottomDetailModel.DetailSql, gc_Bottom);//构建明细表mycontrol
}
else
{
this.SearchDetailObj = new MyControl(bottomDetailModel.SystemModel.MenuSql, gc_Bottom);//构建明细表mycontrol
}
DataTable bottomDetailColumns = new DataTable();
if (!string.IsNullOrEmpty(bottomDetailModel.UnionModule))
{
bottomDetailColumns = BaseModuleImpl.GetBaseGridColumns(bottomDetailModel.UnionModule);
}
else
{
bottomDetailColumns = ReportImpl.GetReportDetailColumns(MainViewModel.SysModel.ModuleCode, bottomDetailModel.Id + "");
}
gc_Bottom.GridView.OptionsBehavior.Editable = false;
gc_Bottom.SetReadOnlyColumns(bottomDetailColumns, GridCustomColumnStruct.BaseMainGridView + bottomDetailModel.FormKey);//明细
DynamicModel dyncBottomDetailModel = new DynamicModuleModel(new string[] { bottomDetailModel.DetailName, ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, MainViewModel.SysModel.Privilege, bottomDetailModel.UnionModule, "0" });
if (!string.IsNullOrEmpty(bottomDetailModel.SystemModel.ModeCode))
{
gc_Bottom.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(bottomDetailModel.SystemModel.ModeCode), dyncBottomDetailModel, OnRightMenuCallBack, SearchDetailObj);
}
else if (!string.IsNullOrEmpty(bottomDetailModel.FormKey))
{
gc_Bottom.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(bottomDetailModel.FormKey), dyncBottomDetailModel, OnRightMenuCallBack, SearchDetailObj);
}
}
else
{
splitMainControl.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
}
}
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
///
/// 右键菜单回调
///
///
///
private void OnRightMenuCallBack(object sender, EventArgs e)
{
try
{
SearchMainObj.SearchGrid();
RefushLeftDetail();
RefushBottomLeftDetail();
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
///
/// 搜索完成后
///
///
///
private void OnSearchAfterCallBack(object sender, EventArgs e)
{
try
{
RefushWaitDetail();
RefushLeftDetail();
RefushBottomLeftDetail();
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
///
/// 行选中改变时刷新明细
///
///
///
public void OnGridViewRowClick(object sender, RowClickEventArgs e)
{
GridView gridView = (GridView)sender;
if (gridView == gc_Top.GridView)
{
RefushLeftDetail();
}
RefushWaitDetail();
RefushBottomLeftDetail();
}
///
/// 刷新左边选中的明细信息
///
public void RefushLeftDetail()
{
try
{
if (MainViewModel.LeftTabControl.TabControlObj.SelectedTabPage != null)
{
ModuleGridDetail moduleGridDetail = MainViewModel.LeftTabControl.TabControlObj.SelectedTabPage.Controls[0] as ModuleGridDetail;
DataRow rowItem = moduleGridDetail.TopGridEx.GetViewFocusedDataRow();
if (rowItem != null && moduleGridDetail.BottomGridEx.Tag != null && moduleGridDetail.BottomGridEx.Tag is GridDetailModel)
{
GridDetailModel leftDetailModel = moduleGridDetail.BottomGridEx.Tag as GridDetailModel;
string sqlValue = this.GetSearchSql(leftDetailModel, rowItem);
DataTable dataTable = MainImpl.GetDataTableResult(sqlValue);
moduleGridDetail.BottomGridEx.SetGridViewDataSource(dataTable);
moduleGridDetail.BottomGridEx.LastSearchSql = sqlValue;
}
}
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
///
/// 刷新左边选中的底部明细信息
///
public void RefushBottomLeftDetail()
{
try
{
if (MainViewModel.LeftTabControl.TabControlObj.SelectedTabPage != null)
{
ModuleGridDetail moduleGridDetail = MainViewModel.LeftTabControl.TabControlObj.SelectedTabPage.Controls[0] as ModuleGridDetail;
DataRow rowItem = null;
if (moduleGridDetail.BottomGridEx.Visible == true)
{
rowItem = moduleGridDetail.BottomGridEx.GetViewFocusedDataRow();
}
if (rowItem == null || moduleGridDetail.BottomGridEx.Visible == false)
{
rowItem = moduleGridDetail.TopGridEx.GetViewFocusedDataRow();
}
if (rowItem != null && MainViewModel.BottomLeftGridEx.Tag != null && MainViewModel.BottomLeftGridEx.Tag is GridDetailModel)
{
GridDetailModel bottomLeftDetailModel = MainViewModel.BottomLeftGridEx.Tag as GridDetailModel;
string sqlValue = this.GetSearchSql(bottomLeftDetailModel, rowItem);
DataTable dataTable = MainImpl.GetDataTableResult(sqlValue);
MainViewModel.BottomLeftGridEx.SetGridViewDataSource(dataTable);
MainViewModel.BottomLeftGridEx.LastSearchSql = sqlValue;
}
}
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
///
/// 刷新等待区
///
public void RefushWaitDetail()
{
try
{
if (MainViewModel.LeftTabControl.TabControlObj.SelectedTabPage != null && MainViewModel.RightTabControl.TabControlObj.SelectedTabPage != null)
{
ModuleGridDetail moduleGridDetail = MainViewModel.LeftTabControl.TabControlObj.SelectedTabPage.Controls[0] as ModuleGridDetail;
ModuleGrid moduleGrid = MainViewModel.RightTabControl.TabControlObj.SelectedTabPage.Controls[0] as ModuleGrid;
DataRow rowItem = moduleGridDetail.TopGridEx.GetViewFocusedDataRow();
if (rowItem != null && moduleGrid.MainGridEx.Tag != null && moduleGrid.MainGridEx.Tag is GridDetailModel)
{
GridDetailModel gridDetailModel = moduleGrid.MainGridEx.Tag as GridDetailModel;
string sqlValue = this.GetSearchSql(gridDetailModel, rowItem);
DataTable dataTable = MainImpl.GetDataTableResult(sqlValue);
moduleGrid.MainGridEx.SetGridViewDataSource(dataTable);
moduleGrid.MainGridEx.LastSearchSql = sqlValue;
}
}
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
///
/// 获取查询条件
///
///
///
///
private string GetSearchSql(GridDetailModel model, DataRow rowItem)
{
string fieldName = !string.IsNullOrEmpty(model.UnionParentField) && rowItem.Table.Columns.Contains(model.UnionParentField) ? model.UnionParentField : MainViewModel.MoudleMainObj.ParmaryKey;
string sqlValue = model.IsReadOnly || model.IsChart ? model.DetailSql : model.SystemModel.MenuSql;
sqlValue = ReplaceHelper.ReplaceRowParam(rowItem, sqlValue);
if (ReplaceHelper.IsSelect(sqlValue) && !string.IsNullOrEmpty(model.UnionValue))
{
sqlValue = ReplaceHelper.ReplaceWhereCond(sqlValue) + string.Format(" and {0}='{1}'", model.UnionValue, rowItem[fieldName] + "");
}
if (!string.IsNullOrEmpty(model.UnionCond))
{
string UnionCond = ReplaceHelper.ReplaceRowParam(rowItem, model.UnionCond);
sqlValue += UnionCond;
}
return sqlValue;
}
}
}