399 lines
15 KiB
C#
399 lines
15 KiB
C#
using log4net;
|
||
using log4net.Repository.Hierarchy;
|
||
using Newtonsoft.Json.Linq;
|
||
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.Linq;
|
||
using System.Speech.Synthesis;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using DM_Weight.Models;
|
||
using DM_Weight.msg;
|
||
using DM_Weight.Port;
|
||
using DM_Weight.util;
|
||
using DM_Weight.Views;
|
||
using DM_Weight.Common;
|
||
using System.Threading;
|
||
|
||
namespace DM_Weight.ViewModels
|
||
{
|
||
public class ReturnEmptyDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime
|
||
{
|
||
|
||
private readonly ILog logger = LogManager.GetLogger(typeof(ReturnEmptyDialogViewModel));
|
||
public string Title => "归还空瓶(记录)";
|
||
|
||
public event Action<IDialogResult> RequestClose;
|
||
|
||
private static readonly DateTime Jan1st1970 = new DateTime
|
||
(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||
|
||
private string WindowName = "ReturnEmpty1Window";
|
||
|
||
private PortUtil _portUtil;
|
||
IEventAggregator _eventAggregator;
|
||
|
||
|
||
public ReturnEmptyDialogViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
|
||
{
|
||
_portUtil = portUtil;
|
||
_eventAggregator = eventAggregator;
|
||
}
|
||
|
||
void DoMyPrismEvent(DeviceMsg msg)
|
||
{
|
||
|
||
if (msg.WindowName.Equals(WindowName))
|
||
{
|
||
|
||
switch (msg.EventType)
|
||
{
|
||
// 抽屉打开
|
||
case EventType.DRAWEROPEN:
|
||
if (Status == 1)
|
||
{
|
||
Status = 2;
|
||
}
|
||
//是冰箱抽屉则开冰箱抽屉时发送延迟报警指令
|
||
CheckIsFridgeOpen();
|
||
break;
|
||
// 抽屉关闭
|
||
case EventType.DRAWERCLOSE:
|
||
if (Status == 2)
|
||
{
|
||
Status = 3;
|
||
if(ChannelStock.DrawerType == (Int32)DrawerTypeEnum.recyle)
|
||
{
|
||
ReturnQuantity = msg.Quantitys[0];
|
||
logger.Info($"抽屉【{ChannelStock.DrawerNo}】回收库位药品数量增加【{msg.Quantitys[0]}】");
|
||
}
|
||
}
|
||
//是冰箱抽屉则开冰箱抽屉时发送延迟报警指令
|
||
CheckIsFridgeClose();
|
||
break;
|
||
// 数量变化
|
||
case EventType.UPDATEQUANTITY:
|
||
if (Status == 2)
|
||
{
|
||
ReturnQuantity = msg.Quantitys[0];
|
||
logger.Info($"抽屉【{ChannelStock.DrawerNo}】回收库位药品数量增加【{msg.Quantitys[0]}】");
|
||
}
|
||
break;
|
||
// 打开失败
|
||
case EventType.OPENERROR:
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = msg.Message,
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
Status = 0;
|
||
break;
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
private int _returnQuantity;
|
||
|
||
public int ReturnQuantity
|
||
{
|
||
get => _returnQuantity;
|
||
set
|
||
{
|
||
if (value < 0)
|
||
{
|
||
throw new ArgumentException("还药数量不能是负数");
|
||
}
|
||
//if (value > MachineRecord.CanReturnQuantity)
|
||
//{
|
||
// throw new ArgumentException("还药数量超出");
|
||
//}
|
||
SetProperty(ref _returnQuantity, value);
|
||
}
|
||
}
|
||
|
||
private int _status = 0;
|
||
|
||
public int Status
|
||
{
|
||
get => _status; set => SetProperty(ref _status, value);
|
||
}
|
||
|
||
|
||
private List<MachineRecord> _machineRecords = new();
|
||
|
||
public List<MachineRecord> MachineRecords
|
||
{
|
||
get => _machineRecords;
|
||
set
|
||
{
|
||
SetProperty(ref _machineRecords, value);
|
||
}
|
||
}
|
||
|
||
private ChannelStock _channelStock;
|
||
|
||
public ChannelStock ChannelStock
|
||
{
|
||
get => _channelStock;
|
||
set => SetProperty(ref _channelStock, value);
|
||
}
|
||
|
||
|
||
|
||
public bool CanCloseDialog()
|
||
{
|
||
return Status == 0;
|
||
}
|
||
|
||
public void OnDialogClosed()
|
||
{
|
||
// 取消消息订阅
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
|
||
}
|
||
|
||
public void OnDialogOpened(IDialogParameters parameters)
|
||
{
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
|
||
|
||
ChannelStock _record = parameters.GetValue<ChannelStock>("channel");
|
||
ChannelStock = _record;
|
||
|
||
RequestData();
|
||
}
|
||
|
||
public void RequestData()
|
||
{
|
||
List<MachineRecord> queryData = SqlSugarHelper.Db.Queryable<MachineRecord>()
|
||
.Includes<UserList>(mr => mr.User)
|
||
.Where(mr => mr.DrugId == ChannelStock.DrugId)
|
||
.Where(mr => mr.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
|
||
.Where(mr => mr.Type == 2)
|
||
.Where(mr => mr.Status != 2)
|
||
.OrderBy(mr => mr.OperationTime)
|
||
.OrderBy(mr => mr.Id)
|
||
.ToList();
|
||
MachineRecords = queryData;
|
||
}
|
||
|
||
public DelegateCommand OpenDrawer
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
if (ChannelStock != null)
|
||
{
|
||
if (Status == 0)
|
||
{
|
||
if (ChannelStock.DrawerType == (Int32)DrawerTypeEnum.recyle && ChannelStock.BoardType ==(Int32)BoardTypeEnum.separation)
|
||
{
|
||
Status = 3 ;
|
||
}
|
||
else
|
||
{
|
||
Status = 1;
|
||
_portUtil.SpeakAsync("正在打开" + ChannelStock.DrawerNo + "号抽屉");
|
||
_portUtil.WindowName = WindowName;
|
||
_portUtil.ColNos = new int[] { ChannelStock.ColNo };
|
||
_portUtil.DrawerNo = ChannelStock.DrawerNo;
|
||
_portUtil.DrawerType = ChannelStock.DrawerType;
|
||
_portUtil.BoardType = ChannelStock.BoardType;
|
||
_portUtil.Start();
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "请选择退还药品要放入的库位!",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
private bool _isFinishClick = false;
|
||
|
||
public bool IsFinishClick { get => _isFinishClick; set => SetProperty(ref _isFinishClick, value); }
|
||
// 完成按钮
|
||
public DelegateCommand TakeFinish
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
IsFinishClick = true;
|
||
List<MachineRecord> records = MachineRecords.FindAll(it => it.IsSelected).ToList();
|
||
if (records.Count > 0 && records.Sum(it => it.Quantity - it.ReturnQuantity1) == ReturnQuantity)
|
||
{
|
||
string InvoiceId = "RETURN_" + CurrentTimeMillis();
|
||
var f = SqlSugarHelper.Db.UseTran(() =>
|
||
{
|
||
|
||
// 更新数据 库存信息
|
||
SqlSugarHelper.Db.Updateable(new ChannelStock()
|
||
{
|
||
Quantity = ChannelStock.Quantity + ReturnQuantity,
|
||
Id = ChannelStock.Id,
|
||
}).UpdateColumns(it => new { it.Quantity }).ExecuteCommand();
|
||
|
||
// 获取更新完库存后的药品库存
|
||
List<ChannelStock> nowChannels = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
.Where(cs => cs.MachineId.Equals(ChannelStock.MachineId))
|
||
.Where(cs => cs.DrugId.Equals(ChannelStock.DrugId))
|
||
.Where(cs => cs.DrawerType == (Int32)DrawerTypeEnum.drawerTypeOne)
|
||
.ToList();
|
||
|
||
for (int i = 0; i < records.Count; i++)
|
||
{
|
||
MachineRecord _MachineRecord = records[i];
|
||
// 更新数据 取药记录 设置还药数量、状态
|
||
SqlSugarHelper.Db.Updateable(new MachineRecord()
|
||
{
|
||
ReturnQuantity2 = _MachineRecord.Quantity - _MachineRecord.ReturnQuantity1,
|
||
Id = _MachineRecord.Id,
|
||
Status = 2,
|
||
}).UpdateColumns(it => new { it.ReturnQuantity1, it.Status }).ExecuteCommand();
|
||
|
||
// 保存数据 还药空瓶记录
|
||
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
||
{
|
||
MachineId = ChannelStock.MachineId,
|
||
DrawerNo = ChannelStock.DrawerNo,
|
||
ColNo = ChannelStock.ColNo,
|
||
DrugId = ChannelStock.DrugId,
|
||
ManuNo = ChannelStock.ManuNo,
|
||
EffDate = !String.IsNullOrEmpty(ChannelStock.EffDate) ? DateTime.ParseExact(ChannelStock.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
|
||
Operator = HomeWindowViewModel.Operator?.Id,
|
||
Reviewer = HomeWindowViewModel.Reviewer?.Id,
|
||
OperationTime = DateTime.Now,
|
||
Quantity = ReturnQuantity,
|
||
Type = 32,
|
||
InvoiceId = InvoiceId,
|
||
GetId = _MachineRecord.Id,
|
||
StockQuantity = nowChannels.Sum(it => it.Quantity)
|
||
}).ExecuteCommand();
|
||
//称重计数或称重+智能显示+管控药盒 类型需要 发26指令
|
||
if (ChannelStock.BoardType == (Int32)BoardTypeEnum.weigh || ChannelStock.BoardType == (Int32)BoardTypeEnum.weighSmartBox)
|
||
{
|
||
//计数数量设置,发送称重26指令
|
||
_portUtil.SetNumCount(ChannelStock.DrawerNo, ChannelStock.ColNo, ChannelStock.AddQuantity);
|
||
Thread.Sleep(80);
|
||
}
|
||
}
|
||
|
||
return true;
|
||
});
|
||
if (f.Data)
|
||
{
|
||
// 更新屏显库存
|
||
if (ChannelStock.BoardType == (Int32)BoardTypeEnum.smart|| ChannelStock.BoardType == (Int32)BoardTypeEnum.weighSmartBox)
|
||
{
|
||
_portUtil.WriteQuantity(ChannelStock.DrawerNo, ChannelStock.ColNo, ChannelStock.Quantity + ReturnQuantity);
|
||
}
|
||
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "操作完成,库存已更新",
|
||
Type = MsgType.SUCCESS,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
}
|
||
if (!f.IsSuccess)
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "库存更新失败!",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
}
|
||
Status = 0;
|
||
IsFinishClick = false;
|
||
RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
|
||
} else
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "选择的归还数目与实际数目不符",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
IsFinishClick = false;
|
||
}
|
||
}, () => !IsFinishClick && ReturnQuantity > 0).ObservesProperty(() => IsFinishClick).ObservesProperty(() => ReturnQuantity);
|
||
}
|
||
|
||
public long CurrentTimeMillis()
|
||
{
|
||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
||
}
|
||
|
||
// 取消按钮
|
||
public DelegateCommand CancleTake
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
IsFinishClick = false;
|
||
_portUtil.ResetData();
|
||
Status = 0;
|
||
});
|
||
}
|
||
//检查是否是冰箱抽屉(冰箱抽屉打开时需要发送冰箱延迟报警的指令)
|
||
public async Task CheckIsFridgeOpen()
|
||
{
|
||
if (ChannelStock != null)
|
||
{
|
||
if (ChannelStock.BoardType == (Int32)BoardTypeEnum.fridge)
|
||
{
|
||
//发送冰箱延迟报警的指令
|
||
await _portUtil.FridgeDelayWarm();
|
||
}
|
||
}
|
||
}
|
||
//检查是否是冰箱抽屉(冰箱抽屉关闭时需要查询冰箱温度如温度不在范围则发送冰箱延迟报警的指令)
|
||
public async Task CheckIsFridgeClose()
|
||
{
|
||
if (ChannelStock != null)
|
||
{
|
||
if (ChannelStock.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]))
|
||
{
|
||
//发送冰箱延迟报警的指令
|
||
await _portUtil.FridgeDelayWarm();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public DelegateCommand BtnCloseCommand
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
if (Status != 0)
|
||
{
|
||
_portUtil.ResetData();
|
||
Status = 0;
|
||
}
|
||
//DialogParameters parameters = new DialogParameters();
|
||
//parameters.Add("",);
|
||
// 关闭当前窗口
|
||
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
|
||
});
|
||
}
|
||
|
||
public bool KeepAlive => false;
|
||
}
|
||
}
|