590 lines
24 KiB
C#
590 lines
24 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;
|
||
|
||
namespace DM_Weight.ViewModels
|
||
{
|
||
public class DrawerTakeDrugWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
|
||
{
|
||
private readonly ILog logger = LogManager.GetLogger(typeof(DrawerTakeDrugWindowViewModel));
|
||
#region 抽屉权限
|
||
/// <summary>
|
||
/// 1号抽屉
|
||
/// </summary>
|
||
private bool _Drawer1 = false;
|
||
public bool Drawer1
|
||
{
|
||
get { return _Drawer1; }
|
||
set { SetProperty(ref _Drawer1, value); }
|
||
}
|
||
/// <summary>
|
||
/// 2号抽屉
|
||
/// </summary>
|
||
private bool _Drawer2 = false;
|
||
public bool Drawer2
|
||
{
|
||
get { return _Drawer2; }
|
||
set { SetProperty(ref _Drawer2, value); }
|
||
}
|
||
/// <summary>
|
||
/// 3号抽屉
|
||
/// </summary>
|
||
private bool _Drawer3 = false;
|
||
public bool Drawer3
|
||
{
|
||
get { return _Drawer3; }
|
||
set { SetProperty(ref _Drawer3, value); }
|
||
}
|
||
/// <summary>
|
||
/// 4号抽屉
|
||
/// </summary>
|
||
private bool _Drawer4 = false;
|
||
public bool Drawer4
|
||
{
|
||
get { return _Drawer4; }
|
||
set { SetProperty(ref _Drawer4, value); }
|
||
}
|
||
/// <summary>
|
||
/// 5号抽屉
|
||
/// </summary>
|
||
private bool _Drawer5 = false;
|
||
public bool Drawer5
|
||
{
|
||
get { return _Drawer5; }
|
||
set { SetProperty(ref _Drawer5, value); }
|
||
}
|
||
/// <summary>
|
||
/// 6号抽屉
|
||
/// </summary>
|
||
private bool _Drawer6 = false;
|
||
public bool Drawer6
|
||
{
|
||
get { return _Drawer6; }
|
||
set { SetProperty(ref _Drawer6, value); }
|
||
}
|
||
/// <summary>
|
||
/// 7号抽屉
|
||
/// </summary>
|
||
private bool _Drawer7 = false;
|
||
public bool Drawer7
|
||
{
|
||
get { return _Drawer7; }
|
||
set { SetProperty(ref _Drawer7, value); }
|
||
}
|
||
/// <summary>
|
||
/// 8号抽屉
|
||
/// </summary>
|
||
private bool _Drawer8 = false;
|
||
public bool Drawer8
|
||
{
|
||
get { return _Drawer8; }
|
||
set { SetProperty(ref _Drawer8, value); }
|
||
}
|
||
#endregion
|
||
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;
|
||
|
||
|
||
public DrawerTakeDrugWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
|
||
{
|
||
_portUtil = portUtil;
|
||
_eventAggregator = eventAggregator;
|
||
}
|
||
|
||
void DoMyPrismEvent(DeviceMsg msg)
|
||
{
|
||
if (msg.WindowName == "DrawerTakeDrugWindow")
|
||
{
|
||
switch (msg.EventType)
|
||
{
|
||
// 抽屉打开
|
||
case EventType.DRAWEROPEN:
|
||
if (Status == 1)
|
||
{
|
||
Status = 2;
|
||
}
|
||
|
||
break;
|
||
// 抽屉关闭
|
||
case EventType.DRAWERCLOSE:
|
||
if (Status == 2)
|
||
{
|
||
Status = 3;
|
||
}
|
||
//保存库存
|
||
SaveStock();
|
||
RequestData();
|
||
Status = 0;
|
||
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 + "号抽屉");
|
||
|
||
IEnumerable<string> strDrugName = ChannelStocks.Select(cs => cs.DrugInfo.DrugName);
|
||
string strNames = string.Empty;
|
||
foreach (string name in strDrugName)
|
||
{
|
||
if (!strNames.Contains(name))
|
||
{
|
||
strNames += name + ";";
|
||
}
|
||
}
|
||
int sumQuantity = ChannelStocks.Sum(cs => cs.TakeQuantity);
|
||
_portUtil.SpeakAsync($"正在打开 {DrawerNo} 号抽屉,请取药 {strNames} ,数量共计 {sumQuantity}");
|
||
|
||
List<ChannelStock> singleChannels = ChannelStocks.FindAll(it => it.BoardType != 1);
|
||
|
||
_portUtil.WindowName = "DrawerTakeDrugWindow";
|
||
_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1;
|
||
_portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray();
|
||
_portUtil.DrawerNo = DrawerNo;
|
||
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, () => _portUtil.Start())
|
||
;
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
//保存库存
|
||
private void SaveStock()
|
||
{
|
||
try
|
||
{
|
||
List<ChannelStock> record = ChannelStocks.FindAll(it => it.TakeQuantity != 0).ToList();
|
||
if (record.Count > 0)
|
||
{
|
||
string InvoiceId = "DRAWER_" + CurrentTimeMillis();
|
||
var f = SqlSugarHelper.Db.UseTran(() =>
|
||
{
|
||
|
||
for (int i = 0; i < record.Count; i++)
|
||
{
|
||
ChannelStock it = record[i];
|
||
it.ManuNo = it.ManuNo;
|
||
it.EffDate = it.EffDate;
|
||
|
||
|
||
// 更新数据 库存信息
|
||
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();
|
||
|
||
logger.Info($"库存已更新{it.DrugId},{it.Quantity - it.TakeQuantity}");
|
||
}
|
||
});
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
logger.Info($"保存库存异常{ex.Message}");
|
||
}
|
||
}
|
||
//private bool _isFinishClick = false;
|
||
|
||
//// 完成按钮
|
||
//public DelegateCommand TakeFinish
|
||
//{
|
||
// get => new DelegateCommand(() =>
|
||
// {
|
||
// if (!_isFinishClick)
|
||
// {
|
||
// _isFinishClick = true;
|
||
// List<ChannelStock> record = ChannelStocks.FindAll(it => it.TakeQuantity != 0).ToList();
|
||
// logger.Info("点击完成按钮,库位数据:" + JsonConvert.SerializeObject(record));
|
||
// if (record.Count > 0)
|
||
// {
|
||
// string InvoiceId = "DRAWER_" + 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> nowChannels = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
// .Where(cs => cs.MachineId.Equals(it.MachineId))
|
||
// .Where(cs => cs.DrugId.Equals(it.DrugId))
|
||
// .Where(cs => cs.DrawerType == 1)
|
||
// .ToList();
|
||
|
||
// // 保存数据 出库记录
|
||
// 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,
|
||
// InvoiceId = InvoiceId
|
||
// //,StockQuantity = nowChannels.Sum(it => it.Quantity)
|
||
// }).ExecuteCommand();
|
||
|
||
|
||
// //保存账册
|
||
// SqlSugarHelper.Db.Insertable(new AccountBookG2()
|
||
// {
|
||
// DrugId = it.DrugId,
|
||
// Type = 2,
|
||
// Department = ConfigurationManager.AppSettings["department"].ToString(),
|
||
// InvoiceNo = InvoiceId,
|
||
// ManuNo = it.ManuNo,
|
||
// EffDate = it.EffDate,
|
||
// OutQuantity = it.TakeQuantity,
|
||
// UserId1 = HomeWindowViewModel.Operator?.Id,
|
||
// UserId2 = HomeWindowViewModel.Reviewer?.Id,
|
||
// MachineId = ConfigurationManager.AppSettings["machineId"].ToString(),
|
||
// CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
|
||
// CreateTime = DateTime.Now
|
||
|
||
// }).ExecuteCommand();
|
||
// //修改凌晨生成的日结存数据
|
||
// AccountBookG2 accountBookG2Day = SqlSugarHelper.Db.Queryable<AccountBookG2>()
|
||
// .Where(ab => ab.MachineId.Equals(it.MachineId))
|
||
// .Where(ab => ab.Type == 3)
|
||
// .Where(ab => ab.DrugId == it.DrugId)
|
||
// .Where(ab => ab.ManuNo == it.ManuNo)
|
||
// .Where(ab => ab.CreateDate == DateTime.Now.ToString("yyyy-MM-dd")).First();
|
||
// if (accountBookG2Day != null)
|
||
// {
|
||
// accountBookG2Day.ManuStock = accountBookG2Day.ManuStock - it.TakeQuantity;
|
||
// SqlSugarHelper.Db.Updateable(accountBookG2Day).ExecuteCommand();
|
||
// }
|
||
// else
|
||
// {
|
||
// //生成日结存时可能没有该库位的绑定信息,需要写入日结存
|
||
// int iDayResult = SqlSugarHelper.Db.Insertable(new AccountBookG2()
|
||
// {
|
||
// DrugId = it.DrugId,
|
||
// Type = 3,
|
||
// ManuNo = it.ManuNo,
|
||
// EffDate = it.EffDate,
|
||
// YQuantity = 0,
|
||
// ManuStock = it.TakeQuantity,
|
||
// TotalStock = it.TakeQuantity,
|
||
// UserId1 = HomeWindowViewModel.Operator?.Id,
|
||
// UserId2 = HomeWindowViewModel.Reviewer?.Id,
|
||
// MachineId = ConfigurationManager.AppSettings["machineId"].ToString(),
|
||
// CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
|
||
// InvoiceNo = "日结存"
|
||
// }).ExecuteCommand();
|
||
// if (iDayResult <= 0)
|
||
// {
|
||
// logger.Info($"未写入日结存数据{it.DrugId}-{it.ManuNo}-{it.EffDate}-{it.AddQuantity}");
|
||
// }
|
||
// }
|
||
// //修改凌晨生成的总结存数据
|
||
// AccountBookG2 accountBookG2Total = SqlSugarHelper.Db.Queryable<AccountBookG2>()
|
||
// .Where(ab => ab.MachineId.Equals(it.MachineId))
|
||
// .Where(ab => ab.Type == 4)
|
||
// .Where(ab => ab.DrugId == it.DrugId)
|
||
// .Where(ab => ab.CreateDate == DateTime.Now.ToString("yyyy-MM-dd")).First();
|
||
// if (accountBookG2Total != null)
|
||
// {
|
||
// accountBookG2Total.TotalStock = accountBookG2Total.TotalStock - it.TakeQuantity;
|
||
// SqlSugarHelper.Db.Updateable(accountBookG2Total).ExecuteCommand();
|
||
// }
|
||
// else
|
||
// {
|
||
// //生成总结存时可能没有该库位的绑定信息,需要写入总结存
|
||
// int iTotalResult = SqlSugarHelper.Db.Insertable(new AccountBookG2()
|
||
// {
|
||
// DrugId = it.DrugId,
|
||
// Type = 4,
|
||
// YQuantity = 0,
|
||
// ManuStock = it.TakeQuantity,
|
||
// TotalStock = it.TakeQuantity,
|
||
// UserId1 = HomeWindowViewModel.Operator?.Id,
|
||
// UserId2 = HomeWindowViewModel.Reviewer?.Id,
|
||
// MachineId = ConfigurationManager.AppSettings["machineId"].ToString(),
|
||
// CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
|
||
// InvoiceNo = "总结存"
|
||
// }).ExecuteCommand();
|
||
// if (iTotalResult <= 0)
|
||
// {
|
||
// logger.Info($"未写入总结存数据{it.DrugId}-{it.AddQuantity}");
|
||
// }
|
||
// }
|
||
|
||
|
||
// logger.Info($"抽屉取药保存->库位【{it.DrawerNo}-{it.ColNo}】取出药品【{it.DrugInfo.DrugName}】个数【{it.TakeQuantity}】,取药前库存【{it.Quantity}】");
|
||
// }
|
||
// return true;
|
||
// });
|
||
// if (f.Data)
|
||
// {
|
||
// RequestData();
|
||
// 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;
|
||
// }
|
||
// else
|
||
// {
|
||
// _isFinishClick = false;
|
||
// AlertMsg alertMsg = new AlertMsg
|
||
// {
|
||
// Message = "没有填写取药数量",
|
||
// Type = MsgType.ERROR
|
||
// };
|
||
// _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
// }
|
||
|
||
// }
|
||
|
||
// });
|
||
//}
|
||
|
||
//// 取消按钮
|
||
//public DelegateCommand CancleTake
|
||
//{
|
||
// get => new DelegateCommand(() =>
|
||
// {
|
||
// _portUtil.ResetData();
|
||
// Status = 0;
|
||
// });
|
||
//}
|
||
|
||
public long CurrentTimeMillis()
|
||
{
|
||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
||
}
|
||
|
||
public bool KeepAlive => false;
|
||
|
||
public void FindDrawerCount()
|
||
{
|
||
int count = 0;
|
||
if (ConfigurationManager.AppSettings["MultiBatch"].ToString().Equals("1"))
|
||
{
|
||
count = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cs => cs.DrawerType != 3)
|
||
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM3")).GroupBy(cs => cs.DrawerNo).Select(cs => SqlFunc.AggregateCount(cs.DrawerNo)).Count();
|
||
}
|
||
else
|
||
{
|
||
count = SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.DrawerType != 3)
|
||
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM3")).GroupBy(cs => cs.DrawerNo).Select(cs => SqlFunc.AggregateCount(cs.DrawerNo)).Count();
|
||
}
|
||
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"] ?? "DM3"))
|
||
.Where(cs => cs.DrawerType == 1)
|
||
.Where(cs => cs.Quantity > 0)
|
||
.OrderBy(cs => cs.ColNo)
|
||
.ToList();
|
||
ChannelStocks = queryData;
|
||
}
|
||
|
||
//接收导航传过来的参数 现在是在此处初始化了表格数据
|
||
public void OnNavigatedTo(NavigationContext navigationContext)
|
||
{
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
|
||
FindDrawerCount();
|
||
RequestData();
|
||
GetDrawer();
|
||
}
|
||
/// <summary>
|
||
/// 查询当前用户对应角色的抽屉权限
|
||
/// </summary>
|
||
void GetDrawer()
|
||
{
|
||
string drawerAuthority = HomeWindowViewModel.Operator.Role.drawer;
|
||
if (!string.IsNullOrEmpty(drawerAuthority))
|
||
{
|
||
string[] iList = drawerAuthority.Split(',');
|
||
foreach (string i in iList)
|
||
{
|
||
switch (i)
|
||
{
|
||
case "1":
|
||
Drawer1 = true;
|
||
break;
|
||
case "2":
|
||
Drawer2 = true;
|
||
break;
|
||
case "3":
|
||
Drawer3 = true;
|
||
break;
|
||
case "4":
|
||
Drawer4 = true;
|
||
break;
|
||
case "5":
|
||
Drawer5 = true;
|
||
break;
|
||
case "6":
|
||
Drawer6 = true;
|
||
break;
|
||
case "7":
|
||
Drawer7 = true;
|
||
break;
|
||
case "8":
|
||
Drawer8 = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
/// <param name="navigationContext"></param>
|
||
/// <returns></returns>
|
||
|
||
//每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建
|
||
public bool IsNavigationTarget(NavigationContext navigationContext)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
//这个方法用于拦截请求, 不让走
|
||
public void OnNavigatedFrom(NavigationContext navigationContext)
|
||
{
|
||
// 取消消息订阅
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
|
||
}
|
||
}
|
||
}
|