using Aucma.Scada.Business; using Aucma.Scada.Model.domain; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using HighWayIot.Config; using HighWayIot.Log4net; using System; using System.Collections; using System.Collections.ObjectModel; using System.Linq; using System.Windows; namespace Aucma.Scada.UI.viewModel.OutStoreInfo { public class OutStoreInfoViewModel : ViewModelBase { private LogHelper logHelper = LogHelper.Instance; private ObservableCollection listItems = new ObservableCollection(); private ObservableCollection taskItems = new ObservableCollection(); private OutStoreBusiness outStoreBusiness = OutStoreBusiness.Instance; private AppConfig appConfig = AppConfig.Instance; public OutStoreInfoViewModel() { outStoreBusiness.RefreshOutStoreTaskEvent += RefreshOutStoreTask; outStoreBusiness.RefreshScanMateriaCodeEvent += RefreshScanInfo; outStoreBusiness.RefreshLogMessageEvent += PrintMessageToListBox; QueryCommand = new RelayCommand(Query); ResetCommand = new RelayCommand(Reset); DeleteTaskInfoCommand = new RelayCommand(obj => DeleteTaskInfo(obj)); this.Query(); } #region 参数定义 /// /// 任务编号 /// private string taskCode = string.Empty; public string TaskCode { get { return taskCode; } set { taskCode = value; RaisePropertyChanged(nameof(TaskCode)); } } /// /// 计划编号 /// private string planCode = string.Empty; public string PlanCode { get { return planCode; } set { planCode = value; RaisePropertyChanged(nameof(PlanCode)); } } /// /// 订单编号 /// private string orderCode = string.Empty; public string OrderCode { get { return orderCode; } set { orderCode = value; RaisePropertyChanged(nameof(OrderCode)); } } /// /// 物料条码 /// private string materialCode = string.Empty; public string MaterialCode { get { return materialCode; } set { materialCode = value; RaisePropertyChanged(nameof(MaterialCode)); } } /// /// 物料名称 /// 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)); } } /// /// LisBox数据模板 /// private IEnumerable logInfoListBox; public IEnumerable LogInfoListBox { get { return logInfoListBox; } set { logInfoListBox = value; RaisePropertyChanged(() => LogInfoListBox); } } /// /// 出库任务DataGrid /// private IEnumerable outstoreTask; public IEnumerable OutstoreTask { get { return outstoreTask; } set { outstoreTask = value; RaisePropertyChanged(() => OutstoreTask); } } /// /// 查询条件-任务编号 /// private string taskCodeSearch = string.Empty; public string TaskCodeSearch { get { return taskCodeSearch; } set { taskCodeSearch = value; RaisePropertyChanged(nameof(TaskCodeSearch)); } } private string materialCodeSearch = string.Empty; /// /// 查询条件-物料编号 /// public string MaterialCodeSearch { get { return materialCodeSearch; } set { materialCodeSearch = value; RaisePropertyChanged(nameof(MaterialCodeSearch)); } } /// /// 查询条件-计划编号 /// private string planCodeSearch = string.Empty; public string PlanCodeSearch { get { return planCodeSearch; } set { planCodeSearch = value; RaisePropertyChanged(nameof(PlanCodeSearch)); } } #endregion #region 事件定义 /// /// 查询事件 /// public RelayCommand QueryCommand { get; set; } /// /// 重置 /// public RelayCommand ResetCommand { get; set; } /// /// 删除 /// public RelayCommand DeleteTaskInfoCommand { 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); } } /// /// 刷新出库任务列表 /// /// private void RefreshOutStoreTask(RealTaskInfo taskInfo) { try { App.Current.Dispatcher.Invoke((Action)(() => { taskInfo.materialType = outStoreBusiness.GetMaterialName(taskInfo.materialType); taskItems.Add(taskInfo); OutstoreTask = taskItems; })); } catch (Exception ex) { logHelper.Error("入库任务列表刷新异常", ex); } } /// /// 刷新扫码信息 /// /// /// /// /// private void RefreshScanInfo(string materialCode, string materialName, string spaceName, string storeCode) { MaterialCode = materialCode; MaterialName = materialName; SpaceName = spaceName; Query(); } /// /// 初始化 /// private void Query() { try { var info = outStoreBusiness.GetOutStoreTask(); if (info != null) { if (!string.IsNullOrEmpty(taskCodeSearch)) { info = info.Where(x => x.taskCode.Contains(taskCodeSearch)).ToList(); } else if (!string.IsNullOrEmpty(materialCodeSearch)) { info = info.Where(x => x.materialCode.Contains(materialCodeSearch)).ToList(); } else if (!string.IsNullOrEmpty(planCodeSearch)) { info = info.Where(x => x.planCode.Contains(planCodeSearch)).ToList(); } App.Current.Dispatcher.Invoke((Action)(() => { taskItems = new ObservableCollection(); foreach (var item in info) { item.materialType = outStoreBusiness.GetMaterialName(item.materialType); taskItems.Add(item); } OutstoreTask = taskItems.OrderBy(x => x.createTime); })); } } catch (Exception ex) { logHelper.Error("OutStoreViewModel初始化异常", ex); } } /// /// 重置 /// private void Reset() { this.TaskCodeSearch = string.Empty; this.MaterialCodeSearch = string.Empty; this.PlanCodeSearch = string.Empty; this.Query(); } /// /// 删除任务 /// /// private void DeleteTaskInfo(object obj) { string taskCode = obj as string; if (outStoreBusiness.DeleteTaskInfoByTaskCode(taskCode)) { MessageBox.Show("任务删除成功"); Query(); } else { MessageBox.Show("任务删除失败"); } } } }