714 lines
36 KiB
C#
714 lines
36 KiB
C#
using DM_Weight.Models;
|
||
using DM_Weight.msg;
|
||
using DM_Weight.Port;
|
||
using DM_Weight.util;
|
||
using log4net;
|
||
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.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Input;
|
||
|
||
namespace DM_Weight.ViewModels
|
||
{
|
||
public class AddToJiaoJieDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime
|
||
{
|
||
private readonly ILog logger = LogManager.GetLogger(typeof(OrderTakeDialogViewModel));
|
||
|
||
|
||
private List<ChannelStock> _channelStocks = new List<ChannelStock>();
|
||
|
||
public List<ChannelStock> ChannelStocks
|
||
{
|
||
get => _channelStocks;
|
||
set => SetProperty(ref _channelStocks, value);
|
||
}
|
||
/// <summary>
|
||
/// 交接柜的库位信息
|
||
/// </summary>
|
||
private List<ChannelStock> _jiaojie_channelStocks;
|
||
|
||
public List<ChannelStock> Jiaojie_ChannelStocks
|
||
{
|
||
get => _jiaojie_channelStocks;
|
||
set => SetProperty(ref _jiaojie_channelStocks, value);
|
||
}
|
||
//交接柜中无该批次添加实体
|
||
public List<ChannelStock> AddJJStock = new List<ChannelStock>();
|
||
//交接柜中有该批次更新实体
|
||
public List<ChannelStock> UpdateJJStock = new List<ChannelStock>();
|
||
/// <summary>
|
||
/// 交接柜的库位信息
|
||
/// </summary>
|
||
private ChannelStock _jiaojei_cs;
|
||
|
||
public ChannelStock Jiaojie_cs
|
||
{
|
||
get => _jiaojei_cs;
|
||
set => SetProperty(ref _jiaojei_cs, value);
|
||
}
|
||
private static readonly DateTime Jan1st1970 = new DateTime
|
||
(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||
|
||
private IEnumerable<IGrouping<int, ChannelStock>> enumerable;
|
||
private IEnumerator<IGrouping<int, ChannelStock>> enumerator;
|
||
|
||
|
||
public string Title => "交接柜补药";
|
||
|
||
public bool KeepAlive => false;
|
||
|
||
public event Action<IDialogResult> RequestClose;
|
||
|
||
public bool CanCloseDialog()
|
||
{
|
||
return Status == 0;
|
||
}
|
||
|
||
public void OnDialogClosed()
|
||
{
|
||
// 取消消息订阅
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
|
||
}
|
||
|
||
public void OnDialogOpened(IDialogParameters parameters)
|
||
{
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
|
||
Jiaojie_ChannelStocks = parameters.GetValue<List<ChannelStock>>("ChannelStocks");
|
||
|
||
for (int i = 0; i < Jiaojie_ChannelStocks.Count; i++)
|
||
{
|
||
ChannelStock copy = TransExpV2<ChannelStock, ChannelStock>.Trans(Jiaojie_ChannelStocks[i]);
|
||
ChannelStocks.Add(copy);
|
||
}
|
||
ChannelStocks = Jiaojie_ChannelStocks.GroupBy(cs =>new { cs.DrugId, cs.ManuNo }).Select(g => new
|
||
{
|
||
Key = g.Key,
|
||
AddQuantity = g.Sum(s => s.AddQuantity)
|
||
}).Select(cs => new ChannelStock() { DrugId = cs.Key.DrugId,ManuNo=cs.Key.ManuNo, AddQuantity = cs.AddQuantity }).ToList();
|
||
RequestData();
|
||
}
|
||
|
||
private PortUtil _portUtil;
|
||
IEventAggregator _eventAggregator;
|
||
IDialogService _dialogService;
|
||
public AddToJiaoJieDialogViewModel(PortUtil portUtil, IEventAggregator eventAggregator, IDialogService DialogService)
|
||
{
|
||
_dialogService = DialogService;
|
||
_portUtil = portUtil;
|
||
_eventAggregator = eventAggregator;
|
||
}
|
||
void DoMyPrismEvent(DeviceMsg msg)
|
||
{
|
||
|
||
if (msg.WindowName == "OrderTakeDrugWindow")
|
||
{
|
||
IGrouping<int, ChannelStock> grouping = enumerator.Current;
|
||
int DrawerNo = grouping.Key;
|
||
List<ChannelStock> channelStocks = grouping.ToList();
|
||
|
||
switch (msg.EventType)
|
||
{
|
||
// 抽屉打开
|
||
case EventType.DRAWEROPEN:
|
||
|
||
|
||
if (Status == 1)
|
||
{
|
||
if (channelStocks[0].process == 1)
|
||
{
|
||
channelStocks.ForEach(it => it.process = 2);
|
||
}
|
||
}
|
||
//是冰箱抽屉则开冰箱抽屉时发送延迟报警指令
|
||
CheckIsFridgeOpen();
|
||
break;
|
||
// 抽屉关闭
|
||
case EventType.DRAWERCLOSE:
|
||
if (Status == 1)
|
||
{
|
||
if (channelStocks[0].process == 2)
|
||
{
|
||
channelStocks.ForEach(it => it.process = 3);
|
||
}
|
||
IGrouping<int, ChannelStock> groupingBefore = enumerator.Current;
|
||
int DrawerNoBefore = groupingBefore.Key;
|
||
if (enumerator.MoveNext())
|
||
{
|
||
IGrouping<int, ChannelStock> groupingAfter = enumerator.Current;
|
||
int DrawerNoAfter = groupingAfter.Key;
|
||
if (DrawerNoBefore < 9 && DrawerNoAfter > 8)
|
||
{
|
||
Thread.Sleep(50);
|
||
}
|
||
OpenOneByOne();
|
||
}
|
||
// 已经全部取出
|
||
else
|
||
{
|
||
Status = 3;
|
||
}
|
||
}
|
||
//是冰箱抽屉则开冰箱抽屉时发送延迟报警指令
|
||
CheckIsFridgeClose();
|
||
break;
|
||
// 数量变化
|
||
case EventType.UPDATEQUANTITY:
|
||
if (Status == 1)
|
||
{
|
||
logger.Info($"抽屉【{DrawerNo}】库位取药数量【{msg.Quantitys}】");
|
||
}
|
||
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 = 0;
|
||
|
||
public int Status
|
||
{
|
||
get => _status; set => SetProperty(ref _status, value);
|
||
}
|
||
public async void RequestData()
|
||
{
|
||
List<ChannelStock> channelStocks = new List<ChannelStock>();
|
||
List<string> msg = new List<string>();
|
||
for (int i = 0; i < ChannelStocks.Count; i++)
|
||
{
|
||
List<ChannelStock> HasQChannels = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
.Includes<DrugInfo>(cs => cs.DrugInfo)
|
||
.Where(cs => cs.Quantity > 0)
|
||
.Where(cs => cs.DrawerType == 1)
|
||
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM3"))
|
||
.Where(cs => cs.DrugId == ChannelStocks[i].DrugId && cs.ManuNo == ChannelStocks[i].ManuNo)
|
||
.OrderBy(cs => cs.EffDate)
|
||
.OrderBy(cs => cs.DrawerNo)
|
||
.ToList();
|
||
if (HasQChannels == null || HasQChannels.Count <= 0)
|
||
{
|
||
msg.Add($"有药品未绑定,请先绑定");
|
||
continue;
|
||
}
|
||
int total = HasQChannels.Sum(it => it.Quantity);
|
||
int TakeQ = ChannelStocks[i].AddQuantity;
|
||
// 说明数量足够
|
||
if (total >= TakeQ)
|
||
{
|
||
for (int j = 0; TakeQ > 0; j++)
|
||
{
|
||
ChannelStock stock = HasQChannels[j];
|
||
if (TakeQ > stock.Quantity)
|
||
{
|
||
stock.TakeQuantity = stock.Quantity;
|
||
channelStocks.Add(stock);
|
||
TakeQ -= stock.Quantity;
|
||
}
|
||
else
|
||
{
|
||
stock.TakeQuantity = TakeQ;
|
||
channelStocks.Add(stock);
|
||
TakeQ = 0;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
msg.Add($"药品【{HasQChannels[0].DrugInfo.DrugName}】库存不足,应取【{TakeQ}】库存【{total}】");
|
||
}
|
||
|
||
}
|
||
if (msg.Count > 0)
|
||
{
|
||
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
|
||
//MessageBox.Show(string.Join("\n", msg));
|
||
DialogParameters dialogParameters = new DialogParameters();
|
||
dialogParameters.Add("msgInfo", msg);
|
||
DialogServiceExtensions.ShowDialogHost(_dialogService, "ShowMessageDialog", dialogParameters, "RootDialog");
|
||
|
||
}
|
||
else
|
||
{
|
||
channelStocks.Sort((a, b) =>
|
||
{
|
||
if ((a.DrawerNo - b.DrawerNo) == 0)
|
||
{
|
||
return a.ColNo - b.ColNo;
|
||
}
|
||
return a.DrawerNo - b.DrawerNo;
|
||
});
|
||
ChannelStocks = channelStocks;
|
||
|
||
|
||
}
|
||
}
|
||
public DelegateCommand OpenDrawer
|
||
{
|
||
get => new DelegateCommand(async () =>
|
||
{
|
||
if (Status == 0)
|
||
{
|
||
if (HomeWindowViewModel.Operator.Role != null && HomeWindowViewModel.Operator.Role.RoleName != "管理员")
|
||
{
|
||
//查看当前用户是否有所在药品抽屉的权限;1-2层所有人能开,其他6层管理员才能开
|
||
bool bDrawer = ChannelStocks.Select(it => it.DrawerNo).Where(n => n > 2).Any();
|
||
if (bDrawer)
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "当前用户没有打开抽屉的权限!",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
return;
|
||
}
|
||
|
||
}
|
||
enumerable = ChannelStocks.GroupBy(cs => cs.DrawerNo, cs => cs);
|
||
enumerator = enumerable.GetEnumerator();
|
||
enumerator.MoveNext();
|
||
Status = 1;
|
||
OpenOneByOne();
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
private void OpenOneByOne()
|
||
{
|
||
IGrouping<int, ChannelStock> grouping = enumerator.Current;
|
||
int DrawerNo = grouping.Key;
|
||
List<ChannelStock> channelStocks = grouping.ToList();
|
||
channelStocks.ForEach(it => it.process = 1);
|
||
_portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉");
|
||
|
||
|
||
List<ChannelStock> singleChannels = channelStocks.FindAll(it => it.BoardType != 1);
|
||
|
||
// 发送取药数量
|
||
singleChannels.ForEach(it =>
|
||
{
|
||
try
|
||
{
|
||
|
||
_portUtil.TakeQuantity(DrawerNo, it.ColNo, it.TakeQuantity, it.Quantity - it.TakeQuantity);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = $"打开抽屉异常{ex.Message}",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
}
|
||
});
|
||
|
||
_portUtil.WindowName = "OrderTakeDrugWindow";
|
||
_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1;
|
||
_portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray();
|
||
_portUtil.DrawerNo = DrawerNo;
|
||
_portUtil.Start();
|
||
}
|
||
|
||
private bool _isFinishClick = false;
|
||
// 完成按钮
|
||
public DelegateCommand TakeFinish
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
if (!_isFinishClick)
|
||
{
|
||
_isFinishClick = true;
|
||
|
||
List<ChannelStock> record = ChannelStocks.FindAll(it => it.TakeQuantity > 0).ToList();
|
||
if (record.Count > 0)
|
||
{
|
||
string InvoiceId = "AddJiaoJie_" + CurrentTimeMillis();
|
||
var f = SqlSugarHelper.Db.UseTran(() =>
|
||
{
|
||
for (int i = 0; i < record.Count; i++)
|
||
{
|
||
ChannelStock it = record[i];
|
||
// 更新数据 库存信息
|
||
SqlSugarHelper.Db.Updateable(new ChannelStock()
|
||
{
|
||
Quantity = it.Quantity - it.TakeQuantity,
|
||
ManuNo = it.ManuNo,
|
||
EffDate = it.EffDate,
|
||
Id = it.Id,
|
||
}).UpdateColumns(it => new { it.Quantity, it.ManuNo, it.EffDate }).ExecuteCommand();
|
||
|
||
////更新 交接柜 批次信息
|
||
//List<ChannelStock> jiaojieStock = Jiaojie_ChannelStocks.Where(cs => cs.DrugId == it.DrugId).ToList();
|
||
////交接柜中有该药品及批次的数据则更新;没有则插入一条
|
||
//if (jiaojieStock != null)
|
||
//{
|
||
// for (int j = 0; j < jiaojieStock.Count; j++)
|
||
// {
|
||
// if(it.Quantity> jiaojieStock[j].Quantity)
|
||
// {
|
||
// //取的数量够不够被
|
||
// }
|
||
// List<ChannelStock> searchStock = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
// .Where(cs => cs.DrawerNo == jiaojieStock[j].DrawerNo
|
||
// && cs.DrugId == jiaojieStock[j].DrugId
|
||
// && cs.DrugId == it.DrugId
|
||
// && cs.ManuNo == it.ManuNo
|
||
// &&cs.MachineId==(ConfigurationManager.AppSettings["jj_machineId"] ?? "DM5"))
|
||
// .ToList();
|
||
// //List<ChannelStock> stockManuList = jiaojieStock.Where(cs => cs.ManuNo == it.ManuNo).ToList();
|
||
// if (searchStock == null || searchStock.Count <= 0)
|
||
// {
|
||
// SqlSugarHelper.Db.Deleteable<ChannelStock>().Where(cs=>cs.DrawerNo== jiaojieStock[j].DrawerNo&&cs.DrugId== jiaojieStock[j].DrugId&&cs.Quantity<=0).ExecuteCommand();
|
||
// jiaojieStock[j].ManuNo = it.ManuNo;
|
||
// jiaojieStock[j].Id = Guid.NewGuid().ToString();
|
||
// SqlSugarHelper.Db.Insertable(jiaojieStock[j]).ExecuteCommand();
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
// 保存数据 出库记录
|
||
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.TakeQuantity,
|
||
Type = 2,
|
||
Status = 2,//给交接柜补药不用还空瓶,等真正用了以后再还空瓶,所以先把状态置为2
|
||
InvoiceId = InvoiceId
|
||
}).ExecuteCommand();
|
||
}
|
||
|
||
//更新 交接柜 批次信息
|
||
for (int i = 0; i < Jiaojie_ChannelStocks.Count; i++)
|
||
{
|
||
ChannelStock jiaoStock = Jiaojie_ChannelStocks[i];
|
||
SqlSugarHelper.Db.Deleteable<ChannelStock>().Where(cs => cs.DrawerNo == jiaoStock.DrawerNo && cs.DrugId == jiaoStock.DrugId && cs.MachineId == jiaoStock.MachineId && cs.Quantity <= 0).ExecuteCommand();
|
||
List<ChannelStock> csStockList = record.Where(cs => cs.DrugId == jiaoStock.DrugId && cs.TakeQuantity > 0).ToList();
|
||
if (csStockList != null && csStockList.Count > 0)
|
||
{
|
||
for (int j = 0; j < csStockList.Count; j++)
|
||
{
|
||
ChannelStock csStock = (ChannelStock)csStockList[j].Clone();
|
||
ChannelStock jjStockManuNo = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
.Where(cs => cs.DrawerNo == jiaoStock.DrawerNo && cs.DrugId == jiaoStock.DrugId && cs.ManuNo == csStock.ManuNo && cs.MachineId == "DM5").First();
|
||
logger.Info($"库中{jiaoStock.DrawerNo}号手术间,{jiaoStock.DrugId},{csStock.ManuNo};有该批次{jjStockManuNo!=null};jjStockManuNo是空:{jjStockManuNo==null}");
|
||
if (jjStockManuNo != null)
|
||
{
|
||
int addNum = csStock.TakeQuantity;
|
||
logger.Info($"需补药数{jiaoStock.NeedNum},该批次取药数{csStock.TakeQuantity}");
|
||
//有该批次
|
||
if (jiaoStock.NeedNum > csStock.TakeQuantity)
|
||
{
|
||
//不够取
|
||
ChannelStock newStock = new ChannelStock();
|
||
//newStock.Chnguid = jiaoStock.Chnguid;
|
||
//newStock.BaseQuantity = jiaoStock.BaseQuantity;
|
||
newStock.ManuNo = jiaoStock.ManuNo;
|
||
newStock.DrawerNo = jiaoStock.DrawerNo;
|
||
newStock.DrugId = jiaoStock.DrugId;
|
||
newStock.AddToJJNum = addNum;
|
||
newStock.Id = jjStockManuNo.Id;
|
||
//newStock.MachineId = jiaoStock.MachineId;
|
||
//newStock.NeedNum = 0;
|
||
//newStock.Quantity = jiaoStock.Quantity + csStock.TakeQuantity;
|
||
UpdateJJStock.Add(newStock);
|
||
csStockList[j].TakeQuantity = 0;
|
||
jiaoStock.NeedNum = jiaoStock.NeedNum - csStock.TakeQuantity;
|
||
}
|
||
else
|
||
{
|
||
//够取
|
||
//jiaoStock.AddQuantity = jiaoStock.Quantity+ jiaoStock.NeedNum;
|
||
//jiaoStock.NeedNum = 0;
|
||
//AddJJStock.Add(jiaoStock);
|
||
//csStock.Quantity = csStock.Quantity - jiaoStock.NeedNum;
|
||
//csStockList[j].Quantity = csStock.Quantity - jiaoStock.NeedNum;
|
||
|
||
ChannelStock newStock = new ChannelStock();
|
||
//newStock.Chnguid = jiaoStock.Chnguid;
|
||
//newStock.BaseQuantity = jiaoStock.BaseQuantity;
|
||
newStock.ManuNo = jiaoStock.ManuNo;
|
||
newStock.DrawerNo = jiaoStock.DrawerNo;
|
||
newStock.DrugId = jiaoStock.DrugId;
|
||
newStock.AddToJJNum = jiaoStock.NeedNum;
|
||
newStock.Id = jjStockManuNo.Id;
|
||
//newStock.MachineId = jiaoStock.MachineId;
|
||
//newStock.NeedNum = 0;
|
||
//newStock.Quantity = jiaoStock.Quantity + csStock.TakeQuantity;
|
||
UpdateJJStock.Add(newStock);
|
||
csStockList[j].TakeQuantity = csStock.TakeQuantity - jiaoStock.NeedNum;
|
||
jiaoStock.NeedNum = 0;
|
||
break;
|
||
}
|
||
}
|
||
else //没有该批次
|
||
{
|
||
if (jiaoStock.NeedNum > csStock.TakeQuantity)
|
||
{
|
||
//不够取
|
||
ChannelStock newStock = new ChannelStock();
|
||
newStock.BaseQuantity = jiaoStock.BaseQuantity;
|
||
newStock.Chnguid = jiaoStock.Chnguid;
|
||
newStock.DrawerNo = jiaoStock.DrawerNo;
|
||
newStock.DrugId = jiaoStock.DrugId;
|
||
newStock.ManuNo = csStock.ManuNo;
|
||
newStock.AddToJJNum = csStock.TakeQuantity;
|
||
newStock.Id = Guid.NewGuid().ToString();
|
||
newStock.MachineId = jiaoStock.MachineId;
|
||
newStock.DrawerType = 1;
|
||
newStock.NeedNum = 0;
|
||
AddJJStock.Add(newStock);
|
||
jiaoStock.NeedNum = jiaoStock.NeedNum - csStock.TakeQuantity;
|
||
csStockList[j].TakeQuantity = 0;
|
||
}
|
||
else
|
||
{
|
||
//够取
|
||
ChannelStock newStock = new ChannelStock();
|
||
newStock.Chnguid = jiaoStock.Chnguid;
|
||
newStock.BaseQuantity = jiaoStock.BaseQuantity;
|
||
newStock.DrawerNo = jiaoStock.DrawerNo;
|
||
newStock.DrugId = jiaoStock.DrugId;
|
||
newStock.ManuNo = csStock.ManuNo;
|
||
newStock.AddToJJNum = jiaoStock.NeedNum;
|
||
newStock.NeedNum = 0;
|
||
newStock.Id = Guid.NewGuid().ToString();
|
||
newStock.MachineId = jiaoStock.MachineId;
|
||
newStock.DrawerType = 1;
|
||
AddJJStock.Add(newStock);
|
||
csStockList[j].TakeQuantity = csStock.TakeQuantity - newStock.AddToJJNum;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
SqlSugarHelper.Db.Updateable<ChannelStock>()
|
||
.SetColumns(cs => new ChannelStock() { NeedNum = 0 })
|
||
.Where(cs => cs.DrawerNo == jiaoStock.DrawerNo)
|
||
.ExecuteCommand();
|
||
}
|
||
|
||
//保存交接柜数据
|
||
if (AddJJStock != null && AddJJStock.Count > 0)
|
||
{
|
||
AddJJStock = AddJJStock.GroupBy(jj => new { jj.DrawerNo, jj.DrugId, jj.ManuNo })
|
||
.Select(jj =>
|
||
{
|
||
var ret = jj.First();
|
||
ret.AddToJJNum = jj.Sum(itx => itx.AddToJJNum);
|
||
return ret;
|
||
}).ToList();
|
||
SqlSugarHelper.Db.Insertable(AddJJStock).ExecuteCommand();
|
||
}
|
||
if (UpdateJJStock != null && UpdateJJStock.Count > 0)
|
||
{
|
||
UpdateJJStock = UpdateJJStock.GroupBy(jj => new { jj.DrawerNo, jj.DrugId, jj.ManuNo })
|
||
.Select(jj =>
|
||
{
|
||
var ret = jj.First();
|
||
ret.AddToJJNum = jj.Sum(itx => itx.AddToJJNum);
|
||
return ret;
|
||
}).ToList();
|
||
for (int i = 0; i < UpdateJJStock.Count; i++)
|
||
{
|
||
SqlSugarHelper.Db.Updateable<ChannelStock>().SetColumns(cs => new ChannelStock() { AddToJJNum = UpdateJJStock[i].AddToJJNum }).Where(cs => cs.Id == UpdateJJStock[i].Id).ExecuteCommand();
|
||
}
|
||
}
|
||
|
||
//保存注射剂报表信息
|
||
for (int j = 0; j < Jiaojie_ChannelStocks.Count; j++)
|
||
{
|
||
ChannelStock jStock = Jiaojie_ChannelStocks[j];
|
||
if (jStock.DrugInfo.Dosage == "注射剂")
|
||
{
|
||
int totalBaseNum = SqlSugarHelper.Db.Queryable<DrugBase>().
|
||
Where(db => db.DrugId == jStock.DrugId && db.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM3")).Select(db => db.BaseQuantity).First();
|
||
|
||
string belognUser = SqlSugarHelper.Db.Queryable<ChannelList>().
|
||
Where(cl => cl.DrawerNo == jStock.DrawerNo && cl.MachineId == jStock.MachineId).Select(cl => cl.BelongUser).First();
|
||
DrugInfo di = SqlSugarHelper.Db.Queryable<DrugInfo>().Where(di => di.DrugId == jStock.DrugId).First();
|
||
SqlSugarHelper.Db.Insertable(new RejectionReport()
|
||
{
|
||
SendDate = DateTime.Parse(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm")),
|
||
SendUser = HomeWindowViewModel.Operator?.Nickname,
|
||
ReceiveUser = belognUser,//Jiaojie_ChannelStocks
|
||
DrugId = jStock.DrugId,
|
||
DrugName = di.DrugName,
|
||
DrugSpec = di.DrugSpec,
|
||
BaseNum = totalBaseNum + "支",//总基数
|
||
DrawerNo = jStock.DrawerNo
|
||
}).ExecuteCommand();
|
||
}
|
||
}
|
||
//更新交接柜状态为 已取药未入库(等在交接柜入库后再更新交接柜库存)
|
||
List<ChannelStock> jiaojie = Jiaojie_ChannelStocks.GroupBy(cs => cs.DrawerNo).Select(cs => cs.FirstOrDefault()).ToList();
|
||
if (jiaojie != null && jiaojie.Count > 0)
|
||
{
|
||
for (int j = 0; j < jiaojie.Count; j++)
|
||
{
|
||
ChannelStock jiaojie_it = jiaojie[j];
|
||
//更新交接柜状态为 已取药未入库
|
||
SqlSugarHelper.Db.Updateable(new ChannelList()
|
||
{
|
||
State = 1,
|
||
Id = jiaojie_it.ChannelLst.Id
|
||
}).UpdateColumns(it => it.State).ExecuteCommand();
|
||
}
|
||
}
|
||
return true;
|
||
});
|
||
if (f.Data)
|
||
{
|
||
Task.Factory.StartNew(() =>
|
||
{
|
||
// 更新屏显库存
|
||
List<ChannelStock> singleChannels = record.FindAll(it => it.BoardType == 5);
|
||
if (singleChannels.Count > 0)
|
||
{
|
||
singleChannels.ForEach(it =>
|
||
{
|
||
_portUtil.WriteQuantity(it.DrawerNo, it.ColNo, it.Quantity - it.TakeQuantity);
|
||
Thread.Sleep(150);
|
||
});
|
||
}
|
||
List<ChannelStock> singleChannelsBoxSmart = record.FindAll(it => it.BoardType == 35);
|
||
if ((singleChannelsBoxSmart.Count > 0 ? singleChannelsBoxSmart[0].BoardType : 1) == 35)
|
||
{
|
||
singleChannelsBoxSmart.ForEach(it =>
|
||
{
|
||
_portUtil.WriteQuantityMethod((it.Quantity - it.TakeQuantity), it.DrawerNo, it.ColNo);
|
||
Thread.Sleep(200);
|
||
});
|
||
}
|
||
});
|
||
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.Cancel));
|
||
RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
|
||
}
|
||
else
|
||
{
|
||
_isFinishClick = false;
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "没有填写取药数量",
|
||
Type = MsgType.ERROR
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
}
|
||
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
public long CurrentTimeMillis()
|
||
{
|
||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
||
}
|
||
|
||
// 取消按钮
|
||
public DelegateCommand CancleTake
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
_portUtil.ResetData();
|
||
Status = 0;
|
||
});
|
||
}
|
||
//检查是否是冰箱抽屉(冰箱抽屉打开时需要发送冰箱延迟报警的指令)
|
||
public async Task CheckIsFridgeOpen()
|
||
{
|
||
//if (ChannelStocks != null && ChannelStocks.Count > 0)
|
||
//{
|
||
// if (_portUtil.BoardType == (Int32)BoardTypeEnum.fridge)
|
||
// {
|
||
// _portUtil.FridgeOperate = true;
|
||
// //发送冰箱延迟报警的指令
|
||
// await _portUtil.FridgeDelayWarm();
|
||
// _portUtil.FridgeOperate = false;
|
||
// }
|
||
//}
|
||
}
|
||
//检查是否是冰箱抽屉(冰箱抽屉关闭时需要查询冰箱温度如温度不在范围则发送冰箱延迟报警的指令)
|
||
public async Task CheckIsFridgeClose()
|
||
{
|
||
//if (ChannelStocks != null && ChannelStocks.Count > 0)
|
||
//{
|
||
// if (_portUtil.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 DelegateCommand BtnCloseCommand
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
if (Status != 0)
|
||
{
|
||
_portUtil.ResetData();
|
||
Status = 0;
|
||
}
|
||
//DialogParameters parameters = new DialogParameters();
|
||
//parameters.Add("",);
|
||
// 关闭当前窗口
|
||
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
|
||
});
|
||
}
|
||
}
|
||
}
|