using DevExpress.XtraGrid.Columns; using Lskj.Business; using Lskj.Business.Impl; using Lskj.Control; using Lskj.Control.Model; using Lskj.Core; using Lskj.Data; using Lskj.Model; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Lskj.PubUserPrivate { public partial class FrmMain : Form { /// /// 全部的人员 /// public DataTable AllUserTable = new DataTable(); /// /// 全部的私有人员人员 /// public DataTable AllPrivateUserTable = new DataTable(); /// /// 全部的私密人员 /// public List AllPrivateUser = new List(); public FrmMain() { InitializeComponent(); this.Load += FrmMain_Load; } /// /// 窗体加载时 /// /// /// private void FrmMain_Load(object sender, EventArgs e) { if (CheckPrivateUserTable()) { this.AllUserTable = GetUsersInfo("", ""); this.AllPrivateUserTable = this.AllUserTable.Clone(); this.AllPrivateUser = GetPrivateUsersInfo(); this.AllUserTable.DefaultView.Sort = "employeeid"; this.AllPrivateUserTable.DefaultView.Sort = "employeeid"; InitGrid(); InitBtn(); } else { Lskj.Control.MessageUtil.Show("私有模块数据表不存在或功能未开启,请联系管理员"); return; } } private void InitBtn() { this.Save.Click += new EventHandler(OnPrivateSave); this.SetRemovePermissions.Click += new EventHandler(OnPrivateDeleteClick); } /// /// 初始化左侧表格 /// private void InitGrid() { this.AllQuickControlEx.GridView.Columns.AddVisible("loginaccount", "员工工号").MinWidth = 100; this.AllQuickControlEx.GridView.Columns.AddVisible("employeename", "员工姓名").MinWidth = 200; this.AllQuickControlEx.GridView.Columns.AddVisible("departmentname", "部门名称").MinWidth = 200; this.checkGrid.GridView.Columns.AddVisible("loginaccount", "员工工号").MinWidth = 100; this.checkGrid.GridView.Columns.AddVisible("employeename", "员工姓名").MinWidth = 200; this.checkGrid.GridView.Columns.AddVisible("departmentname", "部门名称").MinWidth = 200; this.AllQuickControlEx.GridView.BestFitColumns();//设置根据内容填充列宽 this.AllQuickControlEx.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDown; GridDragGrid dragPrivateGrid = new GridDragGrid(this.AllQuickControlEx.GridView, this.checkGrid.GridView); GridDragGrid dragPrivateGridNew = new GridDragGrid(this.checkGrid.GridView, this.AllQuickControlEx.GridView); GridDragGrid.GridAddCheckBox(this.checkGrid.GridView); GridDragGrid.GridAddCheckBox(this.AllQuickControlEx.GridView); this.checkGrid.GridView.OptionsSelection.EnableAppearanceFocusedCell = false; this.AllQuickControlEx.GridControl.AllowDrop = true; this.checkGrid.GridControl.AllowDrop = true; this.AllQuickControlEx.GridView.OptionsBehavior.Editable = false; this.checkGrid.GridView.OptionsBehavior.Editable = false; this.AllQuickControlEx.GridView.OptionsCustomization.AllowSort = false; this.checkGrid.GridView.OptionsCustomization.AllowSort = false; this.AllQuickControlEx.gridControl.DataSource = AllUserTable; this.checkGrid.GridControl.DataSource = AllPrivateUserTable; this.AllQuickControlEx.GridView.BestFitColumns(); this.checkGrid.GridView.BestFitColumns(); foreach (string item in AllPrivateUser) { try { DataRow dataRow = AllUserTable.Select().Where(n => (n["employeeid"] + "").Equals(item)).FirstOrDefault(); if (dataRow != null) { AllPrivateUserTable.Rows.Add(dataRow.ItemArray); AllUserTable.Rows.Remove(dataRow); } } catch (Exception ex) { } } } public void PrivateAccessPermissions(DataRow rowItem, string privateTag) { DataRow dr = this.AllPrivateUserTable.NewRow(); dr.ItemArray = rowItem.ItemArray; this.AllPrivateUserTable.Rows.Add(dr); } /// /// 判断私有模块权限表格是否存在 /// private bool CheckPrivateUserTable() { try { string alterSql = ""; if (!BaseImpl.HasExistsColumn("p_employeeTab", "isPrivateLogin")) { alterSql += "alter table p_employeeTab add isPrivateLogin int;"; } if (!string.IsNullOrEmpty(alterSql)) { SqlHelper.ExecuteNonQuery(alterSql); } return true; } catch (Exception) { return false; } } /// /// 下方数据删除,上方取消勾选 /// /// public void RemovePermissions(DataRow rowItem) { DataRow dr = this.AllUserTable.NewRow(); dr.ItemArray = rowItem.ItemArray; this.AllUserTable.Rows.Add(dr); } #region 事件 /// /// 设置私密权限保存事件 /// /// /// private void OnPrivateSave(object sender, EventArgs e) { //if (!CheckPassWord()) //{ // return; //} int[] rowNumbers = this.AllQuickControlEx.GridView.GetSelectedRows(); List addRows = new List(); foreach (int rowIndex in rowNumbers) { DataRow rowItem = this.AllQuickControlEx.GridView.GetDataRow(rowIndex); addRows.Add(rowItem); } foreach (DataRow dr in addRows) { PrivateAccessPermissions(dr, "1"); this.AllUserTable.Rows.Remove(dr); } SaveProvate(); } /// \ /// 删除权限 QuickOperationControlEx表 /// /// /// private void OnPrivateDeleteClick(object sender, EventArgs e) { //if (!CheckPassWord()) //{ // return; //} int[] rowNumbers = this.checkGrid.GridView.GetSelectedRows(); if (rowNumbers.Length == 0) return; List deleteRows = new List(); foreach (int rowIndex in rowNumbers) { DataRow rowItem = this.checkGrid.GridView.GetDataRow(rowIndex); deleteRows.Add(rowItem); } foreach (DataRow dr in deleteRows) { RemovePermissions(dr); this.AllPrivateUserTable.Rows.Remove(dr); } SaveProvate(); } /// /// 检查密码状态 /// private bool CheckPassWord() { bool isCheck = false; int keyHandle = ERPInfo.Instance.keyHandles[0]; int uPin1 = Convert.ToInt32("0x987F6BCD", 16); int uPin2 = Convert.ToInt32("0xE193C5B2", 16); int uPin3 = Convert.ToInt32("0xD507CC28", 16); int uPin4 = Convert.ToInt32("0x4B125AF6", 16); int rtn = SmartX1Api.SmartX1Open(keyHandle, uPin1, uPin2, uPin3, uPin4); if (rtn == 0) { byte[] readBuffer = new byte[256]; int add = 0; int len = 256; int readPageRtn = SmartX1Api.SmartX1ReadPage(keyHandle, 1, add, ref len, readBuffer); if (readPageRtn == 0) { string savePassword = Encoding.Default.GetString(readBuffer); if (savePassword.Equals(ERPInfo.Instance.dogVerifyPasswordDefault) || string.IsNullOrWhiteSpace(savePassword.Replace("\0", ""))) { MessageUtil.Show("当前未设置U盾密码,请设置后重试"); } else { isCheck = true; } } else { MessageUtil.Show("U盾数据获取失败,请重试或联系管理员"); } } else { MessageUtil.Show("U盾数据获取失败,请重试或联系管理员"); } SmartX1Api.SmartX1Close(keyHandle); return isCheck; } /// /// 获取员工信息 /// /// /// /// private DataTable GetUsersInfo(string depid, string mf) { string where = string.Empty; if (!string.IsNullOrEmpty(depid)) where = " and a.departmentid=" + depid; if (!string.IsNullOrEmpty(mf)) where = string.Format(" and (a.loginaccount like '%{0}%' or a.employeename like '%{0}%' or dbo.P_getpy(a.employeename) like '%{0}%')", mf); string sqlValue = "select a.loginaccount,a.employeeid,a.employeename,a.departmentid,b.departmentname from p_employeeTab a left join P_DepartmentTab b on a.departmentid=b.departmentid where a.sign=0 and a.UseFlag=1 " + where + " order by a.employeeid"; return BaseImpl.GetDataTableResult(sqlValue); } /// /// 获取私密人员 /// private List GetPrivateUsersInfo() { List readVerifyStrList = new List(); try { int keyHandle = ERPInfo.Instance.keyHandles[0]; int uPin1 = Convert.ToInt32("0x987F6BCD", 16); int uPin2 = Convert.ToInt32("0xE193C5B2", 16); int uPin3 = Convert.ToInt32("0xD507CC28", 16); int uPin4 = Convert.ToInt32("0x4B125AF6", 16); int rtn = SmartX1Api.SmartX1Open(keyHandle, uPin1, uPin2, uPin3, uPin4); if (rtn == 0) { string writeKey = string.Format("{0}_{1}", DBConfig.Instance.ServerName, DBConfig.Instance.DataBase); int length = 256; byte[] readBuffer = new byte[length]; int readRtn = SmartX1Api.SmartX1ReadPage(keyHandle, 1, 0, ref length, readBuffer); if (readRtn == 0) { Dictionary readKeysValues = new Dictionary(); string readVerifyStr = Encoding.Default.GetString(readBuffer); string[] readVerifyStrArray = readVerifyStr.Replace("\0", "").Split(';');//获取多个账套设置 foreach (string readVerify in readVerifyStrArray) { string[] readKeyValue = readVerify.Split('^');//获取单个账套 if (readKeyValue.Length == 2) { readKeysValues.Add(readKeyValue[0], readKeyValue[1]); } } if (readKeysValues.ContainsKey(writeKey))//如果存在当前账套 { readVerifyStrList = readKeysValues[writeKey].Split('^').ToList(); } } else { MessageUtil.Show("U盾数据获取失败,请重试或联系管理员"); return readVerifyStrList; } } else { MessageUtil.Show("U盾数据获取失败,请重试或联系管理员"); return readVerifyStrList; } } catch (Exception) { } return readVerifyStrList; } private void SaveProvate() { WaitForm.ShowForm(ResourceKeys.DataSaving); try { string sql = string.Empty; string writeStr = string.Empty; foreach (DataRow dr in this.AllPrivateUserTable.Rows) { writeStr += string.Format("{0},", dr["employeeid"] + ""); } int keyHandle = ERPInfo.Instance.keyHandles[0]; int uPin1 = Convert.ToInt32("0x987F6BCD", 16); int uPin2 = Convert.ToInt32("0xE193C5B2", 16); int uPin3 = Convert.ToInt32("0xD507CC28", 16); int uPin4 = Convert.ToInt32("0x4B125AF6", 16); int rtn = SmartX1Api.SmartX1Open(keyHandle, uPin1, uPin2, uPin3, uPin4); if (rtn == 0) { string writeKey = string.Format("{0}_{1}", DBConfig.Instance.ServerName, DBConfig.Instance.DataBase); int length = 256; byte[] readBuffer = new byte[length]; int readRtn = SmartX1Api.SmartX1ReadPage(keyHandle, 1, 0, ref length, readBuffer); if (readRtn == 0) { Dictionary readKeysValues = new Dictionary(); string readVerifyStr = Encoding.Default.GetString(readBuffer); string[] readVerifyStrArray = readVerifyStr.Replace("\0", "").Split(';');//获取多个账套设置 foreach (string readVerify in readVerifyStrArray) { string[] readKeyValue = readVerify.Split('^');//获取单个账套 if (readKeyValue.Length == 2) { readKeysValues.Add(readKeyValue[0], readKeyValue[1]); } } if (readKeysValues.ContainsKey(writeKey))//如果存在当前账套 { readKeysValues[writeKey] = writeStr.TrimEnd(','); } else { readKeysValues.Add(writeKey, writeStr.TrimEnd(',')); } string allWriteStr = string.Empty; foreach (string key in readKeysValues.Keys) { allWriteStr += string.Format("{0}^{1};", key, readKeysValues[key]); } byte[] writeBuffer = Encoding.Default.GetBytes(allWriteStr); int writeRtn = SmartX1Api.SmartX1WritePage(keyHandle, 1, 0, 256, writeBuffer); if (writeRtn == 0) { MessageUtil.Show("保存成功"); } else { MessageUtil.Show("U盾数据获取失败,请重试或联系管理员"); return; } } else { MessageUtil.Show("U盾数据获取失败,请重试或联系管理员"); return; } } else { MessageUtil.Show("U盾验证失败,请重试或联系管理员"); return; } SmartX1Api.SmartX1Close(keyHandle); } catch (Exception ex) { MessageUtil.Show("保存失败"); } finally { WaitForm.HideForm(); } } #endregion } }