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();
///
/// 定义委托
///
public delegate void save();
///
/// 定义事件,保存数据成功后刷新FormMenu中的数据
///
public event save saveData;
///
/// 构造函数
///
/// 为null时表示添加操作,不为null时表示修改操作
public FormEditMenu( MoMenuInfo menuInfo)
{
InitializeComponent();
moMenu = menuInfo;
InitControls(menuInfo);
}
///
/// 初始化控件
///
///
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 list = BLLFactory.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.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;
}
}
///
/// 选择图标文件
///
///
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.Instance.Update(moMenu,moMenu.Seq);
//BlAuthoryRight bl = new BlAuthoryRight();
BLLFactory.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.Instance.Insert(mo);
if (flag)
{
//BlAuthoryRight bl = new BlAuthoryRight();
BLLFactory.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));
}
}
///
/// 将所选择的图像路径转换为存入数据库的格式
///
///
///
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.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), null);
this.txt_Id.Text = id;
this.txt_FunctionID.Text = id;
}
else
{
MoMenuInfo mo = BLLFactory.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.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.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), treeLookUp_PId.EditValue.ToString());
this.txt_FunctionID.Text = BLLFactory.Instance.CreateFunctionId(lookUp_WinformType.EditValue.ToString(), treeLookUp_PId.EditValue.ToString());
}
}
}