using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using HL_FristAidPlatform_DTO;
using HL_FristAidPlatform_Public;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace HL_FristAidPlatform_SYS
{
public partial class Form_MenuMain : XtraForm
{
#region 变量
///
/// 操作标志 1:新增 2:修改
///
public int Flag = 1;
///
/// 当前图标文件名
///
private string Cur_Icon = string.Empty;
///
/// 当前图标(转换成二进制之后数据)
///
private string Cur_PicData = string.Empty;
///
/// 当前图片的位图源
///
private Bitmap Cur_ImageBitmap;
///
/// 当前页
///
public int curPage = 1;
///
/// 每页大小
///
public int pageSize = 100;
///
/// 所有记录条数
///
public int totalNumber = 0;
#endregion
#region 分页实现
public void ExportEvents(bool singlePage)//单页,所有
{
//导出GridControl代码写在这。
}
public void RefreshGridList()
{
FillGridListCtrlQuery(curPage);//自己实现FillGridListCtrlQuery函数。
}
///
/// 绑定数据源
///
///
private void FillGridListCtrlQuery(int curPage)
{
try
{
string whereStr = string.Empty;
string Key = PublicClass.ToString(txt_Key.Text, "");
if (!string.IsNullOrEmpty(Key))
{
whereStr += "&keyWord=" + Key;
}
string systemModuleID = PublicClass.ToString(LUEdit_SelSystemModule.EditValue, "");
if (!string.IsNullOrEmpty(systemModuleID))
{
whereStr += "&systemModuleID=" + systemModuleID;
}
DataTable ResultDT = DBHelpClass.Get(string.Format("api/admin/T_SYS_Menu?pageIndex={0}&pageSize={1}{2}", curPage, pageSize, whereStr));
grid_Menu.DataSource = ResultDT;//显示分页结果
totalNumber = DBHelpClass.TotalNumber;
userControlForPage.RefreshPager(pageSize, totalNumber, curPage);//更新分页控件显示。
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定数据源:\r\n" + ex);
}
}
private void MyPagerEvents(int curPage, int pageSize)
{
this.curPage = curPage;
this.pageSize = pageSize;
FillGridListCtrlQuery(curPage);
}
private void userControlForPage_exportEvents(bool singlePage)
{
userControlForPage.exportEvents += ExportEvents;
}
private void userControlForPage_myPagerEvents(int curPage, int pageSize)
{
userControlForPage.myPagerEvents += MyPagerEvents;
}
#endregion
public Form_MenuMain()
{
InitializeComponent();
}
///
/// 窗体加载
///
///
///
private void Form_MenuMain_Load(object sender, EventArgs e)
{
BindSystemModule(1);
RefreshGridList();
//详情控件初始化
PublicClass.EnabledControl(this.groupM_Detail, true, false);
//筛选条件中所属模块默认当前选择的系统
LUEdit_SelSystemModule.EditValue = PublicClassForDataBase.Config104;
}
///
/// 窗体快捷键注册
///
///
///
private void Form_MenuMain_KeyDown(object sender, KeyEventArgs e)
{
//同时按下 Ctrl+S 时执行保存方法
if (e.KeyCode == Keys.S && e.Control)
{
btn_Save_ItemClick(null, null);
}
}
private void txt_Key_TextChanged(object sender, EventArgs e)
{
//列表分页数据绑定
RefreshGridList();
}
///
/// 查询
///
///
///
private void btn_Select_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//列表分页数据绑定
RefreshGridList();
}
private void LUEdit_SelSystemModule_EditValueChanged(object sender, EventArgs e)
{
//列表分页数据绑定
RefreshGridList();
}
///
/// 新增
///
///
///
private void btn_Insert_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
BindSystemModule(2);
PublicClass.EnabledControl(this.groupM_Detail, false, true);
//图片置空
Cur_Icon = string.Empty;
Cur_PicData = string.Empty;
btn_Delete.Enabled = false;
ckb_DeleteFlag.Checked = true;//新增时默认启用
pictureEdit_Icon.Image = null;
Flag = 1;
}
///
/// 修改
///
///
///
private void btn_Update_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
BindSystemModule(2);
BindDetail();
PublicClass.EnabledControl(this.groupM_Detail, false, false);
btn_Delete.Enabled = false;
}
///
/// 删除
///
///
///
private void btn_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
try
{
#region 删除
if (grv_Menu.DataRowCount > 0)
{
int selectRow = grv_Menu.GetSelectedRows()[0];
int ID = Convert.ToInt32(grv_Menu.GetRowCellValue(selectRow, "ID").ToString());
string MenuName = grv_Menu.GetRowCellValue(selectRow, "MenuName").ToString();
if (ID == 0)
{
XtraMessageBox.Show("请先选择要删除的菜单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (XtraMessageBox.Show(string.Format("确定要删除菜单【{0}】?", MenuName), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
List list = new List();
T_SYS_MenuDTO model = new T_SYS_MenuDTO();
string Url = "api/admin/T_SYS_Menu/LogicalDelete";
model.ID = ID;
model.DeleteFlag = 1;
list.Add(model);
//初始化两个工厂
ClientFactory httpClient = new HttpClientFactory();
Client client = httpClient.VisitFactory();
//访问
ListEntity t = client.Post(Url, list);
if (t.Success)
{
XtraMessageBox.Show("删除菜单成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
PublicClass.EnabledControl(this.groupM_Detail, true, false);
Flag = 0;
//列表分页数据绑定
RefreshGridList();
}
else
{
XtraMessageBox.Show("删除菜单失败,请稍后重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
#endregion
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "删除:\r\n" + ex);
}
}
///
/// 保存
///
///
///
private void btn_Save_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
try
{
#region 保存
if (string.IsNullOrEmpty(txt_MenuName.Text.ToString().Trim()))
{
XtraMessageBox.Show("菜单名称不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txt_MenuName.Focus();
return;
}
if (PublicClass.ToString(LUEdit_SystemModule.EditValue, "-1") == "-1")
{
XtraMessageBox.Show("请选择所属系统模块!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
LUEdit_SystemModule.Focus();
return;
}
List list = new List();
T_SYS_MenuDTO model = new T_SYS_MenuDTO();
string Url = string.Empty;
if (Flag == 1)
{
Url = "api/admin/T_SYS_Menu";
model.GUID = Guid.NewGuid().ToString();
}
else
{
Url = "api/admin/T_SYS_Menu/Update";
model.ID = Convert.ToInt32(txt_MenuName.Tag.ToString());
model.GUID = txt_ShortName.Tag.ToString();
}
//0为顶级菜单编号
int ParentID = PublicClass.ToInt32(LUEdit_Parent.EditValue, -1);
model.ParentID = ParentID == -1 ? 0 : ParentID;
model.SystemModuleID = PublicClass.ToInt64(LUEdit_SystemModule.EditValue, 0);
model.MenuName = txt_MenuName.Text.ToString().Trim();
model.ShortName = txt_ShortName.Text.ToString().Trim();
model.DLLName = txt_DLLName.Text.ToString().Trim();
model.FunctionName = txt_FunctionName.Text.ToString().Trim();
model.Icon = Cur_Icon;
model.IconData = Convert.FromBase64String(Cur_PicData);
model.OrderBy = Convert.ToInt32(txt_OrderBy.Text.ToString().Trim() == "" ? "0" : txt_OrderBy.Text.ToString().Trim());
model.DeleteFlag = ckb_DeleteFlag.Checked == true ? 0 : 1;
list.Add(model);
//初始化两个工厂
ClientFactory httpClient = new HttpClientFactory();
Client client = httpClient.VisitFactory();
//访问
ListEntity t = client.Post(Url, list);
if (t.Success)
{
XtraMessageBox.Show("保存菜单信息成功!\r\n如果您需要使用该菜单功能,请您重新登录系统!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
PublicClass.EnabledControl(this.groupM_Detail, true, false);
Flag = 0;
//列表分页数据绑定
RefreshGridList();
btn_Delete.Enabled = true;
//定位
PublicClass.LocationForGridView(grv_Menu, model.GUID, 1);
}
else
{
XtraMessageBox.Show("保存失败,请稍后重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "保存:\r\n" + ex);
}
}
///
/// 退出
///
///
///
private void btn_Exit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close();
}
#region 方法
///
/// 绑定详情
///
private void BindDetail()
{
try
{
#region 绑定详情
if (grv_Menu.DataRowCount > 0)
{
int selectRow = grv_Menu.GetSelectedRows()[0];
long ID = Convert.ToInt64(grv_Menu.GetRowCellValue(selectRow, "ID").ToString());
DataTable DetailDT = DBHelpClass.GetDataRow(string.Format("api/admin/T_SYS_Menu/{0}", ID));
if (DetailDT != null && DetailDT.Rows.Count > 0)
{
Flag = 2;//修改标识
txt_MenuName.Tag = DetailDT.Rows[0]["ID"].ToString();
txt_MenuName.Text = DetailDT.Rows[0]["MenuName"].ToString();
string ParentID = PublicClass.ToString(DetailDT.Rows[0]["ParentID"], "0");
LUEdit_Parent.EditValue = ParentID == "0" ? "-1" : ParentID;
LUEdit_SystemModule.EditValue = PublicClass.ToString(DetailDT.Rows[0]["SystemModuleID"], "-1");
txt_ShortName.Tag = DetailDT.Rows[0]["GUID"].ToString();
txt_ShortName.Text = DetailDT.Rows[0]["ShortName"].ToString();
txt_DLLName.Text = DetailDT.Rows[0]["DLLName"].ToString();
txt_FunctionName.Text = DetailDT.Rows[0]["FunctionName"].ToString();
#region 图片
if (!string.IsNullOrEmpty(PublicClass.ToString(DetailDT.Rows[0]["IconData"], "")))
{
MemoryStream ms = new MemoryStream(Convert.FromBase64String(DetailDT.Rows[0]["IconData"].ToString()));
Image img = Image.FromStream(ms);
Cur_ImageBitmap = new Bitmap(img, pictureEdit_Icon.Width, pictureEdit_Icon.Height);
pictureEdit_Icon.Image = Cur_ImageBitmap;
Cur_Icon = DetailDT.Rows[0]["Icon"].ToString();
Cur_PicData = PublicClass.Png2String(Cur_ImageBitmap);
}
else
{
pictureEdit_Icon.Image = null;
Cur_ImageBitmap = null;
Cur_PicData = "";
}
#endregion
txt_OrderBy.Text = DetailDT.Rows[0]["OrderBy"].ToString();
ckb_DeleteFlag.Checked = DetailDT.Rows[0]["DeleteFlag"].ToString().Trim() == "0" ? true : false;
}
}
#endregion
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定详情:\r\n" + ex);
}
}
///
/// 绑定上级菜单
///
private void BindParent(string systemModuleID)
{
try
{
DataTable ResultDT = DBHelpClass.Get(string.Format("api/admin/T_SYS_Menu/GetListBySystemModuleID?systemModuleID={0}", systemModuleID));
PublicClass.SetLookUpList(ref LUEdit_Parent, ResultDT, 0, 3, true, "顶级菜单");
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定数据源:\r\n" + ex);
}
}
///
/// 绑定所属系统模块
///
/// 绑定标记:1:绑定查询;2:绑定详情
private void BindSystemModule(int flag)
{
try
{
DataTable ResultDT = DBHelpClass.Get("/api/admin/T_SYS_SystemModule?isOpen=0&deleteFlag=0");
switch (flag)
{
case 1:
PublicClass.SetLookUpList(ref LUEdit_SelSystemModule, ResultDT, 3, 5, false);
break;
case 2:
PublicClass.SetLookUpList(ref LUEdit_SystemModule, ResultDT, 3, 5, false);
break;
default:
break;
}
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定所属系统模块:\r\n" + ex);
}
}
#endregion
///
/// 单击列表绑定详情
///
///
///
private void gridC_Menu_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
GridHitInfo hi = grv_Menu.CalcHitInfo(new Point(e.X, e.Y));
//单击数据行
if (hi.InRow)//单击的是列头 hi.InColumn
{
PublicClass.EnabledControl(this.groupM_Detail, true, false);
BindDetail();
}
}
///
/// 选择图片
///
///
///
private void btn_SelFlie_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.PNG)|*.PNG;|All files(*.*)|*.*";
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
FileInfo file = new FileInfo(ofd.FileName);
if (file.Extension == ".png" || file.Extension == ".PNG")
{
string Cur_PicAddress = ofd.FileName;
Image imge = Image.FromFile(Cur_PicAddress);
Cur_ImageBitmap = new Bitmap(imge, pictureEdit_Icon.Width, pictureEdit_Icon.Height);
pictureEdit_Icon.Image = Cur_ImageBitmap;
Cur_Icon = file.Name;
//所选图标转换成二进制
Cur_PicData = PublicClass.Png2String(Cur_ImageBitmap);
}
else
{
XtraMessageBox.Show("为确保系统美观度,请上传.png格式的图标!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "选择图片:\r\n" + ex);
}
}
///
/// 筛选上级菜单
///
///
///
private void LUEdit_SystemModule_EditValueChanged(object sender, EventArgs e)
{
string SystemModuleID = PublicClass.ToString(LUEdit_SystemModule.EditValue, "-1");
if (SystemModuleID != "-1")
{
//绑定上级菜单
BindParent(SystemModuleID);
}
}
}
}