XiangTan_JiaoJie_Bak/DM_Weight/ViewModels/ExchangeWindowViewModel.cs

265 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Common.Logging;
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DM_Weight.ViewModels
{
public class ExchangeWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
{
public bool KeepAlive => false;
private readonly ILog logger = LogManager.GetLogger(typeof(CheckOrderNewWindowViewModel));
private List<ChannelStock> _channelStocks=new List<ChannelStock>();
public List<ChannelStock> _ChannelStocks
{
get => _channelStocks;
set=>SetProperty(ref _channelStocks, value);
}
private ChannelStock _selectedChannelStock=new ChannelStock();
public ChannelStock _ChannelStock
{
get=>_selectedChannelStock;
set=>SetProperty(ref _selectedChannelStock, value);
}
//所选药箱号
private int _drawerNo = -1;
public int DrawerNo
{
get => _drawerNo;
set => SetProperty(ref _drawerNo, value);
}
IDialogService _dialogService;
SocketHelper _socketHelper;
IEventAggregator _eventAggregator;
public ExchangeWindowViewModel(IEventAggregator eventAggregator, IDialogService DialogService, SocketHelper socketHelper)
{
_dialogService = DialogService;
_socketHelper = socketHelper;
_eventAggregator = eventAggregator;
}
public DelegateCommand<string> UpdateDrawerNo
{
get => new DelegateCommand<string>(OpenBoxAction);
}
public async void OpenBoxAction(string strDrawerNo)
{
if (_socketHelper.OpenStatus)
{
_socketHelper.speechSynthesizer.SpeakAsyncCancelAll();
_socketHelper.speechSynthesizer.Resume();
_socketHelper.SpeakAsync("请关闭手术间后再打开");
return;
}
DrawerNo = Convert.ToInt32(strDrawerNo);
MachineRecord machineRecord = new MachineRecord();
machineRecord.MachineId = "DM5";
machineRecord.DrawerNo = DrawerNo+1;
machineRecord.Operator = HomeWindowViewModel.Operator?.Id;
machineRecord.OperationTime = DateTime.Now;
machineRecord.Type = 55;
machineRecord.InvoiceId = $"打开{DrawerNo+1}号手术间";
if (DrawerNo > 0)
{
RequestData();
_socketHelper.speechSynthesizer.SpeakAsyncCancelAll();
_socketHelper.speechSynthesizer.Resume();
_socketHelper.SpeakAsync($"正在打开{DrawerNo+1}号手术间");
logger.Info($"正在打开{DrawerNo+1}号手术间");
//记录开药箱日志
SqlSugarHelper.Db.Insertable(machineRecord).ExecuteCommand();
try
{
_socketHelper.SendMessage(new MyBaseMessage() { lockNo = (short)(DrawerNo) });
_socketHelper.dateTime = DateTime.Now;
}
catch (Exception ex)
{
AlertMsg alertMsg = new AlertMsg
{
Message = $"网口连接异常,正在重试{ex.Message}",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
logger.Info($"网口连接异常,正在重试{ex.Message}");
return;
}
_socketHelper.OpenStatus = true;
//记录药箱打开时间
ChannelList channelList = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cl => cl.MachineId == "DM5" && cl.DrawerNo == (DrawerNo+1)).First();
if (channelList != null && (channelList.EffDate is null || Convert.ToDateTime(channelList.EffDate).ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd")))
{
channelList.EffDate = DateTime.Now.ToString();
SqlSugarHelper.Db.Updateable(channelList).UpdateColumns(it => new { it.EffDate }).ExecuteCommand();
}
Thread.Sleep(200);
int iException = 0;
await new PromiseUtil<int>().taskAsyncLoop(500, 0, async (options, next, stop) =>
{
try
{
if (_socketHelper.OpenStatus)
{
_socketHelper.SendMessage(new MyBaseMessage() { lockNo = 0x33, functionCode = 4, delay = 2 });
if (_socketHelper.OpenStatus)
{
next();
}
else
{
_socketHelper.IsMultiThread = false;
_socketHelper.dateTime = DateTime.Now;
stop();
}
}
else
{
_socketHelper.IsMultiThread = false;
_socketHelper.dateTime = DateTime.Now;
stop();
}
iException = 0;
}
catch (Exception ex)
{
iException++;
if (iException >= 3)
{
_socketHelper.OpenStatus = false;
}
AlertMsg alertMsg = new AlertMsg
{
Message = $"网口连接异常,正在重试{ex.Message}",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
logger.Info($"网口连接异常,正在重试{ex.Message}");
next();
}
});
}
}
private void RequestData()
{
List<ChannelStock> queryData = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Includes(cs => cs.DrugInfo)
.Where(cs => cs.DrawerNo == (DrawerNo + 1))
.Where(cs => cs.DrugId != null)
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5"))
.OrderBy(cs => cs.DrugId)
.ToList();
_ChannelStocks = queryData.Select(it => { it.CheckQuantity = it.Quantity; return it; }).ToList();
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
}
public DelegateCommand ExchangeCommand
{
get =>new DelegateCommand(Exchange);
}
//替换
private async void Exchange()
{
if (_ChannelStock == null)
{
logger.Error("请选择要交换的药品");
return;
}
if (_ChannelStock.DrugInfo == null)
{
logger.Error("请选<E8AFB7><E98089>要交换的药品");
return;
}
// 此处延时1毫秒等待页面渲染
await Task.Delay(TimeSpan.FromMilliseconds(1));
//选中药箱号下的所有药品id
DialogParameters dialogParameters = new DialogParameters();
dialogParameters.Add("csStock", _ChannelStock);
DialogServiceExtensions.ShowDialogHost(_dialogService, "ExchangeDialog", dialogParameters, DoDialogResult, "RootDialog");
}
//移出
public DelegateCommand RemoveCommand
{
get => new DelegateCommand(Remove);
}
private async void Remove()
{
if (_ChannelStock != null)
{
// 此处延时1毫秒等待页面渲染
await Task.Delay(TimeSpan.FromMilliseconds(1));
//选中药箱号下的所有药品id
DialogParameters dialogParameters = new DialogParameters();
dialogParameters.Add("csStock", _ChannelStock);
DialogServiceExtensions.ShowDialogHost(_dialogService, "RemoveDialog", dialogParameters, DoDialogResult, "RootDialog");
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "请选择要移出的药品!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
return;
}
}
private void DoDialogResult(IDialogResult dialogResult)
{
RequestData();
}
private DelegateCommand _rowSelected;
public DelegateCommand RowSelected => _rowSelected ??= new DelegateCommand(OpenOrderDialog);
public async void OpenOrderDialog()
{
//if (_ChannelStock != null)
//{
// // 此处延时1毫秒等待页面渲染
// await Task.Delay(TimeSpan.FromMilliseconds(1));
// //选中药箱号下的所有药品id
// DialogParameters dialogParameters = new DialogParameters();
// dialogParameters.Add("_ChannelStock", _ChannelList.channelStocks);
// DialogServiceExtensions.ShowDialogHost(_dialogService, "AddToJiaoJieDialog", dialogParameters, DoDialogResult, "RootDialog");
//}
}
}
}