HuNan_DM/DM_Weight/ViewModels/DrawerAddDrugWindowViewMode...

415 lines
18 KiB
C#
Raw Normal View History

2023-11-13 13:25:46 +08:00
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.Tasks;
using System.Windows.Threading;
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using System.Collections.ObjectModel;
using System.Threading;
2023-11-13 13:25:46 +08:00
namespace DM_Weight.ViewModels
{
public class DrawerAddDrugWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
{
private readonly ILog logger = LogManager.GetLogger(typeof(DrawerAddDrugWindowViewModel));
private List<int> _drawerNoList = new List<int>();
//凭证号
private string _pzh;
public string PZH { get => _pzh; set { SetProperty(ref _pzh, value); } }
private List<ChannelStock>? _channelStocks;
public List<ChannelStock>? ChannelStocks
{
get => _channelStocks;
set => SetProperty(ref _channelStocks, value);
}
//private ObservableCollection<ChannelStock> _obChannelStock=new ObservableCollection<ChannelStock>();
//public ObservableCollection<ChannelStock> obChannelStock
//{
// get => _obChannelStock;
// set=>SetProperty(ref _obChannelStock, 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;
public DrawerAddDrugWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
2023-11-13 13:25:46 +08:00
{
_portUtil = portUtil;
_eventAggregator = eventAggregator;
//this.SqlSugarHelper.Db= sqlSugarScope;
2023-11-13 13:25:46 +08:00
}
void DoMyPrismEvent(DeviceMsg msg)
{
logger.Info("抽屉加药页面_DoMyPrismEvent");
2023-11-13 13:25:46 +08:00
if (msg.WindowName == "DrawerAddDrugWindow")
{
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.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;
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(() =>
{
Status = 1;
_portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉");
try
{
//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();
// singleChannelsFor = singleChannelsFor.GroupBy(it => it.ColNo)
// .Select(it => new
// {
// Quantity = it.Sum(itx => itx.Quantity),
// AddQuantity = it.Sum(itx => itx.AddQuantity)
//})
// .ToList()
// .FindAll(it => it.BoardType != 1);
2023-11-13 13:25:46 +08:00
List<ChannelStock> singleChannels = ChannelStocks.FindAll(it => it.BoardType != 1);
_portUtil.WindowName = "DrawerAddDrugWindow";
_portUtil.Operate = true;
_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1;
_portUtil.ColNos = new int[] { };// singleChannels.Select(it => it.ColNo).ToArray();
2023-11-13 13:25:46 +08:00
_portUtil.DrawerNo = DrawerNo;
_portUtil.Start();
}
catch (Exception ex)
{
logger.Info($"OpenDrawer异常{ex.Message}");
}
}, () => Status == 0).ObservesProperty(() => Status);
}
private bool _isFinishClick = false;
// 完成按钮
public DelegateCommand TakeFinish
{
get => new DelegateCommand(() =>
{
if (!_isFinishClick)
{
_isFinishClick = true;
List<ChannelStock> record = ChannelStocks.FindAll(it => it.AddQuantity > 0).ToList();
2023-11-13 13:25:46 +08:00
if (record.Count > 0)
{
//string InvoiceId = "DRAWER_" + CurrentTimeMillis();
string InvoiceId = PZH;
var f = SqlSugarHelper.Db.UseTran(() =>
2023-11-13 13:25:46 +08:00
{
for (int i = 0; i < record.Count; i++)
{
ChannelStock it = record[i];
it.ManuNo = it.drugManuNo.ManuNo;
it.EffDate = it.drugManuNo.EffDate;
// 更新数据 库存信息
SqlSugarHelper.Db.Updateable(new ChannelStock()
2023-11-13 13:25:46 +08:00
{
Quantity = it.Quantity + it.AddQuantity,
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>()
2023-11-13 13:25:46 +08:00
.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()
2023-11-13 13:25:46 +08:00
{
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,
2023-11-13 13:25:46 +08:00
OperationTime = DateTime.Now,
Quantity = it.AddQuantity,
Type = 1,
InvoiceId = InvoiceId,
StockQuantity = nowChannels.Sum(it => it.Quantity),
ManunoQuantity = nowChannels.FindAll(it2 => it2.ManuNo == it.ManuNo).Sum(it => it.Quantity),
SupplierDept = ConfigurationManager.AppSettings["supplierDept"].ToString(),
ReceiveDept = ConfigurationManager.AppSettings["receiveDept"].ToString()
2023-11-13 13:25:46 +08:00
}).ExecuteCommand();
}
return true;
});
if (f.Data)
{
// 更新屏显库存
//List<ChannelStock> singleChannels = ChannelStocks.Where(it => it.BoardType == 5 && (it.Quantity > 0 || it.AddQuantity > 0))
// .GroupBy(it => it.ColNo)
// .Select(it =>
// {
// var ret = it.First();
// ret.Quantity = it.Sum(it => it.Quantity);
// ret.AddQuantity = it.Sum(it => it.AddQuantity);
// return ret;
// })
// .OrderBy(it => it.EffDate).ToList();
List<ChannelStock> singleChannels = ChannelStocks.Where(it => it.BoardType == 5 && (it.Quantity > 0 || it.AddQuantity > 0))
.GroupBy(it => it.ColNo)
.Select(it =>
{
var ret = it.First();
ret.Quantity = it.Sum(itx => itx.Quantity);
ret.AddQuantity = it.Sum(itx => itx.AddQuantity);
ret.EffDate = it.Min(it => it.EffDate);
ret.ManuNo = it.OrderBy(it => it.ManuNo).Select(it => it.ManuNo).First().ToString();
return ret;
})
.OrderBy(it => it.EffDate)
.ToList();
//singleChannels = singleChannels.Where(it => it.AddQuantity > 0).ToList();
if (singleChannels.Count > 0)
2023-11-13 13:25:46 +08:00
{
//将库位多批次的总库存数更新标签
2023-11-13 13:25:46 +08:00
singleChannels.ForEach(it =>
{
_portUtil.WriteChannelInfo(6, it.EffDate == null ? "" : it.EffDate, it.DrawerNo, it.ColNo);
Thread.Sleep(200);
_portUtil.WriteChannelInfo(5, it.ManuNo, it.DrawerNo, it.ColNo);
Thread.Sleep(200);
_portUtil.ShowContent(it.DrawerNo, it.ColNo);
Thread.Sleep(500);
_portUtil.WriteQuantity(it.DrawerNo, it.ColNo, it.Quantity + it.AddQuantity);
2023-11-13 13:25:46 +08:00
});
}
RequestData();
AlertMsg alertMsg = new AlertMsg
{
Message = "抽屉加药完成,库存已更新",
Type = MsgType.SUCCESS,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "更新库存失败",
Type = MsgType.SUCCESS,
};
_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 DelegateCommand ComboboxSelected
{
get => new DelegateCommand(() =>
{
ChannelStocks.Where(it => it.Location == "").Select(it => it.ManuNo = "12").ToList();
});
}
public bool KeepAlive => true;
2023-11-13 13:25:46 +08:00
public long CurrentTimeMillis()
{
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
public void FindDrawerCount()
{
int count = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cs => cs.DrawerType != 3)
2023-11-13 13:25:46 +08:00
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).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()
{
if (ChannelStocks != null)
{
ChannelStocks.Clear();
}
List<ChannelStock> queryData = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Includes(cs => cs.DrugInfo, di => di.DrugManuNos)
.InnerJoin<ChannelList>((cs, cl) => cs.Chnguid==cl.Id &&cs.DrugId==cl.DrugId)
2023-11-13 13:25:46 +08:00
.Where(cs => cs.DrawerNo == DrawerNo)
.Where(cs => cs.DrawerType == 1)
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
.Where(cs => cs.DrugId != null)
.OrderBy(cs => cs.ColNo)
.ToList();
ChannelStocks = queryData.Select(cs =>
{
cs.drugManuNo = cs.DrugInfo.DrugManuNos.Find(it => it.ManuNo.Equals(cs.ManuNo));
return cs;
}).ToList();
//obChannelStock = new ObservableCollection<ChannelStock>(ChannelStocks);
2023-11-13 13:25:46 +08:00
}
//接收导航传过来的参数 现在是在此处初始化了表格数据
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);
}
}
}