370 lines
16 KiB
C#
370 lines
16 KiB
C#
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;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Threading;
|
||
using DM_Weight.Models;
|
||
using DM_Weight.msg;
|
||
using DM_Weight.Port;
|
||
using DM_Weight.util;
|
||
using Newtonsoft.Json;
|
||
using System.Text.RegularExpressions;
|
||
using System.Windows.Controls;
|
||
using System.Threading.Channels;
|
||
using DM_Weight.Services;
|
||
using System.Linq.Expressions;
|
||
|
||
namespace DM_Weight.ViewModels
|
||
{
|
||
public class DrawerTakeDrugWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
|
||
{
|
||
private readonly ILog logger = LogManager.GetLogger(typeof(DrawerTakeDrugWindowViewModel));
|
||
|
||
private List<ChannelStock> _channelStocks = new List<ChannelStock>();
|
||
|
||
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 PortUtil _portUtil;
|
||
IEventAggregator _eventAggregator;
|
||
|
||
////private SqlSugarScope SqlSugarHelper.Db;
|
||
private SqlHelperService _mysqlHelper;
|
||
public DrawerTakeDrugWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator, SqlHelperService mysqlHelper)
|
||
{
|
||
_portUtil = portUtil;
|
||
_eventAggregator = eventAggregator;
|
||
//this.SqlSugarHelper.Db= sqlSugarScope;
|
||
_mysqlHelper = mysqlHelper;
|
||
}
|
||
|
||
void DoMyPrismEvent(DeviceMsg msg)
|
||
{
|
||
logger.Info($"抽屉取药页面_DoMyPrismEvent:{msg.WindowName}");
|
||
if (msg.WindowName == "DrawerTakeDrugWindow")
|
||
{
|
||
switch (msg.EventType)
|
||
{
|
||
// 抽屉打开
|
||
case EventType.DRAWEROPEN:
|
||
if (Status == 1)
|
||
{
|
||
Status = 2;
|
||
}
|
||
|
||
break;
|
||
// 抽屉关闭
|
||
case EventType.DRAWERCLOSE:
|
||
if (Status == 2)
|
||
{
|
||
Status = 3;
|
||
}
|
||
break;
|
||
// 数量变化
|
||
case EventType.UPDATEQUANTITY:
|
||
if (Status == 2)
|
||
{
|
||
ChannelStocks.ForEach(it => it.TakeQuantity = 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
|
||
);
|
||
}
|
||
|
||
|
||
public DelegateCommand OpenDrawer
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
if (Status == 0)
|
||
{
|
||
Status = 1;
|
||
_portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉");
|
||
|
||
//var groupQuery = (from t in ChannelStocks
|
||
// group t by new { t.ColNo, t.BoardType }
|
||
// into grp
|
||
// select new { grp.Key.ColNo, grp.Key.BoardType, quantity = grp.Sum(t => t.Quantity) }).ToList();
|
||
|
||
List<ChannelStock> singleChannels = ChannelStocks.FindAll(it => it.BoardType != 1);
|
||
|
||
_portUtil.WindowName = "DrawerTakeDrugWindow";
|
||
_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1;
|
||
_portUtil.ColNos = new int[] { };// groupQuery.Select(it => it.ColNo).ToArray();
|
||
_portUtil.DrawerNo = DrawerNo;
|
||
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, () => _portUtil.Start());
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
private bool _isFinishClick = false;
|
||
|
||
// 完成按钮
|
||
public DelegateCommand TakeFinish
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
if (!_isFinishClick)
|
||
{
|
||
try
|
||
{
|
||
_isFinishClick = true;
|
||
List<ChannelStock> record = ChannelStocks.FindAll(it => it.TakeQuantity > 0).ToList();
|
||
logger.Info("点击完成按钮,库位数据:" + JsonConvert.SerializeObject(record));
|
||
if (record.Count > 0)
|
||
{
|
||
logger.Info("record.Count>0");
|
||
string InvoiceId = "DRAWER_" + CurrentTimeMillis();
|
||
//保存数据库
|
||
bool bFlag = _mysqlHelper.SaveDrawerTakeDrug(record);
|
||
if (bFlag)
|
||
{
|
||
logger.Info("更新屏显库存singleChannels");
|
||
// 更新屏显库存
|
||
//List<ChannelStock> singleChannels = record.FindAll(it => it.BoardType != 1);
|
||
|
||
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.Where(it => it.BoardType != 1)
|
||
.GroupBy(it => it.ColNo)
|
||
.Select(it =>
|
||
{
|
||
var ret = it.First();
|
||
ret.Quantity = it.Sum(itx => itx.Quantity);
|
||
ret.TakeQuantity = it.Sum(itx => itx.TakeQuantity);
|
||
return ret;
|
||
}).OrderBy(it => it.Quantity).ToList();
|
||
singleChannels = singleChannels.Where(it => it.TakeQuantity > 0).ToList();
|
||
if ((singleChannels.Count > 0 ? singleChannels[0].BoardType : 1) == 5)
|
||
{
|
||
//将库位多批次的总库存数更新标签
|
||
//_portUtil.WriteQuantity(singleChannels[0].DrawerNo, singleChannels[0].ColNo, singleChannels.Sum(it => it.Quantity) - singleChannels.Sum(it => it.TakeQuantity));
|
||
|
||
singleChannels.ForEach(it =>
|
||
{
|
||
_portUtil.WriteQuantityAsync(it.DrawerNo, it.ColNo, it.Quantity - it.TakeQuantity);
|
||
});
|
||
|
||
Thread.Sleep(500);
|
||
//string varCS = from cs in ChannelStocks orderby cs.EffDate group cs by cs.ColNo into g orderby g.Key select g.;
|
||
|
||
List<ChannelStock> groupCS = ChannelStocks
|
||
.GroupBy(it => it.ColNo)
|
||
.Select(it =>
|
||
{
|
||
var ret = it.First();
|
||
return ret;
|
||
}).ToList();
|
||
|
||
if (groupCS != null)
|
||
{
|
||
for (int i = 0; i < groupCS.Count; i++)
|
||
{
|
||
//全部取出
|
||
if (groupCS[i].Quantity == groupCS[i].TakeQuantity)
|
||
{
|
||
//查询该库位下非0的近效期及批次重新绑定
|
||
//更新绑定的效期、批次
|
||
ChannelStock newBind = _mysqlHelper.GetMinManuEffDate(groupCS[i]);
|
||
if (newBind != null)
|
||
{
|
||
_portUtil.WriteChannelInfo(6, newBind.EffDate == null ? "" : newBind.EffDate, newBind.DrawerNo, newBind.ColNo);
|
||
Thread.Sleep(200);
|
||
_portUtil.WriteChannelInfo(5, newBind.ManuNo == null ? "" : newBind.ManuNo, newBind.DrawerNo, newBind.ColNo);
|
||
Thread.Sleep(200);
|
||
_portUtil.ShowContent(newBind.DrawerNo, newBind.ColNo);
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
logger.Info("更新屏显库存singleChannels_完成");
|
||
RequestData();
|
||
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;
|
||
}
|
||
else
|
||
{
|
||
_isFinishClick = false;
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "没有填写取药数量",
|
||
Type = MsgType.ERROR
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_isFinishClick = false;
|
||
logger.Info($"抽屉取药保存失败{ex.Message}");
|
||
}
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
// 取消按钮
|
||
public DelegateCommand CancleTake
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
_portUtil.ResetData();
|
||
Status = 0;
|
||
});
|
||
}
|
||
|
||
public bool KeepAlive => true;
|
||
|
||
public long CurrentTimeMillis()
|
||
{
|
||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
||
}
|
||
|
||
public void FindDrawerCount()
|
||
{
|
||
//int count = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cs => cs.DrawerType != 3)
|
||
// .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).GroupBy(cs => cs.DrawerNo).Select(cs => SqlFunc.AggregateCount(cs.DrawerNo)).Count();
|
||
int count = _mysqlHelper.FindDrawerCount();
|
||
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)
|
||
// .Where(cs => cs.DrawerNo == DrawerNo)
|
||
// .Where(cs => cs.DrugId != null)
|
||
// .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
|
||
// .Where(cs => cs.DrawerType == 1)
|
||
// .Where(cs => cs.Quantity > 0)
|
||
// .OrderBy(cs => cs.ColNo)
|
||
// .OrderBy(cs => cs.EffDate)
|
||
// .ToList();
|
||
List<ChannelStock> queryData = _mysqlHelper.GetChannelStockByDrawer(DrawerNo);
|
||
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);
|
||
}
|
||
}
|
||
}
|