using CentralControl.BaseData;
using CommonFunc;
using CommonFunc.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using XGL.Data;
using XGL.Data.DBService;
using XGL.Models;
using XGLFinishPro.FormItem;
namespace XGLFinishPro
{
///
/// LoginPage.xaml 的交互逻辑
///
public partial class LoginPage : Window
{
userDB usdb = new userDB();
FormingMachineService formingMachineService = new FormingMachineService();
private BackgroundWorker bw = new BackgroundWorker();
private UserLogin ul;
Main main;
public static string team = "";
public static LoginPage loginpage;
List listShift;
List listFactory;
List listDevice;
public LoginPage()
{
//读取配置文件
Common.ReadConfig();
//初始化
InitializeComponent();
InitProgress();
InitVersion();
InitFactory();
InitDevice();
InitShiftInfo();
}
private void InitProgress()
{
bw.WorkerReportsProgress = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
//进度改变事件
bw.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgessChanged);
//进度完成事件
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
private void InitVersion()
{
try
{
string fileName = "vsion.txt"; // 指定要操作的文件名
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); // 构建完整的文件路径
string fileContent = File.ReadAllText(filePath);
lbVersion.Content = fileContent;
LoginUser.Version = fileContent;
}
catch
{
}
}
public static List GetFactory()
{
string sql = $"SELECT CASE WHEN pool_name LIKE 'ds_%' THEN STUFF( pool_name, 1, 3, '' ) ELSE pool_name END AS pool_name , db_url, park_name FROM sys_datasource WHERE db_url is not null and del_flag=0 ORDER BY pool_name asc";
DataTable dt = Utils.cloudDBHelper.getDataSet(sql).Tables[0];
return Utils.ToDataList(dt);
}
public static List GetBaseEquipment()
{
string sql = $"SELECT equipment_code as EquipmentCode,equipment_name as EquipmentName FROM base_equipment WHERE del_flag=0 and equipment_type_code='equ_type_bzx' ORDER BY equipment_id asc";
DataTable dt = Utils.netClientDBHelper.getDataSet(sql).Tables[0];
return Utils.ToDataList(dt);
}
private void InitFactory()
{
//读取所有工厂数据
listFactory= GetFactory();
//绑定
comboBoxFactory.ItemsSource = listFactory;
comboBoxFactory.SelectedValuePath = "pool_name";
comboBoxFactory.DisplayMemberPath = "park_name";
//添加默认值
comboBoxFactory.SelectedValue= Utils.GetAppSetting("SiteCode");
//添加选择变更事件
comboBoxFactory.SelectionChanged += factory_Changed;
//是否启用工厂选择
comboBoxFactory.IsEnabled = bool.Parse(Utils.GetAppSetting("EnabledSwitchFactory"));
}
public void InitDevice()
{
//读取所有设备数据
listDevice = GetBaseEquipment();
//绑定
comboBoxDeviceCode.ItemsSource = listDevice;
comboBoxDeviceCode.SelectedValuePath = "EquipmentCode";
comboBoxDeviceCode.DisplayMemberPath = "EquipmentCode";
//添加默认值
comboBoxDeviceCode.SelectedValue = Utils.GetAppSetting("DeviceCode");
}
public void InitShiftInfo()
{
DataTable dt = formingMachineService.GetShiftList();
if (dt == null)
{
CustomMessageBox.Show("没有查询到班次信息,请联系管理员!", CustomMessageBoxIcon.Warning);
return;
}
listShift = Utils.ToDataList(dt);
//绑定
comboBoxShift.ItemsSource = listShift;
comboBoxShift.SelectedValuePath = "shift_id";
comboBoxShift.DisplayMemberPath = "shift_desc";
}
private void factory_Changed(object sender, EventArgs e)
{
if (comboBoxFactory.SelectedValue == null)
{
listShift = new List();
return;
}
string factoryCode = comboBoxFactory.SelectedValue.ToString();
if (factoryCode == Utils.GetAppSetting("SiteCode"))
{
return;
}
comboBoxShift.SelectedValue = null;
comboBoxDeviceCode.SelectedValue = null;
listShift = new List();
listDevice = new List();
Utils.SetAppSetting("SiteCode", factoryCode);
Utils.ResetNetClientDBHelper();
InitShiftInfo();
InitDevice();
}
private void comboBoxDeviceCode_KeyUp(object sender, KeyEventArgs e)
{
List mylist = new List();
mylist = listDevice.FindAll(delegate (BaseEquipment s) { return s.EquipmentCode.Contains(comboBoxDeviceCode.Text.Trim()); });
comboBoxDeviceCode.ItemsSource = mylist;
comboBoxDeviceCode.IsDropDownOpen = true;
}
private void showLoading()
{
//遮罩层可见
dldh.Visibility = Visibility.Visible;
zzdl.Visibility = Visibility.Visible;
}
private void closeLoading()
{
//遮罩层不可见
dldh.Visibility = Visibility.Collapsed;
zzdl.Visibility = Visibility.Collapsed;
}
private void btnlogin_Click(object sender, RoutedEventArgs e)
{
try
{
showLoading();
if (string.IsNullOrEmpty(datepickerWorkDate.Text))
{
CustomMessageBox.Show("登录失败,请选择工作日期", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
datepickerWorkDate.Focus();
return;
}
if (comboBoxShift.SelectedValue == null || string.IsNullOrEmpty(comboBoxShift.SelectedValue.ToString()))
{
CustomMessageBox.Show("登录失败,请选择班次", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
comboBoxShift.Focus();
return;
}
if (comboBoxDeviceCode.SelectedValue == null || string.IsNullOrEmpty(comboBoxDeviceCode.SelectedValue.ToString()))
{
CustomMessageBox.Show("登录失败,设备号不存在", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
comboBoxShift.Focus();
return;
}
Utils.SetAppSetting("SiteCode", comboBoxFactory.SelectedValue.ToString());
Utils.SetAppSetting("DeviceCode", comboBoxDeviceCode.SelectedValue.ToString());
var lst = listShift.FindLast(t => t.shift_id == Convert.ToInt32(comboBoxShift.SelectedValue.ToString()));
team = comboBoxShift.SelectedValue.ToString();
ul = new UserLogin
{
username = username.Text.Trim(),
pwd = userpwd.Password.Trim(),
IsChecked = rememberpwd.IsChecked,
shiftID = comboBoxShift.SelectedValue.ToString(),
workDate = datepickerWorkDate.Text,
startTime = lst.Shift_Start_Time,
endTime = lst.Shift_End_Time
};
main = new Main();
bw.RunWorkerAsync();
}
catch (Exception ex)
{
LogHelper.instance.log.Error( "登陆异常:" + ex.Message);
}
finally
{
closeLoading();
}
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
JudgeLogin();
}
private int LoginStatus = 0;
private int JudgeLogin()
{
DateTime dt1 = DateTime.Now;
try
{
if (string.IsNullOrEmpty(ul.username))
{
LoginStatus = -3;
return LoginStatus;
}
if (string.IsNullOrEmpty(ul.pwd))
{
LoginStatus = -2;
return LoginStatus;
}
bw.ReportProgress(1);
bool islogin = usdb.Login(ul.username, ul.pwd, ul.shiftID,ul.teamID,ul.workDate, ul.startTime, ul.endTime);
Utils.userList = usdb.GetUserList();
if (islogin)
{
usdb.UpdateWorkDate(Utils.GetAppSetting("DeviceCode"), ul.shiftID,ul.workDate);
if (ul.IsChecked == true)
{
Common.IniWriteValue("登录明细", "loginUser", ul.username);
Common.IniWriteValue("登录明细", "loginpwd", ul.pwd);
}
else
{
Common.IniWriteValue("登录明细", "loginUser", "");
Common.IniWriteValue("登录明细", "loginpwd", "");
}
bw.ReportProgress(3);
LoginStatus = 200;
}
else
{
LoginStatus = -1;
}
}
catch (Exception ex)
{
LoginStatus = -5;
LogHelper.instance.log.Error("登录异常:" + ex.Message);
}
DateTime dt2 = DateTime.Now;
TimeSpan ts1 = dt2.Subtract(dt1);
LogHelper.instance.log.Info("登录时间差:" + ts1.Seconds);
return LoginStatus;
}
private void bgWorker_ProgessChanged(object sender, ProgressChangedEventArgs e)
{
if (e.ProgressPercentage == 1)
{
zzdl.Content = "校验用户";
}
if (e.ProgressPercentage == 2)
{
zzdl.Content = "正在加载菜单";
}
if (e.ProgressPercentage == 3)
{
zzdl.Content = "校验成功,正在登陆";
}
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
switch (LoginStatus)
{
case 200:
main.Show();
this.Close();
break;
case 0:
CustomMessageBox.Show("登录失败", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
case -1:
CustomMessageBox.Show("账号或密码错误", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
case -2:
CustomMessageBox.Show("请输入密码", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
case -3:
CustomMessageBox.Show("请输入账号", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
case -4:
CustomMessageBox.Show("加载菜单异常,请重试", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
case -5:
CustomMessageBox.Show("登录异常,请重试", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
}
this.dldh.Visibility = Visibility.Hidden;//遮罩层可见
zzdl.Visibility = Visibility.Collapsed;
}
private void btnloginout_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
Application.Current.Shutdown();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
btnCheckUpdate_Click(null, null);
username.Focus();
this.dldh.Visibility = Visibility.Hidden;//遮罩层可见
zzdl.Visibility = Visibility.Collapsed;
string name = Common.IniReadValue("登录明细", "loginUser");
username.Text = name;
string pwd = Common.IniReadValue("登录明细", "loginpwd");
userpwd.Password = pwd;
}
catch (Exception ex)
{
MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
Common.Log.Error(method.DeclaringType.FullName + "-" + method.Name + "\r\n" + ex.Message + "\r\n" + ex.StackTrace);
}
}
private void Window_Closing(object sender, CancelEventArgs e)
{
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();//窗体可移动
}
///
/// 更新按钮
///
///
///
private void btnCheckUpdate_Click(object sender, RoutedEventArgs e)
{
userDB usdb = new userDB();
var sresu = usdb.vsion();
string fileName = "vsion.txt"; // 指定要操作的文件名
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); // 构建完整的文件路径
if (File.Exists(filePath))
{
string fileContent = File.ReadAllText(filePath);
if (sresu != fileContent)
{
if (MessageBox.Show("检测到有新版本,是否更新?", "温馨提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning) == MessageBoxResult.OK)
{
// 设置要启动的应用程序名称和参数
string appName = "WPFupdate.exe"; // 替换为你的目标控制台应用程序的名称
string appArguments = sresu + "," + Process.GetCurrentProcess().MainModule.ModuleName;//sresu;// 替换为你的应用程序需要的参数
// 创建进程启动信息
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = appName,
Arguments = appArguments,
RedirectStandardOutput = false, // 可以选择是否重定向标准输出
UseShellExecute = false, // 必须设置为false,以便在控制台中启动应用程序
CreateNoWindow = false // 设置为true,以便在后台启动应用程序
};
// 创建并启动进程
Process process = new Process
{
StartInfo = startInfo
};
process.Start();
Environment.Exit(0);
}
}
else
{
if(sender != null)
MessageBox.Show("没有检测到新版本", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
else
{
MessageBox.Show("没有找到版本文件", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
Console.WriteLine("文件不存在: " + filePath);
}
}
}
public class UserLogin
{
public string username { get; set; }
public string pwd { get; set; }
public bool? IsChecked { get; set; }
public string teamID { get; set; }
public string shiftID { get; set; }
public string workDate { get; set; }
public string startTime { get; set; }
public string endTime { get; set; }
}
}