HKC/DM_Weight/ViewModels/DestoryEmptyDialogViewModel.cs

320 lines
11 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 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.Configuration;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Windows.Threading;
using static Unity.Storage.RegistrationSet;
namespace DM_Weight.ViewModels
{
public class DestoryEmptyDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime
{
public bool KeepAlive => false;
public string Title => "空瓶取出";
private List<MachineRecord> _machineRecords = new();
public List<MachineRecord> _MachineRecords
{
get => _machineRecords;
set
{
SetProperty(ref _machineRecords, value);
}
}
private MachineRecord _machineRecord = new();
public MachineRecord _MachineRecord
{
get => _machineRecord;
set
{
SetProperty(ref _machineRecord, value);
}
}
private ChannelStock _channelStock;
public ChannelStock _ChannelStock
{
get => _channelStock;
set => SetProperty(ref _channelStock, value);
}
private IEnumerable<IGrouping<int, ChannelStock>> enumerable;
private IEnumerator<IGrouping<int, ChannelStock>> enumerator;
private int _status = 0;
public int Status
{
get => _status; set => SetProperty(ref _status, value);
}
private PortUtil _portUtil;
IEventAggregator _eventAggregator;
public DestoryEmptyDialogViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
{
_portUtil = portUtil;
_eventAggregator = eventAggregator;
}
void DoMyPrismEvent(DeviceMsg msg)
{
if (msg.WindowName == "DrawerTakeDrugWindow")
{
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)
{
_ChannelStock.TakeQuantity = msg.Quantitys[_ChannelStock.ColNo - 1];
}
break;
// 打开失败
case EventType.OPENERROR:
AlertMsg alertMsg = new AlertMsg
{
Message = msg.Message,
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Status = 0;
break;
}
}
}
public event Action<IDialogResult> RequestClose;
public bool CanCloseDialog()
{
return true;
}
public void OnDialogClosed()
{
// 取消消息订阅
_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
}
public void OnDialogOpened(IDialogParameters parameters)
{
_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
ChannelStock _record = parameters.GetValue<ChannelStock>("channel");
_ChannelStock = _record;
RequestData();
}
public void RequestData()
{
List<MachineRecord> queryData = SqlSugarHelper.Db.Queryable<MachineRecord>()
.Includes<UserList>(mr => mr.User)
.Where(mr => mr.DrugId == _ChannelStock.DrugId)
.Where(mr => mr.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
.Where(mr => mr.Type == 32)
//.Where(mr => mr.Status != 2)
.Where(mr=>mr.IsDestroy==0)
.OrderByDescending(mr => mr.OperationTime)
.OrderBy(mr => mr.Id)
.ToList();
_MachineRecords = queryData;
}
// 取消按钮
public DelegateCommand CancleTake
{
get => new DelegateCommand(() =>
{
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
});
}
public DelegateCommand BtnCloseCommand
{
get => new DelegateCommand(() =>
{
//DialogParameters parameters = new DialogParameters();
//parameters.Add("",);
// 关闭当前窗口
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
});
}
public DelegateCommand RowSelected
{
get => new DelegateCommand(() =>
{
if (_MachineRecord != null && _MachineRecord.Quantity > 0)
{
_MachineRecords = _MachineRecords.Select(x =>
{
if (x.Id == _MachineRecord.Id)
{
x.IsSelected = !x.IsSelected;
}
return x;
}).ToList();
//DialogParameters dialogParameters = new DialogParameters();
//dialogParameters.Add("channel", Channel);
//DialogServiceExtensions.ShowDialogHost(_dialogService, "ReturnEmptyDialog", dialogParameters, DoDialogResult, "RootDialog");
}
});
}
/// <summary>
/// 空瓶销毁
/// </summary>
public DelegateCommand DestoryEmptyCommand
{
get => new DelegateCommand(() =>
{
if (Status == 0)
{
if (HomeWindowViewModel.Operator.Role != null && HomeWindowViewModel.Operator.Role.RoleName != "管理员")
{
//查看当前用户是否有所在药品抽屉的权限;1-2层所有人能开其他6层管理员才能开
if (_ChannelStock.DrawerNo>3)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "当前用户没有打开抽屉的权限!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
return;
}
}
Status = 1;
//_portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉");
//IEnumerable<string> strDrugName = _MachineRecords.Select(cs => cs.DrugInfo.DrugName);
//string strNames = string.Empty;
//foreach (string name in strDrugName)
//{
// if (!strNames.Contains(name))
// {
// strNames += name + ";";
// }
//}
int sumQuantity = _MachineRecords.FindAll(it => it.IsSelected).Sum(cs => cs.Quantity);
_portUtil.SpeakAsync($"正在打开 {_ChannelStock.DrawerNo} 号抽屉,请取出空瓶 {_ChannelStock.DrugInfo.DrugName} ,数量共计 {sumQuantity}");
_portUtil.WindowName = "DrawerTakeDrugWindow";
_portUtil.BoardType = _ChannelStock.BoardType;
_portUtil.ColNos=new int[] { _ChannelStock.ColNo };
_portUtil.DrawerNo = _ChannelStock.DrawerNo;
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, () => _portUtil.Start())
;
}
});
}
public DelegateCommand TakeFinish {
get => new DelegateCommand(() =>
{
List<MachineRecord> records = _MachineRecords.FindAll(it => it.IsSelected).ToList();
if (records != null && records.Count > 0)
{
var f = SqlSugarHelper.Db.UseTran(() =>
{
for (int i = 0; i < records.Count; i++)
{
MachineRecord record = records[i];
//修改库存数量
_ChannelStock.Quantity = _ChannelStock.Quantity - record.Quantity;
SqlSugarHelper.Db.Updateable(_ChannelStock).ExecuteCommand();
//修改记录表中状态
SqlSugarHelper.Db.Updateable(record).ReSetValue(mr =>
{
mr.IsDestroy = 1;
}).ExecuteCommand();
//记录销毁明细
SqlSugarHelper.Db.Insertable(new DestoryDetail()
{
MachineId = ConfigurationManager.AppSettings["machineId"].ToString(),
Operatorid = HomeWindowViewModel.Operator?.Id,
Reviewerid = HomeWindowViewModel.Reviewer?.Id,
RecordId = record.Id,
//OrderId = ChannelStock.ManuNo,
//,StockQuantity = nowChannels.Sum(it => it.Quantity)
}).ExecuteCommand();
}
});
if (f.Data)
{
RequestData();
AlertMsg alertMsg = new AlertMsg
{
Message = "取出完成",
Type = MsgType.SUCCESS,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
}
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);
}
});
}
}
}