using HandyControl.Controls;
using Newtonsoft.Json;
using Ping9719.WpfEx;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
namespace 智能仓储WCS管理系统.ViewModel
{
public class MainViewModel : BindableBase
{
Mutex mutex;
private string title = string.Empty;
///
/// 标题
///
public string Title { get => title; set { SetProperty(ref title, value); } }
private bool goToOutVentoryView;
public bool GoToOutVentoryView
{
get { return goToOutVentoryView; }
set
{
SetProperty(ref goToOutVentoryView, value);
}
}
private bool goToStockTakingView;
public bool GoToStockTakingView
{
get { return goToStockTakingView; }
set
{
SetProperty(ref goToStockTakingView, value);
}
}
#region 专用_MainWindow2
//隐藏式选项卡的高度
public double TabItemHeight { get; set; } = 0;
private string SelectedValue_ = "主页";
public string SelectedValue { get => SelectedValue_; set { SetProperty(ref SelectedValue_, value); } }
#endregion
private DateTime time;
///
/// 时间
///
public DateTime Time { get => time; set { SetProperty(ref time, value); } }
///
/// 在初始化前的相关检查
///
public bool InitAgo()
{
//只允许打开一个
mutex = new Mutex(true, string.Concat("MengXun智能仓储WCS管理系统", Path.GetFileNameWithoutExtension(LocalFile.AppName)), out bool createdNew);
if (!createdNew)
{
MessageBox.Warning("已有实列在运行!", "提示");
return false;
}
//初始化
Tuple[] tuples = new Tuple[]
{
new Tuple("检测文件..", "缺少文件,尝试将项目[data/]中的所有文件复制到[bin/data/]中", () =>
{
if(!System.IO.File.Exists(LocalFile.ConfigPath))
throw new Exception("缺少文件,请尝试将项目[data/]中的所有文件复制到[bin/data/]中");
}),
new Tuple("检测文件..", "配置文件中没有内容,请联系管理员", () =>
{
if(string.IsNullOrWhiteSpace(System.IO.File.ReadAllText(LocalFile.ConfigPath, Encoding.UTF8)))
throw new Exception($"配置文件[{LocalFile.ConfigPath}]中没有内容");
}),
new Tuple("初始化数据中..", "初始化数据库失败,请联系管理员", () =>
{
}),
new Tuple("初始化扫码枪连接..", "初始化扫码枪连接失败,请联系管理员", () =>
{
ScannerManager.InitScanners();
}),
new Tuple("初始化WebSocket连接..", "初始化WebSocket连接失败,请联系管理员", () =>
{
WebSocket.InitWebSocket();
}),
};
MainLoadWindow.TaskSleepTime = 200;
if (!MainLoadWindow.Show(tuples))
return false;
//清理日志
Task.Run(() =>
{
if (LocalFile.Config.Sys.SaveLogDay < 0)
return;
Logs.Clear(TimeSpan.FromDays(LocalFile.Config.Sys.SaveLogDay));
});
//不登录,直接使用最高权限
//{
// UserLoginView.NotLogin();
// return true;
//}
//登录
{
var userLoginView = new UserLoginView();
var isok = userLoginView.ShowDialog() == true;
userLoginView = null;
return isok;
}
}
///
/// 初始化并运行时间
///
public async void Init(System.Windows.Window window)
{
//加载 程序名称
var versionInfo = FileVersionInfo.GetVersionInfo(LocalFile.AppPath);
Title = versionInfo.ProductName;
//加载 窗体模式
if (LocalFile.Config.Sys.StartupFull)
window.WindowState = System.Windows.WindowState.Maximized;
//注册 设备
IotDevice.UserChange = ((iot) =>
{
if (string.IsNullOrEmpty(iot.Funct))
Growl.Info($"你点击了[{iot.Name}],尝试改为[{iot.Val}]");
else
Growl.Info($"你点击了[{iot.Name}][{iot.Funct}],尝试改为[{iot.Val}]");
});
//注册 信息
TextBoxLog.TextBoxLogAdd = ((info) =>
{
Logs.Write(info.Text, LogsType.Info, info.Time);
});
//加载 时钟
while (true)
{
Time = DateTime.Now;
await Task.Delay(1);
}
}
public ICommand OpenUserCommand { get => new DelegateCommand(OpenUser); }
///
/// 打开用户
///
public void OpenUser()
{
if (UserInfoView.viewModel.IsLogin)
{
var userInfoView = new UserInfoView();
userInfoView.ShowDialog();
if (userInfoView.IsExitLogin)
{
Growl.Info("您已退出登录。");
userInfoView = null;
}
}
else
{
var userLoginView = new UserLoginView();
//登录成功
if (userLoginView.ShowDialog() == true)
{
Growl.Success($"欢迎您:{UserInfoView.viewModel.LoginName}");
userLoginView = null;
}
}
}
public ICommand OpenLogCommand { get => new DelegateCommand(OpenLog); }
///
/// 打开日记
///
public void OpenLog()
{
if (!System.IO.Directory.Exists(LocalFile.LogDir))
{
Growl.Info("暂时没有日志。");
return;
}
try
{
Folder.OpenFolder(LocalFile.LogDir);
}
catch (Exception)
{
Growl.Error("打开日志目录失败。");
}
}
public ICommand OpenHelpCommand { get => new DelegateCommand(OpenHelp); }
///
/// 打开帮助
///
public void OpenHelp()
{
if (!System.IO.File.Exists(LocalFile.DocPath))
{
Growl.Info("帮助文档正在书写中。");
return;
}
try
{
Folder.OpenFolderAndSelectedFile(LocalFile.DocPath);
}
catch (Exception)
{
Growl.Error("打开帮助文档失败。");
}
}
public ICommand OpenWeCommand { get => new DelegateCommand(OpenWe); }
///
/// 打开关于
///
public void OpenWe()
{
var aboutView = new AboutView();
aboutView.ShowDialog();
aboutView = null;
}
}
}