333 lines
13 KiB
C#
333 lines
13 KiB
C#
using log4net;
|
|
using log4net.Repository.Hierarchy;
|
|
using Prism.Commands;
|
|
using Prism.Events;
|
|
using Prism.Mvvm;
|
|
using Prism.Regions;
|
|
using Prism.Services.Dialogs;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
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 System.Threading;
|
|
using SqlSugar;
|
|
using System.Configuration;
|
|
using DM_Weight.Services;
|
|
|
|
namespace DM_Weight.ViewModels
|
|
{
|
|
public class SelfTakeDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime
|
|
{
|
|
|
|
private readonly ILog logger = LogManager.GetLogger(typeof(SelfTakeDialogViewModel));
|
|
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 = "SelfTakeWindow";
|
|
|
|
private PortUtil _portUtil;
|
|
IEventAggregator _eventAggregator;
|
|
//private SqlSugarScope SqlSugarHelper.Db;
|
|
private SqlHelperService _mysqlHelper;
|
|
public SelfTakeDialogViewModel(PortUtil portUtil, IEventAggregator eventAggregator, SqlSugarScope sqlSugarScope, SqlHelperService mysqlHelper)
|
|
{
|
|
_portUtil = portUtil;
|
|
_eventAggregator = eventAggregator;
|
|
//this.SqlSugarHelper.Db = sqlSugarScope;
|
|
_mysqlHelper = mysqlHelper;
|
|
}
|
|
|
|
void DoMyPrismEvent(DeviceMsg msg)
|
|
{
|
|
|
|
if (msg.WindowName.Equals(WindowName))
|
|
{
|
|
IGrouping<int, ChannelStock> grouping = enumerator.Current;
|
|
int DrawerNo = grouping.Key;
|
|
List<ChannelStock> channelStocks = grouping.ToList();
|
|
|
|
switch (msg.EventType)
|
|
{
|
|
// 抽屉打开
|
|
case EventType.DRAWEROPEN:
|
|
|
|
|
|
logger.Info($"抽屉打开,channelStocks{channelStocks != null}");
|
|
//if (Status == 1)
|
|
//{
|
|
// if (channelStocks != null && channelStocks[0] != null && channelStocks[0].process == 1)
|
|
// {
|
|
// channelStocks.ForEach(it => it.process = 2);
|
|
// }
|
|
//}
|
|
|
|
break;
|
|
// 抽屉关闭
|
|
case EventType.DRAWERCLOSE:
|
|
if (Status == 1)
|
|
{
|
|
//if (channelStocks != null && channelStocks[0] != null && 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;
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
|
|
|
|
private List<ChannelStock> _channelStocks;
|
|
|
|
public List<ChannelStock> ChannelStocks
|
|
{
|
|
get => _channelStocks;
|
|
set => SetProperty(ref _channelStocks, value);
|
|
}
|
|
|
|
private IEnumerable<IGrouping<int, ChannelStock>> enumerable;
|
|
private IEnumerator<IGrouping<int, ChannelStock>> enumerator;
|
|
|
|
|
|
public bool CanCloseDialog()
|
|
{
|
|
return Status == 0;
|
|
}
|
|
|
|
public void OnDialogClosed()
|
|
{
|
|
// 取消消息订阅
|
|
_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
|
|
}
|
|
|
|
public void OnDialogOpened(IDialogParameters parameters)
|
|
{
|
|
_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
|
|
List<ChannelStock> channelStocks = parameters.GetValue<List<ChannelStock>>("ChannelStocks");
|
|
ChannelStocks = channelStocks;
|
|
RequestData();
|
|
}
|
|
|
|
public void RequestData()
|
|
{
|
|
enumerable = ChannelStocks.GroupBy(cs => cs.DrawerNo, cs => cs);
|
|
enumerator = enumerable.GetEnumerator();
|
|
}
|
|
|
|
public DelegateCommand OpenDrawer
|
|
{
|
|
get => new DelegateCommand(() =>
|
|
{
|
|
if (Status == 0)
|
|
{
|
|
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 = new List<ChannelStock>();
|
|
for (int i = 0; i < channelStocks.Count; i++)
|
|
{
|
|
ChannelStock copy = TransExpV2<ChannelStock, ChannelStock>.Trans(channelStocks[i]);
|
|
singleChannels.Add(copy);
|
|
}
|
|
singleChannels = singleChannels
|
|
.GroupBy(it => new { it.DrawerNo, it.ColNo })
|
|
.Select(it =>
|
|
{
|
|
var ret = it.First();
|
|
ret.Quantity = it.Sum(it => it.Quantity);
|
|
ret.TakeQuantity = it.Sum(it => it.TakeQuantity);
|
|
return ret;
|
|
})
|
|
.ToList()
|
|
.FindAll(it => it.BoardType != 1);
|
|
|
|
|
|
// 发送取药数量
|
|
singleChannels.ForEach(it =>
|
|
{
|
|
_portUtil.TakeQuantity(DrawerNo, it.ColNo, it.TakeQuantity, it.Quantity - it.TakeQuantity);
|
|
});
|
|
_portUtil.WindowName = WindowName;
|
|
_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1;
|
|
_portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray();
|
|
_portUtil.Stocks = singleChannels.Select(it => it.Quantity).ToArray();
|
|
_portUtil.DrawerNo = DrawerNo;
|
|
_portUtil.Start();
|
|
}
|
|
|
|
private bool _isFinishClick = false;
|
|
// 完成按钮
|
|
public DelegateCommand TakeFinish
|
|
{
|
|
get => new DelegateCommand(() =>
|
|
{
|
|
if (!_isFinishClick)
|
|
{
|
|
_isFinishClick = true;
|
|
List<ChannelStock> record = ChannelStocks.ToList();
|
|
string InvoiceId = "SELF_" + CurrentTimeMillis();
|
|
|
|
|
|
bool bFlag =_mysqlHelper.SaveSelfTakeInfo(record);
|
|
if (bFlag)
|
|
{
|
|
// 更新屏显库存
|
|
//List<ChannelStock> singleChannels = record.FindAll(it => it.BoardType != 1);
|
|
List<ChannelStock> singleChannels = ChannelStocks.Where(it => it.BoardType == 5)
|
|
.GroupBy(it =>new { it.DrawerNo, it.ColNo })
|
|
.Select(it =>
|
|
{
|
|
var ret = it.First();
|
|
//ret.Quantity = it.Sum(itx => itx.Quantity);
|
|
//ret.AddQuantity = it.Sum(itx => itx.AddQuantity);
|
|
return ret;
|
|
}).ToList();
|
|
if(singleChannels!=null&&singleChannels.Count>0)
|
|
{
|
|
for (int i = 0; i < singleChannels.Count; i++)
|
|
{
|
|
List<ChannelStock> channel = _mysqlHelper.GetChannelStockList(singleChannels[i].DrawerNo, singleChannels[i].ColNo);
|
|
//将库位多批次的总库存数更新标签
|
|
_portUtil.WriteQuantity(channel[0].DrawerNo, channel[0].ColNo, channel.Sum(c => c.Quantity));
|
|
Thread.Sleep(500);
|
|
//如果最近效期的批次库存为0则重新绑定批次效期标签为库存不为0的近效期的标签
|
|
if (channel[0].Quantity == 0)
|
|
{
|
|
ChannelStock cs = channel.Where(cs => cs.Quantity > 0).OrderBy(it => it.EffDate).FirstOrDefault();
|
|
if (cs != null)
|
|
{
|
|
_portUtil.WriteChannelInfo(6, cs.EffDate == null ? "" : cs.EffDate, cs.DrawerNo, cs.ColNo);
|
|
Thread.Sleep(200);
|
|
_portUtil.WriteChannelInfo(5, cs.ManuNo, cs.DrawerNo, cs.ColNo);
|
|
Thread.Sleep(200);
|
|
_portUtil.ShowContent(cs.DrawerNo, cs.ColNo);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
AlertMsg alertMsg = new AlertMsg
|
|
{
|
|
Message = "操作完成,库存已更新",
|
|
Type = MsgType.SUCCESS,
|
|
};
|
|
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
|
}
|
|
else
|
|
{
|
|
logger.Info($"自选取药,库存更新失败");
|
|
AlertMsg alertMsg = new AlertMsg
|
|
{
|
|
Message = "库存更新失败",
|
|
Type = MsgType.ERROR,
|
|
};
|
|
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
|
}
|
|
Status = 0;
|
|
_isFinishClick = false;
|
|
RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
public long CurrentTimeMillis()
|
|
{
|
|
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
|
}
|
|
|
|
// 取消按钮
|
|
public DelegateCommand CancleTake
|
|
{
|
|
get => new DelegateCommand(() =>
|
|
{
|
|
_portUtil.ResetData();
|
|
Status = 0;
|
|
});
|
|
}
|
|
|
|
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 => true;
|
|
}
|
|
}
|