HKC/DM_Weight/ViewModels/InvoiceAddDialogViewModel.cs

470 lines
20 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using DM_Weight.Views;
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;
namespace DM_Weight.ViewModels
{
public class InvoiceAddDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime
{
private readonly ILog logger = LogManager.GetLogger(typeof(InvoiceAddDialogViewModel));
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 = "InvoiceAddWindow";
private PortUtil _portUtil;
IEventAggregator _eventAggregator;
public InvoiceAddDialogViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
{
_portUtil = portUtil;
_eventAggregator = eventAggregator;
}
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:
if (Status == 1)
{
if (channelStocks[0].process == 1)
{
channelStocks.ForEach(it => it.process = 2);
}
}
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;
}
}
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 Invoice? _invoic;
public Invoice? Invoice
{
get => _invoic;
set => SetProperty(ref _invoic, 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");
Invoice o = parameters.GetValue<Invoice>("invoice");
Invoice = o;
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)
{
//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;
// }
//}
enumerator.MoveNext();
Status = 1;
OpenOneByOne();
}
});
}
private async void OpenOneByOne()
{
IGrouping<int, ChannelStock> grouping = enumerator.Current;
int DrawerNo = grouping.Key;
List<ChannelStock> channelStocks = grouping.ToList();
channelStocks.ForEach(it => it.process = 1);
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.AddQuantity);
_portUtil.SpeakAsync($"正在打开 {DrawerNo} 号抽屉,请加药 {strNames} ,数量共计 {sumQuantity}个");
List<ChannelStock> singleChannels = channelStocks.FindAll(it => it.BoardType != 1);
_portUtil.WindowName = WindowName;
_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1;
_portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray();
_portUtil.DrawerNo = DrawerNo;
if ((singleChannels.Count > 0 ? singleChannels[0].BoardType : 1) == 5)
{
// 发送加药数量
singleChannels.ForEach(it =>
{
_portUtil.TakeQuantity(DrawerNo, it.ColNo, it.AddQuantity, it.Quantity + it.AddQuantity);
});
}
if ((singleChannels.Count > 0 ? singleChannels[0].BoardType : 1) == 6)
{
for (int i = 0; i < singleChannels.Count; i++)
{
ChannelStock it = singleChannels[i];
_portUtil.ClearCount(it.DrawerNo, it.ColNo);
await Task.Delay(50);
}
}
_portUtil.Start();
}
private bool _isFinishClick = false;
// 完成按钮
public DelegateCommand TakeFinish
{
get => new DelegateCommand(async () =>
{
if (!_isFinishClick)
{
_isFinishClick = true;
List<ChannelStock> record = ChannelStocks.ToList();
string InvoiceId = Invoice.InvoiceNo;
var f = SqlSugarHelper.Db.UseTran(() =>
{
SqlSugarHelper.Db.Updateable(new InOutInvoice()
{
Status = 1,
InvoiceNo = Invoice.InvoiceNo
}).UpdateColumns(it => new { it.Status }).WhereColumns(it => new { it.InvoiceNo }).ExecuteCommand();
for (int i = 0; i < record.Count; i++)
{
ChannelStock it = record[i];
if (it.BoardType == 6 && it.PosNo == 0)
{
_portUtil.SetNumCount(it.DrawerNo, it.ColNo, it.AddQuantity);
}
// 更新数据 库存信息
SqlSugarHelper.Db.Updateable(new ChannelStock()
{
Quantity = it.Quantity + it.AddQuantity,
PosNo = 1,
ManuNo = it.ManuNo,
EffDate = it.EffDate,
Id = it.Id,
}).UpdateColumns(it => new { it.Quantity, it.ManuNo, it.EffDate, it.PosNo }).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.AddQuantity,
Type = 1,
InvoiceId = InvoiceId
//,StockQuantity = nowChannels.Sum(it => it.Quantity)
}).ExecuteCommand();
//保存账册
SqlSugarHelper.Db.Insertable(new AccountBookG2()
{
DrugId = it.DrugId,
Type = 1,
Department = ConfigurationManager.AppSettings["department"].ToString(),
InvoiceNo = InvoiceId,
ManuNo = it.ManuNo,
EffDate = it.EffDate,
AddQuantity = it.AddQuantity,
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.AddQuantity;
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.AddQuantity,
TotalStock = it.AddQuantity,
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.AddQuantity;
SqlSugarHelper.Db.Updateable(accountBookG2Total).ExecuteCommand();
}
else
{
//生成总结存时可能没有该库位的绑定信息,需要写入总结存
int iTotalResult = SqlSugarHelper.Db.Insertable(new AccountBookG2()
{
DrugId = it.DrugId,
Type = 4,
YQuantity = 0,
ManuStock = it.AddQuantity,
TotalStock = it.AddQuantity,
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}");
}
}
}
return true;
});
if (f.Data)
{
// 更新屏显库存
List<ChannelStock> singleChannels = record.FindAll(it => it.BoardType != 1);
if ((singleChannels.Count > 0 ? singleChannels[0].BoardType : 1) == 5)
{
singleChannels.ForEach(it =>
{
_portUtil.WriteQuantity(it.DrawerNo, it.ColNo, it.Quantity + it.AddQuantity);
});
}
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));
}
});
}
public long CurrentTimeMillis()
{
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
// 取消按钮
public DelegateCommand CancleTake
{
get => new DelegateCommand(() =>
{
RequestData();
_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 => false;
}
}