FormEditMenu.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using SIMDP.BLL;
  12. using SIMDP.Model;
  13. using ProjectBase.Data.Logs;
  14. using System.IO;
  15. using ProjectBase.Util;
  16. using ProjectBase.Data.BaseDAL;
  17. namespace SIMDP.View
  18. {
  19. public partial class FormEditMenu : DevExpress.XtraEditors.XtraForm
  20. {
  21. //BlMenuInfo blMenu = new BlMenuInfo();
  22. private MoMenuInfo moMenu = new MoMenuInfo();
  23. /// <summary>
  24. /// 定义委托
  25. /// </summary>
  26. public delegate void save();
  27. /// <summary>
  28. /// 定义事件,保存数据成功后刷新FormMenu中的数据
  29. /// </summary>
  30. public event save saveData;
  31. /// <summary>
  32. /// 构造函数
  33. /// </summary>
  34. /// <param name="menuInfo">为null时表示添加操作,不为null时表示修改操作</param>
  35. public FormEditMenu( MoMenuInfo menuInfo)
  36. {
  37. InitializeComponent();
  38. moMenu = menuInfo;
  39. InitControls(menuInfo);
  40. }
  41. /// <summary>
  42. /// 初始化控件
  43. /// </summary>
  44. /// <param name="menuInfo"></param>
  45. private void InitControls(MoMenuInfo menuInfo)
  46. {
  47. #region 初始化Winform类型控件
  48. BindingSource bsType = new BindingSource();
  49. bsType.DataSource = SysEnvironment.dirWinformType;
  50. this.lookUp_WinformType.Properties.DataSource = bsType;
  51. this.lookUp_WinformType.Properties.ValueMember = "Key";
  52. this.lookUp_WinformType.Properties.DisplayMember = "Value";
  53. this.lookUp_WinformType.Properties.NullText = "请您选择";
  54. #endregion
  55. #region 初始化父ID控件
  56. DataTable dt = new DataTable();
  57. dt.Columns.Add("ImageIndex",typeof(int));
  58. dt.Columns.Add("ID",typeof(string));
  59. dt.Columns.Add("PID", typeof(string));
  60. dt.Columns.Add("Name", typeof(string));
  61. dt.Columns.Add("FunctionId", typeof(string));
  62. dt.Columns.Add("Seq", typeof(string));
  63. List<MoMenuInfo> list = BLLFactory<BlMenuInfo>.Instance.GetAll();
  64. DataRow dr = null;
  65. foreach (MoMenuInfo info in list)
  66. {
  67. dr = dt.NewRow();
  68. dr["ImageIndex"] = 0;
  69. dr["ID"] = info.ID.ToString();
  70. dr["PID"] = info.PID.ToString();
  71. dr["Name"] = info.Name;
  72. dr["FunctionId"] = info.FunctionId;
  73. dr["Seq"] = info.Seq;
  74. dt.Rows.Add(dr);
  75. }
  76. //增加一行空的
  77. dr = dt.NewRow();
  78. dr["ID"] = "0"; //使用0代替-1,避免出现节点的嵌套显示,因为-1已经作为了一般节点的顶级标识
  79. dr["PID"] = "-1";
  80. dr["Name"] = "无";
  81. dt.Rows.InsertAt(dr, 0);
  82. //设置图形序号
  83. //this.treeListLookUpEdit1TreeList.SelectImageList = this.imageList2;
  84. //this.treeListLookUpEdit1TreeList.StateImageList = this.imageList2;
  85. this.treeLookUp_PId.Properties.TreeList.KeyFieldName = "ID";
  86. this.treeLookUp_PId.Properties.TreeList.ParentFieldName = "PID";
  87. this.treeLookUp_PId.Properties.DataSource = dt;
  88. this.treeLookUp_PId.Properties.ValueMember = "ID";
  89. this.treeLookUp_PId.Properties.DisplayMember = "Name";
  90. #endregion
  91. #region 初始化其他控件
  92. if (menuInfo == null)
  93. {
  94. this.btn_Save.Enabled = false;
  95. this.treeLookUp_PId.EditValue = "0";
  96. this.txt_Seq.Text = Convert.ToString(BLLFactory<BlMenuInfo>.Instance.GetRecordCount()+1);
  97. return;
  98. }
  99. this.btn_Add.Enabled = false;
  100. this.txt_Id.Enabled = false;
  101. this.txt_Id.Text = menuInfo.ID;
  102. this.treeLookUp_PId.EditValue = menuInfo.PID;
  103. this.txt_Name.Text = menuInfo.Name;
  104. this.txt_Seq.Text = menuInfo.Seq.ToString();
  105. this.txt_FunctionID.Text = menuInfo.FunctionId;
  106. this.txt_Icon.Text = menuInfo.Icon;
  107. this.lookUp_WinformType.EditValue = menuInfo.WinformType;
  108. this.txt_WebIcon.Text = menuInfo.WebIcon;
  109. this.txt_Url.Text = menuInfo.Url;
  110. if (menuInfo.Visible)
  111. {
  112. this.check_Visible.CheckState = CheckState.Checked;
  113. }
  114. else
  115. {
  116. this.check_Visible.CheckState = CheckState.Unchecked;
  117. }
  118. if (menuInfo.Deleted)
  119. {
  120. this.check_Delete.CheckState = CheckState.Checked;
  121. }
  122. else
  123. {
  124. this.check_Delete.CheckState = CheckState.Unchecked;
  125. }
  126. #endregion
  127. }
  128. private void btn_Cancel_Click(object sender, EventArgs e)
  129. {
  130. this.Close();
  131. }
  132. private void btn_SelectIcon_Click(object sender, EventArgs e)
  133. {
  134. string file = GetIconPath();
  135. if (!string.IsNullOrEmpty(file))
  136. {
  137. this.txt_Icon.Text = file;
  138. }
  139. }
  140. /// <summary>
  141. /// 选择图标文件
  142. /// </summary>
  143. /// <returns></returns>
  144. private string GetIconPath()
  145. {
  146. //string iconFile = "Icon File(*.ico)|*.ico|Image Files(*.BMP;*.bmp;*.JPG;*.jpg;*.GIF;*.gif;*.png;*.PNG)|(*.BMP;*.bmp;*.JPG;*.jpg;*.GIF;*.gif;*.png;*.PNG)|All File(*.*)|*.*";
  147. string iconFile = "Image Files(*.BMP;*.bmp;*.JPG;*.jpg;*.GIF;*.gif;*.png;*.PNG)|(*.BMP;*.bmp;*.JPG;*.jpg;*.GIF;*.gif;*.png;*.PNG)|Icon File(*.ico)|*.ico|All File(*.*)|*.*";
  148. OpenFileDialog dialog = new OpenFileDialog();
  149. dialog.Filter = iconFile;
  150. dialog.Title = "选择图标文件";
  151. dialog.RestoreDirectory = true;
  152. dialog.FileName = "";
  153. string file = null;
  154. if (!string.IsNullOrEmpty(Application.StartupPath))
  155. {
  156. dialog.InitialDirectory = Application.StartupPath;
  157. }
  158. if (dialog.ShowDialog() == DialogResult.OK)
  159. {
  160. file = dialog.FileName;
  161. }
  162. string result = "";
  163. if (!string.IsNullOrEmpty(file))
  164. {
  165. //result = file.Replace(Application.StartupPath, "").Trim('\\');
  166. result = file;
  167. }
  168. return result;
  169. }
  170. private void btn_SelectWebIcon_Click(object sender, EventArgs e)
  171. {
  172. string file = GetIconPath();
  173. if (!string.IsNullOrEmpty(file))
  174. {
  175. this.txt_WebIcon.Text = file;
  176. }
  177. }
  178. private void btn_Save_Click(object sender, EventArgs e)
  179. {
  180. if (string.IsNullOrEmpty(txt_Id.Text))
  181. {
  182. DevExpress.XtraEditors.XtraMessageBox.Show("请输入ID","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  183. return;
  184. }
  185. if (string.IsNullOrEmpty(txt_Name.Text))
  186. {
  187. DevExpress.XtraEditors.XtraMessageBox.Show("请输入显示名称。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  188. return;
  189. }
  190. if (lookUp_WinformType.Text == "请您选择")
  191. {
  192. DevExpress.XtraEditors.XtraMessageBox.Show("请输入Winform类型。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  193. return;
  194. }
  195. try
  196. {
  197. moMenu.ID = txt_Id.Text;
  198. moMenu.Name = txt_Name.Text;
  199. if (treeLookUp_PId.EditValue == null || treeLookUp_PId.EditValue.Equals("0"))
  200. {
  201. moMenu.PID = null;
  202. }
  203. else
  204. {
  205. moMenu.PID = treeLookUp_PId.EditValue.ToString();
  206. }
  207. moMenu.Seq = Convert.ToInt64(txt_Seq.Text) ;
  208. moMenu.FunctionId = txt_FunctionID.Text;
  209. if (moMenu.Icon != txt_Icon.Text)
  210. {
  211. moMenu.Icon = ConvertIconPath(txt_Icon.Text);
  212. }
  213. moMenu.WinformType = lookUp_WinformType.EditValue.ToString();
  214. if (moMenu.WebIcon != txt_WebIcon.Text)
  215. {
  216. moMenu.WebIcon = ConvertIconPath(txt_WebIcon.Text) ;
  217. }
  218. moMenu.Url = txt_Url.Text;
  219. if (check_Visible.CheckState == CheckState.Checked)
  220. {
  221. moMenu.Visible = true;
  222. }
  223. else if (check_Visible.CheckState == CheckState.Unchecked)
  224. {
  225. moMenu.Visible = false;
  226. }
  227. if (check_Delete.CheckState == CheckState.Checked)
  228. {
  229. moMenu.Deleted = true;
  230. }
  231. else if (check_Delete.CheckState == CheckState.Unchecked)
  232. {
  233. moMenu.Deleted = false;
  234. }
  235. bool flag = BLLFactory<BlMenuInfo>.Instance.Update(moMenu,moMenu.Seq);
  236. //BlAuthoryRight bl = new BlAuthoryRight();
  237. BLLFactory<BlAuthoryRight>.Instance.CreateAuthoryRight();
  238. if (flag)
  239. {
  240. DevExpress.XtraEditors.XtraMessageBox.Show("修改成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  241. saveData();
  242. this.Close();
  243. }
  244. }
  245. catch (Exception ex)
  246. {
  247. DevExpress.XtraEditors.XtraMessageBox.Show("修改失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  248. LogHelper.log.Error(string.Format("修改数据库menu出现错误:{0}", ex));
  249. }
  250. }
  251. private void btn_Add_Click(object sender, EventArgs e)
  252. {
  253. if (string.IsNullOrEmpty(txt_Id.Text))
  254. {
  255. DevExpress.XtraEditors.XtraMessageBox.Show("请输入ID", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  256. return;
  257. }
  258. if (string.IsNullOrEmpty(txt_Name.Text))
  259. {
  260. DevExpress.XtraEditors.XtraMessageBox.Show("请输入显示名称。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  261. return;
  262. }
  263. if (lookUp_WinformType.Text == "请您选择")
  264. {
  265. DevExpress.XtraEditors.XtraMessageBox.Show("请输入Winform类型。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  266. return;
  267. }
  268. try
  269. {
  270. MoMenuInfo mo = new MoMenuInfo();
  271. mo.ID = txt_Id.Text;
  272. mo.Name = txt_Name.Text;
  273. if (treeLookUp_PId.EditValue == null || treeLookUp_PId.EditValue.Equals("0"))
  274. {
  275. mo.PID = null;
  276. }
  277. else
  278. {
  279. mo.PID = treeLookUp_PId.EditValue.ToString();
  280. }
  281. mo.Seq = Convert.ToInt64(txt_Seq.Text) ;
  282. mo.FunctionId = txt_FunctionID.Text;
  283. mo.Icon = ConvertIconPath(txt_Icon.Text) ;
  284. mo.WinformType = lookUp_WinformType.EditValue.ToString();
  285. mo.WebIcon = ConvertIconPath(txt_WebIcon.Text) ;
  286. mo.Url = txt_Url.Text;
  287. mo.Creator_ID = SysEnvironment.CurrentLoginID;
  288. mo.CreateTime = DateTime.Now;
  289. mo.Deleted = false;
  290. if (check_Visible.CheckState == CheckState.Checked)
  291. {
  292. mo.Visible = true;
  293. }
  294. else if (check_Visible.CheckState == CheckState.Unchecked)
  295. {
  296. mo.Visible = false;
  297. }
  298. if (check_Delete.CheckState == CheckState.Checked)
  299. {
  300. mo.Deleted = true;
  301. }
  302. else if (check_Delete.CheckState == CheckState.Unchecked)
  303. {
  304. mo.Deleted = false;
  305. }
  306. bool flag = BLLFactory<BlMenuInfo>.Instance.Insert(mo);
  307. if (flag)
  308. {
  309. //BlAuthoryRight bl = new BlAuthoryRight();
  310. BLLFactory<BlAuthoryRight>.Instance.CreateAuthoryRight();
  311. DevExpress.XtraEditors.XtraMessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  312. saveData();
  313. this.Close();
  314. }
  315. }
  316. catch (Exception ex)
  317. {
  318. DevExpress.XtraEditors.XtraMessageBox.Show("添加失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  319. LogHelper.log.Error(string.Format("添加数据库menu出现错误:{0}", ex));
  320. }
  321. }
  322. /// <summary>
  323. /// 将所选择的图像路径转换为存入数据库的格式
  324. /// </summary>
  325. /// <param name="filePath"></param>
  326. /// <returns></returns>
  327. private string ConvertIconPath(string filePath)
  328. {
  329. try
  330. {
  331. if (string.IsNullOrEmpty(filePath))
  332. {
  333. return null;
  334. }
  335. string str = Application.StartupPath + "\\Images";
  336. if (filePath.Contains(str))
  337. {
  338. filePath = filePath.Replace(Application.StartupPath, string.Empty);
  339. return filePath;
  340. }
  341. string[] array = filePath.Split('\\');
  342. string newFile = Application.StartupPath + "\\Images\\" + array[array.Length - 1];
  343. if (File.Exists(newFile))
  344. {
  345. File.Delete(newFile);
  346. File.Copy(filePath, newFile);
  347. }
  348. else
  349. {
  350. File.Copy(filePath, newFile);
  351. }
  352. string result = "";
  353. result = newFile.Replace(Application.StartupPath, string.Empty);
  354. return result;
  355. }
  356. catch (Exception ex)
  357. {
  358. LogHelper.log.Error(string.Format("选择图像出现错误:{0}", ex));
  359. return null;
  360. }
  361. }
  362. private void ParentId_EditValueChanged(object sender, EventArgs e)
  363. {
  364. if (moMenu != null) //如果是编辑状态,未修改父ID,则ID与功能ID不变
  365. {
  366. if (moMenu.PID == this.treeLookUp_PId.EditValue.ToString())
  367. {
  368. this.txt_Id.Text = moMenu.ID;
  369. this.txt_FunctionID.Text = moMenu.FunctionId;
  370. return;
  371. }
  372. }
  373. if (this.treeLookUp_PId.EditValue.ToString() == "0")
  374. {
  375. this.lookUp_WinformType.EditValue = "1";
  376. string id = BLLFactory<BlMenuInfo>.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), null);
  377. this.txt_Id.Text = id;
  378. this.txt_FunctionID.Text = id;
  379. }
  380. else
  381. {
  382. MoMenuInfo mo = BLLFactory<BlMenuInfo>.Instance.FindSingle(string.Format(" menu_id = '{0}'", treeLookUp_PId.EditValue.ToString()));
  383. if (mo.WinformType == "3")
  384. {
  385. DevExpress.XtraEditors.XtraMessageBox.Show("此菜单无法作为父级ID,请重新选择", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  386. treeLookUp_PId.EditValue = "0";
  387. return;
  388. }
  389. string winformType = (Convert.ToInt32(mo.WinformType) + 1).ToString();
  390. this.lookUp_WinformType.EditValue = winformType ;
  391. string id = BLLFactory<BlMenuInfo>.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), treeLookUp_PId.EditValue.ToString());
  392. this.txt_Id.Text = id;
  393. this.txt_FunctionID.Text = id;
  394. }
  395. }
  396. private void WInformType_EditValueChanged(object sender, EventArgs e)
  397. {
  398. if (treeLookUp_PId.EditValue == null || treeLookUp_PId.EditValue.Equals("0"))
  399. {
  400. return;
  401. }
  402. this.txt_Id.Text = BLLFactory<BlMenuInfo>.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), treeLookUp_PId.EditValue.ToString());
  403. this.txt_FunctionID.Text = BLLFactory<BlMenuInfo>.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), treeLookUp_PId.EditValue.ToString());
  404. }
  405. }
  406. }