FrmDepartmentSet.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace LYFZ.Software.MainBusiness.InitialSet
  10. {
  11. public partial class FrmDepartmentSet : LYFZ.Software.UI.InitialSet.FrmDepartmentSet
  12. {
  13. public FrmDepartmentSet()
  14. {
  15. try
  16. {
  17. TreeNode node = new TreeNode();
  18. node.Text = "所有部门";
  19. this.trvdep.Nodes.Add(node);
  20. GetAllFunc(0, node);
  21. this.trvdep.ExpandAll();
  22. //trvdep.SelectedNode = trvdep.Nodes[0].Nodes[0];
  23. trvdep.SelectedNode = trvdep.Nodes[0];
  24. trvdep.Select();
  25. }
  26. catch (Exception ex) { MessageBoxCustom.Show(ex.Message); }
  27. this.FormClosed += FrmDepartmentSet_FormClosed;
  28. }
  29. void FrmDepartmentSet_FormClosed(object sender, FormClosedEventArgs e)
  30. {
  31. if (bl==true)
  32. {
  33. this.DialogResult = DialogResult.OK;
  34. }
  35. }
  36. bool bl = false;
  37. #region 获取数据
  38. /// <summary>
  39. /// 构造权限树
  40. /// </summary>
  41. /// <param name="parentmenuid"></param>
  42. public void GetAllFunc(int parentmenuid, TreeNode parent)
  43. {
  44. LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
  45. DataTable dt = bll.GetList("Dt_ClassParentID=" + parentmenuid + "").Tables[0];
  46. if (dt != null)
  47. {
  48. for (int i = 0; i < dt.Rows.Count; i++)
  49. {
  50. TreeNode nodel = new TreeNode();
  51. nodel.Text = dt.Rows[i]["Dt_Name"].ToString();
  52. nodel.Tag = dt.Rows[i]["ID"].ToString();
  53. GetparentFunc(nodel); //构造子节点功能
  54. parent.Nodes.Add(nodel);
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// 构造子节点功能
  60. /// </summary>
  61. /// <param name="parent"></param>
  62. public void GetparentFunc(TreeNode parent)
  63. {
  64. int memuid = Convert.ToInt32(parent.Tag.ToString());
  65. LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
  66. DataTable dt = bll.GetList("Dt_ClassParentID=" + memuid + "").Tables[0]; ;
  67. if (dt != null)
  68. {
  69. for (int i = 0; i < dt.Rows.Count; i++)
  70. {
  71. TreeNode nodel = new TreeNode();
  72. nodel.Text = dt.Rows[i]["Dt_Name"].ToString();
  73. nodel.Tag = dt.Rows[i]["ID"].ToString();
  74. parent.Nodes.Add(nodel);
  75. }
  76. }
  77. }
  78. #endregion
  79. #region 增加
  80. protected override void btnSave_Click(object sender, EventArgs e)
  81. {
  82. try
  83. {
  84. //MessageBoxCustom.Show(trvdep.SelectedNode.Tag.ToString());
  85. LYFZ.Model.Model_ErpDepartment model = new Model.Model_ErpDepartment();
  86. LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
  87. DataTable dt = new DataTable();
  88. if (string.IsNullOrEmpty(txtDt_Name.Text))
  89. {
  90. MessageBoxCustom.Show("部门名称不能为空!");
  91. return;
  92. }
  93. if (this.trvdep.SelectedNode.Text != "所有部门")
  94. {
  95. MessageBoxCustom.Show("此节点不能添加!请添加一级部门");
  96. BindRefresh();
  97. return;
  98. }
  99. if (bll.Exists("Dt_Name", this.txtDt_Name.Text) == true)
  100. {
  101. MessageBoxCustom.Show("部门名称已经存在!");
  102. this.txtDt_Name.Text = "";
  103. this.txtDt_Name.Focus();
  104. return;
  105. }
  106. model.Dt_DividedShop = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.GetLocalCompanyInfoID();
  107. model.Dt_Name = this.txtDt_Name.Text;
  108. if (this.trvdep.SelectedNode.Text == "所有部门")
  109. {
  110. model.Dt_ClassParentID = 0;
  111. }
  112. else { model.Dt_ClassParentID = Convert.ToInt32(this.trvdep.SelectedNode.Tag); }
  113. if (bll.Add(model) == true)
  114. {
  115. MessageBoxCustom.Show("增加成功!");
  116. BindRefresh();
  117. }
  118. else { MessageBoxCustom.Show("增加失败!"); }
  119. }
  120. catch { MessageBoxCustom.Show("请选择你要增加的节点"); }
  121. }
  122. #endregion
  123. #region 显示树节点
  124. protected override void trvdep_AfterSelect(object sender, TreeViewEventArgs e)
  125. {
  126. try
  127. {
  128. LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
  129. if (trvdep.SelectedNode.Text == "所有部门")
  130. {
  131. this.txtDt_Name2.Text = "";
  132. }
  133. else
  134. {
  135. //修改
  136. DataTable dt = bll.GetList("ID=" + trvdep.SelectedNode.Tag + "").Tables[0];
  137. this.txtDt_Name2.Text = dt.Rows[0]["Dt_Name"].ToString();
  138. }
  139. }
  140. catch (Exception ex) { MessageBoxCustom.Show(ex.Message); }
  141. }
  142. #endregion
  143. #region 删除
  144. protected override void btnDelete_Click(object sender, EventArgs e)
  145. {
  146. try
  147. {
  148. if (string.IsNullOrEmpty(trvdep.SelectedNode.Text))
  149. {
  150. MessageBoxCustom.Show("请选择你要删除的子节点!");
  151. return;
  152. }
  153. if (trvdep.SelectedNode.Text == "所有部门")
  154. {
  155. MessageBoxCustom.Show("此节点不能删除,请选择子节点!");
  156. return;
  157. }
  158. LYFZ.Model.Model_ErpSystemCategory model = new Model.Model_ErpSystemCategory();
  159. LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
  160. DataTable dt = bll.GetList("ID='" + trvdep.SelectedNode.Tag.ToString() + "'").Tables[0];
  161. int id = Convert.ToInt32(dt.Rows[0]["ID"]);
  162. if (MessageBoxCustom.Show("你确定要删除吗?", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  163. {
  164. if (bll.Delete(id) == true)
  165. {
  166. MessageBoxCustom.Show("删除成功!");
  167. BindRefresh();
  168. }
  169. else { MessageBoxCustom.Show("删除失败!"); }
  170. }
  171. }
  172. catch { MessageBoxCustom.Show("请选择你要删除的节点"); }
  173. }
  174. #endregion
  175. #region 修改
  176. protected override void btnUpdate_Click(object sender, EventArgs e)
  177. {
  178. try
  179. {
  180. LYFZ.Model.Model_ErpDepartment model = new Model.Model_ErpDepartment();
  181. LYFZ.BLL.BLL_ErpDepartment bll = new BLL.BLL_ErpDepartment();
  182. if (string.IsNullOrEmpty(txtDt_Name2.Text))
  183. {
  184. MessageBoxCustom.Show("部门名称不能为空!");
  185. return;
  186. }
  187. if (bll.Exists("Dt_Name", this.txtDt_Name2.Text) == true)
  188. {
  189. MessageBoxCustom.Show("部门名称已经存在!");
  190. this.txtDt_Name.Text = "";
  191. this.txtDt_Name.Focus();
  192. return;
  193. }
  194. model.ID = Convert.ToInt32(trvdep.SelectedNode.Tag);
  195. model.Dt_Name = this.txtDt_Name2.Text;
  196. if (bll.Update(model, "ID", "=", overlookFieldList: "ID,Dt_ClassParentID") == true)
  197. {
  198. MessageBoxCustom.Show("更新成功!");
  199. BindRefresh();
  200. }
  201. else { MessageBoxCustom.Show("更新失败!"); }
  202. }
  203. catch { MessageBoxCustom.Show("请选择你要修改的节点"); }
  204. }
  205. #endregion
  206. #region 关闭
  207. protected override void btnClose_Click(object sender, EventArgs e)
  208. {
  209. this.Close();
  210. if (bl == true)
  211. {
  212. this.DialogResult = DialogResult.OK;
  213. }
  214. }
  215. #endregion
  216. #region 刷新数据
  217. /// <summary>
  218. /// 刷新数据
  219. /// </summary>
  220. public void BindRefresh()
  221. {
  222. this.txtDt_Name2.Text = "";
  223. this.txtDt_Name2.Focus();
  224. this.trvdep.Nodes.Clear();
  225. TreeNode node = new TreeNode();
  226. node.Text = "所有部门";
  227. this.trvdep.Nodes.Add(node);
  228. GetAllFunc(0, node);
  229. this.trvdep.ExpandAll();
  230. trvdep.SelectedNode = trvdep.Nodes[0];
  231. trvdep.Select();
  232. bl = true;
  233. }
  234. #endregion
  235. }
  236. }