using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace LYFZ.Software.MainBusiness.InitialSet
{
public partial class FrmDepartmentSet : LYFZ.Software.UI.InitialSet.FrmDepartmentSet
{
public FrmDepartmentSet()
{
try
{
TreeNode node = new TreeNode();
node.Text = "所有部门";
this.trvdep.Nodes.Add(node);
GetAllFunc(0, node);
this.trvdep.ExpandAll();
//trvdep.SelectedNode = trvdep.Nodes[0].Nodes[0];
trvdep.SelectedNode = trvdep.Nodes[0];
trvdep.Select();
}
catch (Exception ex) { MessageBoxCustom.Show(ex.Message); }
this.FormClosed += FrmDepartmentSet_FormClosed;
}
void FrmDepartmentSet_FormClosed(object sender, FormClosedEventArgs e)
{
if (bl==true)
{
this.DialogResult = DialogResult.OK;
}
}
bool bl = false;
#region 获取数据
///
/// 构造权限树
///
///
public void GetAllFunc(int parentmenuid, TreeNode parent)
{
LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
DataTable dt = bll.GetList("Dt_ClassParentID=" + parentmenuid + "").Tables[0];
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeNode nodel = new TreeNode();
nodel.Text = dt.Rows[i]["Dt_Name"].ToString();
nodel.Tag = dt.Rows[i]["ID"].ToString();
GetparentFunc(nodel); //构造子节点功能
parent.Nodes.Add(nodel);
}
}
}
///
/// 构造子节点功能
///
///
public void GetparentFunc(TreeNode parent)
{
int memuid = Convert.ToInt32(parent.Tag.ToString());
LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
DataTable dt = bll.GetList("Dt_ClassParentID=" + memuid + "").Tables[0]; ;
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeNode nodel = new TreeNode();
nodel.Text = dt.Rows[i]["Dt_Name"].ToString();
nodel.Tag = dt.Rows[i]["ID"].ToString();
parent.Nodes.Add(nodel);
}
}
}
#endregion
#region 增加
protected override void btnSave_Click(object sender, EventArgs e)
{
try
{
//MessageBoxCustom.Show(trvdep.SelectedNode.Tag.ToString());
LYFZ.Model.Model_ErpDepartment model = new Model.Model_ErpDepartment();
LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
DataTable dt = new DataTable();
if (string.IsNullOrEmpty(txtDt_Name.Text))
{
MessageBoxCustom.Show("部门名称不能为空!");
return;
}
if (this.trvdep.SelectedNode.Text != "所有部门")
{
MessageBoxCustom.Show("此节点不能添加!请添加一级部门");
BindRefresh();
return;
}
if (bll.Exists("Dt_Name", this.txtDt_Name.Text) == true)
{
MessageBoxCustom.Show("部门名称已经存在!");
this.txtDt_Name.Text = "";
this.txtDt_Name.Focus();
return;
}
model.Dt_DividedShop = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.GetLocalCompanyInfoID();
model.Dt_Name = this.txtDt_Name.Text;
if (this.trvdep.SelectedNode.Text == "所有部门")
{
model.Dt_ClassParentID = 0;
}
else { model.Dt_ClassParentID = Convert.ToInt32(this.trvdep.SelectedNode.Tag); }
if (bll.Add(model) == true)
{
MessageBoxCustom.Show("增加成功!");
BindRefresh();
}
else { MessageBoxCustom.Show("增加失败!"); }
}
catch { MessageBoxCustom.Show("请选择你要增加的节点"); }
}
#endregion
#region 显示树节点
protected override void trvdep_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
if (trvdep.SelectedNode.Text == "所有部门")
{
this.txtDt_Name2.Text = "";
}
else
{
//修改
DataTable dt = bll.GetList("ID=" + trvdep.SelectedNode.Tag + "").Tables[0];
this.txtDt_Name2.Text = dt.Rows[0]["Dt_Name"].ToString();
}
}
catch (Exception ex) { MessageBoxCustom.Show(ex.Message); }
}
#endregion
#region 删除
protected override void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(trvdep.SelectedNode.Text))
{
MessageBoxCustom.Show("请选择你要删除的子节点!");
return;
}
if (trvdep.SelectedNode.Text == "所有部门")
{
MessageBoxCustom.Show("此节点不能删除,请选择子节点!");
return;
}
LYFZ.Model.Model_ErpSystemCategory model = new Model.Model_ErpSystemCategory();
LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
DataTable dt = bll.GetList("ID='" + trvdep.SelectedNode.Tag.ToString() + "'").Tables[0];
int id = Convert.ToInt32(dt.Rows[0]["ID"]);
if (MessageBoxCustom.Show("你确定要删除吗?", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (bll.Delete(id) == true)
{
MessageBoxCustom.Show("删除成功!");
BindRefresh();
}
else { MessageBoxCustom.Show("删除失败!"); }
}
}
catch { MessageBoxCustom.Show("请选择你要删除的节点"); }
}
#endregion
#region 修改
protected override void btnUpdate_Click(object sender, EventArgs e)
{
try
{
LYFZ.Model.Model_ErpDepartment model = new Model.Model_ErpDepartment();
LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
if (string.IsNullOrEmpty(txtDt_Name2.Text))
{
MessageBoxCustom.Show("部门名称不能为空!");
return;
}
if (bll.Exists("Dt_Name", this.txtDt_Name2.Text) == true)
{
MessageBoxCustom.Show("部门名称已经存在!");
this.txtDt_Name.Text = "";
this.txtDt_Name.Focus();
return;
}
model.ID = Convert.ToInt32(trvdep.SelectedNode.Tag);
model.Dt_Name = this.txtDt_Name2.Text;
if (bll.Update(model, "ID", "=", overlookFieldList: "ID,Dt_ClassParentID") == true)
{
MessageBoxCustom.Show("更新成功!");
BindRefresh();
}
else { MessageBoxCustom.Show("更新失败!"); }
}
catch { MessageBoxCustom.Show("请选择你要修改的节点"); }
}
#endregion
#region 关闭
protected override void btnClose_Click(object sender, EventArgs e)
{
this.Close();
if (bl == true)
{
this.DialogResult = DialogResult.OK;
}
}
#endregion
#region 刷新数据
///
/// 刷新数据
///
public void BindRefresh()
{
this.txtDt_Name2.Text = "";
this.txtDt_Name2.Focus();
this.trvdep.Nodes.Clear();
TreeNode node = new TreeNode();
node.Text = "所有部门";
this.trvdep.Nodes.Add(node);
GetAllFunc(0, node);
this.trvdep.ExpandAll();
trvdep.SelectedNode = trvdep.Nodes[0];
trvdep.Select();
bl = true;
}
#endregion
}
}