FormBlack.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using ProjectBase.Data.Logs;
  6. using ProjectBase.Data.BaseDAL;
  7. using SIMDP.BLL;
  8. using SIMDP.Model;
  9. using ProjectBase.Util;
  10. namespace SIMDP.View
  11. {
  12. public partial class FormBlack : DevExpress.XtraEditors.XtraForm
  13. {
  14. string condition = null;
  15. /// <summary>
  16. /// 授权类型的字典表
  17. /// </summary>
  18. private Dictionary<string, string> Dic_Type
  19. {
  20. get
  21. {
  22. Dictionary<string, string> type = new Dictionary<string, string>();
  23. type.Add("-1", "全部");
  24. type.Add("1", "白名单");
  25. type.Add("0", "黑名单");
  26. return type;
  27. }
  28. }
  29. public FormBlack()
  30. {
  31. InitializeComponent();
  32. }
  33. private void FormBlack_Load(object sender, EventArgs e)
  34. {
  35. InitControl();
  36. InitGrid();
  37. }
  38. /// <summary>
  39. /// 初始化控件
  40. /// </summary>
  41. private void InitControl()
  42. {
  43. //下拉框--类型
  44. this.lookUp_Type.Properties.DataSource = Dic_Type;
  45. this.lookUp_Type.Properties.ValueMember = "Key";
  46. this.lookUp_Type.Properties.DisplayMember = "Value";
  47. this.lookUp_Type.Properties.NullText = null;
  48. this.lookUp_Type.EditValue = "-1";
  49. //下拉框--所属用户
  50. List<MoAuthoryUser> userList = BLLFactory<BlAuthoryUser>.Instance.GetAll();
  51. Dictionary<string, string> dic = new Dictionary<string, string>();
  52. dic.Add("-1","全部用户");
  53. foreach (MoAuthoryUser item in userList)
  54. {
  55. dic.Add(item.LoginAccount,item.UserName);
  56. }
  57. this.lookUp_UserName.Properties.DataSource = dic;
  58. this.lookUp_UserName.Properties.ValueMember = "Key";
  59. this.lookUp_UserName.Properties.DisplayMember = "Value";
  60. this.lookUp_UserName.Properties.NullText = null;
  61. this.lookUp_UserName.EditValue = "-1";
  62. }
  63. /// <summary>
  64. /// 初始化表格控件
  65. /// </summary>
  66. private void InitGrid()
  67. {
  68. //entity
  69. this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
  70. this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
  71. this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
  72. this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
  73. this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
  74. this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
  75. this.winGridViewPager1.gridView1.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(gridView1_CustomColumnDisplayText);
  76. this.winGridViewPager1.gridView1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);
  77. this.winGridViewPager1.ShowLineNumber = true;
  78. this.winGridViewPager1.BestFitColumnWith = true;
  79. this.winGridViewPager1.DisplayColumns = "Name,UserName,Type,Forbid,IPStart,IPEnd,Note,CreatorName,CreateTime,EditorName,EditTime";
  80. #region 添加别名解析
  81. this.winGridViewPager1.AddColumnAlias("ID", "");
  82. this.winGridViewPager1.AddColumnAlias("Name", "显示名称");
  83. this.winGridViewPager1.AddColumnAlias("UserAccount", "");
  84. this.winGridViewPager1.AddColumnAlias("UserName", "所属用户");
  85. this.winGridViewPager1.AddColumnAlias("Type", "授权类型");
  86. this.winGridViewPager1.AddColumnAlias("Forbid", "是否禁用");
  87. this.winGridViewPager1.AddColumnAlias("IPStart", "IP起始地址");
  88. this.winGridViewPager1.AddColumnAlias("IPEnd", "IP结束地址");
  89. this.winGridViewPager1.AddColumnAlias("Note", "备注");
  90. this.winGridViewPager1.AddColumnAlias("CreatorAccount", "");
  91. this.winGridViewPager1.AddColumnAlias("CreatorName", "创建人名称");
  92. this.winGridViewPager1.AddColumnAlias("CreateTime", "创建时间");
  93. this.winGridViewPager1.AddColumnAlias("EditorAccount", "");
  94. this.winGridViewPager1.AddColumnAlias("EditorName", "编辑人名称");
  95. this.winGridViewPager1.AddColumnAlias("EditTime", "编辑时间");
  96. #endregion
  97. this.winGridViewPager1.PrintTitle = "系统黑白名单表";
  98. this.winGridViewPager1.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
  99. BindData(" 1=1");
  100. condition = " 1=1";
  101. }
  102. /// <summary>
  103. /// 绑定数据源
  104. /// </summary>
  105. /// <param name="condition"></param>
  106. private void BindData(string condition)
  107. {
  108. this.winGridViewPager1.DataSource = BLLFactory<BlWhite>.Instance.FindWithPager(condition, this.winGridViewPager1.PagerInfo);
  109. for (int i = 0; i < this.winGridViewPager1.gridView1.Columns.Count; i++) //设置每列内容居中显示
  110. {
  111. this.winGridViewPager1.gridView1.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
  112. }
  113. }
  114. /// <summary>
  115. /// 分页控件翻页的操作
  116. /// </summary>
  117. private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
  118. {
  119. BindData(condition);
  120. }
  121. /// <summary>
  122. /// 分页控件全部导出操作前的操作
  123. /// </summary>
  124. private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
  125. {
  126. this.winGridViewPager1.AllToExport = BLLFactory<BlWhite>.Instance.Find(condition);
  127. }
  128. /// <summary>
  129. /// 分页控件刷新操作
  130. /// </summary>
  131. private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
  132. {
  133. BindData(condition);
  134. }
  135. /// <summary>
  136. /// 分页控件编辑项操作
  137. /// </summary>
  138. private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
  139. {
  140. MoWhite selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoWhite;
  141. FormEditBlack editBlack = new FormEditBlack();
  142. editBlack.white = selectRow;
  143. editBlack.ShowDialog();
  144. if (editBlack.DialogResult == DialogResult.OK)
  145. {
  146. BindData(condition);
  147. }
  148. }
  149. /// <summary>
  150. /// 分页控件新增操作
  151. /// </summary>
  152. private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
  153. {
  154. FormEditBlack editBlack = new FormEditBlack();
  155. editBlack.ShowDialog();
  156. if (editBlack.DialogResult == DialogResult.OK)
  157. {
  158. BindData(condition);
  159. }
  160. }
  161. /// <summary>
  162. /// 分页控件删除操作
  163. /// </summary>
  164. private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
  165. {
  166. try
  167. {
  168. if (DevExpress.XtraEditors.XtraMessageBox.Show("您确定删除选定的记录么?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
  169. {
  170. return;
  171. }
  172. MoWhite selectRow = this.winGridViewPager1.gridView1.GetFocusedRow() as MoWhite;
  173. int id = Convert.ToInt32(selectRow.ID);
  174. bool flag = BLLFactory<BlWhite>.Instance.Delete(id, SysEnvironment.CurrentLoginID);
  175. if (flag)
  176. {
  177. DevExpress.XtraEditors.XtraMessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  178. BindData(condition);
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. DevExpress.XtraEditors.XtraMessageBox.Show("删除失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  184. LogHelper.log.Error(string.Format("删除数据库black出现错误:{0}", ex));
  185. }
  186. }
  187. void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
  188. {
  189. if (e.Column.ColumnType == typeof(DateTime))
  190. {
  191. string columnName = e.Column.FieldName;
  192. if (e.Value != null)
  193. {
  194. if (Convert.ToDateTime(e.Value) <= Convert.ToDateTime("1900-1-1"))
  195. {
  196. e.DisplayText = "";
  197. }
  198. else
  199. {
  200. e.DisplayText = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd HH:mm");//yyyy-MM-dd
  201. }
  202. }
  203. }
  204. if (e.Column.FieldName == "Type")
  205. {
  206. e.DisplayText = Dic_Type[(e.Value).ToString()];
  207. }
  208. }
  209. void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
  210. {
  211. //AuthorizeType,Forbid
  212. if (e.Column.FieldName == "Type")
  213. {
  214. Color color = Color.White;
  215. if (e.CellValue.ToString() == "0")
  216. {
  217. e.Appearance.BackColor = Color.Black;
  218. e.Appearance.BackColor2 = Color.LightCyan;
  219. }
  220. else
  221. {
  222. e.Appearance.BackColor = Color.White;
  223. }
  224. }
  225. if (e.Column.FieldName == "Forbid")
  226. {
  227. Color color = Color.White;
  228. if (e.CellValue.ToString().ToBoolean())
  229. {
  230. e.Appearance.BackColor = Color.Red;
  231. e.Appearance.BackColor2 = Color.LightCyan;
  232. }
  233. }
  234. }
  235. private void btn_Query_Click(object sender, EventArgs e)
  236. {
  237. if ( string.IsNullOrEmpty(txt_Name.Text) && lookUp_UserName.Text == "全部用户" && lookUp_Type.Text == "全部" && check_Forbid.CheckState != CheckState.Checked)
  238. {
  239. BindData(" 1=1");
  240. condition = " 1=1";
  241. return;
  242. }
  243. string sql = " 1=1";
  244. if (!string.IsNullOrEmpty(txt_Name.Text))
  245. {
  246. sql += string.Format(" AND black_name = '{0}'", txt_Name.Text);
  247. }
  248. if (lookUp_UserName.Text != "全部用户")
  249. {
  250. sql += string.Format(" AND black_UserAccount = {0}", lookUp_UserName.EditValue.ToString());
  251. }
  252. if (lookUp_Type.Text != "全部")
  253. {
  254. sql += string.Format(" AND black_type = {0}", lookUp_Type.EditValue.ToString());
  255. }
  256. if (check_Forbid.CheckState == CheckState.Checked)
  257. {
  258. sql += string.Format(" AND black_forbid = {0}", 1);
  259. }
  260. BindData(sql);
  261. condition = sql;
  262. }
  263. private void simpleButton2_Click(object sender, EventArgs e)
  264. {
  265. winGridViewPager1_OnAddNew(sender, e);
  266. }
  267. }
  268. }