507 lines
18 KiB
C#
507 lines
18 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 MaterialDesignThemes.Wpf;
|
|
using Prism.Commands;
|
|
using Prism.Events;
|
|
using Prism.Mvvm;
|
|
using Prism.Regions;
|
|
using Prism.Services.Dialogs;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Media;
|
|
|
|
namespace DM_Weight.ViewModels
|
|
{
|
|
public class BindBoxPackageWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
|
|
{
|
|
private readonly ILog logger = LogManager.GetLogger(typeof(BindBoxPackageWindowViewModel));
|
|
public static BindBoxPackageWindowViewModel vm;
|
|
private int _drawerNo = 0;
|
|
|
|
public int DrawerNo
|
|
{
|
|
get => _drawerNo;
|
|
set => SetProperty(ref _drawerNo, value);
|
|
}
|
|
|
|
private SolidColorBrush _colorBrush;
|
|
|
|
public SolidColorBrush SnackbarBackground
|
|
{
|
|
get => _colorBrush;
|
|
set => SetProperty(ref _colorBrush, value);
|
|
}
|
|
|
|
private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3));
|
|
|
|
public ISnackbarMessageQueue SnackbarMessageQueue
|
|
{
|
|
get => _snackbarMessageQueue;
|
|
set => SetProperty(ref _snackbarMessageQueue, value);
|
|
}
|
|
|
|
private List<ChannelStock>? _channels;
|
|
|
|
public List<ChannelStock>? Channels
|
|
{
|
|
get => _channels;
|
|
set => SetProperty(ref _channels, value);
|
|
}
|
|
|
|
private ChannelStock _channelStock;
|
|
|
|
public ChannelStock Channel
|
|
{
|
|
get { return _channelStock; }
|
|
set
|
|
{
|
|
if (value != null)
|
|
{
|
|
DrugInfo = DrugInfos.Where(di => di.DrugId == value.DrugInfo.DrugId).First();
|
|
BaseQuantity = value.BaseQuantity.ToString();
|
|
}
|
|
SetProperty(ref _channelStock, value);
|
|
}
|
|
}
|
|
|
|
|
|
private List<DrugInfo>? _drugInfos;
|
|
|
|
public List<DrugInfo>? DrugInfos
|
|
{
|
|
get => _drugInfos;
|
|
set => SetProperty(ref _drugInfos, value);
|
|
}
|
|
private DrugInfo? _drugInfo;
|
|
|
|
public DrugInfo? DrugInfo
|
|
{
|
|
get => _drugInfo;
|
|
set
|
|
{
|
|
SetProperty(ref _drugInfo, value);
|
|
}
|
|
}
|
|
//药品基数
|
|
private string _baseQuantity;
|
|
public string BaseQuantity { get => _baseQuantity; set { SetProperty(ref _baseQuantity, value); } }
|
|
|
|
private bool _isEnable = true;
|
|
public bool IsEnable { get => _isEnable; set => SetProperty(ref _isEnable, value); }
|
|
private int _status = 0;
|
|
public int Status { get => _status; set { SetProperty(ref _status, value); } }
|
|
|
|
|
|
|
|
public bool KeepAlive => false;
|
|
//抽屉号列表
|
|
public static List<int> iList = new List<int>();
|
|
//第几个抽屉号下标
|
|
//public static int iNumber = 0;
|
|
|
|
//private PortUtil _portUtil;
|
|
IEventAggregator _eventAggregator;
|
|
public BindBoxPackageWindowViewModel(IEventAggregator eventAggregator)
|
|
{
|
|
vm = this;
|
|
//_portUtil = portUtil;
|
|
_eventAggregator = eventAggregator;
|
|
}
|
|
|
|
private void RequestData()
|
|
{
|
|
Channels?.Clear();
|
|
BaseQuantity = "";
|
|
var list = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
|
//.Includes(cl => cl.channelStocks, cs => cs.DrugInfo,di=>di.drugBase)
|
|
.Includes(cs => cs.DrugInfo)
|
|
.Where(cs => cs.MachineId.Equals("DM5"))
|
|
.Where(cs => cs.DrawerNo == DrawerNo+1).ToList();
|
|
|
|
if (list != null && list.Count > 0)
|
|
{
|
|
Channels=list.GroupBy(cs =>cs.DrugId).Select(cs =>
|
|
{
|
|
var ret = cs.First();
|
|
ret.Quantity = cs.Sum(itx => itx.Quantity);
|
|
return ret;
|
|
}).ToList();
|
|
}
|
|
else
|
|
{
|
|
Channels = null;
|
|
}
|
|
}
|
|
|
|
public void OnNavigatedTo(NavigationContext navigationContext)
|
|
{
|
|
logger.Info("进入OnNavigatedTo");
|
|
RequestDrug();
|
|
RequestData();
|
|
logger.Info("结束RequestData");
|
|
}
|
|
|
|
public bool IsNavigationTarget(NavigationContext navigationContext)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public void OnNavigatedFrom(NavigationContext navigationContext)
|
|
{
|
|
// 取消消息订阅
|
|
//_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
|
|
}
|
|
|
|
|
|
public DelegateCommand<string> UpdateDrawerNo
|
|
{
|
|
get => new DelegateCommand<string>((DrawerNo) =>
|
|
{
|
|
this.DrawerNo = Convert.ToInt32(DrawerNo);
|
|
RequestData();
|
|
}
|
|
);
|
|
}
|
|
private void RequestDrug()
|
|
{
|
|
//var list = SqlSugarHelper.Db.Queryable<DrugInfo>().Includes<DrugManuNo>(di => di.DrugManuNos).OrderBy(di => di.DrugId).ToList();
|
|
//DrugInfos = list;
|
|
string str = "SELECT d.drug_id,d.py_code,d.drug_barcode,d.drug_name,d.drug_brand_name,d.drug_spec,d.dosage,d.pack_unit,d.manufactory,d.max_stock,CONCAT(drug_name,' / ',drug_spec,' / ',manufactory) as drug_name_spec FROM `drug_info` d";
|
|
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
|
|
|
|
}
|
|
public DelegateCommand BindingDrug
|
|
{
|
|
get => new DelegateCommand(BindDrugAction);
|
|
}
|
|
//绑定
|
|
private void BindDrugAction()
|
|
{
|
|
|
|
if (DrugInfo == null || BaseQuantity == null)
|
|
{
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c"));
|
|
SnackbarMessageQueue.Enqueue("请选择药品并输入药品基数");
|
|
return;
|
|
}
|
|
int baseQuantity;
|
|
if (!int.TryParse(BaseQuantity, out baseQuantity))
|
|
{
|
|
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c"));
|
|
SnackbarMessageQueue.Enqueue("药品基数请输入正确的整数");
|
|
return;
|
|
}
|
|
if (Channels != null)
|
|
{
|
|
int iNum = Channels.Where(cs => cs.DrugId == DrugInfo.DrugId.ToString()).Count();
|
|
if (iNum > 0)
|
|
{
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c"));
|
|
SnackbarMessageQueue.Enqueue("该药品已在手术间绑定,请选择其他药品");
|
|
return;
|
|
}
|
|
|
|
}
|
|
var f = SqlSugarHelper.Db.UseTran(() =>
|
|
{
|
|
string chnguid = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cs => cs.MachineId == ConfigurationManager.AppSettings["machineId"].ToString() && cs.DrawerNo == DrawerNo).Select(cs => cs.Id).First();
|
|
SqlSugarHelper.Db.Insertable(new ChannelStock()
|
|
{
|
|
Chnguid = chnguid,
|
|
DrawerNo = DrawerNo,
|
|
DrugId = DrugInfo.DrugId.ToString(),
|
|
BaseQuantity = baseQuantity,
|
|
Id = Guid.NewGuid().ToString(),
|
|
MachineId = "DM5",
|
|
AddToJJNum=0,
|
|
NeedNum=baseQuantity
|
|
}).ExecuteCommand();
|
|
// 保存数据 入库记录
|
|
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
|
{
|
|
MachineId = "DM5",
|
|
DrawerNo = DrawerNo,
|
|
DrugId = DrugInfo.DrugId.ToString(),
|
|
Operator = HomeWindowViewModel.Operator?.Id,
|
|
OperationTime = DateTime.Now,
|
|
Quantity = baseQuantity,
|
|
Type = 55,
|
|
InvoiceId = "绑定手术间",
|
|
}).ExecuteCommand();
|
|
return true;
|
|
});
|
|
if (f.Data)
|
|
{
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00e676"));
|
|
SnackbarMessageQueue.Enqueue("药品已绑定手术间");
|
|
RequestDrug();
|
|
RequestData();
|
|
}
|
|
}
|
|
|
|
public void UpdateComboBoxItems(string text)
|
|
{
|
|
string str = @"SELECT d.drug_id,d.py_code,d.drug_barcode,d.drug_name,d.drug_brand_name,d.drug_spec,d.dosage,d.pack_unit,
|
|
d.manufactory,d.max_stock,CONCAT(drug_name,' / ',drug_spec,' / ',manufactory) as drug_name_spec FROM `drug_info` d";
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
|
|
return;
|
|
}
|
|
if (DrugInfos != null)
|
|
{
|
|
DrugInfos.Clear();
|
|
}
|
|
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).Where(di => di.DrugName.Contains(text) || di.PyCode.Contains(text)).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
|
|
}
|
|
|
|
//解绑
|
|
public DelegateCommand RemoveBinding
|
|
{
|
|
get => new DelegateCommand(() => RemoveBindingAction());
|
|
}
|
|
private void RemoveBindingAction()
|
|
{
|
|
try
|
|
{
|
|
|
|
if (Channels == null)
|
|
{
|
|
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c"));
|
|
SnackbarMessageQueue.Enqueue("所选手术间中无可解绑药品");
|
|
return;
|
|
}
|
|
if (Channel != null)
|
|
{
|
|
//解绑选定行的单个药品
|
|
SqlSugarHelper.Db.Deleteable(Channel).ExecuteCommand();
|
|
// 保存数据 入库记录
|
|
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
|
{
|
|
MachineId = "DM5",
|
|
DrawerNo = DrawerNo,
|
|
DrugId = Channel.DrugId.ToString(),
|
|
Operator = HomeWindowViewModel.Operator?.Id,
|
|
OperationTime = DateTime.Now,
|
|
Quantity = Channel.Quantity,
|
|
Type = 55,
|
|
InvoiceId = "解绑手术间单个药品",
|
|
}).ExecuteCommand();
|
|
}
|
|
else
|
|
{
|
|
//解绑药箱下的所有药品
|
|
Channels.ForEach(item =>
|
|
{
|
|
|
|
SqlSugarHelper.Db.Deleteable(item).ExecuteCommand();
|
|
|
|
// 保存数据 入库记录
|
|
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
|
{
|
|
MachineId = "DM5",
|
|
DrawerNo = DrawerNo,
|
|
DrugId = item.DrugId.ToString(),
|
|
Operator = HomeWindowViewModel.Operator?.Id,
|
|
OperationTime = DateTime.Now,
|
|
Quantity = item.Quantity,
|
|
Type = 55,
|
|
InvoiceId = "绑定手术间全部药品",
|
|
}).ExecuteCommand();
|
|
|
|
});
|
|
|
|
|
|
}
|
|
RequestDrug();
|
|
RequestData();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c"));
|
|
SnackbarMessageQueue.Enqueue("解绑操作异常");
|
|
}
|
|
}
|
|
|
|
//保存修改的药品基数
|
|
public DelegateCommand SaveCommand
|
|
{
|
|
get => new DelegateCommand(() =>
|
|
{
|
|
if (DrugInfo.DrugId != Channel.DrugId)
|
|
{
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c"));
|
|
SnackbarMessageQueue.Enqueue("只能修改药品基数,不能更换绑定药品!");
|
|
return;
|
|
}
|
|
int baseQty = 0;
|
|
if (int.TryParse(BaseQuantity, out baseQty))
|
|
{
|
|
Channel.BaseQuantity = Convert.ToInt32(baseQty);
|
|
int iUpdate= SqlSugarHelper.Db.Updateable<ChannelStock>(Channel).ExecuteCommand();
|
|
if (iUpdate > 0)
|
|
{
|
|
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00e676"));
|
|
SnackbarMessageQueue.Enqueue("保存修改完成!");
|
|
}
|
|
else
|
|
{
|
|
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00e676"));
|
|
SnackbarMessageQueue.Enqueue("保存修改失败!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c"));
|
|
SnackbarMessageQueue.Enqueue("药品基数只能是正整数,请检查输入!");
|
|
return;
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
//刷新
|
|
public DelegateCommand Query
|
|
{
|
|
get => new DelegateCommand(RequestData);
|
|
}
|
|
//打开全部药箱
|
|
public DelegateCommand OpenBox
|
|
{
|
|
get => new DelegateCommand(() => OpenBoxAction());
|
|
}
|
|
|
|
private void OpenBoxAction()
|
|
{
|
|
|
|
iList = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cl => cl.MachineId == "DM5")
|
|
.Select(cl => cl.DrawerNo).ToList();
|
|
if (iList.Count > 0)
|
|
{
|
|
for (int i = 0; i < iList.Count; i++)
|
|
{
|
|
//记录开药箱日志
|
|
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
|
{
|
|
MachineId = "DM5",
|
|
DrawerNo = DrawerNo,
|
|
Operator = HomeWindowViewModel.Operator?.Id,
|
|
OperationTime = DateTime.Now,
|
|
Type = 55,
|
|
InvoiceId = $"打开{DrawerNo}号手术间",
|
|
}).ExecuteCommand();
|
|
IsEnable = false;
|
|
Status = 1;
|
|
Console.WriteLine($"正在打开{iList[i]}号手术间");
|
|
ModbusHelper.GetInstance().OpenBoxDoor(iList[i] - 1);
|
|
//iNumber++;
|
|
}
|
|
|
|
//ModbusHelper.GetInstance().GetAllBoxState();
|
|
//if (Status == 2)
|
|
//{
|
|
// Status = 3;
|
|
//}
|
|
//IsEnable = true;
|
|
}
|
|
|
|
}
|
|
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 (Status == 1)
|
|
// {
|
|
// Status = 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 (Status == 2)
|
|
{
|
|
Status = 3;
|
|
}
|
|
IsEnable = true;
|
|
DrawerNo = -1;
|
|
//_portUtil.Operate = false;
|
|
break;
|
|
// 打开失败
|
|
case EventType.OPENERROR:
|
|
AlertMsg alertMsg = new AlertMsg
|
|
{
|
|
Message = msg.Message,
|
|
Type = MsgType.ERROR
|
|
};
|
|
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
|
|
|
IsEnable = true;
|
|
DrawerNo = -1;
|
|
Status = 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;
|
|
}
|
|
}
|
|
}
|
|
}
|