123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using DevExpress.XtraEditors;
- using SIMDP.BLL;
- using SIMDP.Model;
- using ProjectBase.Data.Logs;
- using System.IO;
- using ProjectBase.Util;
- using ProjectBase.Data.BaseDAL;
- namespace SIMDP.View
- {
- public partial class FormEditMenu : DevExpress.XtraEditors.XtraForm
- {
- //BlMenuInfo blMenu = new BlMenuInfo();
- private MoMenuInfo moMenu = new MoMenuInfo();
- /// <summary>
- /// 定义委托
- /// </summary>
- public delegate void save();
- /// <summary>
- /// 定义事件,保存数据成功后刷新FormMenu中的数据
- /// </summary>
- public event save saveData;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="menuInfo">为null时表示添加操作,不为null时表示修改操作</param>
- public FormEditMenu( MoMenuInfo menuInfo)
- {
- InitializeComponent();
- moMenu = menuInfo;
- InitControls(menuInfo);
- }
- /// <summary>
- /// 初始化控件
- /// </summary>
- /// <param name="menuInfo"></param>
- private void InitControls(MoMenuInfo menuInfo)
- {
-
- #region 初始化Winform类型控件
- BindingSource bsType = new BindingSource();
- bsType.DataSource = SysEnvironment.dirWinformType;
- this.lookUp_WinformType.Properties.DataSource = bsType;
- this.lookUp_WinformType.Properties.ValueMember = "Key";
- this.lookUp_WinformType.Properties.DisplayMember = "Value";
- this.lookUp_WinformType.Properties.NullText = "请您选择";
- #endregion
- #region 初始化父ID控件
- DataTable dt = new DataTable();
- dt.Columns.Add("ImageIndex",typeof(int));
- dt.Columns.Add("ID",typeof(string));
- dt.Columns.Add("PID", typeof(string));
- dt.Columns.Add("Name", typeof(string));
- dt.Columns.Add("FunctionId", typeof(string));
- dt.Columns.Add("Seq", typeof(string));
- List<MoMenuInfo> list = BLLFactory<BlMenuInfo>.Instance.GetAll();
- DataRow dr = null;
- foreach (MoMenuInfo info in list)
- {
- dr = dt.NewRow();
- dr["ImageIndex"] = 0;
- dr["ID"] = info.ID.ToString();
- dr["PID"] = info.PID.ToString();
- dr["Name"] = info.Name;
- dr["FunctionId"] = info.FunctionId;
- dr["Seq"] = info.Seq;
- dt.Rows.Add(dr);
- }
- //增加一行空的
- dr = dt.NewRow();
- dr["ID"] = "0"; //使用0代替-1,避免出现节点的嵌套显示,因为-1已经作为了一般节点的顶级标识
- dr["PID"] = "-1";
- dr["Name"] = "无";
- dt.Rows.InsertAt(dr, 0);
- //设置图形序号
- //this.treeListLookUpEdit1TreeList.SelectImageList = this.imageList2;
- //this.treeListLookUpEdit1TreeList.StateImageList = this.imageList2;
- this.treeLookUp_PId.Properties.TreeList.KeyFieldName = "ID";
- this.treeLookUp_PId.Properties.TreeList.ParentFieldName = "PID";
- this.treeLookUp_PId.Properties.DataSource = dt;
- this.treeLookUp_PId.Properties.ValueMember = "ID";
- this.treeLookUp_PId.Properties.DisplayMember = "Name";
- #endregion
- #region 初始化其他控件
- if (menuInfo == null)
- {
- this.btn_Save.Enabled = false;
- this.treeLookUp_PId.EditValue = "0";
- this.txt_Seq.Text = Convert.ToString(BLLFactory<BlMenuInfo>.Instance.GetRecordCount()+1);
- return;
- }
- this.btn_Add.Enabled = false;
- this.txt_Id.Enabled = false;
- this.txt_Id.Text = menuInfo.ID;
- this.treeLookUp_PId.EditValue = menuInfo.PID;
- this.txt_Name.Text = menuInfo.Name;
- this.txt_Seq.Text = menuInfo.Seq.ToString();
- this.txt_FunctionID.Text = menuInfo.FunctionId;
- this.txt_Icon.Text = menuInfo.Icon;
- this.lookUp_WinformType.EditValue = menuInfo.WinformType;
- this.txt_WebIcon.Text = menuInfo.WebIcon;
- this.txt_Url.Text = menuInfo.Url;
- if (menuInfo.Visible)
- {
- this.check_Visible.CheckState = CheckState.Checked;
- }
- else
- {
- this.check_Visible.CheckState = CheckState.Unchecked;
- }
- if (menuInfo.Deleted)
- {
- this.check_Delete.CheckState = CheckState.Checked;
- }
- else
- {
- this.check_Delete.CheckState = CheckState.Unchecked;
- }
- #endregion
- }
- private void btn_Cancel_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void btn_SelectIcon_Click(object sender, EventArgs e)
- {
- string file = GetIconPath();
- if (!string.IsNullOrEmpty(file))
- {
- this.txt_Icon.Text = file;
- }
- }
- /// <summary>
- /// 选择图标文件
- /// </summary>
- /// <returns></returns>
- private string GetIconPath()
- {
- //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(*.*)|*.*";
- 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(*.*)|*.*";
- OpenFileDialog dialog = new OpenFileDialog();
- dialog.Filter = iconFile;
- dialog.Title = "选择图标文件";
- dialog.RestoreDirectory = true;
- dialog.FileName = "";
- string file = null;
- if (!string.IsNullOrEmpty(Application.StartupPath))
- {
- dialog.InitialDirectory = Application.StartupPath;
- }
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- file = dialog.FileName;
- }
- string result = "";
- if (!string.IsNullOrEmpty(file))
- {
- //result = file.Replace(Application.StartupPath, "").Trim('\\');
- result = file;
- }
- return result;
- }
- private void btn_SelectWebIcon_Click(object sender, EventArgs e)
- {
- string file = GetIconPath();
- if (!string.IsNullOrEmpty(file))
- {
- this.txt_WebIcon.Text = file;
- }
- }
- private void btn_Save_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(txt_Id.Text))
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("请输入ID","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
- return;
- }
- if (string.IsNullOrEmpty(txt_Name.Text))
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("请输入显示名称。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- if (lookUp_WinformType.Text == "请您选择")
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("请输入Winform类型。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- try
- {
- moMenu.ID = txt_Id.Text;
- moMenu.Name = txt_Name.Text;
- if (treeLookUp_PId.EditValue == null || treeLookUp_PId.EditValue.Equals("0"))
- {
- moMenu.PID = null;
- }
- else
- {
- moMenu.PID = treeLookUp_PId.EditValue.ToString();
- }
- moMenu.Seq = Convert.ToInt64(txt_Seq.Text) ;
- moMenu.FunctionId = txt_FunctionID.Text;
- if (moMenu.Icon != txt_Icon.Text)
- {
- moMenu.Icon = ConvertIconPath(txt_Icon.Text);
- }
- moMenu.WinformType = lookUp_WinformType.EditValue.ToString();
- if (moMenu.WebIcon != txt_WebIcon.Text)
- {
- moMenu.WebIcon = ConvertIconPath(txt_WebIcon.Text) ;
- }
- moMenu.Url = txt_Url.Text;
- if (check_Visible.CheckState == CheckState.Checked)
- {
- moMenu.Visible = true;
- }
- else if (check_Visible.CheckState == CheckState.Unchecked)
- {
- moMenu.Visible = false;
- }
- if (check_Delete.CheckState == CheckState.Checked)
- {
- moMenu.Deleted = true;
- }
- else if (check_Delete.CheckState == CheckState.Unchecked)
- {
- moMenu.Deleted = false;
- }
- bool flag = BLLFactory<BlMenuInfo>.Instance.Update(moMenu,moMenu.Seq);
- //BlAuthoryRight bl = new BlAuthoryRight();
- BLLFactory<BlAuthoryRight>.Instance.CreateAuthoryRight();
- if (flag)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("修改成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- saveData();
- this.Close();
- }
- }
- catch (Exception ex)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("修改失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- LogHelper.log.Error(string.Format("修改数据库menu出现错误:{0}", ex));
- }
- }
- private void btn_Add_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(txt_Id.Text))
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("请输入ID", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- if (string.IsNullOrEmpty(txt_Name.Text))
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("请输入显示名称。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- if (lookUp_WinformType.Text == "请您选择")
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("请输入Winform类型。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- try
- {
- MoMenuInfo mo = new MoMenuInfo();
- mo.ID = txt_Id.Text;
- mo.Name = txt_Name.Text;
- if (treeLookUp_PId.EditValue == null || treeLookUp_PId.EditValue.Equals("0"))
- {
- mo.PID = null;
- }
- else
- {
- mo.PID = treeLookUp_PId.EditValue.ToString();
- }
- mo.Seq = Convert.ToInt64(txt_Seq.Text) ;
- mo.FunctionId = txt_FunctionID.Text;
- mo.Icon = ConvertIconPath(txt_Icon.Text) ;
- mo.WinformType = lookUp_WinformType.EditValue.ToString();
- mo.WebIcon = ConvertIconPath(txt_WebIcon.Text) ;
- mo.Url = txt_Url.Text;
- mo.Creator_ID = SysEnvironment.CurrentLoginID;
- mo.CreateTime = DateTime.Now;
- mo.Deleted = false;
- if (check_Visible.CheckState == CheckState.Checked)
- {
- mo.Visible = true;
- }
- else if (check_Visible.CheckState == CheckState.Unchecked)
- {
- mo.Visible = false;
- }
- if (check_Delete.CheckState == CheckState.Checked)
- {
- mo.Deleted = true;
- }
- else if (check_Delete.CheckState == CheckState.Unchecked)
- {
- mo.Deleted = false;
- }
- bool flag = BLLFactory<BlMenuInfo>.Instance.Insert(mo);
- if (flag)
- {
- //BlAuthoryRight bl = new BlAuthoryRight();
- BLLFactory<BlAuthoryRight>.Instance.CreateAuthoryRight();
- DevExpress.XtraEditors.XtraMessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- saveData();
- this.Close();
- }
- }
- catch (Exception ex)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("添加失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- LogHelper.log.Error(string.Format("添加数据库menu出现错误:{0}", ex));
- }
- }
- /// <summary>
- /// 将所选择的图像路径转换为存入数据库的格式
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- private string ConvertIconPath(string filePath)
- {
- try
- {
- if (string.IsNullOrEmpty(filePath))
- {
- return null;
- }
- string str = Application.StartupPath + "\\Images";
- if (filePath.Contains(str))
- {
- filePath = filePath.Replace(Application.StartupPath, string.Empty);
- return filePath;
- }
- string[] array = filePath.Split('\\');
- string newFile = Application.StartupPath + "\\Images\\" + array[array.Length - 1];
- if (File.Exists(newFile))
- {
- File.Delete(newFile);
- File.Copy(filePath, newFile);
- }
- else
- {
- File.Copy(filePath, newFile);
- }
- string result = "";
- result = newFile.Replace(Application.StartupPath, string.Empty);
- return result;
- }
- catch (Exception ex)
- {
- LogHelper.log.Error(string.Format("选择图像出现错误:{0}", ex));
- return null;
- }
- }
- private void ParentId_EditValueChanged(object sender, EventArgs e)
- {
- if (moMenu != null) //如果是编辑状态,未修改父ID,则ID与功能ID不变
- {
- if (moMenu.PID == this.treeLookUp_PId.EditValue.ToString())
- {
- this.txt_Id.Text = moMenu.ID;
- this.txt_FunctionID.Text = moMenu.FunctionId;
- return;
- }
- }
- if (this.treeLookUp_PId.EditValue.ToString() == "0")
- {
- this.lookUp_WinformType.EditValue = "1";
- string id = BLLFactory<BlMenuInfo>.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), null);
- this.txt_Id.Text = id;
- this.txt_FunctionID.Text = id;
- }
- else
- {
- MoMenuInfo mo = BLLFactory<BlMenuInfo>.Instance.FindSingle(string.Format(" menu_id = '{0}'", treeLookUp_PId.EditValue.ToString()));
- if (mo.WinformType == "3")
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("此菜单无法作为父级ID,请重新选择", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- treeLookUp_PId.EditValue = "0";
- return;
- }
- string winformType = (Convert.ToInt32(mo.WinformType) + 1).ToString();
- this.lookUp_WinformType.EditValue = winformType ;
- string id = BLLFactory<BlMenuInfo>.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), treeLookUp_PId.EditValue.ToString());
- this.txt_Id.Text = id;
- this.txt_FunctionID.Text = id;
- }
- }
- private void WInformType_EditValueChanged(object sender, EventArgs e)
- {
- if (treeLookUp_PId.EditValue == null || treeLookUp_PId.EditValue.Equals("0"))
- {
- return;
- }
- this.txt_Id.Text = BLLFactory<BlMenuInfo>.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), treeLookUp_PId.EditValue.ToString());
- this.txt_FunctionID.Text = BLLFactory<BlMenuInfo>.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), treeLookUp_PId.EditValue.ToString());
- }
- }
- }
|