using Aucma.Scada.Business; using Aucma.Scada.Model.domain; using Aucma.Scada.Model.dto; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using HighWayIot.Config; using HighWayIot.Log4net; using LiveCharts; using LiveCharts.Wpf; using MySqlX.XDevAPI.Common; using MySqlX.XDevAPI.Relational; using SqlSugar; using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.InteropServices; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using System.Windows.Media; namespace Aucma.Scada.UI.viewModel.InStoreInfo { public class InStoreInfoViewModel : ViewModelBase { [DllImport("user32.dll")] public static extern int MessageBoxTimeoutA(IntPtr hWnd, string msg, string Caps, int type, int Id, int time); private LogHelper logHelper = LogHelper.Instance; private ObservableCollection listItems = new ObservableCollection(); private ObservableCollection taskItems = new ObservableCollection(); private InStoreBusiness inStoreBusiness = InStoreBusiness.Instance; private OutStoreBusiness outStoreBusiness = OutStoreBusiness.Instance; private InStoreTaskHandle inStoreTaskHandle = InStoreTaskHandle.Instance; private AppConfig appConfig = AppConfig.Instance; public InStoreInfoViewModel() { inStoreBusiness.RefreshInStoreTaskEvent += RefreshInStoreTask; inStoreBusiness.RefreshScanMateriaCodeEvent += RefreshScanInfo; inStoreBusiness.RefreshLogMessageEvent += PrintMessageToListBox; outStoreBusiness.RefreshStoreStockEvent += Init; inStoreTaskHandle.SpaceCapacityAlarmEvent += async (storeCode, spaceName) => { // MessageBoxTimeoutA((IntPtr)0, $"货道预警:{spaceName};库存即将放满请尽快清理库存,10秒后关闭提示", "告警提示", 0, 0, 10000); AlarmMessage = $"货道预警:{spaceName}库存即将放满请尽快清理库存"; Task.Delay(1000*10).Wait(); AlarmMessage = string.Empty; }; QueryCommand = new RelayCommand(Query); ResetCommand = new RelayCommand(Reset); materialTypeCombox = "所有"; Init(); } #region 参数定义 /// /// 箱壳物料条码 /// private string shellMaterialCode = string.Empty; public string ShellMaterialCode { get { return shellMaterialCode; } set { shellMaterialCode = value; RaisePropertyChanged(nameof(ShellMaterialCode)); } } /// /// 箱壳物料名称 /// private string shellMaterialName = string.Empty; public string ShellMaterialName { get { return shellMaterialName; } set { shellMaterialName = value; RaisePropertyChanged(nameof(ShellMaterialName)); } } /// /// 箱壳入库货道 /// private string shellSpaceName = string.Empty; public string ShellSpaceName { get { return shellSpaceName; } set { shellSpaceName = value; RaisePropertyChanged(nameof(ShellSpaceName)); } } /// /// 内胆物料条码 /// private string linerMaterialCode = string.Empty; public string LinerMaterialCode { get { return linerMaterialCode; } set { linerMaterialCode = value; RaisePropertyChanged(nameof(LinerMaterialCode)); } } /// /// 内胆物料名称 /// private string linerMaterialName = string.Empty; public string LinerMaterialName { get { return linerMaterialName; } set { linerMaterialName = value; RaisePropertyChanged(nameof(LinerMaterialName)); } } /// /// 内胆入库货道 /// private string linerSpaceName = string.Empty; public string LinerSpaceName { get { return linerSpaceName; } set { linerSpaceName = value; RaisePropertyChanged(nameof(LinerSpaceName)); } } private string alarmMessage = string.Empty; public string AlarmMessage { get { return alarmMessage; } set { alarmMessage = value; RaisePropertyChanged(nameof(AlarmMessage)); } } /// /// LisBox数据模板 /// private IEnumerable logInfoListBox; public IEnumerable LogInfoListBox { get { return logInfoListBox; } set { logInfoListBox = value; RaisePropertyChanged(() => LogInfoListBox); } } /// /// 箱壳入库任务DataGrid /// private IEnumerable instoreTask; public IEnumerable InstoreTask { get { return instoreTask; } set { instoreTask = value; RaisePropertyChanged(() => InstoreTask); } } /// /// 内胆入库任务DataGrid /// private ObservableCollection linerInstoreTask; public ObservableCollection LinerInstoreTask { get { return linerInstoreTask; } set { linerInstoreTask = value; RaisePropertyChanged(() => LinerInstoreTask); } } /// /// 任务列表搜索条件 /// private String search = String.Empty; public String Search { get { return search; } set { search = value; RaisePropertyChanged(() => Search); } } /// /// 下拉框 /// public string materialTypeCombox; public string MaterialTypeCombox { get { return materialTypeCombox; } set { if (materialTypeCombox != value) { materialTypeCombox = value; RaisePropertyChanged(() => MaterialTypeCombox); } } } /// /// 库存统计柱状图 /// private SeriesCollection achievement = new SeriesCollection(); public SeriesCollection Achievement { get { return achievement; } set { achievement = value; } } /// /// 库存统计柱状图X轴物料信息 /// private List materialNameList; public List MaterialNameList { get { return materialNameList; } set { materialNameList = value; } } #endregion #region 事件定义 /// /// 查询事件 /// public RelayCommand QueryCommand { get; set; } /// /// 重置 /// public RelayCommand ResetCommand { get; set; } #endregion /// /// listBox绑定日志 /// /// private void PrintMessageToListBox(string message) { try { listItems.Add($"{DateTime.Now.ToString("HH:mm:ss")}==>{message}"); while (listItems.Count > 120) { listItems.RemoveAt(0); } LogInfoListBox = listItems.OrderByDescending(x => x); } catch (Exception ex) { logHelper.Error("日志数据绑定异常", ex); } } /// /// 箱壳入库任务列表查询 /// public void Query() { lock (string.Empty) { try { taskItems = new ObservableCollection(); #region 通过数据库获取数据进行刷新 var info = inStoreBusiness.GetInStoreTask(); if (info != null) { if (materialTypeCombox == "箱壳") { materialTypeCombox = appConfig.shellStoreCode; } else if (materialTypeCombox == "内胆") { materialTypeCombox = appConfig.linerStoreCode; } else if (materialTypeCombox == "所有") { materialTypeCombox = string.Empty; } //info.ForEach(x => RefreshInStoreTask(x)); try { info = info.Where(x => !string.IsNullOrEmpty(search) ? x.materialCode == search : 1 == 1 && !string.IsNullOrEmpty(materialTypeCombox) ? x.storeCode == materialTypeCombox : 1 == 1).ToList(); App.Current.Dispatcher.BeginInvoke((Action)(() => { taskItems.Clear(); foreach (var item in info) { item.materialType = inStoreBusiness.GetMaterialName(item.materialType); taskItems.Add(item); } InstoreTask = taskItems; })); } catch (Exception ex) { logHelper.Error("入库任务列表刷新异常", ex); } } #endregion } catch (Exception ex) { logHelper.Error("入库任务加载异常", ex); } } } /// /// 重置 /// public void Reset() { Search = string.Empty; MaterialTypeCombox = string.Empty; this.Query(); } /// /// 柱状体加载 /// public void Init() { try { Query(); ChartValues shellAchievement = new ChartValues(); ChartValues linerAchievement = new ChartValues(); MaterialNameList = new List(); // List info = inStoreBusiness.GetMaterialStock(); List list = inStoreBusiness.GetStock(); App.Current.Dispatcher.BeginInvoke((Action)(() => { if (list != null && list.Count > 0) { Achievement.Clear(); foreach (var item in list) { MaterialNameList.Add(SubStringByName(item.typeNameA)); if (item.storeCode == appConfig.shellStoreCode) { shellAchievement.Add(item.spaceStock); linerAchievement.Add(0); } else if (item.storeCode == appConfig.linerStoreCode) { linerAchievement.Add(item.spaceStock); shellAchievement.Add(0); } } } var shellColumn = new ColumnSeries(); shellColumn.DataLabels = true; shellColumn.Title = "箱壳"; shellColumn.Values = shellAchievement; shellColumn.Foreground = Brushes.White; var linerColumn = new ColumnSeries(); linerColumn.DataLabels = true; linerColumn.Title = "内胆"; linerColumn.Values = linerAchievement; linerColumn.Foreground = Brushes.White; linerColumn.Fill = new SolidColorBrush(Color.FromRgb(15, 209, 226)); Achievement.Add(shellColumn); Achievement.Add(linerColumn); })); } catch (Exception ex) { logHelper.Error(ex.Message.ToString()); } } /// /// 截取逗号之间的名称 /// /// /// private string SubStringByName(string name) { if (string.IsNullOrEmpty(name)) return string.Empty; string result = string.Empty; try { int firstCommaIndex = name.IndexOf(','); int secondCommaIndex = name.IndexOf(',', firstCommaIndex + 1); if (firstCommaIndex != -1 && secondCommaIndex != -1) { result = name.Substring(firstCommaIndex + 1, secondCommaIndex - firstCommaIndex - 1); } } catch (Exception ex) { logHelper.Error(ex.Message.ToString()); } return result; } /// /// 刷新入库任务列表 /// /// private void RefreshInStoreTask(RealTaskInfo taskInfo, bool isFinsih = false) { if (isFinsih) { Init(); } else { Query(); } } /// /// 刷新扫码信息 /// /// /// /// /// private void RefreshScanInfo(string materialCode, string materialName, string spaceName, string materialType) { if (materialType == appConfig.shellStoreCode) { ShellMaterialCode = materialCode; ShellMaterialName = materialName; ShellSpaceName = spaceName; } else if (materialType == appConfig.linerStoreCode) { LinerMaterialCode = materialCode; LinerMaterialName = materialName; LinerSpaceName = spaceName; } } } }