using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Lskj.Business.Impl;
using Lskj.Control.Model;
using Lskj.Core;
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.Control
{
public partial class FrmSetExport : Form
{
#region 公共变量
///
/// 外部表格
///
public GridControlEx SourceGridEx;
///
/// CustomColumKey
///
public string CustomColumKey = string.Empty;
#endregion
#region 私有变量
///
/// 左侧数据源
///
private DataTable leftDataSource = new DataTable();
///
/// 右侧数据源
///
private DataTable rightDataSource = new DataTable();
#endregion
public FrmSetExport(GridControlEx sourceGrid)
{
InitializeComponent();
this.SourceGridEx = sourceGrid;
this.Load += new EventHandler(OnFrmSetExport_Load);
}
#region 事件
///
/// 窗体初始化
///
///
///
private void OnFrmSetExport_Load(object sender, EventArgs e)
{
InitRightGridColumns();
InitRightGridData();
InitLeftGridColumns();
InitLeftGridData();
GridDragGrid dragLeftGrid = new GridDragGrid(this.leftGrid.GridView, this.rightGrid.GridView);
dragLeftGrid.OnDragComplete += new GridFragGridCompleteEventHandler(OnDragMainGridCompleted);
GridDragGrid dragRightGrid = new GridDragGrid(this.rightGrid.GridView, this.leftGrid.GridView);
dragRightGrid.OnDragComplete += new GridFragGridCompleteEventHandler(OnDragMainGridCompleted);
this.leftGrid.GridView.DoubleClick += new EventHandler(OnGridView_DoubleClick);
this.rightGrid.GridView.DoubleClick += new EventHandler(OnGridView_DoubleClick);
this.saveBtn.Click += new EventHandler(OnSaveBtn_Click);
this.SizeChanged += new EventHandler(OnFrmSetExport_SizeChanged);
}
///
/// 拖拽完成时
///
///
///
private void OnDragMainGridCompleted(object sender, GridDragGridArgs e)
{
if (e.SourceGridViewObj == this.leftGrid.GridView)
{
foreach (DataRow selectRow in e.GridViewSelectRows)
{
DataRow newRow = this.rightDataSource.NewRow();
newRow.ItemArray = selectRow.ItemArray;
this.rightDataSource.Rows.Add(newRow);
this.leftDataSource.Rows.Remove(selectRow);
}
}
else if (e.SourceGridViewObj == this.rightGrid.GridView)
{
foreach (DataRow selectRow in e.GridViewSelectRows)
{
DataRow newRow = this.leftDataSource.NewRow();
newRow.ItemArray = selectRow.ItemArray;
this.leftDataSource.Rows.Add(newRow);
this.rightDataSource.Rows.Remove(selectRow);
}
}
}
///
/// 双击行移动数据
///
///
///
private void OnGridView_DoubleClick(object sender, EventArgs e)
{
GridView gridView = (GridView)sender;
int[] selectRowsIndex = gridView.GetSelectedRows();
if (gridView == this.leftGrid.GridView)
{
foreach (int index in selectRowsIndex)
{
DataRow selectRow = gridView.GetDataRow(index);
DataRow newRow = this.rightDataSource.NewRow();
newRow.ItemArray = selectRow.ItemArray;
this.rightDataSource.Rows.Add(newRow);
this.leftDataSource.Rows.Remove(selectRow);
}
}
else if (gridView == this.rightGrid.GridView)
{
foreach (int index in selectRowsIndex)
{
DataRow selectRow = gridView.GetDataRow(index);
DataRow newRow = this.leftDataSource.NewRow();
newRow.ItemArray = selectRow.ItemArray;
this.leftDataSource.Rows.Add(newRow);
this.rightDataSource.Rows.Remove(selectRow);
}
}
}
///
/// 窗体大小改变时
///
///
///
private void OnFrmSetExport_SizeChanged(object sender, EventArgs e)
{
this.leftGroup.Width = this.Width / 2;
}
///
/// 保存按钮点击
///
///
///
private void OnSaveBtn_Click(object sender, EventArgs e)
{
DataRow rowItem = null;
bool success = BaseImpl.HasExistsDataRow(ResourceKeys.SettingExportTableName, "formKey", CustomColumKey.ToString(), string.Format(" and OperatorId='{0}' ", ERPInfo.Instance.UserId), out rowItem);
if (!success)
{
MessageUtil.Show("未开启导出列设置功能,请联系朗速!");
return;
}
if (MessageUtil.Show(ResourceKeys.IsSaveColumn, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
try
{
if (rowItem == null)
{
success = this.SaveExportColumnToDatabase(this.CustomColumKey);
}
else
{
success = this.UpdateExportColumnToDatabase(this.CustomColumKey);
}
MessageUtil.Show(success ? ResourceKeys.SaveSuccess : ResourceKeys.SaveFault);
}
catch (Exception ex)
{
LogHelper.Instance.WriteError(ex);
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
}
#endregion
#region 方法
///
/// 初始化左侧表格列
///
private void InitLeftGridColumns()
{
GridColumn systemFieldNameCol = new GridColumn();
systemFieldNameCol.Caption = "系统名称";
systemFieldNameCol.FieldName = "fieldName";
systemFieldNameCol.Visible = true;
systemFieldNameCol.VisibleIndex = 1;
GridColumn userFieldNameCol = new GridColumn();
userFieldNameCol.Caption = "用户名称";
userFieldNameCol.FieldName = "userName";
userFieldNameCol.Visible = true;
userFieldNameCol.VisibleIndex = 2;
this.leftGrid.Controls.Remove(this.leftGrid.plbuttomObj);
this.leftGrid.GridView.OptionsBehavior.Editable = false;
this.leftGrid.GridView.Columns.AddRange(new GridColumn[] { systemFieldNameCol, userFieldNameCol });
}
///
/// 初始化左侧表格列
///
private void InitRightGridColumns()
{
GridColumn systemFieldNameCol = new GridColumn();
systemFieldNameCol.Caption = "系统名称";
systemFieldNameCol.FieldName = "fieldName";
systemFieldNameCol.Visible = true;
systemFieldNameCol.VisibleIndex = 1;
GridColumn userFieldNameCol = new GridColumn();
userFieldNameCol.Caption = "用户名称";
userFieldNameCol.FieldName = "userName";
userFieldNameCol.Visible = true;
userFieldNameCol.VisibleIndex = 2;
this.rightGrid.Controls.Remove(this.rightGrid.plbuttomObj);
this.rightGrid.GridView.OptionsBehavior.Editable = false;
this.rightGrid.GridView.Columns.AddRange(new GridColumn[] { systemFieldNameCol, userFieldNameCol });
}
///
/// 初始化左侧数据源
///
private void InitLeftGridData()
{
this.leftDataSource.Columns.Add("fieldName");
this.leftDataSource.Columns.Add("userName");
foreach (GridColumn column in this.SourceGridEx.GridView.Columns)
{
DataRow dataRow = this.leftDataSource.NewRow();
dataRow["fieldName"] = column.FieldName;
dataRow["userName"] = column.Caption;
this.leftDataSource.Rows.Add(dataRow);
}
//this.leftDataSource = this.leftDataSource.Select().Where(n => this.rightDataSource.Select(string.Format("fieldName='{0}'", n["fieldName"] + "")).Length == 0).CopyToDataTable();
foreach (DataRow row in this.rightDataSource.Rows)
{
DataRow[] selectRows = this.leftDataSource.Select().Where(n => (n["fieldName"] + "").Equals(row["fieldName"] + "")).ToArray();
if (selectRows.Length > 0)
{
DataRow selectRow = selectRows[0];
this.leftDataSource.Rows.Remove(selectRow);
}
}
this.leftGrid.gridControl.DataSource = this.leftDataSource;
}
///
/// 初始化右侧数据源
///
private void InitRightGridData()
{
string sqlstr = "select fieldName,userName from {0} where {1}='{2}' and isExport='1' and operatorid='{3}'";
sqlstr = string.Format(sqlstr, ResourceKeys.SettingExportTableName, "formkey", this.CustomColumKey,ERPInfo.Instance.UserId);
this.rightDataSource = SqlHelper.ExecuteDataTable(sqlstr);
this.rightGrid.gridControl.DataSource = this.rightDataSource;
}
///
/// 说明:保存导出配置列
/// 创建人:唐德馨
/// 创建日期:2022-10-25
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The grid view.
/// The key.
/// true if XXXX, false otherwise.
public bool SaveExportColumnToDatabase(string key)
{
StringBuilder sBuilder = new StringBuilder();
foreach (DataRow row in this.rightDataSource.Rows)
{
string tempSql = string.Format(@"insert into {6}(formkey,fieldname,username,operatorid,operatorName,operatedate,isExport) values('{0}','{1}','{2}','{3}','{4}',getdate(),'{5}');",
key, row["fieldName"] + "", row["userName"] + "", ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, 1, ResourceKeys.SettingExportTableName);
sBuilder.Append(tempSql);
}
foreach (DataRow row in this.leftDataSource.Rows)
{
string tempSql = string.Format(@"insert into {6}(formkey,fieldname,username,operatorid,operatorName,operatedate,isExport) values('{0}','{1}','{2}','{3}','{4}',getdate(),'{5}');",
key, row["fieldName"] + "", row["userName"] + "", ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, 0, ResourceKeys.SettingExportTableName);
sBuilder.Append(tempSql);
}
return BaseImpl.ExecSqlValue(sBuilder.ToString()) > 0;
}
///
/// 说明:保存导出配置列
/// 创建人:唐德馨
/// 创建日期:2022-10-25
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The grid view.
/// The key.
/// true if XXXX, false otherwise.
public bool UpdateExportColumnToDatabase(string key)
{
StringBuilder sBuilder = new StringBuilder();
string sqlValue = string.Format("select * from {0} where formKey='{1}' and OperatorId='{2}'", ResourceKeys.SettingExportTableName, key, ERPInfo.Instance.UserId);
DataTable exportTable = SqlHelper.ExecuteDataTable(sqlValue);
foreach (DataRow row in this.rightDataSource.Rows)
{
string tempSql = string.Empty;
DataRow[] dataRows = exportTable.Select().Where(n => (n["fieldName"] + "").Equals(row["fieldName"] + "")).ToArray();
if (dataRows.Length == 0)
{
tempSql = string.Format(@"insert into {6}(formkey,fieldname,username,operatorid,operatorName,operatedate,isExport) values('{0}','{1}','{2}','{3}','{4}',getdate(),'{5}');",
key, row["fieldName"] + "", row["userName"] + "", ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, 1, ResourceKeys.SettingExportTableName);
sBuilder.Append(tempSql);
}
else
{
tempSql = string.Format(@"update {4} set isExport= '{0}',operatedate = getdate() where formkey='{1}' and fieldname='{2}' and operatorid='{3}';",
1, key, row["fieldName"] + "", ERPInfo.Instance.UserId, ResourceKeys.SettingExportTableName);
sBuilder.Append(tempSql);
}
}
foreach (DataRow row in this.leftDataSource.Rows)
{
string tempSql = string.Empty;
DataRow[] dataRows = exportTable.Select().Where(n => (n["fieldName"] + "").Equals(row["fieldName"] + "")).ToArray();
if (dataRows.Length == 0)
{
tempSql = string.Format(@"insert into {6}(formkey,fieldname,username,operatorid,operatorName,operatedate,isExport) values('{0}','{1}','{2}','{3}','{4}',getdate(),'{5}');",
key, row["fieldName"] + "", row["userName"] + "", ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, 0, ResourceKeys.SettingExportTableName);
sBuilder.Append(tempSql);
}
else
{
tempSql = string.Format(@"update {4} set isExport= '{0}',operatedate = getdate() where formkey='{1}' and fieldname='{2}' and operatorid='{3}';",
0, key, row["fieldName"] + "", ERPInfo.Instance.UserId, ResourceKeys.SettingExportTableName);
sBuilder.Append(tempSql);
}
}
return BaseImpl.ExecSqlValue(sBuilder.ToString()) > 0;
}
#endregion
}
}