using Aucma.Scada.Business; using Aucma.Scada.Model.domain; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using HighWayIot.Config; using HighWayIot.Log4net; using LiveCharts; using LiveCharts.Wpf; using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows.Media; namespace Aucma.Scada.UI.viewModel.InStoreInfo { public class InStoreInfoViewModel : ViewModelBase { 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 AppConfig appConfig = AppConfig.Instance; public InStoreInfoViewModel() { inStoreBusiness.RefreshInStoreTaskEvent += RefreshInStoreTask; inStoreBusiness.RefreshScanMateriaCodeEvent += RefreshScanInfo; inStoreBusiness.RefreshLogMessageEvent += PrintMessageToListBox; outStoreBusiness.RefreshStoreStockEvent += Init; QueryCommand = new RelayCommand(Query); ResetCommand = new RelayCommand(Reset); materialTypeCombox = "所有"; Init(); } #region 参数定义 /// /// 物料条码 /// private string materialCode = string.Empty; public string MaterialCode { get { return materialCode; } set { materialCode = value; RaisePropertyChanged(nameof(MaterialCode)); } } /// /// 扫码时间 /// private string scanTime = string.Empty; public string ScanTime { get { return scanTime; } set { scanTime = value; RaisePropertyChanged(nameof(ScanTime)); } } /// /// 物料名称 /// private string materialName = string.Empty; public string MaterialName { get { return materialName; } set { materialName = value; RaisePropertyChanged(nameof(MaterialName)); } } /// /// 入库货道 /// private string spaceName = string.Empty; public string SpaceName { get { return spaceName; } set { spaceName = value; RaisePropertyChanged(nameof(SpaceName)); } } /// /// 提示信息 /// private string promptInfo = string.Empty; public string PromptInfo { get { return promptInfo; } set { promptInfo = value; RaisePropertyChanged(nameof(PromptInfo)); } } /// /// 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); } } /// /// 任务列表搜索条件 /// 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.ss")}==>{message}"); LogInfoListBox = listItems.OrderByDescending(x => x); } catch (Exception ex) { logHelper.Error("日志数据绑定异常", ex); } } /// /// 箱壳入库任务列表查询 /// public void Query() { lock (string.Empty) { try { #region 通过数据库获取数据进行刷新 var info = inStoreBusiness.GetInStoreTask(); if (info != null) { taskItems = new ObservableCollection(); try { info = info.Where(x => !string.IsNullOrEmpty(search) ? x.materialCode == search : 1 == 1).ToList(); App.Current.Dispatcher.BeginInvoke((Action)(() => { 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() { Query(); ChartValues achievement = new ChartValues(); MaterialNameList = new List(); var info = inStoreBusiness.GetFoamStoreStock(); App.Current.Dispatcher.BeginInvoke((Action)(() => { if (info != null) { Achievement.Clear(); foreach (var item in info) { if (!string.IsNullOrEmpty(item.materialType)) { string name = inStoreBusiness.GetMaterialName(item.materialType); MaterialNameList.Add(name.Substring(0,Math.Min(name.Length,10))); achievement.Add(item.storeStock); } } } var column = new ColumnSeries(); column.DataLabels = true; column.Title = "泡后库"; column.Values = achievement; column.Foreground = Brushes.White; Achievement.Add(column); })); } /// /// 刷新入库任务列表 /// /// private void RefreshInStoreTask(RealTaskInfo taskInfo, bool isFinsih = false) { if (isFinsih) { Init(); } else { Query(); } } /// /// 刷新扫码信息 /// /// /// /// /// private void RefreshScanInfo(string materialCode, string materialName, string spaceName, string materialType) { MaterialCode = materialCode; MaterialName = materialName; SpaceName = spaceName; ScanTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } } }