XiangTan_JiaoJie_Bak/DM_Weight/ViewModels/OpenBoxWindowViewModel.cs

265 lines
9.6 KiB
C#

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 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));
private int _drawerType = -1;
public int DrawerType
{
get => _drawerType;
set => SetProperty(ref _drawerType, 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 string _selfContent;
public string SelfContent { get => _selfContent; set => SetProperty(ref _selfContent, value); }
//开公共药箱按钮的显示状态
private bool _publicEnable = true;
public bool PublicEnable { get => _publicEnable; set => SetProperty(ref _publicEnable, value); }
//公共药箱按钮显示内容
private string _publicContent;
public string PublicContent { get => _publicContent; set => SetProperty(ref _publicContent, 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<ChannelList> iList = new List<ChannelList>();
//第几个抽屉号
//public static int iNumber = 1;
private PortUtil _portUtil;
IEventAggregator _eventAggregator;
public OpenBoxWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
{
_portUtil = portUtil;
_eventAggregator = eventAggregator;
}
public DelegateCommand<string> OpenBoxDelegate
{
get => new DelegateCommand<string>((DrawerType) =>
{
this.DrawerType = Convert.ToInt32(DrawerType);
switch (this.DrawerType)
{
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.DrawerType)
.WhereIF(this.DrawerType == 0, cl => cl.BelongUser == HomeWindowViewModel.Operator.UserBarcode).ToList();
// .Select(cl => cl.DrawerNo).ToList();
if (iList.Count > 0)
{
//_portUtil.SpeakAsync("正在打开药箱");
//_portUtil.DrawerNo = iList[iNumber];
//iNumber++;
//_portUtil.OpenBox();
for (int i = 0; i < iList.Count; i++)
{
ChannelList channelList = iList[i];
//记录开药箱日志
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = "DM5",
DrawerNo = channelList.DrawerNo,
Operator = HomeWindowViewModel.Operator?.Id,
OperationTime = DateTime.Now,
Type = 55,
InvoiceId = $"打开{iList[i]}号药箱",
}).ExecuteCommand();
//记录药箱打开时间
channelList.EffDate = DateTime.Now.ToString("yyyy-MM-dd");
channelList.DrawerState = 0;
SqlSugarHelper.Db.Updateable(channelList).UpdateColumns(it => new { it.EffDate, it.DrawerState }).ExecuteCommand();
_portUtil.SpeakAsync($"正在打开{channelList.DrawerNo}号药箱");
logger.Info($"正在打开{channelList.DrawerNo}号药箱");
ModbusHelper.GetInstance().OpenBoxDoor(channelList.DrawerNo - 1);
Thread.Sleep(1000);
}
PublicStatus = 0;
SelfStatus = 0;
PublicEnable = true;
SelfEnable = true;
DrawerType = -1;
}
}
void DoMyPrismEvent(DeviceMsg msg)
{
switch (msg.EventType)
{
// 药箱打开
case EventType.DRAWEROPEN:
//记录开药箱日志
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = "DM5",
//DrawerNo = _portUtil.DrawerNo,
Operator = HomeWindowViewModel.Operator?.Id,
OperationTime = DateTime.Now,
Type = 55,
InvoiceId = "药箱打开",
}).ExecuteCommand();
//if (iNumber < iList.Count)
//{
//_portUtil.DrawerNo = iList[iNumber];
//iNumber++;
if (PublicStatus == 1)
{
PublicStatus = 2;
}
else
{
SelfStatus = 2;
}
//_portUtil.OpenBox();
//}
//else
//{
//iNumber = 0;
//_portUtil.GetBoxStatus();
//}
break;
// 药箱关闭
case EventType.DRAWERCLOSE:
//记录药箱操作日志
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = "DM5",
//DrawerNo = _portUtil.DrawerNo,
Operator = HomeWindowViewModel.Operator?.Id,
OperationTime = DateTime.Now,
Type = 55,
InvoiceId = "药箱关闭",
}).ExecuteCommand();
if (PublicStatus == 2)
{
PublicStatus = 3;
}
else
{
SelfStatus = 3;
}
PublicEnable = true;
SelfEnable = true;
DrawerType = -1;
//_portUtil.Operate = false;
break;
// 打开失败
case EventType.OPENERROR:
AlertMsg alertMsg = new AlertMsg
{
Message = msg.Message,
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
PublicEnable = true;
SelfEnable = true;
DrawerType = -1;
PublicStatus = 0;
SelfStatus = 0;
//记录药箱操作日志
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = "DM5",
//DrawerNo = _portUtil.DrawerNo,
Operator = HomeWindowViewModel.Operator?.Id,
OperationTime = DateTime.Now,
Type = 55,
InvoiceId = "药箱打开失败",
}).ExecuteCommand();
//_portUtil.Operate = false;
break;
}
}
public bool KeepAlive => false;
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
// 取消消息订阅
//_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
CheckDrawerState();
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
//_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
}
//查询名下药箱与公共药箱状态
private void CheckDrawerState()
{
PublicContent = "开公共药箱";
int listSelfState = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cl => cl.MachineId == "DM5")
.Where(cl => cl.BelongUser == HomeWindowViewModel.Operator.UserBarcode).Select(cl => cl.DrawerState).First();
if (listSelfState > 0)
{
SelfContent = "还名下药箱";
}
else
{
SelfContent = "取名下药箱";
}
}
}
}