135 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			C#
		
	
	
	
using MaterialDesignThemes.Wpf;
 | 
						|
using Prism.Commands;
 | 
						|
using Prism.Events;
 | 
						|
using Prism.Mvvm;
 | 
						|
using Prism.Regions;
 | 
						|
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Linq;
 | 
						|
using System.Text;
 | 
						|
using System.Threading.Tasks;
 | 
						|
using System.Windows.Media;
 | 
						|
using DM_Weight.msg;
 | 
						|
using DM_Weight.Port;
 | 
						|
using DM_Weight.util;
 | 
						|
using DM_Weight.Finger;
 | 
						|
using DM_Weight.Views;
 | 
						|
using Unity;
 | 
						|
using DM_Weight.Models;
 | 
						|
using System.Runtime.CompilerServices;
 | 
						|
using SqlSugar;
 | 
						|
using System.Configuration;
 | 
						|
 | 
						|
namespace DM_Weight.ViewModels
 | 
						|
{
 | 
						|
    internal class MainWindowViewModel : BindableBase
 | 
						|
    {
 | 
						|
        private string _title = "Prism App";   //标题
 | 
						|
 | 
						|
        private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3));
 | 
						|
 | 
						|
        private SolidColorBrush _colorBrush;
 | 
						|
 | 
						|
 | 
						|
        //private PortUtil _portUtil;
 | 
						|
 | 
						|
        private ScreenUtil _screenUtil;
 | 
						|
 | 
						|
        public SolidColorBrush Background
 | 
						|
        {
 | 
						|
            get => _colorBrush;
 | 
						|
            set => SetProperty(ref _colorBrush, value);
 | 
						|
        }
 | 
						|
 | 
						|
        public ISnackbarMessageQueue SnackbarMessageQueue
 | 
						|
        {
 | 
						|
            get => _snackbarMessageQueue;
 | 
						|
            set => SetProperty(ref _snackbarMessageQueue, value);
 | 
						|
        }
 | 
						|
        public string Title
 | 
						|
        {
 | 
						|
            get { return _title; }
 | 
						|
            set { SetProperty(ref _title, value); }
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
        IEventAggregator eventAggregator;
 | 
						|
 | 
						|
 | 
						|
        //public MainWindowViewModel(IEventAggregator eventAggregator, PortUtil portUtil, ScreenUtil screenUtil)
 | 
						|
        //{
 | 
						|
        //    _portUtil = portUtil;
 | 
						|
        //    this.eventAggregator = eventAggregator;
 | 
						|
        //    this.eventAggregator.GetEvent<SnackbarEvent>().Subscribe(doMyPrismEvent2);
 | 
						|
        //    _screenUtil = screenUtil;
 | 
						|
        //}
 | 
						|
        IRegionManager _regionManager;
 | 
						|
        IUnityContainer _container;
 | 
						|
        PortUtil _portUtil;
 | 
						|
        private List<ChannelStock> _channelStocks = new List<ChannelStock>();
 | 
						|
        //private SqlSugarScope SqlSugarHelper.Db;
 | 
						|
        public MainWindowViewModel(IRegionManager regionManager, IUnityContainer container, IEventAggregator eventAggregator, ScreenUtil screenUtil, PortUtil portUtil)
 | 
						|
        {
 | 
						|
            //_portUtil = portUtil;
 | 
						|
            this.eventAggregator = eventAggregator;
 | 
						|
            this.eventAggregator.GetEvent<SnackbarEvent>().Subscribe(doMyPrismEvent2);
 | 
						|
            _screenUtil = screenUtil;
 | 
						|
            _regionManager = regionManager;
 | 
						|
            _container = container;
 | 
						|
            _portUtil = portUtil;
 | 
						|
            //this.SqlSugarHelper.Db=sqlSugarScope;
 | 
						|
 | 
						|
            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
 | 
						|
            {
 | 
						|
                //_container.RegisterType<object, LoginWindow>("LoginWindow");
 | 
						|
                //_regionManager.RequestNavigate("MainRegion", "LoginWindow");
 | 
						|
 | 
						|
                _container.RegisterType<object, BeforeLogin>("BeforeLogin");
 | 
						|
                _regionManager.RequestNavigate("MainRegion", "BeforeLogin");
 | 
						|
 | 
						|
            }));
 | 
						|
            //string bAutoWriteNum = ConfigurationManager.AppSettings["AutoWriteNum"] ?? "false";
 | 
						|
            //if (bAutoWriteNum == "true")
 | 
						|
            //{
 | 
						|
            //    Task.Factory.StartNew(() => BindStock());
 | 
						|
            //}
 | 
						|
        }
 | 
						|
 | 
						|
        void doMyPrismEvent2(AlertMsg msg)
 | 
						|
        {
 | 
						|
            switch (msg.Type)
 | 
						|
            {
 | 
						|
                case MsgType.INFO:
 | 
						|
                    Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00e676"));
 | 
						|
                    break;
 | 
						|
                case MsgType.ERROR:
 | 
						|
                    Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c"));
 | 
						|
                    break;
 | 
						|
                default:
 | 
						|
                    Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00e676"));
 | 
						|
                    break;
 | 
						|
            }
 | 
						|
            SnackbarMessageQueue.Enqueue(msg.Message);
 | 
						|
        }
 | 
						|
        //写标签数量
 | 
						|
        async Task BindStock()
 | 
						|
        {
 | 
						|
            List<ChannelStock> singleChannels = SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.BoardType == 5 && cs.Quantity > 0)
 | 
						|
                                                                                           .Where(cs => cs.DrugId != null)
 | 
						|
                                                                                           .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).ToList();
 | 
						|
            singleChannels = singleChannels.GroupBy(cs => new { cs.DrawerNo, cs.ColNo })
 | 
						|
                                         .Select(cs =>
 | 
						|
                                         {
 | 
						|
                                             var ret = cs.First();
 | 
						|
                                             ret.Quantity = cs.Sum(xt => xt.Quantity);
 | 
						|
                                             return ret;
 | 
						|
                                         }).ToList();
 | 
						|
            for (int i = 0; i < singleChannels.Count; i++)
 | 
						|
            {
 | 
						|
                _portUtil.WriteQuantity(singleChannels[i].DrawerNo, singleChannels[i].ColNo, singleChannels[i].Quantity);
 | 
						|
                await Task.Delay(TimeSpan.FromMilliseconds(20));
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |