/****************************** * 说明:切换账户 * 创建人:龚宇超 * 创建日期:2017-08-14 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ 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; using Lskj.Control; using Lskj.Business; using Lskj.Model; using Lskj.Main2.Model; using Lskj.Business.Impl; using Lskj.Core; using DevExpress.LookAndFeel; using Lskj.Util; using Lskj.Data; using DevExpress.XtraEditors.Controls; using Lskj.Control.Model; namespace Lskj.Main2 { /// /// 切换账户 /// public partial class FrmSwap : BaseForm { /// /// 记录窗口位置 /// private DataTable _userTable; private DataTable _ledgerTable; /// /// 记录初始套账信息 /// private string _ledgerName; public FrmSwap() { InitializeComponent(); //lueUserName.AutoShowPopup = false; } /// /// 说明:帐套 /// 创建人:龚宇超 /// 创建日期:2019-11-11 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Name of the type. private void StartLedger(string ledgerValue) { DataRow focusRow = _ledgerTable.Rows.Cast().FirstOrDefault(x => ledgerValue.Equals(x["ShowName"] + "")); if (focusRow != null) { string serverIP = focusRow["IP"] + ""; string dbName = focusRow["DBName"] + ""; string dataBook = focusRow["ShowName"] + ""; if (string.IsNullOrWhiteSpace(serverIP) || string.IsNullOrWhiteSpace(dbName)) { MessageUtil.Show(ResourceKeys.UnSetServerAddress); return; } else { DBConfig.Instance.DataBook = dataBook; DBConfig.Instance.ServerName = serverIP; DBConfig.Instance.DataBase = dbName; if (!DBConfig.Instance.CreateConnection()) { MessageUtil.Show(ResourceKeys.ServerAddressFault); return; } else { if (DelphiHelper.Delphi_Init(new StringBuilder(DBConfig.Instance.GetDelphiConnection())) == 0) { MessageUtil.Show(ResourceKeys.UnConnectServer + "[By Delphi]"); return; } ERPInfo.Instance.AccountBook = ResourceKeys.CurrentAccountBook + ledgerValue; SystemInfo.RefreshSystemParam(); this.SetUsers(); } } } } /// /// 说明:设置帐套信息 /// 创建人:龚宇超 /// 创建日期:2017-08-09 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetLedgers() { // 获取帐套信息 try { _ledgerTable = MainImpl.GetLedgerList(); cbLedger.Properties.Items.Clear(); if (_ledgerTable.Rows.Count > 0) { foreach (DataRow item in _ledgerTable.Rows) { cbLedger.Properties.Items.Add(item["ShowName"]); } if (!string.IsNullOrEmpty(DBConfig.Instance.DataBook)) { DataRow ledRow = _ledgerTable.Rows.Cast().FirstOrDefault(x => DBConfig.Instance.DataBook.Equals(x["ShowName"] + "")); cbLedger.Text = ledRow != null ? ledRow["ShowName"] + "" : _ledgerTable.Rows[0]["ShowName"] + ""; } else { cbLedger.Text = _ledgerTable.Rows[0]["ShowName"] + ""; } //this.StartLedger(cbLedger.EditValue + ""); } else { cbLedger.Enabled = false; } _ledgerName = cbLedger.Text; } catch (Exception) { } } /// /// 说明:设置系统用户 /// 创建人:龚宇超 /// 创建日期:2017-08-09 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// void SetUsers() { _userTable = MainImpl.GetEmployeeList(); lueUserName.AutoShowPopup = "1".Equals(BaseImpl.GetResult("SELECT LoginPopupFlag FROM P_SystemTab") + ""); lueUserName.Focus(); lueUserName.ValueField = lueUserName.ValueMember = "UserId"; lueUserName.TextMember = "UserName"; lueUserName.SourceSQL = "select EmployeeId UserId,LoginAccount UserCode,EmployeeName UserName from P_EmployeeTab WHERE sign=0 and UseFlag=1"; lueUserName.DataSource = _userTable; DataRow[] rowItems = _userTable.Select(" UserName = '" + DBConfig.Instance.LoginName + "'"); if (rowItems != null && rowItems.Length > 0) { lueUserName.EditValue = rowItems[0][lueUserName.ValueField] + ""; } } /// /// 说明:设置delphi参数 /// 创建人:龚宇超 /// 创建日期:2018-03-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// void SetDelphiDllParams() { try { StringBuilder sb1 = new StringBuilder(); sb1.Append(ERPInfo.Instance.UserName); StringBuilder sb2 = new StringBuilder(); sb2.Append(ERPInfo.Instance.WindowName); StringBuilder sb3 = new StringBuilder(); sb3.Append(ERPInfo.Instance.SkinName); //给delphi库设置参数(用户id、子系统id、操作员名称、计算机名称) DelphiHelper.Delphi_SetParams(Convert.ToInt32(ERPInfo.Instance.UserId), Convert.ToInt32(ERPInfo.Instance.SubSysId), sb1, sb2, sb3); } catch (Exception ex) { MessageUtil.Show(ex); LogHelper.Instance.WriteError(ex); } } /// /// 保存修改 /// private void saveChange() { try { string userId = lueUserName.EditValue + ""; string userName = lueUserName.Text.Trim(); string password = txtPassword.Text.Trim(); bool isConstraint = chkLogin.Checked; if (String.IsNullOrWhiteSpace(userName)) { MessageUtil.Show(ResourceKeys.UserNameNotNull); return; } DataTable table = MainImpl.GetEmployeeList(); DataRow[] drRows = table.Select("UserName='" + userName + "' or UserCode='" + userName + "'"); if ((drRows == null) || (drRows.Count() < 1)) { MessageUtil.Show(ResourceKeys.NameOrPasswordError); return; } //找到多个操作员 if (drRows.Count() > 1) { MessageUtil.Show(ResourceKeys.DuplicatedNameError); return; } DataRow drRow = drRows[0]; if (drRow != null) { userId = drRow["UserId"] + ""; userName = drRow["UserName"] + ""; } else { userId = ""; } if (string.IsNullOrWhiteSpace(userId)) { MessageUtil.Show(ResourceKeys.UserNotFound); return; } if (!string.IsNullOrWhiteSpace(userId) && !string.IsNullOrWhiteSpace(userName)) { int loginResult = MainImpl.Login(userId, password, isConstraint); switch (loginResult) { case 0: // 登录成功 // 设置皮肤 string skinName = MainImpl.GetUserSkin(userId); skinName = string.IsNullOrEmpty(skinName) ? ERPInfo.Instance.SkinName : skinName; UserLookAndFeel.Default.SetSkinStyle(skinName); ERPInfo.Instance.UserId = userId; ERPInfo.Instance.UserName = userName; ERPInfo.Instance.Password = SHAHelper.EncryptPassword(password); ERPInfo.Instance.SkinName = skinName; ERPInfo.Instance.AccountBook = cbLedger.Text; DBConfig.Instance.DataBook = cbLedger.Text; DBConfig.Instance.LoginName = userName; DBConfig.Instance.NoticeUserID = userId; DBConfig.Instance.NoticeUserName = userName; DBConfig.Instance.WriteConfig(); this.SetDelphiDllParams(); // 设置调用delphi参数 bool isStartMain = true; if (SystemInfo.Instance.ForceUpdatePassword && string.IsNullOrEmpty(password)) { // 弹出修改密码窗口 FrmPassword frmPassword = new FrmPassword(); frmPassword.UserId = userId; frmPassword.UserName = userName; DialogResult result = frmPassword.ShowDialog(); isStartMain = result == DialogResult.OK; } ERPInfo.Instance.UserId = userId; ERPInfo.Instance.UserName = userName; this.Close(); IniHelper.Write("isRefresh", "1"); Manager.ReStartMain(true); //this.DialogResult = System.Windows.Forms.DialogResult.OK; break; case 1: // 密码不正确 MessageUtil.Show(ResourceKeys.NameOrPasswordError); break; case 2: // 已有此用户登录 MessageUtil.Show(ResourceKeys.AlreadyLogin); break; case -1: // 登录失败 default: MessageUtil.Show(ResourceKeys.LoginFault); break; } } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show("保存修改出错!" + Message); } } /// /// 说明:窗口加载 /// 创建人:龚宇超 /// 创建日期:2017-08-15 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void FrmSwap_Load(object sender, EventArgs e) { try { this.SetLedgers(); this.SetUsers(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 说明:切换帐套 /// 创建人:龚宇超 /// 创建日期:2017-08-15 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void OnLedgerClosed(object sender, ClosedEventArgs e) { try { WaitForm.ShowForm(ResourceKeys.ServerConnecting); string ledgerValue = cbLedger.EditValue + ""; this.StartLedger(ledgerValue); } catch (Exception ex) { // LogUtil.WriteError(ResourceKeys.LoginModuleName, ex); string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } finally { WaitForm.HideForm(); } } /// /// 说明:切换用户 /// 创建人:龚宇超 /// 创建日期:2017-08-15 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void btnSave_Click(object sender, EventArgs e) { try { saveChange(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 说明:取消 /// 创建人:龚宇超 /// 创建日期:2017-08-15 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void simpleButton1_Click(object sender, EventArgs e) { try { if (!_ledgerName.Equals(cbLedger.EditValue + "")) { this.StartLedger(_ledgerName); } this.Close(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 点击右上角关闭判断 /// /// protected override void WndProc(ref Message msg) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; if (msg.Msg == WM_SYSCOMMAND && ((int)msg.WParam == SC_CLOSE)) { if (string.IsNullOrWhiteSpace(_ledgerName)) return; if (!_ledgerName.Equals(cbLedger.EditValue + "")) { this.StartLedger(_ledgerName); } } base.WndProc(ref msg); } /// /// 说明:打开搜索面板 /// 创建人:龚宇超 /// 创建日期:2017-08-15 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void lueUserName_Click(object sender, EventArgs e) { try { lueUserName.ShowPopup(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } } }