Files
lserp_cs_5.0/插件库/Lskj.PubPageModuleDetail/TabPageDetail.cs
T
2026-07-10 15:25:05 +08:00

468 lines
20 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lskj.Common;
using CommonLib;
using CommonLib.utils;
using DevExpress.XtraGrid.Columns;
using ExcelPlus;
using System.Data.SqlClient;
using Lskj.MyControl;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
namespace Lskj.PubPageModuleDetail
{
public partial class TabPageDetail : UserControl {
//模块ID
public string DLLCoid { get; set; }
//管理员Id
public string OperatorId { get; set; }
//管理员名称
public string OperatorName { get; set; }
//权限
public string Privilege { get; set; }
//窗体标题
public string FormText { get; set; }
//对象Id
public string objectId { get; set; }
//对象值
public string objectValue { get; set; }
public string dllSql { get; set; }
public string autoRefresh { get; set; }
/// <summary>
/// 是否报表
/// </summary>
public bool HasReport { get; set; }
private string addCond; // 添加条件
private string deleteCond; // 删除条件
private string modifyCond; // 修改条件
//主表数据
private DataTable dtSystemDlltab;
//主键数据
private DataTable idFiledKey;
private bool needModified;
/// <summary>
/// 条件颜色
/// </summary>
private DataTable dtSysColorList;
/// <summary>
/// DbDataAdapter
/// </summary>
private SqlDataAdapter da;
/// <summary>
/// 操作Dataset
/// </summary>
private DataSet ds = new DataSet();
public string pageText;
public TabPageDetail() {
InitializeComponent();
}
/// <summary>
/// 初始化数据
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e) {
dtSystemDlltab = CommonLib.SqlHelper.ExecuteDataSet("select * from P_systemDlltab where DLLCoid='" + DLLCoid + "'").Tables[0];
if (dtSystemDlltab != null && dtSystemDlltab.Rows.Count > 0) {
addCond = dtSystemDlltab.Rows[0]["addCond"] + "";
deleteCond = dtSystemDlltab.Rows[0]["deleteCond"] + "";
modifyCond = dtSystemDlltab.Rows[0]["modifyCond"].ToString();
needModified = dtSystemDlltab.Rows[0]["modifyEnable"].ToString() == "1";
bool addEnable = dtSystemDlltab.Rows[0]["addEnable"] + "" == "1";
bool deleteEnable = dtSystemDlltab.Rows[0]["deleteEnable"] + "" == "1";
bool importEnable = dtSystemDlltab.Rows[0]["importEnable"] + "" == "1";
bool exportEnable = dtSystemDlltab.Rows[0]["exportEnable"] + "" == "1";
bool searchEnable = dtSystemDlltab.Rows[0]["searchEnable"] + "" == "1";
btnAdd.Enabled = addEnable;
btnDelete.Enabled = deleteEnable;
btnImport.Enabled = importEnable;
btnExport.Enabled = exportEnable;
btnCondSearch.Enabled = searchEnable;
btnUpdate.Enabled = needModified;
btnSave.Enabled = needModified;
}
// 当前标签为报表
if (HasReport)
{
btnAdd.Visible = btnDelete.Visible = btnImport.Visible = btnUpdate.Visible = btnSave.Visible = false;
}
idFiledKey = SqlHelper.ExecuteDataSet("select top 1 * from P_systemwordbooktab where tab='" + DLLCoid + "' order by orderid").Tables[0];
GridMenu gm = new GridMenu(FormText, OperatorId, OperatorName, Privilege, DLLCoid, "", gridList.gridControl);
gm.DirId = dtSystemDlltab.Rows[0]["dirid"] + "";
gm.filedKey = idFiledKey.Rows[0]["fieldname"].ToString();
gm.BindGridMenuWQ(DLLCoid);
gm.OnGridMenuClickRefreshData += new EventHandler(gm_OnGridMenuClickRefreshData);
gridList.gridControl.DoubleClick += new EventHandler(gridControl_DoubleClick);
gridList.gridView.SelectionChanged += new DevExpress.Data.SelectionChangedEventHandler(gridView_SelectionChanged);
dtSysColorList = SqlHelper.ExecuteDataSet("select * from P_systemwordbookcolor where tab='" + DLLCoid + "' and isnull(useflag,0)=0 order by orderid").Tables[0];
gridList.gridView.RowStyle += new RowStyleEventHandler(gridView_RowStyle);
DataTable cbSearchFiled = SqlHelper.ExecuteDataSet("select * from P_systemwordbooktab where tab='" + DLLCoid + "' and ifSearch =0 and isnull(username1,'')<>''").Tables[0];
//加载筛选框
cbField.DataSource = cbSearchFiled;
cbField.DisplayMember = "username1";
cbField.ValueMember = "fieldname";
if (autoRefresh == "1") {
InitData(gridList.Sql);
}
}
/// <summary>
/// 执行右键菜单后刷新表格数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gm_OnGridMenuClickRefreshData(object sender, EventArgs e)
{
InitData(gridList.Sql);
}
#region 行颜色
private void gridView_RowStyle(object sender, RowStyleEventArgs e) {
if (e.RowHandle >= 0) {
DataRow row = gridList.gridView.GetDataRow(e.RowHandle);
string cond = string.Empty;
string color = string.Empty;
string forecolor = string.Empty;
foreach (DataRow dr in dtSysColorList.Rows) {
//string[] strSplit = dr["condition"].ToString().Split('=');
//if (strSplit.Length >= 2)
//{
// string filed = strSplit[0].Replace("{", "").Replace("}", "");
// string value = strSplit[1];
// if (value == row[filed] + "")
// {
// System.Drawing.Color color = System.Drawing.ColorTranslator.FromHtml(dr["backcolor"].ToString());
// System.Drawing.Color foreColor = System.Drawing.ColorTranslator.FromHtml(dr["forcecolor"].ToString());
// e.Appearance.BackColor = color;
// e.Appearance.ForeColor = foreColor;
// }
//}
List<string> condition = PubClass.GetParamValue(dr["condition"].ToString());
cond = dr["condition"].ToString();
foreach (string item in condition) {
string filedName = item.Replace("{", "").Replace("}", "");
cond = cond.Replace(item, row[filedName].ToString());
}
cond = PubClass.SqlToCode(cond);
color = dr["backcolor"] + "";
forecolor = dr["forcecolor"].ToString() + "";
string obj = Evaluator.Eval(cond) + "";
if (obj.IndexOf("true", StringComparison.OrdinalIgnoreCase) != -1) {
if (!string.IsNullOrEmpty(color)) {
System.Drawing.Color color1 = System.Drawing.ColorTranslator.FromHtml(color);
e.Appearance.BackColor = color1;
}
if (!string.IsNullOrEmpty(forecolor)) {
System.Drawing.Color foreColor = System.Drawing.ColorTranslator.FromHtml(forecolor);
e.Appearance.ForeColor = foreColor;
}
FontStyle fst = new FontStyle();
if (dr["ifBold"].ToString() == "1") {
fst = fst | FontStyle.Bold;
}
if (dr["ifItalic"].ToString() == "1") {
fst = fst | FontStyle.Italic;
}
if (dr["ifStrickOut"].ToString() == "1") {
fst = fst | FontStyle.Strikeout;
}
if (dr["ifUnderLine"].ToString() == "1") {
fst = fst | FontStyle.Underline;
}
e.Appearance.Font = new Font(e.Appearance.Font, fst);
}
}
}
}
#endregion
private void gridView_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
{
int[] rows = gridList.gridView.GetSelectedRows();
if (rows != null && rows.Length > 0)
{
DataRow row = gridList.gridView.GetDataRow(rows[0]);
bool isAdd = true;
bool isDelete = true;
bool isUpdate = true;
// 添加权限
if (!string.IsNullOrEmpty(addCond))
{
string tempCond = PubClass.ReqSqlWhere(row, PubClass.ReqSqlParam(addCond, OperatorId, OperatorName));
isAdd = Convert.ToBoolean(Evaluator.Eval(tempCond));
}
// 删除权限
if (!string.IsNullOrEmpty(deleteCond))
{
string tempCond = PubClass.ReqSqlWhere(row, PubClass.ReqSqlParam(deleteCond, OperatorId, OperatorName));
isDelete = Convert.ToBoolean(Evaluator.Eval(tempCond));
}
// 修改、保存权限
if (!string.IsNullOrEmpty(modifyCond))
{
string tempCond = PubClass.ReqSqlWhere(row, PubClass.ReqSqlParam(modifyCond, OperatorId, OperatorName));
isUpdate = Convert.ToBoolean(Evaluator.Eval(tempCond));
gridList.gridView.OptionsBehavior.Editable = isUpdate;
}
btnAdd.Enabled = isAdd;
btnDelete.Enabled = isDelete;
btnUpdate.Enabled = btnSave.Enabled = isUpdate;
}
}
private void gridControl_DoubleClick(object sender, EventArgs e) {
if (!string.IsNullOrEmpty(dtSystemDlltab.Rows[0]["AddDllName"] + ""))
UpdateModel();
}
private void btnPrint_Click(object sender, EventArgs e) {
PubClass.Preview(gridList.gridControl);
}
private void btnDelete_Click(object sender, EventArgs e) {
int[] rowid = gridList.gridView.GetSelectedRows();
if (rowid.Length == 0) {
XtraMessageBox.Show("没有选择要删除数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else {
if (XtraMessageBox.Show("您确定要删除选中记录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
string ids = string.Empty;
foreach (int rid in rowid) {
DataRow dr = gridList.gridView.GetDataRow(rid);
//判断删除条件是否符合
Boolean canDelete = string.IsNullOrEmpty(deleteCond);
string tempCond = deleteCond;
if (!canDelete) {
tempCond = PubClass.ReqSqlParam(tempCond, OperatorId, OperatorName);
tempCond = PubClass.ReqSqlWhere(dr, tempCond);
canDelete = Convert.ToBoolean(Evaluator.Eval(tempCond));
}
if (canDelete)
ids += "'" + dr[idFiledKey.Rows[0]["fieldname"] + ""] + "',";
}
if (ids != string.Empty) {
string sql = "delete from " + dtSystemDlltab.Rows[0]["SQLDT1"] + " where " + idFiledKey.Rows[0]["fieldname"] + " in(" + ids.Trim(',') + ")";
SqlHelper.ExecuteNonQuery(sql);
InitData(gridList.Sql);
XtraMessageBox.Show("记录删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else {
XtraMessageBox.Show("没有符合删除条件的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
private void btnAdd_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(objectValue))
{
XtraMessageBox.Show("请选择条数据后再添加", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (!string.IsNullOrEmpty(dtSystemDlltab.Rows[0]["AddDllName"] + ""))
{
string[] args = {
FormText,
OperatorId,
OperatorName,
Privilege,
DLLCoid,
"",
"",
"",
objectId,
objectValue,
dllSql
};
IForm form = PubUtil.LoadDllForm(dtSystemDlltab.Rows[0]["AddDllName"] + "", args);
DialogResult result = form.SubForm.ShowDialog();
InitData(gridList.Sql);
}
else
{
AddRow(gridList.gridView);
}
}
}
/// <summary>
/// 添加行
/// </summary>
/// <param name="view"></param>
protected void AddRow(DevExpress.XtraGrid.Views.Grid.GridView view) {
int prevDataRowIndex = view.GetFocusedDataSourceRowIndex();
view.AddNewRow();
DataTable dt = null;
if (!string.IsNullOrEmpty(dllSql) && dllSql.Contains("select")) {
dt = SqlHelper.ExecuteDataSet(dllSql).Tables[0];
}
//默认值
foreach (GridColumn item in view.Columns) {
if (item.Tag != null) {
MyTagModel cmt = item.Tag as MyTagModel;
if (cmt != null) {
string defaultValue = PubClass.GetDefaultValue(cmt.DefaultValue.ToString(), OperatorId, OperatorName);
if (item.FieldName.ToUpper().Equals(objectId.ToUpper())) {
view.SetRowCellValue(view.FocusedRowHandle, item, objectValue);
}
else if (dt != null && dt.Rows.Count > 0) {
foreach (DataColumn dc in dt.Columns) {
if (dc.ColumnName.ToUpper() == item.FieldName.ToUpper()) {
view.SetRowCellValue(view.FocusedRowHandle, item, dt.Rows[0][dc.ColumnName]);
}
}
}
else {
if (!string.IsNullOrEmpty(defaultValue))
{
view.SetRowCellValue(view.FocusedRowHandle, item, defaultValue);
}
}
}
}
}
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExport_Click(object sender, EventArgs e) {
PubClass.SaveXls(gridList.gridControl);
}
/// <summary>
/// 导入Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImport_Click(object sender, EventArgs e) {
//frmImport frm = new frmImport(gridList.gridControl, dtSystemDlltab.Rows[0]["SQLDT1"].ToString());
string columnPrefix = PubClass.GetColumnPrefix(dtSystemDlltab.Rows[0]["SQLDT1"].ToString());
frmImport frm = new frmImport(gridList, dtSystemDlltab.Rows[0]["SQLDT1"].ToString(), "", "", OperatorId, OperatorName, columnPrefix);
frm.ShowDialog();
}
public bool UpdateModel() {
if (dtSystemDlltab.Rows[0]["AddDllName"] + "" == "")
{
return false;
}
int[] rows = gridList.gridView.GetSelectedRows();
if (rows.Length > 0)
{
DataRow dr = gridList.gridView.GetDataRow(rows[0]);
//判断修改条件是否符合
Boolean canModify = string.IsNullOrEmpty(modifyCond);
if (needModified)
{
string tempCond = modifyCond;
if (!canModify)
{
tempCond = PubClass.ReqSqlParam(tempCond, OperatorId, OperatorName);
tempCond = PubClass.ReqSqlWhere(dr, tempCond);
canModify = Convert.ToBoolean(Evaluator.Eval(tempCond));
}
}
else
{
canModify = false;
}
canModify = canModify && (!Privilege.Equals("1"));
string productId = dr[idFiledKey.Rows[0]["fieldname"] + ""].ToString();
string[] args = {
FormText,
OperatorId,
OperatorName,
Privilege,
DLLCoid,
"",
Convert.ToInt32(!canModify).ToString(),
productId,
"",
"",
"",
dllSql
};
IForm form = PubUtil.LoadDllForm(dtSystemDlltab.Rows[0]["AddDllName"] + "", args);
form.SubForm.ShowDialog();
}
InitData(gridList.Sql);
return true;
}
private void btnUpdate_Click(object sender, EventArgs e) {
UpdateModel();
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
if (da == null) return;
da.Update(ds);
}
catch (DBConcurrencyException dce)
{
// 并发异常
XtraMessageBox.Show("数据已更新,请刷新后重试.");
return;
}
catch (Exception ex)
{
if (ex.Message.IndexOf("ix", StringComparison.OrdinalIgnoreCase) != -1)
{
XtraMessageBox.Show("系统检测到录入信息重复,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
XtraMessageBox.Show(ex.Message);
return;
}
}
XtraMessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 初始化数据
/// </summary>
/// <param name="sql"></param>
public void InitData(string sql) {
int oldRowHandle = (gridList.gridView).FocusedRowHandle;
da = SqlHelper.ExecuteAdapter(CommandType.Text, PubClass.ReplaceWhereCond(sql));
if (ds != null && ds.Tables.Count > 0)
ds.Clear();
da.Fill(ds);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
gridList.gridControl.DataSource = ds.Tables[0];
(gridList.gridView).ClearSelection();
(gridList.gridView).FocusedRowHandle = oldRowHandle;
(gridList.gridView).SelectRow(oldRowHandle);
}
private void btnCondSearch_Click(object sender, EventArgs e) {
InitData(gridList.Sql);
}
}
}