454 lines
18 KiB
C#
454 lines
18 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.Configuration;
|
||
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;
|
||
|
||
namespace DM_Weight.ViewModels
|
||
{
|
||
public class ReturnDrugDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime
|
||
{
|
||
|
||
private readonly ILog logger = LogManager.GetLogger(typeof(ReturnDrugDialogViewModel));
|
||
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 = "ReturnDrug1Window";
|
||
|
||
private PortUtil _portUtil;
|
||
IEventAggregator _eventAggregator;
|
||
|
||
private MachineRecord _machineRecord;
|
||
public MachineRecord MachineRecord
|
||
{
|
||
get { return _machineRecord; }
|
||
set { SetProperty(ref _machineRecord, value); }
|
||
}
|
||
|
||
public ReturnDrugDialogViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
|
||
{
|
||
_portUtil = portUtil;
|
||
_eventAggregator = eventAggregator;
|
||
}
|
||
|
||
void DoMyPrismEvent(DeviceMsg msg)
|
||
{
|
||
|
||
if (msg.WindowName.Equals(WindowName))
|
||
{
|
||
|
||
switch (msg.EventType)
|
||
{
|
||
// 抽屉打开
|
||
case EventType.DRAWEROPEN:
|
||
|
||
|
||
if (Status == 1)
|
||
{
|
||
Status = 2;
|
||
}
|
||
|
||
break;
|
||
// 抽屉关闭
|
||
case EventType.DRAWERCLOSE:
|
||
if (Status == 2)
|
||
{
|
||
Status = 3;
|
||
}
|
||
break;
|
||
// 数量变化
|
||
case EventType.UPDATEQUANTITY:
|
||
try
|
||
{
|
||
if (Status == 2)
|
||
{
|
||
ReturnQuantity = msg.Quantitys[ChannelStock.ColNo - 1];
|
||
|
||
logger.Info($"抽屉【{ChannelStock.DrawerNo}】库位药品数量【{msg.Quantitys}】");
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
logger.Error(e);
|
||
}
|
||
|
||
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 ChannelStock _channelStock;
|
||
|
||
public ChannelStock ChannelStock
|
||
{
|
||
get => _channelStock;
|
||
set => SetProperty(ref _channelStock, value);
|
||
}
|
||
|
||
private int _returnQuantity;
|
||
|
||
public int ReturnQuantity
|
||
{
|
||
get => _returnQuantity;
|
||
set
|
||
{
|
||
if (value < 0)
|
||
{
|
||
throw new ArgumentException("还药数量不能是负数");
|
||
}
|
||
if (value > MachineRecord.CanReturnQuantity)
|
||
{
|
||
throw new ArgumentException("还药数量超出");
|
||
}
|
||
SetProperty(ref _returnQuantity, value);
|
||
}
|
||
}
|
||
|
||
string drawerAuthority = string.Empty;
|
||
string[] drawerAuthorityList;
|
||
|
||
public bool CanCloseDialog()
|
||
{
|
||
return Status == 0;
|
||
}
|
||
|
||
public void OnDialogClosed()
|
||
{
|
||
// 取消消息订阅
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
|
||
}
|
||
|
||
public void OnDialogOpened(IDialogParameters parameters)
|
||
{
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
|
||
|
||
MachineRecord _record = parameters.GetValue<MachineRecord>("record");
|
||
MachineRecord = _record;
|
||
ReturnQuantity = MachineRecord.CanReturnQuantity;
|
||
|
||
RequestData();
|
||
}
|
||
|
||
public void RequestData()
|
||
{
|
||
List<ChannelStock> queryData = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
.Where(cs => cs.DrugId == MachineRecord.DrugId)
|
||
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
|
||
.WhereIF(MachineRecord.ManuNo != null, cs => cs.ManuNo == MachineRecord.ManuNo)
|
||
.OrderBy(cs => cs.DrawerNo)
|
||
.OrderBy(cs => cs.ColNo)
|
||
.ToList();
|
||
ChannelStocks = queryData;
|
||
if (ChannelStocks.Count > 0)
|
||
{
|
||
ChannelStock = ChannelStocks[0];
|
||
}
|
||
}
|
||
|
||
public DelegateCommand OpenDrawer
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
//if (HomeWindowViewModel.Operator.Role != null && HomeWindowViewModel.Operator.Role.RoleName != "管理员")
|
||
//{
|
||
// //查看当前用户是否有所在药品抽屉的权限;1-2层所有人能开,其他6层管理员才能开
|
||
// bool bDrawer = ChannelStocks.Select(it => it.DrawerNo).Where(n => n > 3).Any();
|
||
// if (bDrawer)
|
||
// {
|
||
// AlertMsg alertMsg = new AlertMsg
|
||
// {
|
||
// Message = "当前用户没有打开抽屉的权限!",
|
||
// Type = MsgType.ERROR,
|
||
// };
|
||
// _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
// return;
|
||
// }
|
||
|
||
//}
|
||
|
||
bool hasAuthority = Array.Exists(drawerAuthorityList, element => element == ChannelStock.DrawerNo.ToString());
|
||
if (!hasAuthority)
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = $"当前用户没有打开{ChannelStock.DrawerNo}号抽屉的权限!",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
return;
|
||
}
|
||
|
||
if (ChannelStock != null)
|
||
{
|
||
Status = 1;
|
||
|
||
//_portUtil.SpeakAsync("正在打开" + ChannelStock.DrawerNo + "号抽屉");
|
||
|
||
|
||
_portUtil.SpeakAsync($"正在打开 {ChannelStock.DrawerNo} 号抽屉,请加药 {MachineRecord.DrugInfo.DrugName} ,数量共计 {ReturnQuantity}个");
|
||
|
||
_portUtil.WindowName = WindowName;
|
||
_portUtil.BoardType = ChannelStock.BoardType;
|
||
_portUtil.ColNos = new int[] { ChannelStock.ColNo };
|
||
_portUtil.DrawerNo = ChannelStock.DrawerNo;
|
||
_portUtil.Start();
|
||
}
|
||
else
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "请选择退还药品要放入的库位!",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
}
|
||
|
||
}, () => Status == 0);
|
||
}
|
||
|
||
|
||
private bool _isFinishClick = false;
|
||
|
||
public bool IsFinishClick { get => _isFinishClick; set => SetProperty(ref _isFinishClick, value); }
|
||
// 完成按钮
|
||
public DelegateCommand TakeFinish
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
IsFinishClick = true;
|
||
string InvoiceId = "RETURN_" + CurrentTimeMillis();
|
||
var f = SqlSugarHelper.Db.UseTran(() =>
|
||
{
|
||
|
||
// 更新数据 库存信息
|
||
SqlSugarHelper.Db.Updateable(new ChannelStock()
|
||
{
|
||
Quantity = ChannelStock.Quantity + ReturnQuantity,
|
||
Id = ChannelStock.Id,
|
||
}).UpdateColumns(it => new { it.Quantity }).ExecuteCommand();
|
||
|
||
// 获取更新完库存后的药品库存
|
||
List<ChannelStock> nowChannels = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
.Where(cs => cs.MachineId.Equals(ChannelStock.MachineId))
|
||
.Where(cs => cs.DrugId.Equals(ChannelStock.DrugId))
|
||
.Where(cs => cs.DrawerType == 1)
|
||
.ToList();
|
||
// 更新数据 取药记录 设置还药数量、状态
|
||
SqlSugarHelper.Db.Updateable(new MachineRecord()
|
||
{
|
||
ReturnQuantity1 = MachineRecord.ReturnQuantity1 + ReturnQuantity,
|
||
Id = MachineRecord.Id,
|
||
Status = (MachineRecord.CanReturnQuantity - ReturnQuantity) == 0 ? 2 : 1,
|
||
}).UpdateColumns(it => new { it.ReturnQuantity1, it.Status }).ExecuteCommand();
|
||
|
||
// 保存数据 还药记录
|
||
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
||
{
|
||
MachineId = ChannelStock.MachineId,
|
||
DrawerNo = ChannelStock.DrawerNo,
|
||
ColNo = ChannelStock.ColNo,
|
||
DrugId = ChannelStock.DrugId,
|
||
ManuNo = ChannelStock.ManuNo,
|
||
EffDate = !String.IsNullOrEmpty(ChannelStock.EffDate) ? DateTime.ParseExact(ChannelStock.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
|
||
Operator = HomeWindowViewModel.Operator?.Id,
|
||
OperationTime = DateTime.Now,
|
||
Quantity = ReturnQuantity,
|
||
Type = 31,
|
||
InvoiceId = InvoiceId,
|
||
GetId = MachineRecord.Id
|
||
//,StockQuantity = nowChannels.Sum(it => it.Quantity)
|
||
}).ExecuteCommand();
|
||
|
||
//保存账册
|
||
SqlSugarHelper.Db.Insertable(new AccountBookG2()
|
||
{
|
||
DrugId = ChannelStock.DrugId,
|
||
Type = 1,
|
||
Department = ConfigurationManager.AppSettings["department"].ToString(),
|
||
InvoiceNo = InvoiceId,
|
||
ManuNo = ChannelStock.ManuNo,
|
||
EffDate = ChannelStock.EffDate,
|
||
AddQuantity = ReturnQuantity,
|
||
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(ChannelStock.MachineId))
|
||
.Where(ab => ab.Type == 3)
|
||
.Where(ab => ab.DrugId == ChannelStock.DrugId)
|
||
.Where(ab => ab.ManuNo == ChannelStock.ManuNo)
|
||
.Where(ab => ab.CreateDate == DateTime.Now.ToString("yyyy-MM-dd")).First();
|
||
if (accountBookG2Day != null)
|
||
{
|
||
accountBookG2Day.ManuStock = accountBookG2Day.ManuStock + ChannelStock.ReturnQuantity;
|
||
SqlSugarHelper.Db.Updateable(accountBookG2Day).ExecuteCommand();
|
||
}
|
||
else
|
||
{
|
||
//生成日结存时可能没有该库位的绑定信息,需要写入日结存
|
||
int iDayResult = SqlSugarHelper.Db.Insertable(new AccountBookG2()
|
||
{
|
||
DrugId = ChannelStock.DrugId,
|
||
Type = 3,
|
||
ManuNo = ChannelStock.ManuNo,
|
||
EffDate = ChannelStock.EffDate,
|
||
YQuantity = 0,
|
||
ManuStock = ChannelStock.ReturnQuantity,
|
||
TotalStock = ChannelStock.ReturnQuantity,
|
||
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($"未写入日结存数据{ChannelStock.DrugId}-{ChannelStock.ManuNo}-{ChannelStock.EffDate}-{ChannelStock.AddQuantity}");
|
||
}
|
||
}
|
||
//修改凌晨生成的总结存数据
|
||
AccountBookG2 accountBookG2Total = SqlSugarHelper.Db.Queryable<AccountBookG2>()
|
||
.Where(ab => ab.MachineId.Equals(ChannelStock.MachineId))
|
||
.Where(ab => ab.Type == 4)
|
||
.Where(ab => ab.DrugId == ChannelStock.DrugId)
|
||
.Where(ab => ab.CreateDate == DateTime.Now.ToString("yyyy-MM-dd")).First();
|
||
if (accountBookG2Total != null)
|
||
{
|
||
accountBookG2Total.TotalStock = accountBookG2Total.TotalStock + ChannelStock.ReturnQuantity;
|
||
SqlSugarHelper.Db.Updateable(accountBookG2Total).ExecuteCommand();
|
||
}
|
||
else
|
||
{
|
||
//生成总结存时可能没有该库位的绑定信息,需要写入总结存
|
||
int iTotalResult = SqlSugarHelper.Db.Insertable(new AccountBookG2()
|
||
{
|
||
DrugId = ChannelStock.DrugId,
|
||
Type = 4,
|
||
YQuantity = 0,
|
||
ManuStock = ChannelStock.ReturnQuantity,
|
||
TotalStock = ChannelStock.ReturnQuantity,
|
||
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($"未写入总结存数据{ChannelStock.DrugId}-{ChannelStock.AddQuantity}");
|
||
}
|
||
}
|
||
|
||
|
||
return true;
|
||
});
|
||
if (f.Data)
|
||
{
|
||
// 更新屏显库存
|
||
if (ChannelStock.BoardType == 5)
|
||
{
|
||
_portUtil.WriteQuantity(ChannelStock.DrawerNo, ChannelStock.ColNo, ChannelStock.Quantity + ReturnQuantity);
|
||
}
|
||
|
||
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.OK));
|
||
|
||
}, () => !IsFinishClick && ReturnQuantity > 0).ObservesProperty(() => IsFinishClick).ObservesProperty(() => ReturnQuantity);
|
||
}
|
||
|
||
public long CurrentTimeMillis()
|
||
{
|
||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
||
}
|
||
|
||
// 取消按钮
|
||
public DelegateCommand CancleTake
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
IsFinishClick = false;
|
||
_portUtil.ResetData();
|
||
Status = 0;
|
||
});
|
||
}
|
||
|
||
public DelegateCommand BtnCloseCommand
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
// 关闭当前窗口
|
||
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
|
||
}, () => Status == 0).ObservesProperty(() => Status);
|
||
}
|
||
|
||
public bool KeepAlive => false;
|
||
}
|
||
}
|
||
|