HKC/DM_Weight/ViewModels/OpenBoxWindowViewModel.cs

231 lines
8.1 KiB
C#
Raw Normal View History

2024-08-07 09:45:18 +08:00
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using log4net;
2024-08-07 09:45:18 +08:00
using log4net.Repository.Hierarchy;
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;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace DM_Weight.ViewModels
{
public class OpenBoxWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
{
private readonly ILog logger = LogManager.GetLogger(typeof(CheckOrderNewWindowViewModel));
2024-08-07 09:45:18 +08:00
private int _drawerNo = -1;
public int DrawerNo
{
get => _drawerNo;
set => SetProperty(ref _drawerNo, value);
}
private int status = 0;
public int Status { get => status; set => SetProperty(ref status, value); }
//开名下药箱按钮的显示状态
private bool _selfEnable = true;
public bool SelfEnable { get => _selfEnable; set => SetProperty(ref _selfEnable, value); }
//开公共药箱按钮的显示状态
private bool _publicEnable = true;
public bool PublicEnable { get => _publicEnable; set => SetProperty(ref _publicEnable, value); }
//公共药箱状态
private int _publicStatus = 0;
public int PublicStatus { get => _publicStatus;set=>SetProperty(ref _publicStatus, value); }
//名下药箱状态
private int _selfStatus = 0;
public int SelfStatus { get => _publicStatus;set=>SetProperty(ref _publicStatus,value); }
//抽屉号列表
public static List<int> iList = new List<int>();
//第几个抽屉号
//public static int iNumber = 1;
2024-08-07 09:45:18 +08:00
private PortUtil _portUtil;
IEventAggregator _eventAggregator;
public OpenBoxWindowViewModel(PortUtil portUtil,IEventAggregator eventAggregator)
2024-08-07 09:45:18 +08:00
{
_portUtil = portUtil;
_eventAggregator = eventAggregator;
}
public DelegateCommand<string> OpenBoxDelegate
{
get => new DelegateCommand<string>((DrawerNo) =>
{
this.DrawerNo = Convert.ToInt32(DrawerNo);
switch (this.DrawerNo)
{
case 0:
PublicEnable = false;
PublicStatus=1;
break;
case 1:
SelfEnable = false;
SelfStatus = 1;
break;
}
SearchBox();
}
);
}
private void SearchBox()
{
iList = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cl => cl.MachineId == "DM5" && cl.DrawerType == this.DrawerNo)
.WhereIF(this.DrawerNo==0,cl=> cl.BelongUser == HomeWindowViewModel.Operator.UserBarcode)
2024-08-07 09:45:18 +08:00
.Select(cl => cl.DrawerNo).ToList();
if (iList.Count > 0)
{
//_portUtil.SpeakAsync("正在打开药箱");
//_portUtil.DrawerNo = iList[iNumber];
//iNumber++;
//_portUtil.OpenBox();
logger.Info("");
for (int i = 0; i < iList.Count; i++)
{
//记录开药箱日志
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = "DM5",
DrawerNo = iList[i],
Operator = HomeWindowViewModel.Operator?.Id,
OperationTime = DateTime.Now,
Type = 55,
InvoiceId = $"打开{iList[i]}号药箱",
}).ExecuteCommand();
_portUtil.SpeakAsync($"正在打开{iList[i]}号药箱");
ModbusHelper.GetInstance().OpenBoxDoor(iList[i] - 1);
Thread.Sleep(1000);
}
PublicStatus = 0;
SelfStatus = 0;
PublicEnable = true;
SelfEnable = true;
DrawerNo = -1;
2024-08-07 09:45:18 +08:00
}
}
void DoMyPrismEvent(DeviceMsg msg)
{
switch (msg.EventType)
{
// 药箱打开
case EventType.DRAWEROPEN:
//记录开药箱日志
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = "DM5",
//DrawerNo = _portUtil.DrawerNo,
2024-08-07 09:45:18 +08:00
Operator = HomeWindowViewModel.Operator?.Id,
OperationTime = DateTime.Now,
Type = 55,
InvoiceId = "药箱打开",
}).ExecuteCommand();
//if (iNumber < iList.Count)
//{
//_portUtil.DrawerNo = iList[iNumber];
//iNumber++;
2024-08-07 09:45:18 +08:00
if(PublicStatus==1)
{
PublicStatus = 2;
}
else
{
SelfStatus = 2;
}
//_portUtil.OpenBox();
//}
//else
//{
//iNumber = 0;
//_portUtil.GetBoxStatus();
//}
2024-08-07 09:45:18 +08:00
break;
// 药箱关闭
case EventType.DRAWERCLOSE:
//记录药箱操作日志
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = "DM5",
//DrawerNo = _portUtil.DrawerNo,
2024-08-07 09:45:18 +08:00
Operator = HomeWindowViewModel.Operator?.Id,
OperationTime = DateTime.Now,
Type = 55,
InvoiceId = "药箱关闭",
}).ExecuteCommand();
if (PublicStatus == 2)
{
PublicStatus = 3;
}
else
{
SelfStatus = 3;
}
PublicEnable = true;
SelfEnable = true;
DrawerNo = -1;
//_portUtil.Operate = false;
2024-08-07 09:45:18 +08:00
break;
// 打开失败
case EventType.OPENERROR:
AlertMsg alertMsg = new AlertMsg
{
Message = msg.Message,
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
PublicEnable = true;
SelfEnable = true;
DrawerNo = -1;
PublicStatus = 0;
SelfStatus = 0;
//记录药箱操作日志
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = "DM5",
//DrawerNo = _portUtil.DrawerNo,
2024-08-07 09:45:18 +08:00
Operator = HomeWindowViewModel.Operator?.Id,
OperationTime = DateTime.Now,
Type = 55,
InvoiceId = "药箱打开失败",
}).ExecuteCommand();
//_portUtil.Operate = false;
2024-08-07 09:45:18 +08:00
break;
}
}
public bool KeepAlive => false;
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
// 取消消息订阅
//_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
2024-08-07 09:45:18 +08:00
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
//_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
2024-08-07 09:45:18 +08:00
}
}
}