XiChang/DM_Weight/ViewModels/DrawerAddDrugWindowViewMode...

685 lines
31 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 log4net;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using DM_Weight.Common;
using System.Threading;
namespace DM_Weight.ViewModels
{
public class DrawerAddDrugWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
{
private readonly ILog logger = LogManager.GetLogger(typeof(DrawerAddDrugWindowViewModel));
private List<int> _drawerNoList = new List<int>();
private List<ChannelStock>? _channelStocks;
public List<ChannelStock>? ChannelStocks
{
get => _channelStocks;
set => SetProperty(ref _channelStocks, value);
}
private static readonly DateTime Jan1st1970 = new DateTime
(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private bool _openBoxVisibility = false;
public bool OpenBoxVisibility
{
get => _openBoxVisibility;
set => SetProperty(ref _openBoxVisibility, value);
}
private PortUtil _portUtil;
IEventAggregator _eventAggregator;
public DrawerAddDrugWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
{
_portUtil = portUtil;
_eventAggregator = eventAggregator;
}
void DoMyPrismEvent(DeviceMsg msg)
{
if (msg.WindowName == "DrawerAddDrugWindow")
{
switch (msg.EventType)
{
// 抽屉打开
case EventType.DRAWEROPEN:
if (Status == 1)
{
Status = 2;
}
//是冰箱抽屉则开冰箱抽屉时发送延迟报警指令
CheckIsFridgeOpen();
break;
// 抽屉关闭
case EventType.DRAWERCLOSE:
if (Status == 2)
{
Status = 3;
}
//是冰箱抽屉则开冰箱抽屉时发送延迟报警指令
CheckIsFridgeClose();
//抽屉关闭查询称重稳定数据
CompleteIsEnable = true;
break;
// 数量变化
case EventType.UPDATEQUANTITY:
if (Status == 2 || Status == 3)
{
ChannelStocks.Where(cs => _portUtil.ColNoLst.Contains(cs.ColNo)).ToList().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;
break;
}
}
}
private int _status;
public int Status { get => _status; set => SetProperty(ref _status, value); }
private int _drawerNo = 1;
public int DrawerNo
{
get => _drawerNo;
set => SetProperty(ref _drawerNo, value);
}
private bool _is8Drawer = true;
public bool Is8Drawer { get => _is8Drawer; set => SetProperty(ref _is8Drawer, value); }
private bool _is16Drawer = false;
public bool Is16Drawer { get => _is16Drawer; set => SetProperty(ref _is16Drawer, value); }
private bool _is17Drawer = false;
public bool Is17Drawer { get => _is17Drawer; set => SetProperty(ref _is17Drawer, value); }
public DelegateCommand<string> UpdateDrawerNo
{
get => new DelegateCommand<string>((DrawerNo) =>
{
this.DrawerNo = Convert.ToInt32(DrawerNo);
RequestData();
}, (DrawerNo) => Status == 0
);
}
/// <summary>
/// 完成按钮是否可用
/// </summary>
private bool _completeIsEnable = false;
public bool CompleteIsEnable
{
get => _completeIsEnable;
set
{
SetProperty(ref _completeIsEnable, value);
}
}
/// <summary>
/// 取消按钮是否可用
/// </summary>
private bool _cancleIsEnable = true;
public bool CancleIsEnable
{
get => _cancleIsEnable;
set
{
SetProperty(ref _cancleIsEnable, value);
}
}
int currentCol = 0;
private int[] BeforeQuantity { get; set; } = new int[9];
private int[] AfterQuantity { get; set; } = new int[9];
public DelegateCommand OpenDrawer
{
get => new DelegateCommand(async () =>
{
if (Status > 0 || _portUtil.Operate)
{
return;
}
Status = 1;
_portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉");
if (ChannelStocks != null && ChannelStocks.Count > 0 && (ChannelStocks[0].BoardType == (Int32)BoardTypeEnum.weighSmartBox))
{
try
{
_portUtil.DrawerNo = DrawerNo;
_portUtil.Operate = true;
_portUtil.WindowName = "DrawerAddDrugWindow";
byte[] buffer = await _portUtil.OpenDrawer();
CancleIsEnable = false;
if (buffer == null)
{
logger.Info("抽屉打开返回空");
Status = 0;
return;
}
int[] r = buffer.Select(it => Convert.ToInt32(it)).ToArray();
logger.Info($"OpenDrawer{string.Join(",", r)}");
if (_portUtil.DrawerState(r))
{
_portUtil.ColNoLst.Clear();
new PromiseUtil<int>().taskAsyncLoop(200, 0, async (options, next, stop) =>
{
logger.Info("循环查抽屉状态药盒数量");
if (Status == 3 || Status == 0)
{
stop();
}
else
{
try
{
// 查询抽屉状态
byte[] buffer = await _portUtil.CheckDrawerStatus();
int[] r = buffer.Select(it => Convert.ToInt32(it)).ToArray();
logger.Info($"抽屉状态返回{Convert.ToHexString(buffer)}");
if (_portUtil.DrawerState(r))
{
Status = 2;
if (_portUtil.ColNoLst != null && _portUtil.ColNoLst.Count > 0 || currentCol > 0)
{
//库位列表不为空查数
if (currentCol > 0)
{
if (!_portUtil.ColNoLst.Contains(currentCol)) //已经开过一次药盒则不再查开药合前的数量
{
CancleIsEnable = false;
//开药盒锁前先查数
int beforeQuantity = await _portUtil.CheckQuantityForSingle(currentCol);
Thread.Sleep(200);
BeforeQuantity[currentCol - 1] = beforeQuantity;
logger.Info($"BeforeQuantity:{currentCol}-{beforeQuantity}数量{string.Join(",", BeforeQuantity)}");
_portUtil.ColNoLst.Add(currentCol);
}
await _portUtil.OpenBoxByColNo(currentCol);
Thread.Sleep(200);
currentCol = 0;
}
// 继续查询抽屉药品数量
// 查询抽屉药品数量
int[] quantity = await _portUtil.CheckQuantityByAddrForMulti();
AfterQuantity = quantity;
logger.Info($"AfterQuantity{string.Join(",", AfterQuantity)}");
int[] Quantitys = new int[BeforeQuantity.Length];
for (int i = 0; i < BeforeQuantity.Length; i++)
{
Quantitys[i] = AfterQuantity[i] - BeforeQuantity[i];
}
// 告诉前台数据变化
_eventAggregator.GetEvent<PortUtilEvent>().Publish(new util.DeviceMsg()
{
EventType = util.EventType.UPDATEQUANTITY,
WindowName = "DrawerAddDrugWindow",
Quantitys = Quantitys
});
logger.Info($"数量变化【{string.Join(",", Quantitys)}】");
}
next();
}
else
{
stop();
//关闭抽屉
logger.Info($"抽屉【{DrawerNo}】已关闭");
// 重新初始化数据
//_portUtil.ResetData();
if (_portUtil.ColNoLst != null && _portUtil.ColNoLst.Count > 0)
{
Thread.Sleep(200);
//关闭抽屉后获取称重稳定数量
GetWeightQuantity();
//_portUtil.ColNoLst.Clear();
Status = 3;
//CancleIsEnable = true;
}
else
{
Status = 0;
_portUtil.ResetData();
}
CompleteIsEnable = false;
}
}
catch (Exception ex)
{
logger.Info($"异常{ex.Message}");
//AlertMsg alertMsg = new AlertMsg
//{
// Message = $"异常{ex.Message}",
// Type = MsgType.ERROR,
//};
//_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
next();
}
}
});
}
else
{
Status = 0;
AlertMsg alertMsg = new AlertMsg
{
Message = "抽屉打开失败",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
catch (Exception ex)
{
logger.Info($"抽屉操作异常:{ex.Message}");
AlertMsg alertMsg = new AlertMsg
{
Message = $"抽屉操作异常{ex.Message}",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
else
{
List<ChannelStock> singleChannels = ChannelStocks.FindAll(it => it.BoardType != (Int32)BoardTypeEnum.separation);
_portUtil.WindowName = "DrawerAddDrugWindow";
_portUtil.Operate = true;
_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : (Int32)BoardTypeEnum.separation;
_portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray();
_portUtil.DrawerNo = DrawerNo;
_portUtil.Start();
}
}, () => Status == 0).ObservesProperty(() => Status);
}
//开药盒
public DelegateCommand<object> OpenBoxCommand
{
get => new DelegateCommand<object>((param) =>
{
if (Status == 2)
{
ChannelStock cs = param as ChannelStock;
currentCol = cs.ColNo;
logger.Info($"开药盒{cs.ColNo}");
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "请先开抽屉后再打开药盒",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
logger.Info($"请先开抽屉后再打开药盒");
}
});
}
private bool _isFinishClick = false;
// 完成按钮
public DelegateCommand TakeFinish
{
get => new DelegateCommand(() =>
{
if (!_isFinishClick)
{
_isFinishClick = true;
List<ChannelStock> record = ChannelStocks.FindAll(it => it.AddQuantity != 0).ToList();
if (record.Count > 0)
{
string InvoiceId = "DRAWER_" + CurrentTimeMillis();
var f = SqlSugarHelper.Db.UseTran(() =>
{
for (int i = 0; i < record.Count; i++)
{
ChannelStock it = record[i];
it.ManuNo = it.drugManuNo == null ? it.ManuNo : it.drugManuNo.ManuNo;
it.EffDate = it.drugManuNo == null ? it.EffDate : it.drugManuNo.EffDate;
// 更新数据 库存信息
SqlSugarHelper.Db.Updateable(new ChannelStock()
{
Quantity = it.Quantity + it.AddQuantity,
ManuNo = it.ManuNo,
EffDate = it.EffDate,
Id = it.Id,
}).UpdateColumns(it => new { it.Quantity, it.ManuNo, it.EffDate }).ExecuteCommand();
// 获取更新完库存后的药品库存
List<ChannelStock> nowChannels = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Where(cs => cs.MachineId.Equals(it.MachineId))
.Where(cs => cs.DrugId.Equals(it.DrugId))
.Where(cs => cs.DrawerType == (Int32)DrawerTypeEnum.drawerTypeOne)
.ToList();
// 保存数据 入库记录
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = it.MachineId,
DrawerNo = it.DrawerNo,
ColNo = it.ColNo,
DrugId = it.DrugId,
ManuNo = it.ManuNo,
EffDate = !String.IsNullOrEmpty(it.EffDate) ? DateTime.ParseExact(it.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
Operator = HomeWindowViewModel.Operator?.Id,
Reviewer = HomeWindowViewModel.Reviewer?.Id,
OperationTime = DateTime.Now,
Quantity = it.AddQuantity,
Type = 1,
InvoiceId = InvoiceId,
StockQuantity = nowChannels.Sum(it => it.Quantity)
}).ExecuteCommand();
//称重计数或称重+智能显示+管控药盒 类型需要 发26指令
//if (it.BoardType == (Int32)BoardTypeEnum.weigh || it.BoardType == (Int32)BoardTypeEnum.weighSmartBox)
//{
// //计数数量设置发送称重26指令
// _portUtil.SetNumCount(it.DrawerNo, it.ColNo, it.AddQuantity);
// Thread.Sleep(80);
//}
}
return true;
});
if (f.Data)
{
// 更新屏显库存
List<ChannelStock> singleChannels = record.FindAll(it => it.BoardType != 1 && (it.BoardType == (Int32)BoardTypeEnum.smart || it.BoardType == (Int32)BoardTypeEnum.weighSmartBox));
//if ((singleChannels.Count > 0 ? singleChannels[0].BoardType : 1) == (Int32)BoardTypeEnum.smart || (singleChannels.Count > 0 ? singleChannels[0].BoardType : 1) == (Int32)BoardTypeEnum.weighSmartBox)
if (singleChannels.Count > 0)
{
singleChannels.ForEach(it =>
{
_portUtil.WriteQuantityMethod(it.Quantity + it.AddQuantity, it.DrawerNo, it.ColNo);
Thread.Sleep(200);
});
}
RequestData();
AlertMsg alertMsg = new AlertMsg
{
Message = "抽屉加药完成,库存已更新",
Type = MsgType.SUCCESS,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "更新库存失败",
Type = MsgType.SUCCESS,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
Status = 0;
_isFinishClick = false;
_portUtil.ColNoLst = new List<int>();
}
else
{
_isFinishClick = false;
AlertMsg alertMsg = new AlertMsg
{
Message = "没有填写加药数量",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
});
}
// 取消按钮
public DelegateCommand CancleTake
{
get => new DelegateCommand(() =>
{
_portUtil.ResetData();
Status = 0;
});
}
//检查是否是冰箱抽屉(冰箱抽屉打开时需要发送冰箱延迟报警的指令)
public async Task CheckIsFridgeOpen()
{
if (ChannelStocks != null && ChannelStocks.Count > 0)
{
if (ChannelStocks[0].BoardType == (Int32)BoardTypeEnum.fridge)
{
_portUtil.FridgeOperate = true;
//发送冰箱延迟报警的指令
await _portUtil.FridgeDelayWarm();
_portUtil.FridgeOperate = false;
}
}
}
//检查是否是冰箱抽屉(冰箱抽屉关闭时需要查询冰箱温度如温度不在范围则发送冰箱延迟报警的指令)
public async Task CheckIsFridgeClose()
{
if (ChannelStocks != null && ChannelStocks.Count > 0)
{
if (ChannelStocks[0].BoardType == (Int32)BoardTypeEnum.fridge)
{
string[] iTempertureRange = ConfigurationManager.AppSettings["temperatureRange"].Split('-');
//发送查询冰箱温度的指令
float temperature = await _portUtil.GetFridgeTemperature();
if (temperature > Convert.ToSingle(iTempertureRange[0]) && temperature < Convert.ToSingle(iTempertureRange[1]))
{
_portUtil.FridgeOperate = true;
//发送冰箱延迟报警的指令
await _portUtil.FridgeDelayWarm();
_portUtil.FridgeOperate = false;
}
}
}
}
public long CurrentTimeMillis()
{
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
public bool KeepAlive => false;
public void FindDrawerCount()
{
int count = 0;
if (ConfigurationManager.AppSettings["MultiBatch"].ToString().Equals("1"))
{
count = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cs => cs.DrawerType != (Int32)DrawerTypeEnum.recyle)
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).GroupBy(cs => cs.DrawerNo).Select(cs => SqlFunc.AggregateCount(cs.DrawerNo)).Count();
}
else
{
count = SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.DrawerType != (Int32)DrawerTypeEnum.recyle)
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).GroupBy(cs => cs.DrawerNo).Select(cs => SqlFunc.AggregateCount(cs.DrawerNo)).Count();
}
Is8Drawer = count < 9;
Is16Drawer = count >= 16;
Is17Drawer = count > 16;
}
//这个方法用于拦截请求,continuationCallback(true)就是不拦截continuationCallback(false)拦截本次操作
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
continuationCallback(true);
}
public void RequestData()
{
List<ChannelStock> queryData = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Includes(cs => cs.DrugInfo, di => di.DrugManuNos)
.Where(cs => cs.DrawerNo == DrawerNo)
.Where(cs => cs.DrawerType == (Int32)DrawerTypeEnum.drawerTypeOne)
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
.Where(cs => cs.DrugId != null)
.OrderBy(cs => cs.ColNo)
.ToList();
if (queryData != null && queryData.Count > 0)
{
OpenBoxVisibility = (queryData[0].BoardType == (Int32)BoardTypeEnum.box || queryData[0].BoardType == (Int32)BoardTypeEnum.weighBox || queryData[0].BoardType == (Int32)BoardTypeEnum.weighSmartBox) ? true : false;
}
ChannelStocks = queryData;
}
//接收导航传过来的参数 现在是在此处初始化了表格数据
public void OnNavigatedTo(NavigationContext navigationContext)
{
_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
FindDrawerCount();
RequestData();
}
//每次导航的时候该实列用不用重新创建true是不重新创建,false是重新创建
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
//这个方法用于拦截请求
public void OnNavigatedFrom(NavigationContext navigationContext)
{
// 取消消息订阅
_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
}
//关闭抽屉后获取称重稳定数量
public void GetWeightQuantity()
{
List<int[]> finalyQuantity = new List<int[]>();
new PromiseUtil<int>().taskAsyncLoop(200, 0, async (options, next, stop) =>
{
if (!_portUtil.Operate|| Status==0)
{
stop();
}
else
{
try
{
logger.Info("关闭抽屉后获取称重稳定数量");
if (finalyQuantity.Count >= 10)
{
for (int i = 0; i < finalyQuantity.Count; i++)
{
logger.Info($"finalyQuantity{i} {string.Join(",", finalyQuantity[i])}");
}
if (AreAllArraysEqual(finalyQuantity))
{
stop();
// 告诉前台数据变化
_eventAggregator.GetEvent<PortUtilEvent>().Publish(new util.DeviceMsg()
{
EventType = util.EventType.UPDATEQUANTITY,
WindowName = "DrawerTakeDrugWindow",
Quantitys = finalyQuantity[0]
});
logger.Info($"数量变化【{string.Join(",", finalyQuantity[0])}】");
// 重新初始化数据
_portUtil.ResetData();
CompleteIsEnable = true;
CancleIsEnable = true;
}
else
{
finalyQuantity.RemoveAt(0);
CancleIsEnable = true;
next();
}
}
else
{
if(finalyQuantity.Count>=9)
{
CancleIsEnable = true;
}
else
{
CancleIsEnable = false;
}
CompleteIsEnable = false;
_portUtil.DrawerNo = DrawerNo;
_portUtil.Operate = true;
int[] quantity = await _portUtil.CheckQuantityByAddrForMulti();
AfterQuantity = quantity;
int[] Quantitys = new int[BeforeQuantity.Length];
for (int i = 0; i < BeforeQuantity.Length; i++)
{
Quantitys[i] = AfterQuantity[i] - BeforeQuantity[i];
}
finalyQuantity.Add(Quantitys);
logger.Info($"Quantity{string.Join(",", Quantitys)}");
// 告诉前台数据变化
_eventAggregator.GetEvent<PortUtilEvent>().Publish(new util.DeviceMsg()
{
EventType = util.EventType.UPDATEQUANTITY,
WindowName = "DrawerAddDrugWindow",
Quantitys = finalyQuantity[0]
});
next();
}
}
catch (Exception ex)
{
logger.Info($"关抽屉后获取称重稳定数量异常{ex.Message}");
next();
}
}
});
}
public bool AreAllArraysEqual(List<int[]> arrays)
{
if (arrays == null || arrays.Count == 0) return false;
// 取第一个数组作为参照进行比较
var referenceArray = arrays[0];
// 检查列表中除了第一个数组外的所有数组是否与第一个数组相等
return arrays.Skip(1).All(array => array.SequenceEqual(referenceArray));
}
}
}