Files
lserp_cs_6.0/插件库/Lskj.PubControlDetail/FrmMain.cs
T
2026-07-02 10:11:39 +00:00

387 lines
18 KiB
C#

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.PubControlDetail
{
public partial class FrmMain : Form
{
public DynamicControlDetailModel Model;
public MainDataModel DataModel;
public FrmMain()
{
InitializeComponent();
this.Load += OnFrmMainLoad;
}
/// <summary>
/// 初始化时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnFrmMainLoad(object sender, EventArgs e)
{
GetControlData(Model);
InitSpiltLocation();
InitControlView();
InitControlEvents();
SetControlData();
}
/// <summary>
/// 获取数据
/// </summary>
public void GetControlData(DynamicControlDetailModel mainModel)
{
DataModel = new MainDataModel();
DataModel.MainSysModel = mainModel;
DataRow topSysRow = MainImpl.GetSystemdllTab(DataModel.MainSysModel.ModuleCode);
DataModel.MainModuleModel = new ModuleModel(topSysRow);
DataModel.MainModuleModel.ParmaryKey = BaseImpl.GetBasePrimaryKey(DataModel.MainSysModel.ModuleCode);
DataModel.MainGridColumns = BaseModuleImpl.GetBaseGridColumns(DataModel.MainSysModel.ModuleCode);
DataModel.MainRightMenus = BaseModuleImpl.GetBaseGridRightMenus(DataModel.MainModuleModel.FormKey);
DataModel.MainRowColors = BaseModuleImpl.GetBaseGridRowColors(DataModel.MainModuleModel.FormKey);
DataModel.MainSearchTab = BaseModuleImpl.GetCustomQueryFields(DataModel.MainModuleModel.CondKey);
DataModel.MainFixedSearchTab = BaseModuleImpl.GetFixedQueryFields(DataModel.MainSysModel.ModuleCode);//固定查询条件
DataModel.MainSchemesTable = BaseModuleImpl.GetSchemesList(DataModel.MainSysModel.ModuleId);
DataModel.MainSearchObj = new MyControl(DataModel.MainModuleModel.MenuSql, gcMain);
DataModel.MainSearchObj.Model = DataModel.MainSysModel;
DataTable detailsTab = BaseModuleImpl.GetBaseDetailPages(DataModel.MainSysModel.ModuleCode);
if (detailsTab != null && detailsTab.Rows.Count > 0)
{
string moduleCode = detailsTab.Rows[0]["unionModule"] + "";
DataModel.DetailDetailModel = new GridDetailModel(detailsTab.Rows[0], null, GridCustomColumnStruct.BaseDetailGridView);
DataModel.DetailSysModel = new DynamicModel(new string[] { DataModel.MainSysModel.FormText, DataModel.MainSysModel.UserId, DataModel.MainSysModel.UserName, DataModel.MainSysModel.Privilege, moduleCode });
DataRow sysRow = MainImpl.GetSystemdllTab(DataModel.DetailSysModel.ModuleCode);
DataModel.DetailModuleModel = new ModuleModel(sysRow);
DataModel.DetailDetailModel.SystemModel = DataModel.DetailModuleModel;
DataModel.DetailModuleModel.ParmaryKey = BaseImpl.GetBasePrimaryKey(DataModel.DetailSysModel.ModuleCode);
DataModel.DetailControlTab = BaseModuleImpl.GetControlLocation(DataModel.DetailModuleModel.FormKey, DataModel.DetailModuleModel.ModeCode);
DataModel.DetailControlObj = new MyControl()
{
SystemModel = DataModel.DetailModuleModel,
OtherParams = DataModel.DetailSysModel.OtherParams(),
Model = DataModel.DetailSysModel
};
DataModel.DetailControlObj.SetGridLeft(gcMain);
}
if (detailsTab != null && detailsTab.Rows.Count > 1)
{
string moduleCode = detailsTab.Rows[1]["unionModule"] + "";
DataModel.BottomDetailModel = new GridDetailModel(detailsTab.Rows[1], null, GridCustomColumnStruct.BaseDetailGridView);
DataModel.BottomSysModel = new DynamicModel(new string[] { DataModel.MainSysModel.FormText, DataModel.MainSysModel.UserId, DataModel.MainSysModel.UserName, DataModel.MainSysModel.Privilege, moduleCode });
DataRow sysRow = MainImpl.GetSystemdllTab(DataModel.BottomSysModel.ModuleCode);
DataModel.BottomModuleModel = new ModuleModel(sysRow);
DataModel.BottomDetailModel.SystemModel = DataModel.BottomModuleModel;
DataModel.BottomModuleModel.ParmaryKey = BaseImpl.GetBasePrimaryKey(DataModel.BottomSysModel.ModuleCode);
DataModel.BottomGridColumns = BaseModuleImpl.GetBaseGridColumns(DataModel.BottomSysModel.ModuleCode);
DataModel.BottomRightMenus = BaseModuleImpl.GetBaseGridRightMenus(DataModel.BottomModuleModel.FormKey);
DataModel.BottomRowColors = BaseModuleImpl.GetBaseGridRowColors(DataModel.BottomModuleModel.FormKey);
}
}
/// <summary>
/// 初始化位置
/// </summary>
private void InitSpiltLocation()
{
try
{
string splitMainWidth = IniHelper.Read(string.Format("ControlDetail_SplitMain_{0}", DataModel.MainSysModel.ModuleCode));
if (!string.IsNullOrEmpty(splitMainWidth))
{
splitMainControl.SplitterPosition = Convert.ToInt32(splitMainWidth);
}
string splitLeftWidth = IniHelper.Read(string.Format("ControlDetail_SplitLeft_{0}", DataModel.MainSysModel.ModuleCode));
if (!string.IsNullOrEmpty(splitLeftWidth))
{
splitLeftControl.SplitterPosition = Convert.ToInt32(splitLeftWidth);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// 初始化控件
/// </summary>
public void InitControlView()
{
//搜索控件
bool fixedQuery = DataModel.MainSearchTab == null || DataModel.MainSearchTab.Rows.Count == 0;
if (!fixedQuery)
{
pl_top_fix_search.Visible = false;
pl_mainSearch.Height = DataModel.MainSearchObj.InitSearchControl(DataModel.MainSearchTab, this.pl_search, true, DataModel.MainSchemesTable, DataModel.MainSysModel);
pl_search.Width = DataModel.MainSearchObj.ParentPanel.Controls.Cast<System.Windows.Forms.Control>().Max(n => n.Location.X + n.Width) + 5;
}
else
{
pl_top_fix_search.Visible = true;
cbField.DisplayMember = "fieldText";
cbField.ValueMember = "fieldName";
cbField.DataSource = DataModel.MainFixedSearchTab;
DataModel.MainSearchObj.InitDefaultSearchControl(DataModel.MainFixedSearchTab, pl_top_fix_search);
}
//左侧表格
gcMain.Model = DataModel.MainSysModel;
gcMain.SetEditColumns(DataModel.MainGridColumns, GridCustomColumnStruct.BaseMainGridView + DataModel.MainModuleModel.FormKey);
gcMain.SetGridRightMenus(DataModel.MainRightMenus, DataModel.MainSysModel);
gcMain.SetGridRowColors(DataModel.MainRowColors);
//右侧控件
DataModel.DetailControlObj.InitControl(DataModel.DetailControlTab, pl_main, BaseModuleImpl.GetAddGroupData(DataModel.DetailModuleModel.FormKey), true);
//下方表格
gcBottom.Model = DataModel.BottomSysModel;
gcBottom.SetEditColumns(DataModel.MainGridColumns, GridCustomColumnStruct.BaseMainGridView + DataModel.BottomModuleModel.FormKey);
gcBottom.SetGridRightMenus(DataModel.BottomRightMenus, DataModel.BottomSysModel);
gcBottom.SetGridRowColors(DataModel.BottomRowColors);
}
/// <summary>
/// 绑定控件事件
/// </summary>
public void InitControlEvents()
{
splitMainControl.SplitterPositionChanged += OnSplitContainerPositionChanged;
splitLeftControl.SplitterPositionChanged += OnSplitContainerPositionChanged;
gcMain.GridView.RowCellClick += OnGridViewRowCellClick;
GridDragControl dragGrid = new GridDragControl(gcMain.GridView, pl_main);
dragGrid.OnDragComplete += OnDragGridOnDragComplete;
btnClear.Click += OnBtnClearClick;
btnAdd.Click += OnBtnAddClick;
}
/// <summary>
/// 设置数据
/// </summary>
public void SetControlData()
{
}
/// <summary>
/// 分隔条位置改变时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnSplitContainerPositionChanged(object sender, EventArgs e)
{
try
{
if (DataModel.MainSysModel != null)
{
if (sender == splitMainControl)
{
IniHelper.Write(string.Format("ControlDetail_SplitMain_{0}", DataModel.MainSysModel.ModuleCode), splitMainControl.SplitterPosition + "");
}
if (sender == splitLeftControl)
{
IniHelper.Write(string.Format("ControlDetail_SplitLeft_{0}", DataModel.MainSysModel.ModuleCode), splitLeftControl.SplitterPosition + "");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// 表格行点击时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnGridViewRowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
{
try
{
DataRow rowItem = gcMain.GridView.GetFocusedDataRow();
string searchSql = GetSearchSql(DataModel.BottomDetailModel, rowItem);
gcBottom.SetGridViewDataSource(BaseImpl.GetDataTableResult(searchSql));
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
/// <summary>
/// 表格拖拽到控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnDragGridOnDragComplete(object sender, GridDragControlArgs e)
{
try
{
DataRow dataRow = e.SourceGridViewObj.GetFocusedDataRow();
if (dataRow != null)
{
DataModel.DetailControlObj.SetAllControlValue(dataRow);
}
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
/// <summary>
/// 修改数据
/// </summary>
/// <returns></returns>
public void UpdateControlRecord()
{
if (DataModel.DetailControlObj.VerifyNull())
{
string primaryKey = DataModel.DetailControlObj.GetControlValue(DataModel.DetailModuleModel.ParmaryKey);
string PrimaryValue = "";
if (DataModel.DetailControlObj.SaveBaseModuleData(DataModel.DetailSysModel.ModuleCode, DataModel.DetailModuleModel.MenuTable, DataModel.DetailModuleModel.ParmaryKey, ref PrimaryValue, false, "", true, DataModel.DetailModuleModel.NewVer))
{
SetUpdateData(primaryKey, PrimaryValue);
}
}
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="isTip"></param>
/// <returns></returns>
public void AddControlRecord(bool isTip = true)
{
if (DataModel.DetailControlObj.VerifyNull())
{
string primaryKey = DataModel.DetailControlObj.GetControlValue(DataModel.DetailModuleModel.ParmaryKey);
if (DataModel.DetailControlObj.SaveBaseModuleData(DataModel.DetailSysModel.ModuleCode, DataModel.DetailModuleModel.MenuTable, DataModel.DetailModuleModel.ParmaryKey, ref primaryKey, true, "", isTip, DataModel.DetailModuleModel.NewVer))
{
string PrimaryValue = DataModel.MainModuleModel.NewVer == 0 ? DataModel.DetailControlObj.GetControlValue(DataModel.DetailModuleModel.ParmaryKey) : primaryKey;
if (primaryKey.Contains("LskjIdebtity"))
{
PrimaryValue = primaryKey.Replace("LskjIdebtity", "");
}
DataModel.DetailControlObj.SetControlValue(DataModel.DetailModuleModel.ParmaryKey, PrimaryValue);
DataRow rowItem = BaseImpl.GetDataRowResult(string.Format("select * from {0} where {1}='{2}'", DataModel.DetailModuleModel.MenuTable, DataModel.DetailModuleModel.ParmaryKey, PrimaryValue));
DataModel.DetailControlObj.CanExecControl = false;
DataModel.DetailControlObj.SetAllControlValue(rowItem);
DataModel.DetailControlObj.CanExecControl = true;
DataModel.DetailControlObj.SyncLocalModel();
SetUpdateData(primaryKey, PrimaryValue);//重新加载数据
OnGridViewRowCellClick(null, null);
}
}
}
/// <summary>
/// <para>说明:设置修改数据</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-11-09 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
private void SetUpdateData(string primaryKey, string PrimaryValue)
{
if (!string.IsNullOrWhiteSpace(PrimaryValue))
{
// 修改默认赋值
string sqlValue = ReplaceHelper.ReplaceWhereCond(DataModel.DetailModuleModel.MenuSql);
sqlValue = string.Format("{0} AND {1}='{2}'", sqlValue, primaryKey, PrimaryValue);
DataTable tableResult = BaseImpl.GetDataTableResult(sqlValue);
if (tableResult != null && tableResult.Rows.Count > 0)
{
DataRow rowItem = tableResult.Rows[0];
DataModel.DetailControlObj.CurrentData = rowItem;
DataModel.DetailControlObj.SetAllControlValue(rowItem);
// 同步本地数据,用于关闭时检查数据是否已修改.
DataModel.DetailControlObj.SyncLocalModel();
}
}
DataModel.DetailControlObj.CanExecControl = true;
}
/// <summary>
/// 保存按钮点击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnBtnClearClick(object sender, EventArgs e)
{
try
{
DataModel.DetailControlObj.ResetControlValue();
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
/// <summary>
/// 新增按钮点击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnBtnAddClick(object sender, EventArgs e)
{
btnClear.Enabled = false;
try
{
AddControlRecord();
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
btnClear.Enabled = true;
}
/// <summary>
/// 修改按钮点击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnBtnUpdateClick(object sender, EventArgs e)
{
try
{
UpdateControlRecord();
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
}
}
/// <summary>
/// 获取查询条件
/// </summary>
/// <param name="model"></param>
/// <param name="rowItem"></param>
/// <param name="isLeft"></param>
/// <returns></returns>
protected string GetSearchSql(GridDetailModel model, DataRow rowItem, bool isLeft = false)
{
string fieldName = !string.IsNullOrEmpty(model.UnionParentField) && rowItem.Table.Columns.Contains(model.UnionParentField) ? model.UnionParentField : DataModel.DetailModuleModel.ParmaryKey;
string sqlValue = model.IsReadOnly || model.IsChart ? model.DetailSql : model.SystemModel.MenuSql;
sqlValue = ReplaceHelper.ReplaceRowParam(rowItem, sqlValue);
DataRow row = gcMain.GridView.GetFocusedDataRow();
if (isLeft)
{
//先替换左侧选中行,在替换主表
sqlValue = ReplaceHelper.ReplaceRowParam(row, sqlValue);
}
if (ReplaceHelper.IsSelect(sqlValue) && !string.IsNullOrEmpty(model.UnionValue))
{
string Conditions = string.Empty;
//是左侧选中行,并且没有替换字段,则使用主表选中行
Conditions = isLeft ? rowItem.Table.Columns.Contains(fieldName) ? rowItem[fieldName] + "" : row[fieldName] + "" : rowItem[fieldName] + "";
sqlValue = ReplaceHelper.ReplaceWhereCond(sqlValue) + string.Format(" and {0}='{1}'", model.UnionValue, Conditions);
}
if (!string.IsNullOrEmpty(model.UnionCond))
{
string UnionCond = ReplaceHelper.ReplaceRowParam(rowItem, model.UnionCond);
sqlValue += UnionCond;
}
return sqlValue;
}
}
}