using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraTreeList.Nodes;
using HL_FristAidPlatform_DTO;
using HL_FristAidPlatform_Public;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace HL_FristAidPlatform_Base
{
public partial class Form_HospitalMain : XtraForm
{
#region 变量
///
/// 当前操作的医院ID
///
public int Cur_HospitalID = 0;
///
/// 当前操作的医院名称
///
public string Cur_HospitalName = string.Empty;
///
/// 当前操作的医院GUID
///
private string Cur_HospitalGuid = string.Empty;
///
/// 当前页
///
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 ParentGUID = PublicClass.ToString(lookUp_Sel_ParentGUID.EditValue, "-1");
if (ParentGUID != "-1")
{
whereStr += "&parentGUID=" + ParentGUID;
}
//院区属性
string AttributeTag = PublicClass.ToString(LookUp_Sel_AttributeTag.EditValue, "-1");
if (AttributeTag != "-1")
{
whereStr += "&attributeTag=" + AttributeTag;
}
//关键词
string KeyWord = txt_Key.Text.ToString().Trim();
if (!string.IsNullOrEmpty(KeyWord))
{
whereStr += "&keyWord=" + KeyWord;
}
string Url = string.Format("api/base/T_Base_Hospital?pageIndex={0}&pageSize={1}{2}", curPage, pageSize, whereStr);
DataTable ResultDT = DBHelpClass.Get(Url);
grid_Hospital.DataSource = ResultDT;//显示分页结果
grv_Hospital.BestFitColumns();//列宽自适应
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_HospitalMain()
{
InitializeComponent();
}
///
/// 窗体加载
///
///
///
private void Form_HospitalMain_Load(object sender, EventArgs e)
{
//基础控件数据
BindData();
//绑定列表
RefreshGridList();
}
#region BindData()
///
/// 绑定控件基础数据
///
private void BindData()
{
BindParentHospital();//绑定上级医院
BindAttributeTag();//绑定院区属性
}
#endregion
///
/// 绑定上级医院
///
private void BindParentHospital()
{
try
{
DataTable ResultDT = DBHelpClass.Get("api/base/T_Base_Hospital/GetList");
PublicClass.SetLookUpList(lookUp_Sel_ParentGUID, ResultDT, "GUID", "Name", true, "全部");
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定上级医院:\r\n" + ex);
}
}
///
/// 绑定院区属性
///
private void BindAttributeTag()
{
try
{
DataTable ResultDT = PublicClass.EnumToDataTable(typeof(Enumerate.HospitalAttributeTag), "Name", "ID");
PublicClass.SetLookUpList(LookUp_Sel_AttributeTag, ResultDT, "ID", "Name", true, "全部");
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定院区属性:\r\n" + ex);
}
}
///
/// 查询
///
///
///
private void btn_Select_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
RefreshGridList();
}
///
/// 上级医院
///
///
///
private void lookUp_Sel_ParentGUID_EditValueChanged(object sender, EventArgs e)
{
RefreshGridList();
}
///
/// 医院属性
///
///
///
private void LookUp_Sel_AttributeTag_EditValueChanged(object sender, EventArgs e)
{
RefreshGridList();
}
///
/// 关键词
///
///
///
private void txt_Key_EditValueChanged(object sender, EventArgs e)
{
RefreshGridList();
}
///
/// 新增
///
///
///
private void btn_Insert_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Form_Hospital frm = new Form_Hospital();
if (frm.ShowDialog() == DialogResult.OK)
{
RefreshGridList();
}
}
///
/// 修改
///
///
///
private void btn_Update_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (grv_Hospital.DataRowCount > 0)
{
int selectRow = grv_Hospital.GetSelectedRows()[0];
long HospitalID = PublicClass.ToInt64(grv_Hospital.GetRowCellValue(selectRow, "ID").ToString(), 0);
Cur_HospitalGuid = PublicClass.ToString(grv_Hospital.GetRowCellValue(selectRow, "GUID").ToString(), "");
if (!string.IsNullOrEmpty(Cur_HospitalGuid))
{
Form_Hospital frm = new Form_Hospital(HospitalID);
if (frm.ShowDialog() == DialogResult.OK)
{
RefreshGridList();
//定位
PublicClass.LocationForGridView(grv_Hospital, HospitalID.ToString(), 0);
}
}
}
}
///
/// 删除
///
///
///
private void btn_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
try
{
#region 删除
if (grv_Hospital.DataRowCount > 0)
{
int selectRow = grv_Hospital.GetSelectedRows()[0];
int ID = Convert.ToInt32(grv_Hospital.GetRowCellValue(selectRow, "ID").ToString());
string guid = grv_Hospital.GetRowCellValue(selectRow, "GUID").ToString();
string HospitalName = grv_Hospital.GetRowCellValue(selectRow, "Name").ToString();
if (ID == 0)
{
XtraMessageBox.Show("请先选择要删除的医院信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (XtraMessageBox.Show(string.Format("确定要删除医院信息【{0}】?\r\n1、删除院区后系统中所有用户的院区权限将一并删除!\r\n2、当前删除的院区所开启的系统模块权限将一并删除!", HospitalName), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
List list = new List();
T_Base_HospitalDTO model = new T_Base_HospitalDTO();
string Url = "api/base/T_Base_Hospital/LogicalDelete";
model.ID = ID;
model.DeleteFlag = 1;
model.CreationDate = PublicClass.DefaultTime;
list.Add(model);
//初始化两个工厂
ClientFactory httpClient = new HttpClientFactory();
Client client = httpClient.VisitFactory();
//访问
ListEntity t = client.Post(Url, list);
if (t.Success)
{
#region 删除用户关联的源泉权限
List list_UserHospital = new List();
T_SYS_UserHospitalDTO model_UserHospital = new T_SYS_UserHospitalDTO();
Url = "api/admin/T_SYS_UserHospital/DeleteByHospitalGuid";
model_UserHospital.HospitalGUID = guid;
list_UserHospital.Add(model_UserHospital);
//初始化两个工厂
ClientFactory httpClient_UserHospital = new HttpClientFactory();
Client client_UserHospital = httpClient_UserHospital.VisitFactory();
//访问
client_UserHospital.Post(Url, list_UserHospital);
#endregion
#region 删除开启的模块权限
DeleteRoleMenu(ID);
#endregion
XtraMessageBox.Show("删除医院信息成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//列表分页数据绑定
RefreshGridList();
}
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();
}
///
/// 单击绑定详情
///
///
///
private void grid_Hospital_MouseClick(object sender, MouseEventArgs e)
{
GridHitInfo hi = grv_Hospital.CalcHitInfo(new Point(e.X, e.Y));
//单击数据行
if (hi.InRow)//单击的是列头 hi.InColumn
{
BindHospitalRightList();
}
}
///
/// 绑定医院模块详情
///
private void BindHospitalRightList()
{
try
{
if (grv_Hospital.DataRowCount > 0)
{
int selectRow = this.grv_Hospital.GetSelectedRows()[0];
Cur_HospitalID = Convert.ToInt32(grv_Hospital.GetRowCellValue(selectRow, "ID").ToString());
Cur_HospitalName = grv_Hospital.GetRowCellValue(selectRow, "Name").ToString();
#region 拥有模块信息
BindSystemModuleList();
DataTable HospitalRightDT = DBHelpClass.Get(string.Format("/api/base/T_Base_HospitalRight?hospitalID={0}", Cur_HospitalID));
if (HospitalRightDT != null && HospitalRightDT.Rows.Count > 0)
{
SetCheckMenu(HospitalRightDT);
}
#endregion
}
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "删除:\r\n" + ex);
}
}
///
/// 绑定树形菜单
///
private void BindSystemModuleList()
{
try
{
DataTable SystemModuleDT = DBHelpClass.Get("api/admin/T_SYS_SystemModule?isOpen=0&deleteFlag=0");
if (SystemModuleDT != null && SystemModuleDT.Rows.Count > 0)
{
treeList_HospitalRight.DataSource = SystemModuleDT;
treeList_HospitalRight.KeyFieldName = "ID";//是节点id
//treeList_HospitalRight.ParentFieldName = "id";//是父节点id
treeList_HospitalRight.ExpandAll();
}
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定树形菜单:\r\n" + ex);
}
}
public void SetCheckMenu(DataTable HospitalRightDT)
{
for (int i = 0; i < HospitalRightDT.Rows.Count; i++)
{
string SystemModuleID = HospitalRightDT.Rows[i]["SystemModuleID"].ToString().Trim();
SetNodeChecked(SystemModuleID, treeList_HospitalRight.Nodes);
}
}
private void SetNodeChecked(string key, TreeListNodes node)
{
foreach (TreeListNode childNode in node)
{
DataRowView data = treeList_HospitalRight.GetDataRecordByNode(childNode) as DataRowView;
if (data["ID"].ToString() == key)
{
childNode.Checked = true;
}
if (childNode.HasChildren)
{
SetNodeChecked(key, childNode.Nodes);
}
}
}
#region 菜单权限树事件
private void treeList_HospitalRight_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
///
/// 设置子节点的状态
///
///
///
private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}
///
/// 设置父节点的状态
///
///
///
private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
if (node.ParentNode != null)
{
bool b = false;
CheckState state;
for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
{
state = (CheckState)node.ParentNode.Nodes[i].CheckState;
if (!check.Equals(state))
{
b = !b;
break;
}
}
node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
SetCheckedParentNodes(node.ParentNode, check);
}
}
#endregion
///
/// 保存权限
///
///
///
private void btn_SaveHospitalRight_Click(object sender, EventArgs e)
{
if (Cur_HospitalID > 0)
{
if (DeleteRoleMenu(Cur_HospitalID))
{
//然后再保存当前赋予的权限
List list = PublicClass.GetTreeKeyValue(treeList_HospitalRight, "ID");
string Ids = string.Empty;
foreach (string item in list)
{
SaveRoleMenu(Cur_HospitalID, PublicClass.ToInt64(item, 0));
}
XtraMessageBox.Show(string.Format("给医院【{0}】开通系统模块成功!", Cur_HospitalName), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
XtraMessageBox.Show(string.Format("给医院【{0}】开通系统模块失败,请稍后重试!", Cur_HospitalName), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
XtraMessageBox.Show("请先在左侧列表中选择需要开通系统模块得医院!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
///
/// 删除当前医院已经开通的模块
///
///
///
public bool DeleteRoleMenu(int hospitalID)
{
try
{
//先删除当前角色已经赋予的所有权限
List list = new List();
T_Base_HospitalRightDTO model = new T_Base_HospitalRightDTO();
string Url = "api/base/T_Base_HospitalRight/PhysicalDelete";
model.GUID = Guid.NewGuid().ToString();
model.HospitalID = hospitalID;
model.DeleteFlag = 0;
list.Add(model);
//初始化两个工厂
ClientFactory httpClient = new HttpClientFactory();
Client client = httpClient.VisitFactory();
//访问
ListEntity t = client.Post(Url, list);
return t.Success;
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "删除当前医院已经开通的模块:\r\n" + ex);
return false;
}
}
///
/// 保存当前医院已经开通的模块
///
/// 医院编号
/// 系统模块编号
///
private bool SaveRoleMenu(int hospitalID, long systemModuleID)
{
try
{
List list = new List();
T_Base_HospitalRightDTO model = new T_Base_HospitalRightDTO();
string Url = "api/base/T_Base_HospitalRight";
model.GUID = Guid.NewGuid().ToString();
model.HospitalID = hospitalID;
model.SystemModuleID = systemModuleID;
model.DeleteFlag = 0;
list.Add(model);
//初始化两个工厂
ClientFactory httpClient = new HttpClientFactory();
Client client = httpClient.VisitFactory();
//访问
ListEntity t = client.Post(Url, list);
return t.Success;
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "保存当前医院已经开通的模块:\r\n" + ex);
return false;
}
}
///
/// 自定义显示列
///
///
///
private void grv_Hospital_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
//院区属性
if (e.Column.FieldName == "AttributeTag")
{
int value = PublicClass.ToInt32(e.Value, 0);
switch (value)
{
case (int)Enumerate.HospitalAttributeTag.主院区:
e.DisplayText = Enumerate.HospitalAttributeTag.主院区.ToString();
break;
case (int)Enumerate.HospitalAttributeTag.分院区:
e.DisplayText = Enumerate.HospitalAttributeTag.分院区.ToString();
break;
case (int)Enumerate.HospitalAttributeTag.网络医院:
e.DisplayText = Enumerate.HospitalAttributeTag.网络医院.ToString();
break;
case (int)Enumerate.HospitalAttributeTag.胸痛救治单元:
e.DisplayText = Enumerate.HospitalAttributeTag.胸痛救治单元.ToString();
break;
case (int)Enumerate.HospitalAttributeTag.其他:
e.DisplayText = Enumerate.HospitalAttributeTag.其他.ToString();
break;
default:
e.DisplayText = "";
break;
}
}
}
}
}