HKC/DM_Weight/ViewModels/AddToJiaoJieWindowViewModel.cs

242 lines
8.6 KiB
C#
Raw Normal View History

2024-08-08 08:47:17 +08:00
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using log4net;
using log4net.Repository.Hierarchy;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using SqlSugar;
using System;
2024-08-01 13:42:50 +08:00
using System.Collections.Generic;
2024-08-08 08:47:17 +08:00
using System.Configuration;
2024-08-01 13:42:50 +08:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DM_Weight.ViewModels
{
2024-08-08 08:47:17 +08:00
public class AddToJiaoJieWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
2024-08-01 13:42:50 +08:00
{
2024-08-08 08:47:17 +08:00
private readonly ILog logger = LogManager.GetLogger(typeof(AddToJiaoJieWindowViewModel));
public bool KeepAlive => false;
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
2024-08-01 13:42:50 +08:00
2024-08-08 08:47:17 +08:00
public void OnNavigatedFrom(NavigationContext navigationContext)
{
// 取消消息订阅
_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
}
private List<ChannelStock> channelStocks;
public List<ChannelStock> ChannelStocks
{
get => channelStocks;
set => SetProperty(ref channelStocks, value);
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
RequestData();
}
private PortUtil _portUtil;
IEventAggregator _eventAggregator;
IDialogService _dialogService;
public AddToJiaoJieWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator, IDialogService DialogService)
{
_portUtil = portUtil;
_eventAggregator = eventAggregator;
_dialogService = DialogService;
}
private void RequestData()
{
ChannelStocks = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Includes<ChannelList>(cs => cs.ChannelLst)
.Includes<DrugInfo>(cs => cs.DrugInfo)
.Where(cs => cs.MachineId == (ConfigurationManager.AppSettings["jj_machineId"] ?? "DM5") && cs.BaseQuantity > cs.Quantity)
.OrderBy(cs => cs.Chnguid)
.OrderBy(cs => cs.DrawerNo)
.ToList();
ChannelStocks.ForEach(cs => cs.AddQuantity = cs.BaseQuantity - cs.Quantity);
}
private int _status = 0;
public int Status { get => _status; set => SetProperty(ref _status, value); }
private bool _isEnable = true;
public bool IsEnable { get => _isEnable; set => SetProperty(ref _isEnable, value); }
private List<int> iDrawerNoLst
{ get; set; }
private int CurrentNum { get; set; }
//刷新
public DelegateCommand QueryCommand
{
get => new DelegateCommand(() => RequestData());
}
//一键补药
public DelegateCommand OpenDragCommand
{
get => new DelegateCommand(() =>
{
try
{
Status = 1;
IsEnable = false;
var varDrawerNO = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Where(cs => cs.MachineId == (ConfigurationManager.AppSettings["machineId"] ??"DM3"))
.GroupBy(cs=>cs.DrawerNo).Select(DrawerNo=>DrawerNo).ToList();
iDrawerNoLst= varDrawerNO.Select(item => item.DrawerNo).ToList();
CurrentNum = 0;
_portUtil.SpeakAsync($"正在打开 {iDrawerNoLst[CurrentNum]} 号抽屉");
_portUtil.WindowName = "AddToJiaoJieWindow";
_portUtil.Operate = true;
//_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1;
//_portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray();
_portUtil.DrawerNo = iDrawerNoLst[CurrentNum];
_portUtil.Start();
}
catch (Exception ex)
{
AlertMsg alertMsg = new AlertMsg
{
Message = $"补药异常{ex.Message}",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
logger.Info($"AddToJiaoJieWindowViewModel异常{ex.Message}");
_portUtil.Operate = false;
}
});
}
void DoMyPrismEvent(DeviceMsg msg)
{
if (msg.WindowName == "AddToJiaoJieWindow")
{
switch (msg.EventType)
{
// 抽屉打开
case EventType.DRAWEROPEN:
if (Status == 1)
{
Status = 2;
}
CurrentNum+=1;
if (CurrentNum< iDrawerNoLst.Count)
{
_portUtil.WindowName = "AddToJiaoJieWindow";
_portUtil.Operate = true;
_portUtil.DrawerNo = iDrawerNoLst[CurrentNum];
_portUtil.Start();
}
break;
// 抽屉关闭
case EventType.DRAWERCLOSE:
if (Status == 2)
{
Status = 3;
}
_portUtil.Operate = false;
IsEnable=false;
CurrentNum = 0;
break;
// 数量变化
case EventType.UPDATEQUANTITY:
if (Status == 2)
{
ChannelStocks.ForEach(it => it.AddQuantity = msg.Quantitys[it.ColNo - 1]);
}
break;
// 打开失败
case EventType.OPENERROR:
AlertMsg alertMsg = new AlertMsg
{
Message = msg.Message,
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Status = 0;
_portUtil.Operate = false;
IsEnable = false;
CurrentNum = 0;
break;
}
}
}
//完成按钮
//public DelegateCommand AddFinish
//{
// get => new DelegateCommand(() =>
// {
// });
//}
//取消
public DelegateCommand CancleAdd
{
get => new DelegateCommand(() =>
{
_portUtil.ResetData();
Status = 0;
IsEnable = true;
CurrentNum = 0;
});
}
private List<ChannelStock> csList = new List<ChannelStock>();
//取药 弹出出药列表
public DelegateCommand TakeDrugCommand
{
get => new DelegateCommand(async () =>
{
csList = ChannelStocks.FindAll(cs => cs.IsSelected == true).GroupBy(cs => cs.DrugId).Select(g => new {
DrugId = g.Key, Quantity = g.Sum(s => s.Quantity)
}).Select(cs=>new ChannelStock() { DrugId=cs.DrugId,Quantity=cs.Quantity}).ToList();
if (csList != null && csList.Count>0)
{
// 此处延时1毫秒等待页面渲染
await Task.Delay(TimeSpan.FromMilliseconds(1));
DialogParameters dialogParameters = new DialogParameters();
dialogParameters.Add("ChannelStocks", csList);
DialogServiceExtensions.ShowDialogHost(_dialogService, "AddToJiaoJieDialog", dialogParameters, DoDialogResult, "RootDialog");
}
});
}
private void DoDialogResult(IDialogResult dialogResult)
{
// 委托 被动执行 被子窗口执行
// dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态
//if(dialogResult.Result == ButtonResult.OK)
//{
RequestData();
//}
//MessageBox.Show("返回值:" + dialogResult.Result.ToString());
}
2024-08-01 13:42:50 +08:00
}
2024-08-08 08:47:17 +08:00
}