XiangTan_JiaoJie/DM_Weight/ViewModels/AdditionWindowViewModel.cs

222 lines
8.6 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;
private PortUtil _portUtil;
public AdditionWindowViewModel(IDialogService dialogService, IEventAggregator eventAggregator, PortUtil portUtil)
{
_dialogService = dialogService;
_eventAggregator = eventAggregator;
_portUtil = portUtil;
}
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= ChannelStocks.GroupBy(it=>new {it.DrawerNo,it.DrugId})
.Select(it=>
{
var ret = it.First();
ret.Quantity = it.Sum(itx => itx.Quantity);
return ret;
}).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)
{
int drawerNo = 0;
for (int i = 0; i < selectedStock.Count; i++)
{
if (!(drawerNo == selectedStock[i].DrawerNo))
{
drawerNo = selectedStock[i].DrawerNo;
_portUtil.SpeakAsync($"正在打开{selectedStock[i].DrawerNo}号药箱");
}
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(() =>
{
var f = SqlSugarHelper.Db.UseTran(() =>
{
//更新 交接柜 库存信息
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,
DrugId=jiaojie_it.DrugId,
DrawerNo=jiaojie_it.DrawerNo,
MachineId=jiaojie_it.MachineId,
}).UpdateColumns(jiaojie_it => new { jiaojie_it.Quantity }).WhereColumns(jiaojie_it=>new { jiaojie_it.DrugId, jiaojie_it.DrawerNo, jiaojie_it.MachineId }).ExecuteCommand();
}
List<ChannelStock> jiaojie = selectedStock.GroupBy(cs => cs.DrawerNo).Select(cs => cs.FirstOrDefault()).ToList();
if (jiaojie != null && jiaojie.Count > 0)
{
for (int j = 0; j < jiaojie.Count; j++)
{
ChannelStock jiaojie_it = jiaojie[j];
ChannelList jiaojieList = new ChannelList();
jiaojieList.State = 0;
jiaojieList.Id = jiaojie_it.ChannelLst.Id;
//更新交接柜状态为 已取药未入库
var result = SqlSugarHelper.Db.Updateable(jiaojieList)
.UpdateColumns(it => new { it.State, it.Id }).ExecuteCommand();
}
}
}
});
if (f.Data)
{
RequestData();
AlertMsg alertMsg = new AlertMsg
{
Message = "操作完成",
Type = MsgType.SUCCESS,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
if (!f.IsSuccess)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "操作失败!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
});
}
//刷新
public DelegateCommand QueryCommand
{
get => new DelegateCommand(() => RequestData());
}
//设置选中药箱的复选框状态
private void SetIsSelected(ChannelStock channelStock)
{
if (channelStock != null)
{
if (channelStock.ChannelLst.State == 1)
{
channelStock.ChannelLst.IsSelected = !channelStock.ChannelLst.IsSelected;
}
else
{
return;
}
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"));
}
}
}
}