Files
lserp_cs_6.0/插件库/Lskj.PubBillManagement/FrmDocumentSource.cs
T
2026-07-28 10:16:51 +08:00

602 lines
24 KiB
C#

using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Lskj.Business;
using Lskj.Business.Impl;
using Lskj.Control;
using Lskj.Control.Model;
using Lskj.Data;
using Lskj.Model;
using Lskj.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Lskj.PubBillManagement
{
public partial class FrmDocumentSource : BaseForm
{
/// <summary>
/// 配置的关联ModuleCodel值
/// </summary>
/// <value>The Union ModuleCodel.</value>
public string UnionModuleCodel;
/// <summary>
/// 左侧树数据
/// </summary>
public DataRow LeftRowObj;
/// <summary>
/// 系统模块实体对象
/// </summary>
/// <value>The system model.</value>
public ModuleModel SysModel { get; private set; }
/// <summary>
/// 自定义查询条件
/// </summary>
public MyControl SearchObj { get; private set; }
/// <summary>
/// 第一次进入
/// </summary>
private bool isFirst = true;
/// <summary>
/// 第一次进入
/// </summary>
private bool isFirstSearch = true;
/// <summary>
/// 左侧树结构
/// </summary>
public TreeViewEx LeftTreeViewEx;
/// <summary>
/// 左侧表格
/// </summary>
public GridControlEx LeftGridEx;
/// <summary>
/// 左侧结构关联字段
/// </summary>
public string ParentUnionField;
/// <summary>
/// 左侧树结构、表格主键
/// </summary>
public string ParentKeyField;
/// <summary>
/// CurrentOperColumnKey值(保存表格属性会用)
/// </summary>
/// <value>The data source.</value>
public string CurrentOperColumnKey = string.Empty;
/// <summary>
/// 左侧表格查询条件
/// </summary>
private MyControl _leftGridSearchObj;
/// <summary>
/// 返回行
/// </summary>
public DataRow[] DataRows = null;
public FrmDocumentSource(string ModuleCodel)
{
InitializeComponent();
this.UnionModuleCodel = ModuleCodel;
this.Disposed += new EventHandler(OnFrmModelLookUp_Disposed);
this.gcMain.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDown;
DataRow modelRow = MainImpl.GetSystemdllTab(this.UnionModuleCodel);//获取单个模块信息
if (modelRow == null)
{
MessageUtil.Show("模块编号[" + UnionModuleCodel + "],配置错误!\r\n该模块信息未找到!");
return;
}
this.SysModel = new ModuleModel(modelRow);// 系统模块实体对象
//设置关联模块(左侧)
this.InitLeftControls();
//设置主表
this.InitMainControls();
this.btnCancel.Click += btnCancel_Click;
this.btnOk.Click += btnOk_Click;
}
/// <summary>
/// 初始化左侧表
/// </summary>
public void InitLeftControls()
{
if (this.SysModel != null)
{
switch (this.SysModel.MenuType)
{
case 1: // 左侧为树节点
this.pl_left_top.Visible = false;
this.gridLeft.Visible = false;
this.treeLeft.Visible = true;
this.InitlizeSpiltLocation();//设置分割条位置
this.SetLeftTreeView();//设置左侧树数据
this.LeftTreeViewEx = this.treeLeft;
break;
case 2: // 左侧为表格
this.gridLeft.Visible = true;
this.treeLeft.Visible = false;
this.InitlizeSpiltLocation();
this.SetLeftGridView();
this.LeftGridEx = this.gridLeft;
break;
case 3: // 左侧为空
default:
this.splitMain.PanelVisibility = SplitPanelVisibility.Panel2;
break;
}
}
else
{
this.splitMain.PanelVisibility = SplitPanelVisibility.Panel2;
}
}
/// <summary>
/// 初始化主表
/// </summary>
public void InitMainControls()
{
//设置上方条件
this.pl_top.Controls.Clear();
DataTable queryTable = BaseModuleImpl.GetCustomQueryFields(this.SysModel.CondKey);//加载自定义配置字段
this.SearchObj = new MyControl(this.SysModel.MenuSql, this.gcMain, LeftTreeViewEx, LeftGridEx);
this.SearchObj.ParentKeyField = this.ParentKeyField;
this.SearchObj.ParentUnionField = this.ParentUnionField;
this.SearchObj.OnDataSourceBindCallBack += new EventHandler(OnFirstPageDataSourceBindCallBack);
this.SearchObj.OnSearchAfterCallBack += new EventHandler(OnSearchAfterd);
this.pl_top.Height = 0;
this.pl_top.Height = this.SearchObj.InitSearchControl(queryTable, this.pl_top);
//创建表格列
DataTable _gridColumns = BaseModuleImpl.GetBaseGridColumns(this.UnionModuleCodel);
this.gcMain.SetReadOnlyColumns(_gridColumns, GridCustomColumnStruct.BaseMainGridView + this.SysModel.FormKey);
//this.SetCustomColumns();
//设置复选框
GridDragGrid.GridAddCheckBox(gcMain.GridView);
//右键
DataTable dt = BaseModuleImpl.GetBaseGridRightMenus(this.UnionModuleCodel);
string[] obj = new string[]{
"",
ERPInfo.Instance.UserId,
ERPInfo.Instance.UserName,
"3",
this.UnionModuleCodel,
}; ;
DynamicModel dynamicModel = new DynamicModel(obj);
this.gcMain.SetGridRightMenus(dt, dynamicModel, this.OnGridViewRightCallBack, this.SearchObj, null);
//this.gcMain.GridView.OptionsView.ColumnAutoWidth = true;
}
/// <summary>
/// 查询后清空复选框勾选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnSearchAfterd(object sender, EventArgs e)
{
this.gcMain.GridView.ClearSelection();
}
/// <summary>
/// 初始化后默认查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnFirstPageDataSourceBindCallBack(object sender, EventArgs e)
{
//打开时默认选中第一条
if (LeftTreeViewEx != null && LeftTreeViewEx.TreeView.Nodes.Count > 0)
{
LeftTreeViewEx.TreeView.SelectedNode = LeftTreeViewEx.TreeView.Nodes[0];
LeftTreeViewEx.TreeView.Nodes[0].Expand();
}
else if (LeftGridEx != null)
{
LeftGridEx.GridView.SelectRow(0);
}
this.SearchObj.SearchGrid();
}
#region 设置分割条位置
/// <summary>
/// <para>说明:设置分割条位置</para>
/// <para>创建人:王一帆</para>
/// <para>创建日期:2021-07-21 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
protected void InitlizeSpiltLocation()
{
try
{
string width = IniHelper.Read(string.Format("bill_Special_Width_{0}", this.UnionModuleCodel));//通过Key获取Value值
if (!string.IsNullOrEmpty(width))
{
this.splitMain.SplitterPosition = Convert.ToInt32(width);
}
}
catch (Exception ex)
{
LogHelper.Instance.WriteError(ex);
MessageUtil.Show(ex);
}
}
#endregion
#region 保存表格属性
/// <summary>
/// <para>说明:保存表格属性</para>
/// <para>创建人:王一帆</para>
/// <para>创建日期:2021-07-20 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected virtual void SaveItemClick(object sender, EventArgs e)
{
DataRow rowItem = null;
bool success = BaseImpl.HasExistsDataRow(ResourceKeys.SettingTableName, "formKey", CurrentOperColumnKey, string.Format(" and OperatorId='{0}' ", ERPInfo.Instance.UserId), out rowItem);
if (!success)
{
MessageUtil.Show(ResourceKeys.NotFoundSettingColumnTable);
return;
}
if (MessageUtil.Show(ResourceKeys.IsSaveColumn, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
try
{
if (rowItem == null)
{
success = this.gcMain.GridView.SaveCustomColumnToDatabase(this.CurrentOperColumnKey);
}
else
{
success = this.gcMain.GridView.UpdateCustomColumnToDatabase(this.CurrentOperColumnKey);
}
MessageUtil.Show(success ? ResourceKeys.SaveSuccess : ResourceKeys.SaveFault);
}
catch (SqlException sex)
{
if (sex.Message.Contains(ResourceKeys.FieldLenIsShort) &&
MessageUtil.Show(ResourceKeys.SaveFault + "\r\n" + ResourceKeys.CanUpgradeTable, MessageBoxButtons.YesNo) == DialogResult.Yes &&
BaseImpl.UpdateTableField(ResourceKeys.SettingTableName, "formKey", "varchar(200)"))
{
MessageUtil.Show(ResourceKeys.UpdateSuccess);
}
}
catch (Exception ex)
{
LogHelper.Instance.WriteError(ex);
LogUtil.WriteError("保存表格属性出错", ex);
MessageUtil.Show(ex);
}
}
}
#endregion
#region 加载自定义列
/// <summary>
/// <para>说明:加载自定义列</para>
/// <para>创建人:王一帆</para>
/// <para>创建日期:2019-9-16 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
private void SetCustomColumns()
{
if (string.IsNullOrEmpty(this.CurrentOperColumnKey) || !BaseImpl.HasExistsTable(ResourceKeys.SettingTableName)) return;
DataTable customTable = this.gcMain.GridView.GetCustomColumnByDatabase(this.CurrentOperColumnKey);
if (customTable != null && customTable.Rows.Count > 0)
{
foreach (DataRow rowItem in customTable.Rows)
{
string fieldName = rowItem["fieldName"] + "";
int fieldWidth = Convert.ToInt32(rowItem["fieldWidth"]);
int index = Convert.ToInt32(rowItem["orderid"]);
bool visible = "1".Equals(rowItem["isVisible"] + "") && fieldWidth > 0;
bool isFix = "True".Equals(rowItem["IfFixColumn"] + "", StringComparison.OrdinalIgnoreCase);
GridColumn gridColumn = this.gcMain.GridView.Columns.ColumnByName(fieldName);
if (gridColumn != null && gridColumn.Visible && gridColumn.Width > 0)
{
GridColumn colObj = this.gcMain.GridView.Columns[fieldName];
colObj.VisibleIndex = index;
colObj.Width = fieldWidth;
colObj.Visible = visible;
colObj.Fixed = isFix ? FixedStyle.Left : FixedStyle.None;
}
}
}
}
#endregion
#region 刷新左侧树数据
/// <summary>
/// <para>说明:设置左侧树数据</para>
/// <para>创建人:王一帆</para>
/// <para>创建日期:2021-07-21 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
protected void SetLeftTreeView()
{
this.LeftRowObj = BaseModuleImpl.GetBaseLeftTreeField(this.UnionModuleCodel);//获取表格左侧树字段
if (this.LeftRowObj != null)
{
//this.ModuleGridObj.ParentUnionField = this.LeftRowObj["fieldname"] + "";
this.ParentUnionField = this.LeftRowObj["fieldname"] + "";
this.ParentKeyField = this.LeftRowObj["fieldsqlid"] + "";
this.treeLeft.TreeNodeKeyField = this.LeftRowObj["fieldsqlid"] + "";
this.treeLeft.TreeNodeTextField = this.LeftRowObj["fieldsqlname"] + "";
this.treeLeft.TreeNodeSelectAfter += new TreeViewEventHandler(OnTreeLeftTreeNodeSelected);
this.treeLeft.BindTreeView(BaseImpl.GetDataTableResult(this.LeftRowObj["fieldsql"] + ""));
}
else
{
// 根据模块上配置的sql加载树
if (this.SysModel != null && !string.IsNullOrEmpty(this.SysModel.TreeSQL))
{
this.ParentUnionField = "SpeciesNo";
this.ParentKeyField = "SpeciesNo";
this.treeLeft.TreeNodeKeyField = "SpeciesNo";
this.treeLeft.TreeNodeTextField = "SpeciesName";
this.treeLeft.TreeNodeSelectAfter += new TreeViewEventHandler(OnTreeLeftTreeNodeSelected);
this.treeLeft.BindTreeView(BaseImpl.GetDataTableResult(this.SysModel.TreeSQL));
}
}
}
/// <summary>
/// <para>说明:树节点点击后刷新数据</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-10-26 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="TreeViewEventArgs"/> instance containing the event data.</param>
protected void OnTreeLeftTreeNodeSelected(object sender, TreeViewEventArgs e)
{
string where = string.Format(" and {0} like '{1}%' ", this.treeLeft.TreeNodeKeyField, e.Node.Name);
string sqlValue = ReplaceHelper.IsProcedure(this.SysModel.MenuSql) ?
ReplaceHelper.ReplaceTreeViewParentKeyCond(this.SysModel.MenuSql, treeLeft.GetSelectNodeValue()) :
ReplaceHelper.ReplaceWhereCond(this.SysModel.MenuSql) + where;
//this.gcMain.gridControl.DataSource = MainImpl.GetDataTableResult(sqlValue);
this.SearchObj.SearchGrid(sqlValue);
this.gcMain.GridView.ClearSelection();
}
#region 设置左侧表格数据
/// <summary>
/// <para>说明:设置左侧表格数据</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-10-25 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
protected void SetLeftGridView()
{
this.LeftRowObj = BaseModuleImpl.GetBaseLeftTreeField(UnionModuleCodel);//获取表格左侧树字段
if (this.LeftRowObj != null)
{
DataTable dtTable = BaseModuleImpl.GetBaseLeftGridField(UnionModuleCodel);
DataTable dtColorTable = BaseModuleImpl.GetBaseGridRowColors(this.LeftRowObj["fieldKey"] + "");
this.gridLeft.GridView.RowClick += new DevExpress.XtraGrid.Views.Grid.RowClickEventHandler(OnGridViewRowClick);
this.gridLeft.SetGridRowColors(dtColorTable);
this.gridLeft.SetReadOnlyColumns(dtTable, GridCustomColumnStruct.BaseLeftGridView + UnionModuleCodel);
this.ParentUnionField = this.LeftRowObj["fieldname"] + "";
this.ParentKeyField = this.LeftRowObj["fieldsqlid"] + "";
//this.ModuleGridObj.ParentKeyField = this.LeftRowObj["fieldsqlid"] + "";
//this.ModuleGridObj.ParentUnionField = this.LeftRowObj["fieldname"] + "";
DataTable table = BaseModuleImpl.GetCustomQueryFields(this.LeftRowObj["fieldKey"] + "");
string searchSql = this.LeftRowObj["fieldsql"] + "";
if (table != null && table.Rows.Count > 0)
{
// 自定义查询条件
this._leftGridSearchObj = new MyControl(searchSql, this.gridLeft);
this.pl_left_top.Height = this._leftGridSearchObj.InitSearchControl(table, this.pl_left_top);
this._leftGridSearchObj.OnDataSourceBindCallBack += new EventHandler(OnLeftControlObjDataSourceBindCallBack);
}
else
{
DataTable leftGridCondtionTable = BaseImpl.GetDataTableResult(searchSql);
this.pl_left_top.Visible = false;
this.gridLeft.GridControl.DataSource = leftGridCondtionTable;
}
}
}
#endregion
#region 刷新右侧表格数据
/// <summary>
/// <para>说明:刷新右侧表格数据</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-03-19 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DevExpress.Data.SelectionChangedEventArgs"/> instance containing the event data.</param>
protected void OnGridViewRowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
{
try
{
string sqlValue = ReplaceHelper.ReplaceWhereCond(this.SysModel.MenuSql);
if (this.gridLeft.Visible)
{
DataRow rowitem = this.gridLeft.GridView.GetFocusedDataRow();
sqlValue = this._leftGridSearchObj != null ? this._leftGridSearchObj.ReplaceParentControlValue(sqlValue) : sqlValue;
sqlValue = ReplaceHelper.ReplaceRowParam(rowitem, sqlValue);
}
this.SearchObj.SearchGrid(sqlValue);
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
#endregion
#endregion
#region 事件
/// <summary>
/// <para>说明:数据绑定完成后加载数据</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected void OnLeftControlObjDataSourceBindCallBack(object sender, EventArgs e)
{
try
{
this._leftGridSearchObj.SearchGrid();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
#region 确定
/// <summary>
/// <para>说明:确定</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-03-20 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void btnOk_Click(object sender, EventArgs e)
{
this.DataRows = gcMain.GetViewFocusedDataRows();
if (DataRows.Length > 0)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
this.Close();
}
#endregion
#region 取消
/// <summary>
/// <para>说明:取消</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-03-20 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
#endregion
#region 释放时清理数据源
/// <summary>
/// <para>说明:释放时清理数据源</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-03-16 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
private void OnFrmModelLookUp_Disposed(object sender, EventArgs e)
{
GC.Collect();
}
#endregion
#endregion
/// <summary>
/// <para>说明:右键菜单执行后刷新数据,刷新规则按照上一次查询条件刷新</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-11-02 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected void OnGridViewRightCallBack(object sender, EventArgs e)
{
try
{
this.SearchObj.SearchLastGrid();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
}
}