/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:F_MST_0101.cs
* 2.功能描述:组织机构一览
* 编辑履历:
* 作者 日期 版本 修改内容
* 王鑫 2014/09/12 1.00 新建
*******************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Dongke.IBOSS.PRD.Basics.BaseResources;
using Dongke.IBOSS.PRD.Basics.DockPanel;
using Dongke.IBOSS.PRD.Client.CommonModule;
using Dongke.IBOSS.PRD.Client.DataModels;
using Dongke.IBOSS.PRD.WCF.DataModels;
using Dongke.IBOSS.PRD.WCF.Proxys;
using Dongke.IBOSS.PRD.WCF.Proxys.SystemModuleService;
namespace Dongke.IBOSS.PRD.Client.SystemModule
{
///
/// 组织机构一览
///
public partial class F_MST_0101 : DockPanelBase
{
#region 成员变量
// 单例模式
private static F_MST_0101 _instance;
//存储新建id
private int _newId;
// 存储查询条件
private OrganizationEntity _organization = new OrganizationEntity();
// 保存treeview改变前的状态
private Hashtable _nodesStatus = new Hashtable();
//存储新建id
private string _CodeTimp;
#endregion
#region 单例模式
///
/// 单例模式,防止重复创建窗体
///
public static F_MST_0101 Instance
{
get
{
if (_instance == null)
{
_instance = new F_MST_0101();
}
return _instance;
}
}
#endregion
#region 构造函数
public F_MST_0101()
{
InitializeComponent();
//正常标识数据源
this.chkValueFlag.Items.Add("正常", true);
this.chkValueFlag.Items.Add("停用");
//窗体标题
this.Text = FormTitles.F_MST_0101;
//新建
this.tsbtnAdd.Text = ButtonText.TSBTN_ADD;
//编辑
this.tsbtnEdit.Text = ButtonText.TSBTN_EDIT;
//停用
this.tsbtnAdaptive.Text = ButtonText.TSBTN_ADAPTIVE;
//关闭
this.tsbtnClose.Text = ButtonText.TSBTN_CLOSE;
//查询
this.btnSearch.Text = ButtonText.BTN_SEARCH;
//清空条件
this.btnClearCondition.Text = ButtonText.BTN_CLEARCONDITION;
//查询条件
this.gbxCondition.Text = Constant.LABEL_QUERY_CONDITIONS;
}
#endregion
#region 事件
///
/// 节点收缩之后,显示关闭图标
///
///
///
private void tvwOrganization_AfterCollapse(object sender, TreeViewEventArgs e)
{
e.Node.SelectedImageIndex = Constant.INT_IS_ZERO;
e.Node.ImageIndex = Constant.INT_IS_ZERO;
}
///
/// 树节点变化的事件
///
///
///
private void tvwOrganization_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
// 获取相应节点数据
if (tvwOrganization.SelectedNode != null)
{
string selectCode = tvwOrganization.SelectedNode.Name.Trim();
// 封装查询条件
OrganizationEntity organization = new OrganizationEntity();
organization.OrganizationCode = selectCode;
//票头字母
organization.LetterMarket = this.txtLetterMarket.Text.Trim();
// 联系电话
organization.Telephone = this.txtTelephone.Text.Trim();
// 地址
organization.Address = this.txtAddress.Text.Trim();
// 备注
organization.Remarks = this.txtRemarks.Text.Trim();
// 正常标识
organization.ValueFlags = this.chkValueFlag.SelectedValues;
organization.OrganizationName = this.txtOrganizationName.Text.Trim();
organization.in_UserID = LogInUserInfo.CurrentUser.CurrentUserEntity.UserID;
organization.in_AccountID = LogInUserInfo.CurrentUser.CurrentUserEntity.AccountID;
this._organization = organization;
DataSet result = (DataSet)DoAsync(new AsyncMethod(this.SelectOrganixationData));
this.dgvOrganization.ReadOnly = true;
if (result != null && result.Tables.Count > Constant.INT_IS_ZERO)
{
this.dgvOrganization.DataSource = result.Tables[0];
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 获取选定树节点结果集
///
/// 组织机构实体
/// 数据集
private DataSet SelectOrganixationData()
{
DataSet dsOrganizationData = null;
try
{
dsOrganizationData = SystemModuleProxy.Service.SelectOrganizationData(this._organization);
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
return dsOrganizationData;
}
///
/// 节点展开之后,显示打开图标
///
///
///
private void tvwOrganization_AfterExpand(object sender, TreeViewEventArgs e)
{
e.Node.SelectedImageIndex = Constant.INT_IS_TWO;
e.Node.ImageIndex = Constant.INT_IS_TWO;
}
///
/// 清空条件按钮事件
///
///
///
private void btnClearCondition_Click(object sender, EventArgs e)
{
// 组织机构名称
this.txtOrganizationCode.Clear();
this.txtOrganizationName.Clear();
// 票号开头字母
this.txtLetterMarket.Clear();
// 联系电话
this.txtTelephone.Clear();
// 地址
this.txtAddress.Clear();
// 备注
this.txtRemarks.Clear();
this.chkValueFlag.SelectedValues = new object[] { Constant.INT_IS_ONE };
}
///
/// 查询按钮点击事件
///
///
///
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
// 没有选择正常标识的时候返回
if (Constant.INT_IS_ZERO >= this.chkValueFlag.SelectedValues.Length
|| Constant.INT_IS_ZERO >= this.chkValueFlag.SelectedValues.Length)
{
// 清空DataGridView
DataTable dataTable = (DataTable)this.dgvOrganization.DataSource;
if (dataTable != null && dataTable.Rows.Count > Constant.INT_IS_ZERO)
{
dataTable.Rows.Clear();
dgvOrganization.DataSource = dataTable;
}
// 提示未查找到数据
MessageBox.Show(Messages.MSG_CMN_I002, this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
// 编辑,移动按钮不可用
this.tsbtnEdit.Enabled = false;
return;
}
// 查询数据,并将结果显示到列表中
this.btnSearch.Enabled = false;
this.btnClearCondition.Enabled = false;
object result = DoAsync(new AsyncMethod(this.GetOrganization));
this.btnSearch.Enabled = true;
this.btnClearCondition.Enabled = true;
this.dgvOrganization.ReadOnly = true;
if (result != null)
{
base.DataSource = (DataSet)result;
if (this.DataSource != null && this.DataSource.Tables.Count > Constant.INT_IS_ZERO)
{
this.dgvOrganization.DataSource = this.DataSource.Tables[0];
if (this.DataSource.Tables[0].Rows.Count <= Constant.INT_IS_ZERO)
{
// 提示未查找到数据
MessageBox.Show(Messages.MSG_CMN_I002, this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
catch (Exception ex)
{
this.btnSearch.Enabled = true;
this.btnClearCondition.Enabled = true;
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 画面加载
///
///
///
private void FrmOrganization_Load(object sender, EventArgs e)
{
try
{
// 加载权限
FormPermissionManager.FormPermissionControl(this.Name, this,
LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData, LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData);
this.dgvOrganization.AutoGenerateColumns = false;
DataTable dt = new DataTable();
dt.Columns.Add("ValidFlagID");
dt.Columns.Add("ValidFlagName");
DataRow dr = dt.NewRow();
dr[0] = "1";
dr[1] = "正常";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "0";
dr[1] = "停用";
dt.Rows.Add(dr);
this.chkValueFlag.DataSource = dt;
object[] checkValueFlags = new object[1];
checkValueFlags[0] = 1;
this.chkValueFlag.SelectedValues = checkValueFlags;
// 生成组织机构树形
this.CreateOrganizationTree();
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 释放成员变量
///
///
///
private void FrmOrganization_FormClosed(object sender, FormClosedEventArgs e)
{
_instance = null;
}
///
/// 关闭按钮
///
///
///
private void tsbtnClose_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 自适应列宽按钮事件
///
///
///
private void tsbtnAdaptive_Click(object sender, EventArgs e)
{
this.dgvOrganization.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
///
/// 编辑按钮事件
///
///
///
private void tsbtnEdit_Click(object sender, EventArgs e)
{
try
{
DataGridViewRow currentRow = this.dgvOrganization.CurrentRow;
if (currentRow != null)
// 获取相应节点数据
{
F_MST_0102 frmMST0102 = new F_MST_0102(Constant.FormMode.Edit,
Convert.ToInt32(currentRow.Cells["OrganizationID"].Value));
DialogResult dialogresult = frmMST0102.ShowDialog();
if (dialogresult.Equals(DialogResult.OK))
{
// 刷新窗口数据
btnSearch_Click(sender, e);
// 重新生成组织机构树形
this.CreateOrganizationTree();
// 还原保存前选择的根节点
SetTreeNodesStatus(tvwOrganization.Nodes);
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 新建按钮事件
///
///
///
private void tsbtnAdd_Click(object sender, EventArgs e)
{
try
{
F_MST_0102 frmMST0102 = new F_MST_0102(Constant.FormMode.Add);
// 获取相应节点数据
if (tvwOrganization.SelectedNode != null)
{
string selectOrganizationCode = tvwOrganization.SelectedNode.Name.Trim();
frmMST0102.ParentOrganizationID = Convert.ToInt32(tvwOrganization.SelectedNode.Tag);
this._newId = Convert.ToInt32(tvwOrganization.SelectedNode.Tag);
frmMST0102.ParentOrganizationCode = tvwOrganization.SelectedNode.Name.Trim();
frmMST0102.ParentOrganizationName = tvwOrganization.SelectedNode.Text.Trim();
// 判断所选节点是已到最底层,不能新建
if (Constant.S_CODE_MAXLEVEL <= selectOrganizationCode.Length / Constant.INT_IS_THREE)
{
MessageBox.Show(string.Format(Messages.MSG_CMN_W013, "组织机构",
"级数", Constant.S_CODE_MAXLEVEL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 判断选择的组织机构的下级组织是否超过系统设定的最大值999
if (Constant.S_CODE_LEVEL_MAXCODE <= this.GetOrgLevelMax(selectOrganizationCode))
{
MessageBox.Show("组织机构已达到极限(999),无法再增加!", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 判断选择的组织机构下是否有员工
if (this.IsExistStaff(frmMST0102.ParentOrganizationID))
{
MessageBox.Show(string.Format(Messages.MSG_CMN_W007, "选择的组织机构存在员工"),
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 显示新建窗口
DialogResult dialogresult = frmMST0102.ShowDialog();
if (dialogresult.Equals(DialogResult.OK))
{
_CodeTimp = frmMST0102.ParentOrganizationCodeTemp;
//查询条件赋值为空
this.txtLetterMarket.Text = string.Empty;
this.txtOrganizationCode.Text = string.Empty;
this.txtOrganizationName.Text = string.Empty;
this.txtTelephone.Text = string.Empty;
this.txtAddress.Text = string.Empty;
this.txtRemarks.Text = string.Empty;
// 新建的组织机构ID列表
List organizationIDList = frmMST0102.OrganizationIDList;
int[] IDList = new int[organizationIDList.Count];
for (int i = 0; i < organizationIDList.Count; i++)
{
IDList[i] = organizationIDList[i];
}
this._organization = new OrganizationEntity();
this._organization.IDList = IDList;
// 查询时先清空数据源
this.dgvOrganization.DataSource = null;
DataSet result = (DataSet)DoAsync(new AsyncMethod(GetOrganization));
this.dgvOrganization.ReadOnly = true;
if (result != null && result.Tables.Count > Constant.INT_IS_ZERO)
{
this.dgvOrganization.DataSource = result.Tables[0];
}
// 重新生成组织机构树形
this.CreateOrganizationTree();
this.tvwOrganization.ExpandAll();
// 还原保存前选择的根节点
SetTreeNodesStatus(tvwOrganization.Nodes);
}
}
else
{
MessageBox.Show("请先在左侧树型列表中,选择一个根节点作为上级部门!", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 单元格获取焦点事件:控制按钮状态
///
///
///
private void dgvOrganization_CellEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
DataGridViewRow currentRow = this.dgvOrganization.CurrentRow;
if (currentRow != null)
{
this.tsbtnEdit.Enabled = true;
}
else
{
this.tsbtnEdit.Enabled = false;
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// 双击进入编辑页面
///
///
///
private void dgvOrganization_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
// 判断是否是双击列头,如果是双击列头的话,不弹出编辑画面
if (-Constant.INT_IS_ONE < e.RowIndex && -Constant.INT_IS_ONE < e.ColumnIndex && tsbtnEdit.Enabled && this.tsbtnEdit.Visible)
{
tsbtnEdit_Click(sender, e);
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
///
/// KeyDown事件
///
///
///
private void dgvOrganization_KeyDown(object sender, KeyEventArgs e)
{
try
{
// 拷贝
if (e.KeyData == (Keys.Control | Keys.C))
{
if (dgvOrganization.CurrentRow != null
&& !string.IsNullOrEmpty(dgvOrganization.CurrentRow.Cells[dgvOrganization.CurrentCell.ColumnIndex].EditedFormattedValue + string.Empty))
{
try
{
Clipboard.SetText(dgvOrganization.CurrentRow.Cells[dgvOrganization.CurrentCell.ColumnIndex].EditedFormattedValue + string.Empty);
}
catch { }
}
}
}
catch (Exception ex)
{
// 对异常进行共通处理
ExceptionManager.HandleEventException(this.ToString(),
System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
}
}
#endregion
#region 私有方法
///
/// 获取组织机构结果集
///
///
private DataSet GetOrganization()
{
try
{
OrganizationEntity organization = new OrganizationEntity();
//组织机构
organization.OrganizationCode = this.txtOrganizationCode.Text.Trim();
organization.OrganizationName = this.txtOrganizationName.Text.Trim();
// 票号开头字母
organization.LetterMarket = this.txtLetterMarket.Text.Trim();
// 联系电话
organization.Telephone = this.txtTelephone.Text.Trim();
// 地址
organization.Address = this.txtAddress.Text;
// 备注
organization.Remarks = this.txtRemarks.Text;
// 正常标识
organization.ValueFlags = this.chkValueFlag.SelectedValues;
organization.in_UserID = LogInUserInfo.CurrentUser.CurrentUserEntity.UserID;
organization.in_AccountID = LogInUserInfo.CurrentUser.CurrentUserEntity.AccountID;
this._organization = organization;
return SystemModuleProxy.Service.SelectOrganizationData(organization);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 根据上级组织机构的编码获取其下级的最大级数
///
/// 上级组织机构的Code
/// 下级的最大级数
private int GetLevel(string parentCode)
{
try
{
// 下级的最大级数
int level = Constant.INT_IS_ZERO;
// 查询数据库获取下级最大编码
this._organization = new OrganizationEntity();
this._organization.OrganizationCode = parentCode;
DataSet result = (DataSet)DoAsync(new AsyncMethod(GetOrganization));
this.dgvOrganization.ReadOnly = true;
if (result != null
&& result.Tables.Count > Constant.INT_IS_ZERO
&& result.Tables[0].Rows.Count > Constant.INT_IS_ZERO)
{
string tmpCode = Convert.ToString(result.Tables[0].Compute("MAX(OrganizationCode)", string.Empty));
level += (tmpCode.Length - parentCode.Length) / Constant.INT_IS_THREE;
}
return level;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 还原保存treeview的节点
///
/// 节点
private void SetTreeNodesStatus(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
SetTreeNodesStatus(node.Nodes);
}
}
///
/// 根据所取得的组织数据,生成树形
///
private void CreateOrganizationTree()
{
try
{
// 取得组织机构数据
OrganizationEntity organization = new OrganizationEntity();
object[] valueFlags = new object[1];
valueFlags[0] = 1;
organization.ValueFlags = valueFlags;
this._organization = organization;
// 获取有效的组织机构
DataSet result = (DataSet)DoAsync(new AsyncMethod(GetOrganization));
this.dgvOrganization.ReadOnly = true;
if (result != null && result.Tables.Count > Constant.INT_IS_ZERO)
{
// 组织为空时,直接返回
if (result.Tables[0].Rows.Count < Constant.INT_IS_ONE)
{
return;
}
if (tvwOrganization.Nodes.Count > Constant.INT_IS_ZERO)
{
// 添加节点前,保存状态
SaveTreeNodesStatus(tvwOrganization.Nodes);
}
// 将所有节点清除
this.tvwOrganization.Nodes.Clear();
TreeNode organizationNode = null;
DataRow[] organizationRows
= result.Tables[0].Select("OrganizationCode LIKE '00%' AND LEN(OrganizationCode) = 3");
// 显示节点
InitTreeView(result.Tables[0], organizationRows, organizationNode);
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 递归显示组织树形目录
///
/// 组织机构数据源
/// 根节点
/// 树节点
private void InitTreeView(DataTable orgData, DataRow[] rows, TreeNode node)
{
try
{
foreach (DataRow row in rows)
{
TreeNode sNode = null;
if (node == null)
{
// 新建树节点并添加
sNode = new TreeNode();
sNode.Text = row["OrganizationName"].ToString();
sNode.Name = row["OrganizationCode"].ToString();
sNode.Tag = row["OrganizationID"].ToString();
this.tvwOrganization.Nodes.Add(sNode);
}
else
{
// 在原有树中加入节点
sNode = node.Nodes.Add(row["OrganizationName"].ToString());
sNode.Name = row["OrganizationCode"].ToString();
sNode.Tag = row["OrganizationID"].ToString();
}
if (this._CodeTimp == row["OrganizationCode"].ToString())
{
tvwOrganization.SelectedNode = sNode;
}
// 查找子节点
string filterExpression = "OrganizationCode LIKE '" + row["OrganizationCode"].ToString()
+ "%' AND LEN(OrganizationCode) = " + (row["OrganizationCode"].ToString().Length + 3);
DataRow[] subRows = orgData.Select(filterExpression);
// 递归绑定子节点
InitTreeView(orgData, subRows, sNode);
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 保存treeview状态
///
///
private void SaveTreeNodesStatus(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
SaveTreeNodesStatus(node.Nodes);
}
}
///
/// 判断该组织机构下是否存在员工
///
/// 组织机构编码
///
private bool IsExistStaff(int organizationID)
{
try
{
if (organizationID <= Constant.INT_IS_ZERO)
{
return false;
}
// 获取员工数据
this._organization = new OrganizationEntity();
this._organization.OrganizationID = organizationID;
return false;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取上级组织机构中已经存在的组织机构数量
///
/// 上级组织机构的Code
/// 组织机构数量
private int GetOrgLevelMax(string parentCode)
{
try
{
// 获取数据源中的结构信息
this._organization = new OrganizationEntity();
this._organization.OrganizationCode = parentCode;
DataSet result = (DataSet)DoAsync(new Dongke.IBOSS.PRD.Basics.DockPanel.AsyncMethod(GetOrganization));
this.dgvOrganization.ReadOnly = true;
if (result != null && result.Tables.Count > Constant.INT_IS_ZERO)
{
if (result.Tables[0].Rows.Count > Constant.INT_IS_ONE)
{
// 从数据源中查找组织结构最大值
DataTable filterDataTable = result.Tables[0].Clone();
// 过滤本级组织机构
string filterExpression = "OrganizationCode LIKE '" + parentCode + "%'"
+ " AND LEN(OrganizationCode) = " + (parentCode.Length + Constant.INT_IS_THREE);
DataRow[] dataRows = result.Tables[0].Select(filterExpression, "OrganizationCode DESC");
foreach (DataRow row in dataRows)
{
filterDataTable.Rows.Add(row.ItemArray);
}
if (Constant.INT_IS_ZERO < filterDataTable.Rows.Count)
{
// 取得本级以下所有子级的组织机构集合
string organizationCode = filterDataTable.Rows[0]["OrganizationCode"].ToString();
// 当前最大的组织机构代码
organizationCode = organizationCode.Substring(organizationCode.Length - Constant.INT_IS_THREE);
return Convert.ToInt32(organizationCode);
}
return Constant.INT_IS_ZERO;
}
}
return Constant.INT_IS_ZERO;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}