HKC/DM_Weight/ViewModels/AdditionWindowViewModel.cs

160 lines
5.7 KiB
C#

using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using log4net;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace DM_Weight.ViewModels
{
public class AdditionWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
{
public bool KeepAlive => false;
private readonly ILog logger = LogManager.GetLogger(typeof(AdditionWindowViewModel));
private List<ChannelStock> channelStocks;
public List<ChannelStock> ChannelStocks
{
get => channelStocks;
set => SetProperty(ref channelStocks, value);
}
List<ChannelStock> selectedStock=new List<ChannelStock>();
private object _finishStatus = Visibility.Collapsed;
public object FinishStatus
{
get => _finishStatus;
set => SetProperty(ref _finishStatus, value);
}
IDialogService _dialogService;
IEventAggregator _eventAggregator;
public AdditionWindowViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
{
_dialogService = dialogService;
_eventAggregator = eventAggregator;
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
_eventAggregator.GetEvent<IsSelectedEvent>().Unsubscribe(SetIsSelected);
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
_eventAggregator.GetEvent<IsSelectedEvent>().Subscribe(SetIsSelected);
RequestData();
}
private void RequestData()
{
ChannelStocks = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Includes<ChannelList>(cs => cs.ChannelLst)
.Includes<DrugInfo>(cs => cs.DrugInfo)
.Where(cs => cs.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cs.BaseQuantity > cs.Quantity)
.OrderBy(cs => cs.Chnguid)
.OrderBy(cs => cs.DrawerNo)
.ToList();
ChannelStocks.ForEach(cs => cs.AddQuantity = cs.BaseQuantity - cs.Quantity);
}
//开药箱放入药品
public DelegateCommand OpenBoxCommand
{
get => new DelegateCommand(() =>
{
selectedStock = ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).ToList();
if (selectedStock != null && selectedStock.Count > 0)
{
for (int i = 0; i < selectedStock.Count; i++)
{
ModbusHelper.GetInstance().OpenBoxDoor(selectedStock[i].DrawerNo - 1);
Thread.Sleep(100);
}
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = $"未选择药箱,请先选择药箱",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
return;
}
FinishStatus = Visibility.Visible;
});
}
//完成按钮
public DelegateCommand AddFinish
{
get => new DelegateCommand(() =>
{
//更新 交接柜 库存信息
if (selectedStock != null && selectedStock.Count > 0)
{
for (int j = 0; j < selectedStock.Count; j++)
{
// 更新数据 交接柜 库存信息
ChannelStock jiaojie_it = selectedStock[j];
SqlSugarHelper.Db.Updateable(new ChannelStock()
{
Quantity = jiaojie_it.BaseQuantity,
//ManuNo = it.ManuNo,
//EffDate = it.EffDate,
Id = jiaojie_it.Id,
}).UpdateColumns(jiaojie_it => new { jiaojie_it.Quantity }).ExecuteCommand();
}
}
});
}
//刷新
public DelegateCommand QueryCommand
{
get => new DelegateCommand(() => RequestData());
}
//设置选中药箱的复选框状态
private void SetIsSelected(ChannelStock channelStock)
{
if (channelStock != null)
{
channelStock.ChannelLst.IsSelected = !channelStock.ChannelLst.IsSelected;
if (channelStock != null && ChannelStocks != null)
{
ChannelStocks = ChannelStocks.Select(x =>
{
for (int i = 0; i < ChannelStocks.Count; i++)
{
if (ChannelStocks[i].DrawerNo == channelStock.DrawerNo)
{
ChannelStocks[i].ChannelLst = channelStock.ChannelLst;
}
}
return x;
}).ToList();
}
ICollectionView vw = CollectionViewSource.GetDefaultView(ChannelStocks);
vw.GroupDescriptions.Add(new PropertyGroupDescription("ChannelLst"));
}
}
}
}