/*******************************************************************************
* Copyright(c) 2018 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:F_PM_0906.cs
* 2.功能描述:新建编辑成型破损原因
* 编辑履历:
* 作者 日期 版本 修改内容
* 周兴 2018/3/26 1.00 新建
*******************************************************************************/
using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Dongke.IBOSS.PRD.Basics.BaseControls;
using Dongke.IBOSS.PRD.Basics.BaseResources;
using Dongke.IBOSS.PRD.Basics.Library;
using Dongke.IBOSS.PRD.Client.CommonModule;
using Dongke.IBOSS.PRD.Client.DataModels;
using Dongke.IBOSS.PRD.WCF.DataModels;
using Dongke.IBOSS.PRD.WCF.Proxys;
namespace Dongke.IBOSS.PRD.Client.PMModule
{
public partial class F_PM_0906 : FormBase
{
#region 成员变量
private static F_PM_0906 _instance; // 窗体的实例
private byte _valueFalg; // 有效标识
private string _scrapType; // 报损类型
private DataTable _dtSourse; // 数据源
private Hashtable _nodesStatus = new Hashtable(); // 数据字典Treeview改变前的状态
private string _selectNodeFullPath; // 数据字典TreeView选中节点的路径
private DataTable _initDataSource; // 初始加载的数据源
#endregion
#region 属性
///
/// 数据字典TreeView改变前的状态
///
public Hashtable NodesStatus
{
get
{
return _nodesStatus;
}
set
{
_nodesStatus = value;
}
}
///
/// 数据字典TreeView选中节点的路径
///
public string SelectNodeFullPath
{
get
{
return _selectNodeFullPath;
}
set
{
_selectNodeFullPath = value;
}
}
#endregion
#region 构造函数
private F_PM_0906()
{
InitializeComponent();
// 窗口标题
this.Text = FormTitles.F_MST_0906;
// 按钮
this.btnSave.Text = ButtonText.BTN_SAVE;
this.btnCancel.Text = ButtonText.BTN_CLOSE;
}
///
/// 单例模式提供实例
///
///
public static F_PM_0906 Instance
{
get
{
if (_instance == null)
{
_instance = new F_PM_0906();
}
return _instance;
}
}
#endregion
#region 事件
///
/// 窗体加载事件
///
///
///
private void F_PM_0906_Load(object sender, EventArgs e)
{
try
{
// 屏蔽根据数据源的实际情况,自动生成datagridview表格列
this.dgvScrapReason.AutoGenerateColumns = false;
this.btnSave.Enabled = false;
//生成树形结构
this.CreateScrapTypeTree();
// 获取报损原因数据
this.GetScrapTypeData();
// 窗体显示的时候鼠标停在第一个子节点
this.tvwScrapType.SelectedNode = tvwScrapType.Nodes[0];
this.dgvScrapReason.IsSetInputColumnsColor = true;
//权限控制
FormPermissionManager.FormPermissionControl(this.Name, this,
LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData, LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData);
this.dgvScrapReason.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 根据是否选中停用数据,加载数据
///
///
///
private void chkDisplayDisabledData_CheckedChanged(object sender, EventArgs e)
{
try
{
this.RefreshData();
this.SetSaveBtnStatus();
this.dgvScrapReason.IsSetInputColumnsColor = true;
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 窗体关闭事件
///
///
///
private void F_PM_0906_FormClosed(object sender, FormClosedEventArgs e)
{
_instance = null;
}
#endregion
#region 私有方法
///
/// 根据取得的数据字典数据,生成树形结构
///
private void CreateScrapTypeTree()
{
try
{
if (this.tvwScrapType.Nodes.Count > Constant.INT_IS_ZERO)
{
// 添加节点前,保存状态
SaveTreeNodesStatus(this.tvwScrapType.Nodes);
}
// 将所有节点清除
this.tvwScrapType.Nodes.Clear();
DataTable scrapTypeTable = new DataTable();
scrapTypeTable.Columns.Add("ScrapType");
scrapTypeTable.Columns.Add("ScrapTypeName");
DataRow row = scrapTypeTable.NewRow();
row["ScrapType"] = "0";
row["ScrapTypeName"] = "开模报损";
scrapTypeTable.Rows.Add(row);
row = scrapTypeTable.NewRow();
row["ScrapType"] = "1";
row["ScrapTypeName"] = "普通报损";
scrapTypeTable.Rows.Add(row);
//生成树形结构
foreach (DataRow newRootRow in scrapTypeTable.Rows)
{
TreeNode rootNode = new TreeNode();
rootNode.Name = newRootRow["ScrapType"] + "";
rootNode.Text = newRootRow["ScrapTypeName"] + "";
rootNode.Tag = newRootRow["ScrapType"] + "";
this.tvwScrapType.Nodes.Add(rootNode);
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取报损原因数据
///
private void GetScrapTypeData()
{
try
{
// 取得报损原因
ClientRequestEntity cre = new ClientRequestEntity();
cre.NameSpace = "MST_ScrapReason";
cre.Name = "GetScrapReasonData";
ServiceResultEntity result = new ServiceResultEntity();
result = (ServiceResultEntity)DoAsync(new BaseAsyncMethod(() => { return SystemModuleProxy.Service.DoBarCodePrint(cre); }));
// 没有数据时,直接返回
if (result == null || result.Data == null || result.Data.Tables.Count <= 0)
{
return;
}
_initDataSource = result.Data.Tables[0];
_dtSourse = result.Data.Tables[0];
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 保存事件
///
///
///
private void btnSave_Click(object sender, EventArgs e)
{
try
{
SaveScrapReason();
}
catch (Exception ex)
{
this.btnSave.Enabled = true;
this.btnCancel.Enabled = true;
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 关闭按钮事件
///
///
///
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 选择数据字典TreeView中的节点前判断,如果数据有改变提示保存
///
///
///
private void tvwDictionaryType_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
try
{
this.dgvScrapReason.AllowUserToAddRows = true;
// 判断数据是否被修改过,修改过需要提示保存消息
if (DataJudge.IsChange((DataTable)this.dgvScrapReason.DataSource))
{
DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001, this.Text,
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
// 保存修改内容
if (dialogResult == DialogResult.Yes)
{
SaveScrapReason();
}
else if (dialogResult == DialogResult.Cancel)
{
e.Cancel = true;
}
//选择否回滚
else
{
((DataTable)this.dgvScrapReason.DataSource).RejectChanges();
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 选择树中任意值,右边数据跟着变动
///
///
///
private void tvwDictionaryType_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
this.lblDictionaryType.Text = "当前选择的字典类型:" + e.Node.Text;
//判断父节点是否存在,表格联动
TreeNode node = this.tvwScrapType.SelectedNode;
if (node != null)
{
this.dgvScrapReason.ReadOnly = false;
this._dtSourse.AcceptChanges();
this.RefreshData();
}
else
{
this.dgvScrapReason.DataSource = null;
this.dgvScrapReason.ReadOnly = true;
this.dgvScrapReason.AllowUserToAddRows = false;
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 选中新行时,设置默认值
///
///
///
private void dgvScrapReason_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
// 设置有效属性为选中状态
e.Row.Cells["ValueFlag"].Value = Constant.INT_IS_ONE;
// 增加一行时,给类型赋值
e.Row.Cells["ScrapType"].Value = this.tvwScrapType.SelectedNode.Tag + "";
e.Row.Cells["DisplayNo"].Value = e.Row.Index + 1;
}
///
/// 数据字典信息输入格式控制
///
///
///
private void dgvScrapReason_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
try
{
// 只对文本框做控制
if (e.Control is DataGridViewTextBoxEditingControl)
{
DataGridViewTextBoxEditingControl EditingControl = (DataGridViewTextBoxEditingControl)e.Control;
EditingControl.KeyPress += new KeyPressEventHandler(dgvScrapReason_KeyPress);
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 屏蔽除了数字以外的输入(显示顺序)
///
///
///
private void dgvScrapReason_KeyPress(object sender, KeyPressEventArgs e)
{
// 显示顺序列只允许输入数字和回格
if ("DisplayNo".Equals(this.dgvScrapReason.Columns[this
.dgvScrapReason.CurrentCell.ColumnIndex].Name))
{
if (e.KeyChar != '\b' && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
}
///
/// 检查是否有重复的字典名称以及将为输入顺序设置成0
///
///
///
private void dgvScrapReason_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
try
{
if (!dgvScrapReason.CurrentRow.IsNewRow)
{
DataGridViewRow rowItem = dgvScrapReason.Rows[e.RowIndex];
DataGridViewColumn columnItem = dgvScrapReason.Columns[e.ColumnIndex];
object columnvalue = rowItem.Cells[columnItem.Name].EditedFormattedValue;
// 破损原因
if ("ScrapReason".Equals(columnItem.Name))
{
if (columnvalue != null || !string.IsNullOrEmpty(columnvalue.ToString().Trim()))
{
if (!Utility.IsUnique(columnvalue.ToString(),
dgvScrapReason.CurrentRow.Index, this.dgvScrapReason, "ScrapReason"))
{
// 单元格的错误消息
this.dgvScrapReason.CurrentRow.ErrorText = string.Format(
Messages.MSG_CMN_W006, "报损原因");
e.Cancel = true;
this.btnSave.Enabled = false;
return;
}
}
}
// 清除单元格的错误消息
this.dgvScrapReason.CurrentRow.ErrorText = string.Empty;
this.dgvScrapReason.IsSetInputColumnsColor = true;
// 显示顺序为空时设置为0
if ("DisplayNo".Equals(columnItem.Name)
&& string.IsNullOrEmpty(columnvalue.ToString()))
{
dgvScrapReason.CurrentCell.Value = Constant.INT_IS_ZERO;
dgvScrapReason.RefreshEdit();
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 单元格校验:根据数据是否变化,设置保存按钮的可用状态
///
///
///
private void dgvScrapReason_CellValidated(object sender, DataGridViewCellEventArgs e)
{
try
{
this.SetSaveBtnStatus();
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 行校验,不能为空
///
///
///
private void dgvScrapReason_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
try
{
// 按Esc键时不校验
if (!dgvScrapReason.IsCurrentRowDirty)
{
this.dgvScrapReason.IsSetInputColumnsColor = true;
return;
}
DataGridViewRow row = dgvScrapReason.CurrentRow;
if (!row.IsNewRow)
{
// 报损原因不能为空
if (row.Cells["ScrapReason"].Value == null
|| string.IsNullOrEmpty(row.Cells["ScrapReason"].Value.ToString().Trim()))
{
// 单元格的错误消息
this.dgvScrapReason.CurrentRow.ErrorText
= string.Format(Messages.MSG_CMN_W005, "报损原因");
e.Cancel = true;
this.btnSave.Enabled = false;
return;
}
// 显示顺序不能为空
if (row.Cells["DisplayNo"].Value == null
|| string.IsNullOrEmpty(row.Cells["DisplayNo"].Value.ToString().Trim()))
{
this.dgvScrapReason.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "显示顺序");
e.Cancel = true;
this.btnSave.Enabled = false;
return;
}
// 过滤掉数据字典名称,备注首尾的空格
row.Cells["ScrapReason"].Value = row.Cells["ScrapReason"].Value.ToString().Trim();
row.Cells["Remarks"].Value = row.Cells["Remarks"].Value == null ? "" : row.Cells["Remarks"].Value.ToString().Trim();
// 清除单元格的错误消息
this.dgvScrapReason.CurrentRow.ErrorText = string.Empty;
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 行校验:根据数据是否变化,设置保存按钮的可用状态
///
///
///
private void dgvScrapReason_RowValidated(object sender, DataGridViewCellEventArgs e)
{
try
{
this.SetSaveBtnStatus();
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 刷新数据
///
private object RefreshData()
{
try
{
// 获取相应节点数据
this._scrapType = this.tvwScrapType.SelectedNode.Tag.ToString();
// 获取是否查询正常数据的标识
this._valueFalg = Convert.ToByte(this.chkDisplayDisabledData.Checked);
// 获取查询的数据集
StringBuilder sbFilter = new StringBuilder();
sbFilter.Append(" 1=1");
if (this._valueFalg == Constant.INT_IS_ZERO)
{
sbFilter.Append(" and ValueFlag = 1");
}
if (this._scrapType != "")
{
sbFilter.Append(" and ScrapType = '" + _scrapType + "'");
}
this._dtSourse.DefaultView.RowFilter = sbFilter.ToString();
// 选中新行时,自动显示顺序
this._dtSourse.TableNewRow += new DataTableNewRowEventHandler(Table_SelectNewRow);
this.dgvScrapReason.DataSource = this._dtSourse;
this.dgvScrapReason.IsSetInputColumnsColor = true;
return Constant.INT_IS_ZERO;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 保存的共同方法
///
private void SaveScrapReason()
{
try
{
// 异步处理
ClientRequestEntity cre = new ClientRequestEntity();
cre.NameSpace = "MST_ScrapReason";
cre.Name = "SaveScrapReasonData";
DataTable returnTable = (DataTable)this.dgvScrapReason.DataSource;
cre.Data = new DataSet();
cre.Data.Tables.Add(returnTable.Copy());
ServiceResultEntity result = new ServiceResultEntity();
result = (ServiceResultEntity)DoAsync(new BaseAsyncMethod(() => { return SystemModuleProxy.Service.DoBarCodePrint(cre); }));
int resultRow = (int)result.Result;
// 如果保存出错,不关闭窗体
if (resultRow > Constant.INT_IS_ZERO)
{
RefreshData();
this._dtSourse.AcceptChanges();
this.btnSave.Enabled = false;
MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "报损原因", "保存"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
this.btnSave.Enabled = false;
MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "报损原因", "保存"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 增加一行,自动计算显示顺序
///
///
///
private void Table_SelectNewRow(object sender, DataTableNewRowEventArgs e)
{
DataTable itemTable = sender as DataTable;
//判断最大值
if (itemTable != null)
{
int maxValue = Utility.GetMaxValue(itemTable.DefaultView.ToTable(), "DisplayNo");
if (maxValue < Constant.INT_IS_ZERO)
{
maxValue = Constant.INT_IS_ZERO;
}
// 判断显示顺序
if (Constant.S_DISPLAYNO_MAX < maxValue + Constant.INT_IS_ONE)
{
e.Row["DisplayNo"] = maxValue;
}
else
{
e.Row["DisplayNo"] = ++maxValue;
}
e.Row["ScrapType"] = this.tvwScrapType.SelectedNode.Tag.ToString();
}
}
///
/// 保存数据字典TreeView的状态
///
///
private void SaveTreeNodesStatus(TreeNodeCollection nodes)
{
try
{
foreach (TreeNode node in nodes)
{
// 保存所有节点折叠,展开状态
if (node.IsExpanded)
{
this.NodesStatus[node.FullPath] = true;
}
else
{
this.NodesStatus.Remove(node.FullPath);
}
// 当节点处于选中状态时获取根树节点到当前树节点的路径
if (node.IsSelected)
{
SelectNodeFullPath = node.FullPath;
}
SaveTreeNodesStatus(node.Nodes);
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 还原保存前产品类型TreeView的状态
///
/// 节点
private void SetTreeNodesStatus(TreeNodeCollection nodes)
{
try
{
foreach (TreeNode node in nodes)
{
if (this.NodesStatus[node.FullPath] != null)
{
// 展开树节点
node.Expand();
}
// 节点路径和前选中的路径相同 设置节点为选中状态
if (node.FullPath == SelectNodeFullPath)
{
this.tvwScrapType.SelectedNode = node;
}
SetTreeNodesStatus(node.Nodes);
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 设置保存按钮的可用状态
///
private void SetSaveBtnStatus()
{
if (DataJudge.IsChange((DataTable)this.dgvScrapReason.DataSource))
{
this.btnSave.Enabled = true;
}
else
{
this.btnSave.Enabled = false;
}
}
#endregion
}
}