/******************************************************************************* * Copyright(c) 2014 dongke All rights reserved. / Confidential * 类的信息: * 1.程序名称:dkOrganizationSearchBox.cs * 2.功能描述:组织结构选择控件 * 编辑履历: * 作者 日期 版本 修改内容 * 陈冰 2014/08/29 1.00 新建 * 王鑫 2014/09/13 1.00 修改(添加一个当前用户组织机构属性) *******************************************************************************/ using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using Dongke.IBOSS.PRD.Basics.BaseControls; using Dongke.IBOSS.PRD.WCF.Proxys; using Dongke.IBOSS.PRD.Client.DataModels; using Dongke.IBOSS.PRD.Basics.BaseResources; namespace Dongke.IBOSS.PRD.Client.Controls { /// /// 组织结构选择控件 /// public partial class dkOrganizationSearchBox : UserControl { #region 成员变量 private S_CMN_001 _frmOrganization; private Label lblOrganization; // 项目名称标签 private TextBox txtOrganization; // 输入文本框 private Button btnSearch; // 查询按钮 private bool _isEnabled = true; // 控件是否可用 private DataTable _dataSource = null; // 数据源 private int? _organizationID; // 组织机构ID private string _organizationIDS; //id集 private string _organizationCode; // 组织机构Code private string _organizationName; // 组织机构名称 private bool _isOnlyDisplayEnd; // 只显示末端组织结构标识 private bool _isMustInput; // 控件是否是必须输入项目 private bool _IsOnlyShowValid = true; // 是否只显示有效数据 private string _currentuserorganizationCode; // 当前用户组织机构用于过滤 #endregion #region 构造函数 public dkOrganizationSearchBox() { InitializeComponent(); this.IsOnlyDisplayEnd = false; this.ReadOnly = true; } #endregion #region 属性 /// /// 是否只显示有效数据 /// [Description("设置控件是否只显示有效数据。")] [DefaultValue(true)] public bool IsOnlyShowValid { get { return _IsOnlyShowValid; } set { _IsOnlyShowValid = value; } } /// /// 获取或者设定控件是否是必须输入项目 /// [DefaultValue("False")] [Description("获取或者设定控件是否是必须输入项目。")] public bool IsMustInput { get { return _isMustInput; } set { _isMustInput = value; // 项目为必须输入项时,需要修改字体颜色 if (_isMustInput) { this.lblOrganization.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(155)))), ((int)(((byte)(39)))), ((int)(((byte)(39))))); } else { this.lblOrganization.ForeColor = System.Drawing.SystemColors.ControlText; } } } [DefaultValue("组织机构")] [Description("获取或者设定Lable标签字符。")] public string Title { get { return this.lblOrganization.Text; } set { this.lblOrganization.Text = value; AdjustControl(); } } /// /// 获取和设置是否只显示末端组织机构。 /// [DefaultValue(true)] [Description("获取和设置是否只显示末端组织机构。")] public bool IsOnlyDisplayEnd { get { return _isOnlyDisplayEnd; } set { _isOnlyDisplayEnd = value; } } [DefaultValue(false)] public bool IsDeleteSelf { get; set; } /// /// 获取或者设定控件的数据源。 /// [Description("获取或者设定控件的数据源。")] public DataTable DataSource { get { return _dataSource; } set { DataTable dataSource = SetFilter(value); _dataSource = dataSource; // 如果数据源有值,需要过滤范围权限 if (_dataSource != null && _dataSource.Rows.Count > 0) { string filter = string.Empty; // 只显示叶节点部门 if (IsOnlyDisplayEnd) { for (int i = _dataSource.Rows.Count - 1; i >= 0; i--) { filter = "LEN(OrganizationCode) > 3 AND OrganizationCode LIKE '" + _dataSource.Rows[i]["OrganizationCode"] + "%'"; _dataSource.DefaultView.RowFilter = filter; DataTable table = _dataSource.DefaultView.ToTable(); // 说明有下级种类 if (table.Rows.Count > 1) { _dataSource.Rows.RemoveAt(i); } } } // 是否是结算部门 if (IsAccountOrganization) { filter = "LEN(OrganizationCode) = 6"; _dataSource.DefaultView.RowFilter = filter; _dataSource = _dataSource.DefaultView.ToTable(); } } } } /// /// 获取或者设定控件的尺寸大小。 /// [Description("获取或者设定控件的尺寸大小。")] public new Size Size { get { return base.Size; } set { base.Size = value; AdjustControl(); } } /// /// 获取或者设定控件的文本框的文本字符串。 /// [Description("获取或者设定控件的文本框的文本字符串。")] public override string Text { get { return this.txtOrganization.Text; } set { this.txtOrganization.Text = value; } } /// /// 获取或者设定控件的文本框的背景颜色。 /// [Description("获取或者设定控件的文本框的背景颜色。")] public Color TxtOrganizationBackColor { get { return this.txtOrganization.BackColor; } set { this.txtOrganization.BackColor = value; } } /// /// 获取或者设定控件的组织机构ID。 /// [Description("获取或者设定控件的组织机构ID。")] [DefaultValue(null)] public int? OrganizationID { get { return _organizationID; } set { this._organizationID = null; this._organizationCode = string.Empty; this._organizationName = string.Empty; this.Text = string.Empty; if (value != null) { // 当数据源为null时,查询数据源 if (DataSource == null) { // 查询数据源 DataSource = SystemModuleProxy.Service.GetOrganizationData(1, _isOnlyDisplayEnd ? 1 : 0); // 过滤数据 SetFilter(DataSource); } if (value != null && value != 0 && this.DataSource != null) { DataTable organizationTable = this.DataSource.Copy(); DataRow[] dataRows = organizationTable.Select("OrganizationID = " + value); if (dataRows.Length == 1) { this._organizationID = value; this._organizationCode = dataRows[0]["OrganizationCode"] as string; this._organizationName = dataRows[0]["OrganizationName"] as string; this.Text = dataRows[0]["OrganizationName"] as string; } } } } } /// /// 获取或者设定控件的组织机构id集 /// [Description("获取或者设定控件的组织机构id集。")] public string OrganizationIDS { get { return _organizationIDS; } set { _organizationIDS = value; } } /// /// 获取或者设定控件的组织机构Code /// [Description("获取或者设定控件的组织机构Code。")] public string OrganizationCode { get { return _organizationCode; } set { _organizationCode = value; } } /// /// 获取或者设定控件的当前用户组织机构Code /// [Description("获取或者设定控件的当前用户组织机构Code。")] public string CurrentUserOrganizationCode { get { return _currentuserorganizationCode; } set { _currentuserorganizationCode = value; } } /// /// 获取或者设定控件的组织机构名称 /// [Description("获取或者设定控件的组织机构名称。")] public string OrganizationName { get { return _organizationName; } set { _organizationName = value; } } /// /// 获取或者设定控件是否可用。 /// [System.ComponentModel.DefaultValue(null)] [Description("获取或者设定控件是否可用。")] public new bool Enabled { get { return _isEnabled; } set { _isEnabled = value; this.txtOrganization.Enabled = _isEnabled; //this.btnSearch.Enabled = _isEnabled; this.TabStop = _isEnabled; } } /// /// 获取或者设定控件是否只读。 /// [System.ComponentModel.DefaultValue(null)] [Description("获取或者设定控件是否只读。")] public bool ReadOnly { set { this.txtOrganization.ReadOnly = value; } } /// /// 范围权限 /// public byte PurviewType { get; set; } /// /// 控件是否是核算部门。 /// [System.ComponentModel.DefaultValue(false)] [Description("控件是否是核算部门。")] public bool IsAccountOrganization { get; set; } /// /// 控件部门操作权限。 /// [System.ComponentModel.DefaultValue(false)] [Description("控件部门操作权限。")] public bool PurviewOperateFlag { get; set; } /// /// 控件部门查看权限。 /// [System.ComponentModel.DefaultValue(false)] [Description("控件部门查看权限。")] public bool PurviewViewFlag { get; set; } /// /// 多选。 /// [System.ComponentModel.DefaultValue(false)] [Description("多选。")] public bool SelectMore { get; set; } #endregion #region 定义委托事件 public delegate void TextBoxChangedHandle(object sender, TextChangeEventArgs e); public event TextBoxChangedHandle OrganizationValueChanged; #endregion #region 控件事件 [Description("控件光标点击按钮事件。")] public new event EventHandler Click { add { this.btnSearch.Click += value; } remove { this.btnSearch.Click -= value; } } /// /// 控件尺寸大小改变事件 /// /// /// private void dkOrganizationSearchBox_SizeChanged(object sender, System.EventArgs e) { AdjustControl(); } /// /// 查询按钮按下事件 /// /// /// private void btnSearch_Click(object sender, System.EventArgs e) { // 如果属性是不可用,是不能进行点击事件的 if (!Enabled) { return; } // 当数据源为null时,查询数据源 if (DataSource == null) { // 查询数据源 DataSource = SystemModuleProxy.Service.GetOrganizationData(1, _isOnlyDisplayEnd ? 1 : 0); } // 释放窗体资源 if (null != _frmOrganization) { _frmOrganization.Dispose(); _frmOrganization = null; } // 打开查询窗体 //判断是否多选 if (this.SelectMore) { _frmOrganization = new S_CMN_001(1); } else { _frmOrganization = new S_CMN_001(this.txtOrganization.Text); } DataTable organizationTable = this.DataSource.Copy(); if (CurrentUserOrganizationCode == string.Empty) _frmOrganization.DataSource = this.DataSource; else { organizationTable.DefaultView.RowFilter = string.Format(" OrganizationCode LIKE '{0}%'", CurrentUserOrganizationCode); _frmOrganization.DataSource = organizationTable.DefaultView.ToTable(); } DialogResult dialogResult = _frmOrganization.ShowDialog(); // 查询窗体返回值给控件赋值 if (dialogResult.Equals(DialogResult.OK)) { if (this.SelectMore) { _organizationCode = string.Empty; _organizationName = string.Empty; _organizationIDS = string.Empty; if (_frmOrganization.OrganizationRow != null || _frmOrganization.dataDT.Rows.Count > 0) { for (int i = 0; i < _frmOrganization.dataDT.Rows.Count; i++) { if (_frmOrganization.dataDT.Rows[i]["sel"].ToString().Equals("1")) { if (string.IsNullOrEmpty(_organizationCode)) { _organizationCode = _frmOrganization.dataDT.Rows[i]["OrganizationCode"].ToString(); _organizationName = _frmOrganization.dataDT.Rows[i]["OrganizationName"].ToString(); _organizationIDS = _frmOrganization.dataDT.Rows[i]["OrganizationID"].ToString(); } else { _organizationCode += "," + _frmOrganization.dataDT.Rows[i]["OrganizationCode"].ToString(); _organizationName += "," + _frmOrganization.dataDT.Rows[i]["OrganizationName"].ToString(); _organizationIDS += "," + _frmOrganization.dataDT.Rows[i]["OrganizationID"].ToString(); } } } this.Text = _organizationName; } } else { if (_frmOrganization.OrganizationRow != null) { _organizationID = Convert.ToInt32(_frmOrganization.OrganizationRow["OrganizationID"]); _organizationCode = _frmOrganization.OrganizationRow["OrganizationCode"].ToString(); _organizationName = _frmOrganization.OrganizationRow["OrganizationName"].ToString(); //this.Text = _frmOrganization.OrganizationRow["OrganizationName"].ToString(); this.Text = _frmOrganization.OrganizationRow["OrganizationCode"].ToString() + "→" + _frmOrganization.OrganizationRow["OrganizationFullName"].ToString(); } } } } /// /// 清除控件的值 /// /// /// private void txtOrganization_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete) { ClearControl(); } } private void txtOrganization_TextChanged(object sender, EventArgs e) { if (OrganizationValueChanged != null) { OrganizationValueChanged(this, new TextChangeEventArgs(this.txtOrganization.Text)); } } public class TextChangeEventArgs : EventArgs { public TextChangeEventArgs(string message) { } } #endregion #region 私有方法/函数 /// /// 初始化控件界面控件内容 /// private void InitializeComponent() { this.lblOrganization = new System.Windows.Forms.Label(); this.txtOrganization = new System.Windows.Forms.TextBox(); this.btnSearch = new System.Windows.Forms.Button(); this.SuspendLayout(); // // lblOrganization // this.lblOrganization.AutoSize = true; this.lblOrganization.BackColor = System.Drawing.Color.Transparent; //this.lblOrganization.Image = global::Dongke.IBOSS.Framework.Controls.Properties.Resources.bg; this.lblOrganization.Location = new System.Drawing.Point(3, 4); this.lblOrganization.Name = "lblOrganization"; this.lblOrganization.Size = new System.Drawing.Size(53, 12); this.lblOrganization.TabIndex = 0; this.lblOrganization.Text = "组织机构"; // // txtOrganization // this.txtOrganization.Font = new System.Drawing.Font("宋体", 9F); this.txtOrganization.Location = new System.Drawing.Point(62, 0); this.txtOrganization.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.txtOrganization.Name = "txtOrganization"; this.txtOrganization.ReadOnly = true; this.txtOrganization.Size = new System.Drawing.Size(155, 21); this.txtOrganization.TabIndex = 1; this.txtOrganization.TextChanged += new System.EventHandler(this.txtOrganization_TextChanged); this.txtOrganization.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtOrganization_KeyDown); // // btnSearch // this.btnSearch.Image = global::Dongke.IBOSS.PRD.Client.Controls.Properties.Resources.searchButton; this.btnSearch.Location = new System.Drawing.Point(223, 0); this.btnSearch.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.btnSearch.Name = "btnSearch"; this.btnSearch.Size = new System.Drawing.Size(21, 21); this.btnSearch.TabIndex = 2; this.btnSearch.UseVisualStyleBackColor = true; this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click); // // dkOrganizationSearchBox // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.Transparent; this.Controls.Add(this.btnSearch); this.Controls.Add(this.txtOrganization); this.Controls.Add(this.lblOrganization); this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.Name = "dkOrganizationSearchBox"; this.Size = new System.Drawing.Size(245, 21); this.SizeChanged += new System.EventHandler(this.dkOrganizationSearchBox_SizeChanged); this.ResumeLayout(false); this.PerformLayout(); } /// /// 根据控件的尺寸变化,改变控件内部控件的尺寸大小 /// protected void AdjustControl() { // 取得按钮控件的宽度和高度 int buttonWidth = (this.btnSearch.Visible) ? this.btnSearch.Width : 0; this.Height = this.btnSearch.Height; // 设置控件的尺寸和位置。 this.txtOrganization.Location = new System.Drawing.Point(this.lblOrganization.Width + ControlsConst.CONTROLSPACE, (this.Height - this.txtOrganization.Height) / 2); this.txtOrganization.Size = new System.Drawing.Size(this.Width - this.btnSearch.Width - (this.lblOrganization.Width + ControlsConst.CONTROLSPACE * 2), this.Height); this.btnSearch.Location = new System.Drawing.Point(this.Width - this.btnSearch.Width, (this.Height - this.btnSearch.Height) / 2); // 设置标签的尺寸和位置。 this.lblOrganization.Location = new System.Drawing.Point(0, (this.Height - this.lblOrganization.Height) / 2); this.lblOrganization.Size = new System.Drawing.Size(this.lblOrganization.Width, this.lblOrganization.Height); } /// /// 清空控件的值 /// /// /// public void ClearControl() { this.txtOrganization.Text = ""; this._organizationID = null; this._organizationCode = string.Empty; this._organizationName = string.Empty; this._organizationIDS = string.Empty; } /// /// 过滤数据源 /// /// /// protected DataTable SetFilter(DataTable dataSource) { if (dataSource is DataTable) { #region 过滤部门操作权限 if (PurviewOperateFlag) { if (LogInUserInfo.CurrentUser.CurrentUserEntity.UserRangeRightData != null) { string rowFilter = "0"; foreach (DataRow row in LogInUserInfo.CurrentUser.CurrentUserEntity.UserRangeRightData.Rows) { // 操作部门类型 if (int.Parse(row["PurviewType"].ToString()) == Constant.PurviewType.OperateOrganization.GetHashCode()) { if ("-1" == row["PurviewID"].ToString()) { rowFilter = ""; break; } rowFilter += "," + row["PurviewID"].ToString(); } } if (rowFilter != "") { dataSource.DefaultView.RowFilter = "OrganizationID IN (" + rowFilter + ")"; dataSource = dataSource.DefaultView.ToTable(); } } } #endregion #region 过滤部门查看权限 if (PurviewViewFlag) { if (LogInUserInfo.CurrentUser.CurrentUserEntity.UserRangeRightData != null) { string rowFilter = "0"; foreach (DataRow row in LogInUserInfo.CurrentUser.CurrentUserEntity.UserRangeRightData.Rows) { // 查看部门类型 if (int.Parse(row["PurviewType"].ToString()) == Constant.PurviewType.ViewOrganization.GetHashCode()) { if ("-1" == row["PurviewID"].ToString()) { rowFilter = ""; break; } rowFilter += "," + row["PurviewID"].ToString(); } } if (rowFilter != "") { dataSource.DefaultView.RowFilter = "OrganizationID IN (" + rowFilter + ")"; dataSource = dataSource.DefaultView.ToTable(); } } } #endregion } return dataSource; } #endregion } }