using DM_Weight.Models; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.util; using DM_Weight.Views; using log4net; using log4net.Repository.Hierarchy; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Drawing.Printing; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using System.Windows.Data; namespace DM_Weight.ViewModels { public class CheckOrderWindowViewModel : BindableBase,INavigationAware, IRegionMemberLifetime { //private int _pageNum = 1; //public int PageNum //{ // get => _pageNum; // set // { // SetProperty(ref _pageNum, value); // RequestData(); // } //} //private int _pageCount = 1; //public int PageCount //{ // get => _pageCount; // set // { // SetProperty(ref _pageCount, value); // } //} //private int _pageSize = 8; //public int PageSize //{ // get => _pageSize; // set // { // SetProperty(ref _pageSize, value); // } //} //private int _totalCount = 0; //public int TotalCount //{ // get => _totalCount; // set // { // SetProperty(ref _totalCount, value); // } //} private readonly ILog logger = LogManager.GetLogger(typeof(CheckOrderWindowViewModel)); private int _status=0; public int Status { get => _status; set => SetProperty(ref _status, value); } private bool _btnEnable=true; public bool BtnEnable { get => _btnEnable;set=>SetProperty(ref _btnEnable, value); } //抽屉号列表 public static List iList = new List(); //第几个抽屉号 //public static int iNumber = 1; private List orderDetailList=new(); public List OrderDetailList { get=> orderDetailList; set=>SetProperty(ref orderDetailList, value); } private string _orderDate = DateTime.Now.ToString("yyyy-MM-dd"); /// /// 查询条件 处方日期 /// public string OrderDate { get { return _orderDate; } set { if (!String.IsNullOrEmpty(value)) { SetProperty(ref _orderDate, DateTime.Parse(value).ToString("yyyy-MM-dd")); } else { SetProperty(ref _orderDate, value); } RequestData(); } } private PortUtil _portUtil; IEventAggregator _eventAggregator; public CheckOrderWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator) { _portUtil = portUtil; _eventAggregator = eventAggregator; } public bool KeepAlive => false; public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { // 取消消息订阅 //_eventAggregator.GetEvent().Unsubscribe(DoMyPrismEvent); } public void OnNavigatedTo(NavigationContext navigationContext) { RequestData(); //_eventAggregator.GetEvent().Subscribe(DoMyPrismEvent); } public DelegateCommand Query { get => new DelegateCommand(()=> { RequestData(); }); } public void RequestData() { OrderDetailList.Clear(); //int totalCount = 0; List queryData = SqlSugarHelper.Db.Queryable() .Includes(od => od._OrderInfo) .Includes(od => od.DrugInfo) .WhereIF(OrderDate != null, od => od._OrderInfo.RecvDate.ToString("yyyy-MM-dd") == OrderDate) .WhereIF(!String.IsNullOrEmpty(ConfigurationManager.AppSettings["storage"]), od => od._OrderInfo.Pharmacy == ConfigurationManager.AppSettings["storage"]) .Where(od => od._OrderInfo.DmStatus == 0) .Where(od => od._OrderInfo.HisDispFlag == 0) .Where(od => od._OrderInfo.CancelFlag == 0) //.Where(od=>od._OrderInfo.OrderNo== "202312219183416") .Where(od=>od._OrderInfo.DoctorCode== HomeWindowViewModel.Operator.UserBarcode) .OrderBy(od => od.OrderId) .ToList(); //.ToPageList(PageNum, PageSize, ref totalCount); ICollectionView vw = CollectionViewSource.GetDefaultView(queryData); vw.GroupDescriptions.Add(new PropertyGroupDescription("_OrderInfo")); OrderDetailList = queryData; //TotalCount = totalCount; //PageCount = (int)Math.Ceiling((double)TotalCount / PageSize); } //核对确认 public DelegateCommand ConfirmCommand { get => new DelegateCommand(() => { ConfirmAction(); }); } private void ConfirmAction() { //打开当前用户下的药箱 iList = SqlSugarHelper.Db.Queryable().Where(cl => cl.MachineId == "DM5") .Where(cl => cl.BelongUser == HomeWindowViewModel.Operator.UserName) .Select(cl => cl.DrawerNo).ToList(); if (iList.Count > 0) { _portUtil.SpeakAsync("正在打开药箱"); _portUtil.DrawerNo = iList[0]; Status = 1; _portUtil.OpenBox(); } } //void DoMyPrismEvent(DeviceMsg msg) //{ // switch (msg.EventType) // { // // 药箱打开 // case EventType.DRAWEROPEN: // BtnEnable = false; // //记录开药箱日志 // SqlSugarHelper.Db.Insertable(new MachineRecord() // { // MachineId = "DM5", // DrawerNo = _portUtil.DrawerNo, // Operator = HomeWindowViewModel.Operator?.Id, // OperationTime = DateTime.Now, // Type = 55, // InvoiceId = "药箱打开", // }).ExecuteCommand(); // if (iNumber < iList.Count) // { // _portUtil.DrawerNo = iList[iNumber]; // iNumber++; // if (Status == 1) // { // Status = 2; // } // _portUtil.OpenBox(); // } // else // { // iNumber = 0; // _portUtil.GetBoxStatus(); // } // break; // // 药箱关闭 // case EventType.DRAWERCLOSE: // //记录药箱操作日志 // SqlSugarHelper.Db.Insertable(new MachineRecord() // { // MachineId = "DM5", // DrawerNo = _portUtil.DrawerNo, // Operator = HomeWindowViewModel.Operator?.Id, // OperationTime = DateTime.Now, // Type = 55, // InvoiceId = "药箱关闭", // }).ExecuteCommand(); // if (Status == 2) // { // Status = 3; // } // BtnEnable = true; // _portUtil.Operate = false; // //减库存,生成操作记录 // SaveDate(); // break; // // 打开失败 // case EventType.OPENERROR: // AlertMsg alertMsg = new AlertMsg // { // Message = msg.Message, // Type = MsgType.ERROR // }; // _eventAggregator.GetEvent().Publish(alertMsg); // BtnEnable = true; // Status = 0; // _portUtil.Operate = false; // //记录药箱操作日志 // SqlSugarHelper.Db.Insertable(new MachineRecord() // { // MachineId = "DM5", // DrawerNo = _portUtil.DrawerNo, // Operator = HomeWindowViewModel.Operator?.Id, // OperationTime = DateTime.Now, // Type = 55, // InvoiceId = "药箱打开失败", // }).ExecuteCommand(); // _portUtil.Operate = false; // break; // } //} //减库存,生成操作记录 private void SaveDate() { var confirmData = OrderDetailList.Where(od => od._OrderInfo.ItemIsChecked == true).ToList(); if(confirmData.Count > 0 ) { var f = SqlSugarHelper.Db.UseTran(() => { string empChannelStock=string.Empty; for (int i = 0; i < confirmData.Count; i++) { OrderDetail od = confirmData[i]; //更新处方状态 if(od._OrderInfo.DmStatus==0) { SqlSugarHelper.Db.Updateable(new OrderInfo() { DmStatus = 1, OrderNo = od.OrderNo }).UpdateColumns(it => new { it.DmStatus }).WhereColumns(it => new { it.OrderNo }).ExecuteCommand(); SqlSugarHelper.Db.Insertable(new OrderFinish() { OrderNo = od.OrderNo, PatientId = od._OrderInfo.PatientId, Pharmacy = od._OrderInfo.Pharmacy, State = 1, Operator = HomeWindowViewModel.Operator?.Nickname, }); } Expression> updateExp = it => it.ManuNo == od.SetManuNo && it.EffDate == od.SetEffDate; ChannelStock cs = SqlSugarHelper.Db.Queryable().Where(cs =>cs.DrugId==od.DrugId && cs.ManuNo == od.SetManuNo && cs.EffDate == od.SetEffDate && cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM3")).First(); if(cs==null) { empChannelStock += $"{od.OrderNo},{od.DrugId},{od.SetManuNo},{od.SetEffDate}"; continue; } cs.Quantity = cs.Quantity - od.Quantity; // 更新数据 库存信息 SqlSugarHelper.Db.Updateable(cs).UpdateColumns(it => new { it.Quantity }).ExecuteCommand(); if (cs != null) { SqlSugarHelper.Db.Insertable(new MachineRecord() { MachineId = ConfigurationManager.AppSettings["machineId"].ToString(), DrawerNo = cs.DrawerNo, ColNo = cs.ColNo, DrugId = cs.DrugId, ManuNo = cs.ManuNo, EffDate = !String.IsNullOrEmpty(cs.EffDate) ? DateTime.ParseExact(cs.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, Operator = HomeWindowViewModel.Operator?.Id, Reviewer = HomeWindowViewModel.Reviewer?.Id, OperationTime = DateTime.Now, Quantity = od.Quantity, Type = 2, InvoiceId = od.OrderNo //, StockQuantity = nowChannels.Sum(it => it.Quantity) }).ExecuteCommand(); } //保存账册 int iInsertResult = SqlSugarHelper.Db.Insertable(new AccountBookG2() { DrugId = od.DrugId, Type = 2, Department = od._OrderInfo.DeptName, OrderNo = od.OrderNo, ManuNo = cs.ManuNo, EffDate = cs.EffDate, OutQuantity = od.Quantity, UserId1 = HomeWindowViewModel.Operator?.Id, UserId2 = HomeWindowViewModel.Reviewer?.Id, MachineId = ConfigurationManager.AppSettings["machineId"].ToString(), CreateDate = DateTime.Now.ToString("yyyy-MM-dd"), CreateTime = DateTime.Now, InvoiceNo = od.OrderNo }).ExecuteCommand(); //修改凌晨生成的日结存与总结存数据 AccountBookG2 accountBookG2Day = SqlSugarHelper.Db.Queryable() .Where(ab => ab.MachineId.Equals(ConfigurationManager.AppSettings["machineId"].ToString())) .Where(ab => ab.Type == 3) .Where(ab => ab.DrugId == od.DrugId) .Where(ab => ab.ManuNo == cs.ManuNo) .Where(ab => ab.CreateDate == DateTime.Now.ToString("yyyy-MM-dd")).First(); if (accountBookG2Day != null) { accountBookG2Day.ManuStock = accountBookG2Day.ManuStock - od.Quantity; SqlSugarHelper.Db.Updateable(accountBookG2Day).ExecuteCommand(); } else { //生成日结存时可能没有该库位的绑定信息,需要写入日结存 int iDayResult = SqlSugarHelper.Db.Insertable(new AccountBookG2() { DrugId = od.DrugId, Type = 3, ManuNo = cs.ManuNo, EffDate = cs.EffDate, YQuantity = 0, ManuStock = od.Quantity, TotalStock = od.Quantity, 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($"未写入日结存数据{od.DrugId}-{cs.ManuNo}-{cs.EffDate}-{cs.Quantity}"); } } //修改凌晨生成的日结存与总结存数据 AccountBookG2 accountBookG2Total = SqlSugarHelper.Db.Queryable() .Where(ab => ab.MachineId.Equals(ConfigurationManager.AppSettings["machineId"].ToString())) .Where(ab => ab.Type == 4) .Where(ab => ab.DrugId == od.DrugId) .Where(ab => ab.CreateDate == DateTime.Now.ToString("yyyy-MM-dd")).First(); if (accountBookG2Total != null) { accountBookG2Total.TotalStock = accountBookG2Total.TotalStock - od.Quantity; SqlSugarHelper.Db.Updateable(accountBookG2Total).ExecuteCommand(); } else { //生成总结存时可能没有该库位的绑定信息,需要写入总结存 int iTotalResult = SqlSugarHelper.Db.Insertable(new AccountBookG2() { DrugId = od.DrugId, Type = 4, YQuantity = 0, ManuStock = od.Quantity, TotalStock = od.Quantity, 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($"未写入总结存数据{od.DrugId}-{od.Quantity}"); } } } if(!string.IsNullOrEmpty(empChannelStock)) { AlertMsg alertMsg = new AlertMsg { Message = $"所选处方对应药品批次效期无库存{empChannelStock}", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); logger.Info($"所选处方对应药品批次效期无库存{empChannelStock}"); } return true; }); if (f.Data) { RequestData(); AlertMsg alertMsg = new AlertMsg { Message = "所选处方已核对完成", Type = MsgType.SUCCESS, }; _eventAggregator.GetEvent().Publish(alertMsg); } if (!f.IsSuccess) { AlertMsg alertMsg = new AlertMsg { Message = "处方核对失败!", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } Status = 0; } else { AlertMsg alertMsg = new AlertMsg { Message = "请勾选要核对的处方数据", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); } } } }