507 lines
21 KiB
C#
507 lines
21 KiB
C#
using DM_Weight.Models;
|
||
using DM_Weight.msg;
|
||
using DM_Weight.Port;
|
||
using DM_Weight.util;
|
||
using log4net;
|
||
using Prism.Commands;
|
||
using Prism.Events;
|
||
using Prism.Mvvm;
|
||
using Prism.Regions;
|
||
using Prism.Services.Dialogs;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Configuration;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Channels;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Data;
|
||
|
||
namespace DM_Weight.ViewModels
|
||
{
|
||
public class AddToJiaoJieWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
|
||
{
|
||
private readonly ILog logger = LogManager.GetLogger(typeof(AddToJiaoJieWindowViewModel));
|
||
private static readonly DateTime Jan1st1970 = new DateTime
|
||
(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||
public long CurrentTimeMillis()
|
||
{
|
||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
||
}
|
||
public bool KeepAlive => false;
|
||
public bool IsNavigationTarget(NavigationContext navigationContext)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
public void OnNavigatedFrom(NavigationContext navigationContext)
|
||
{
|
||
// 取消消息订阅
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
|
||
_eventAggregator.GetEvent<IsSelectedEvent>().Unsubscribe(SetIsSelected);
|
||
}
|
||
|
||
|
||
private List<ChannelStock> channelStocks;
|
||
public List<ChannelStock> ChannelStocks
|
||
{
|
||
get => channelStocks;
|
||
set => SetProperty(ref channelStocks, value);
|
||
}
|
||
|
||
|
||
|
||
public void OnNavigatedTo(NavigationContext navigationContext)
|
||
{
|
||
_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
|
||
_eventAggregator.GetEvent<IsSelectedEvent>().Subscribe(SetIsSelected);
|
||
RequestData();
|
||
}
|
||
private PortUtil _portUtil;
|
||
IEventAggregator _eventAggregator;
|
||
IDialogService _dialogService;
|
||
public AddToJiaoJieWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator, IDialogService DialogService)
|
||
{
|
||
_portUtil = portUtil;
|
||
_eventAggregator = eventAggregator;
|
||
_dialogService = DialogService;
|
||
}
|
||
private void RequestData()
|
||
{
|
||
ChannelStocks = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
.Includes<ChannelList>(cs => cs.ChannelLst)
|
||
.Includes<DrugInfo>(cs => cs.DrugInfo)
|
||
.Where(cs => cs.MachineId == (ConfigurationManager.AppSettings["jj_machineId"] ?? "DM5") && cs.BaseQuantity > cs.Quantity)
|
||
.OrderBy(cs => cs.Chnguid)
|
||
.OrderBy(cs => cs.DrawerNo)
|
||
.ToList();
|
||
ChannelStocks = ChannelStocks.GroupBy(it => new { it.DrawerNo, it.DrugId })
|
||
.Select(it =>
|
||
{
|
||
var ret = it.First();
|
||
ret.Quantity = it.Sum(itx => itx.Quantity);
|
||
ret.NeedNum=it.Sum(itx => itx.NeedNum);
|
||
return ret;
|
||
}).Where(it=>it.BaseQuantity>it.Quantity)
|
||
.ToList();
|
||
ChannelStocks.ForEach(cs => cs.AddQuantity = cs.BaseQuantity - cs.Quantity);
|
||
|
||
ICollectionView vw = CollectionViewSource.GetDefaultView(ChannelStocks);
|
||
vw.GroupDescriptions.Add(new PropertyGroupDescription("ChannelLst"));
|
||
}
|
||
private int _status = 0;
|
||
|
||
public int Status { get => _status; set => SetProperty(ref _status, value); }
|
||
|
||
private bool _isEnable = true;
|
||
public bool IsEnable { get => _isEnable; set => SetProperty(ref _isEnable, value); }
|
||
|
||
private List<int> iDrawerNoLst
|
||
{ get; set; }
|
||
private int CurrentNum { get; set; }
|
||
|
||
//刷新
|
||
public DelegateCommand QueryCommand
|
||
{
|
||
get => new DelegateCommand(() => RequestData());
|
||
}
|
||
//一键补药
|
||
public DelegateCommand OpenDragCommand
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
try
|
||
{
|
||
Status = 1;
|
||
IsEnable = false;
|
||
|
||
var varDrawerNO = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
.Where(cs => cs.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM3"))
|
||
.GroupBy(cs => cs.DrawerNo).Select(DrawerNo => DrawerNo).ToList();
|
||
|
||
iDrawerNoLst = varDrawerNO.Select(item => item.DrawerNo).ToList();
|
||
CurrentNum = 0;
|
||
_portUtil.SpeakAsync($"正在打开 {iDrawerNoLst[CurrentNum]} 号抽屉");
|
||
|
||
|
||
_portUtil.WindowName = "AddToJiaoJieWindow";
|
||
_portUtil.Operate = true;
|
||
//_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1;
|
||
//_portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray();
|
||
_portUtil.DrawerNo = iDrawerNoLst[CurrentNum];
|
||
_portUtil.OpenAllDrawer();
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = $"补药异常{ex.Message}",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
logger.Info($"AddToJiaoJieWindowViewModel异常:{ex.Message}");
|
||
_portUtil.Operate = false;
|
||
}
|
||
|
||
});
|
||
}
|
||
void DoMyPrismEvent(DeviceMsg msg)
|
||
{
|
||
if (msg.WindowName == "AddToJiaoJieWindow")
|
||
{
|
||
switch (msg.EventType)
|
||
{
|
||
// 抽屉打开
|
||
case EventType.DRAWEROPEN:
|
||
if (Status == 1)
|
||
{
|
||
Status = 2;
|
||
}
|
||
CurrentNum += 1;
|
||
if (CurrentNum < iDrawerNoLst.Count)
|
||
{
|
||
_portUtil.WindowName = "AddToJiaoJieWindow";
|
||
_portUtil.Operate = true;
|
||
_portUtil.DrawerNo = iDrawerNoLst[CurrentNum];
|
||
_portUtil.OpenAllDrawer();
|
||
}
|
||
else
|
||
{
|
||
_portUtil.GetAllDrawerLockState();
|
||
}
|
||
break;
|
||
// 抽屉关闭
|
||
case EventType.DRAWERCLOSE:
|
||
if (Status == 2)
|
||
{
|
||
Status = 3;
|
||
}
|
||
_portUtil.Operate = false;
|
||
IsEnable = true;
|
||
CurrentNum = 0;
|
||
|
||
break;
|
||
// 数量变化
|
||
case EventType.UPDATEQUANTITY:
|
||
if (Status == 2)
|
||
{
|
||
ChannelStocks.ForEach(it => it.AddQuantity = 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;
|
||
_portUtil.Operate = false;
|
||
IsEnable = false;
|
||
CurrentNum = 0;
|
||
break;
|
||
}
|
||
}
|
||
|
||
}
|
||
//完成按钮
|
||
public DelegateCommand AddFinish
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
if (ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).Count <= 0)
|
||
{
|
||
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "请选择药箱",
|
||
Type = MsgType.SUCCESS,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
return;
|
||
}
|
||
List<ChannelStock> jiaojieStocks = new List<ChannelStock>();
|
||
List<ChannelStock> cs = ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).ToList();
|
||
for (int i = 0; i < cs.Count; i++)
|
||
{
|
||
ChannelStock copy = TransExpV2<ChannelStock, ChannelStock>.Trans(cs[i]);
|
||
jiaojieStocks.Add(copy);
|
||
}
|
||
csList = jiaojieStocks.FindAll(cs => cs.ChannelLst.IsSelected).GroupBy(cs => cs.DrugId).Select(g => new
|
||
{
|
||
DrugId = g.Key,
|
||
AddQuantity = g.Sum(s => s.AddQuantity)
|
||
}).Select(cs => new ChannelStock() { DrugId = cs.DrugId, AddQuantity = cs.AddQuantity }).ToList();
|
||
|
||
List<ChannelStock> channelStocks = new List<ChannelStock>();
|
||
List<string> msg = new List<string>();
|
||
for (int i = 0; i < csList.Count; i++)
|
||
{
|
||
List<ChannelStock> HasQChannels = SqlSugarHelper.Db.Queryable<ChannelStock>()
|
||
.Includes<DrugInfo>(cs => cs.DrugInfo)
|
||
.Where(cs => cs.Quantity > 0)
|
||
.Where(cs => cs.DrawerType == 1)
|
||
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM3"))
|
||
.Where(cs => cs.DrugId == csList[i].DrugId)
|
||
.OrderBy(cs => cs.EffDate)
|
||
.OrderBy(cs => cs.DrawerNo)
|
||
.ToList();
|
||
int total = HasQChannels.Sum(it => it.Quantity);
|
||
int TakeQ = csList[i].AddQuantity;
|
||
// 说明数量足够
|
||
if (total >= TakeQ)
|
||
{
|
||
for (int j = 0; TakeQ > 0; j++)
|
||
{
|
||
ChannelStock stock = HasQChannels[j];
|
||
if (TakeQ > stock.Quantity)
|
||
{
|
||
stock.TakeQuantity = stock.Quantity;
|
||
channelStocks.Add(stock);
|
||
TakeQ -= stock.Quantity;
|
||
}
|
||
else
|
||
{
|
||
stock.TakeQuantity = TakeQ;
|
||
channelStocks.Add(stock);
|
||
TakeQ = 0;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
msg.Add($"药品【{ChannelStocks[i].DrugInfo.DrugName}】库存不足,应取【{TakeQ}】库存【{total}】");
|
||
}
|
||
|
||
}
|
||
if (msg.Count > 0)
|
||
{
|
||
DialogParameters dialogParameters = new DialogParameters();
|
||
dialogParameters.Add("msgInfo", msg);
|
||
DialogServiceExtensions.ShowDialogHost(_dialogService, "ShowMessageDialog", dialogParameters, "RootDialog");
|
||
return;
|
||
|
||
}
|
||
else
|
||
{
|
||
channelStocks.Sort((a, b) =>
|
||
{
|
||
if ((a.DrawerNo - b.DrawerNo) == 0)
|
||
{
|
||
return a.ColNo - b.ColNo;
|
||
}
|
||
return a.DrawerNo - b.DrawerNo;
|
||
});
|
||
}
|
||
List<ChannelStock> record = channelStocks.FindAll(it => it.TakeQuantity > 0).ToList();
|
||
if (record.Count > 0)
|
||
{
|
||
string InvoiceId = "AddJiaoJie_" + 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> jiaojie = jiaojieStocks.Where(cs => cs.DrugId == it.DrugId).ToList();
|
||
if (jiaojie != null && jiaojie.Count > 0)
|
||
{
|
||
for (int j = 0; j < jiaojie.Count; j++)
|
||
{
|
||
// 更新数据 交接柜 库存信息
|
||
ChannelStock jiaojie_it = jiaojie[j];
|
||
SqlSugarHelper.Db.Updateable(new ChannelStock()
|
||
{
|
||
Quantity = jiaojie_it.BaseQuantity,
|
||
//ManuNo = it.ManuNo,
|
||
//EffDate = it.EffDate,
|
||
Id = jiaojie_it.Id,
|
||
}).UpdateColumns(jiaojie_it => new { jiaojie_it.Quantity }).ExecuteCommand();
|
||
}
|
||
}
|
||
|
||
|
||
// 保存数据 出库记录
|
||
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
|
||
}).ExecuteCommand();
|
||
}
|
||
return true;
|
||
});
|
||
if (f.Data)
|
||
{
|
||
// 更新屏显库存
|
||
List<ChannelStock> singleChannels = record.FindAll(it => it.BoardType != 5);
|
||
if (singleChannels.Count > 0)
|
||
{
|
||
singleChannels.ForEach(it =>
|
||
{
|
||
_portUtil.WriteQuantity(it.DrawerNo, it.ColNo, it.Quantity - it.TakeQuantity);
|
||
});
|
||
}
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "补药完成,库存已更新",
|
||
Type = MsgType.SUCCESS,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
RequestData();
|
||
}
|
||
if (!f.IsSuccess)
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "补药操作失败,库存更新失败!",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = "补药数量有误",
|
||
Type = MsgType.ERROR
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
}
|
||
});
|
||
}
|
||
|
||
//取消
|
||
public DelegateCommand CancleAdd
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
_portUtil.ResetData();
|
||
Status = 0;
|
||
IsEnable = true;
|
||
CurrentNum = 0;
|
||
});
|
||
}
|
||
|
||
|
||
public DelegateCommand RejectReport_Download
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
//GridReportUtil.RejectionReport("");
|
||
});
|
||
}
|
||
public DelegateCommand Account_Download
|
||
{
|
||
get => new DelegateCommand(() =>
|
||
{
|
||
//GridReportUtil.AccountNewReport();
|
||
});
|
||
}
|
||
|
||
|
||
private List<ChannelStock> csList = new List<ChannelStock>();
|
||
//取药 弹出出药列表
|
||
public DelegateCommand TakeDrugCommand
|
||
{
|
||
get => new DelegateCommand(async () =>
|
||
{
|
||
//选中channelStock.channel_list的isSelected则选中channelStock的isSelected
|
||
//var o= ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).ToList();
|
||
|
||
//csList = ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).GroupBy(cs => cs.DrugId).Select(g => new {
|
||
// DrugId = g.Key, AddQuantity = g.Sum(s => s.AddQuantity)
|
||
//}).Select(cs=>new ChannelStock() { DrugId=cs.DrugId,AddQuantity=cs.AddQuantity }).ToList();
|
||
|
||
csList = ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).ToList();
|
||
|
||
if (csList != null && csList.Count > 0)
|
||
{
|
||
// 此处延时1毫秒,等待页面渲染
|
||
await Task.Delay(TimeSpan.FromMilliseconds(1));
|
||
DialogParameters dialogParameters = new DialogParameters();
|
||
dialogParameters.Add("ChannelStocks", csList);
|
||
DialogServiceExtensions.ShowDialogHost(_dialogService, "AddToJiaoJieDialog", dialogParameters, DoDialogResult, "RootDialog");
|
||
|
||
}
|
||
else
|
||
{
|
||
AlertMsg alertMsg = new AlertMsg
|
||
{
|
||
Message = $"未选择药品,请先勾选要药箱号",
|
||
Type = MsgType.ERROR,
|
||
};
|
||
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
_portUtil.Operate = false;
|
||
}
|
||
});
|
||
}
|
||
private void DoDialogResult(IDialogResult dialogResult)
|
||
{
|
||
// 委托 被动执行 被子窗口执行
|
||
// dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态
|
||
//if(dialogResult.Result == ButtonResult.OK)
|
||
//{
|
||
RequestData();
|
||
//}
|
||
//MessageBox.Show("返回值:" + dialogResult.Result.ToString());
|
||
}
|
||
//设置选中药箱的复选框状态
|
||
private void SetIsSelected(ChannelStock channelStock)
|
||
{
|
||
if (channelStock != null)
|
||
{
|
||
//channelStock.ChannelLst.IsSelected = !channelStock.ChannelLst.IsSelected;
|
||
if (channelStock.ChannelLst.State == 0)
|
||
{
|
||
channelStock.ChannelLst.IsSelected = !channelStock.ChannelLst.IsSelected;
|
||
}
|
||
else
|
||
{
|
||
return;
|
||
}
|
||
if (channelStock != null && ChannelStocks != null)
|
||
{
|
||
ChannelStocks = ChannelStocks.Select(x =>
|
||
{
|
||
for (int i = 0; i < ChannelStocks.Count; i++)
|
||
{
|
||
if (ChannelStocks[i].DrawerNo == channelStock.DrawerNo)
|
||
{
|
||
ChannelStocks[i].ChannelLst = channelStock.ChannelLst;
|
||
}
|
||
}
|
||
return x;
|
||
}).ToList();
|
||
}
|
||
ICollectionView vw = CollectionViewSource.GetDefaultView(ChannelStocks);
|
||
vw.GroupDescriptions.Add(new PropertyGroupDescription("ChannelLst"));
|
||
}
|
||
}
|
||
}
|
||
}
|