using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DM_Weight.Models; using DM_Weight.util; using System.Reflection.PortableExecutable; using System.Configuration; using DM_Weight.msg; using Prism.Events; using DM_Weight.Port; using System.Threading.Channels; namespace DM_Weight.ViewModels { public class ReturnEmptyWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime { private List? _channelStocks; public List? Channels { get { return _channelStocks; } set { SetProperty(ref _channelStocks, value); } } private ChannelStock _channelStock; public ChannelStock Channel { get { return _channelStock; } set { SetProperty(ref _channelStock, value); } } public event Action RequestClose; IDialogService _dialogService; IEventAggregator _eventAggregator; public ReturnEmptyWindowViewModel(IDialogService dialogService, IEventAggregator eventAggregator) { _dialogService = dialogService; _eventAggregator = eventAggregator; } public DelegateCommand RowSelected { get => new DelegateCommand(() => { if (Channel != null && Channel.DrugId != null) { Channels = Channels.Select(x => { if (x.Id == Channel.Id) { x.IsSelected = !x.IsSelected; } return x; }).ToList(); } else if(Channel != null && Channel.DrugId == null) { DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("DrawerNo", Channel.DrawerNo); DialogServiceExtensions.ShowDialogHost(_dialogService, "BindingChannelDialog", dialogParameters, DoDialogResult, "RootDialog"); } //if (Channel != null && Channel.DrugId == null) //{ // DialogParameters dialogParameters = new DialogParameters(); // dialogParameters.Add("DrawerNo", Channel.DrawerNo); // DialogServiceExtensions.ShowDialogHost(_dialogService, "BindingChannelDialog", dialogParameters, DoDialogResult, "RootDialog"); //} //else if (Channel != null && Channel.CanReturnQuantity > 0) //{ // DialogParameters dialogParameters = new DialogParameters(); // dialogParameters.Add("channel", Channel); // DialogServiceExtensions.ShowDialogHost(_dialogService, "ReturnEmptyDialog", dialogParameters, DoDialogResult, "RootDialog"); //} }); } //还空瓶 public DelegateCommand ReturnEmptyCommand { get => new DelegateCommand(() => { List records = Channels.FindAll(it => it.IsSelected).ToList(); if (records.Count > 0) { if(records.Count > 1&&records.FindAll(rs=>rs.CanReturnQuantity>0).Count>1) { //还空瓶只能选择一条进行归还 AlertMsg alertMsg = new AlertMsg { Message = "请选择一条可归还数量不为0的数据进行归还", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } else { DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("channel", records.Single(rs => rs.CanReturnQuantity > 0)); DialogServiceExtensions.ShowDialogHost(_dialogService, "ReturnEmptyDialog", dialogParameters, DoDialogResult, "RootDialog"); } } else { AlertMsg alertMsg = new AlertMsg { Message = "请选择要还的空瓶", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } }); } private List csList=new List(); //销毁 public DelegateCommand DestoryCommand { get => new DelegateCommand(() => { if (Channels != null && Channels.Count > 0) { csList = Channels.FindAll(it => it.IsSelected && it.Quantity > 0).ToList(); if (csList != null && csList.Count > 0) { //RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); //DialogParameters dialogParameters = new DialogParameters(); //List msg = new List(); //msg.Add("确认"); //msg.Add("确认要销毁吗?"); //dialogParameters.Add("msgInfo", msg); //DialogServiceExtensions.ShowDialogHost(_dialogService, "ConfirmMessageDialog", dialogParameters, DestoryDialogResult, "RootDialog"); csList.ForEach(it => it.Quantity = 0); var f = SqlSugarHelper.Db.UseTran(() => { SqlSugarHelper.Db.Updateable(csList).ExecuteCommand(); string[] str=new string[csList.Count]; str=csList.Select(t=>t.DrugId).ToArray(); List machines = SqlSugarHelper.Db.Queryable() //.InnerJoin((mr, cs) => mr.DrugId == cs.DrugId) //.Where(mr => mr.DrugId.Contains(csList.Select(it => it.DrugId).ToList()) && mr.Type = 32) .Where(mr => mr.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")) .In(mr=>mr.DrugId, str) .Where(mr => mr.Type == 32) .ToList(); SqlSugarHelper.Db.Updateable(machines).ReSetValue(mr => { mr.IsDestroy = 1; }).ExecuteCommand(); 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); } } else { AlertMsg alertMsg = new AlertMsg { Message = "未勾选要销毁的数据或勾选的数据库存为0", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } } else { AlertMsg alertMsg = new AlertMsg { Message = "没有要操作销毁的数据", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } }); } private void DestoryDialogResult(IDialogResult dialogResult) { if(dialogResult.Result==ButtonResult.Yes) { DestoryAction(); } } //执行销毁操作 private void DestoryAction() { if (csList != null && csList.Count > 0) { csList.ForEach(it => it.Quantity = 0); var f = SqlSugarHelper.Db.UseTran(() => { SqlSugarHelper.Db.Updateable(csList).ExecuteCommand(); List machines = SqlSugarHelper.Db.Queryable() .InnerJoin((mr, cs) => mr.DrugId == cs.DrugId) //.Where(mr => mr.DrugId.Contains(csList.Select(it => it.DrugId).ToList()) && mr.Type = 32) .Where(mr => mr.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")) .Where(mr => mr.Type == 32) .ToList(); SqlSugarHelper.Db.Updateable(machines).ReSetValue(mr => { mr.IsDestroy = 1; }).ExecuteCommand(); 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); } } } private void DoDialogResult(IDialogResult dialogResult) { // 委托 被动执行 被子窗口执行 // dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态 RequestData(); //MessageBox.Show("返回值:" + dialogResult.Result.ToString()); } public bool KeepAlive => false; public DelegateCommand Query { get => new DelegateCommand(() => { RequestData(); }); } //这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作 public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { continuationCallback(true); } //接收导航传过来的参数 public void OnNavigatedTo(NavigationContext navigationContext) { //查询表格数据 RequestData(); } void RequestData() { Channels = SqlSugarHelper.Db.Queryable() .LeftJoin((cs, di) => cs.DrugId == di.DrugId.ToString()) .Where((cs) => cs.DrawerType != 1 && cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")) .Select((cs, di) => new ChannelStock { CanReturnQuantity = SqlFunc.Subqueryable().Where(mr => mr.DrugId == cs.DrugId).Where(mr => mr.Type == 2).Where(mr => mr.Status != 2).Select(mr => SqlFunc.IsNull(SqlFunc.AggregateSumNoNull(mr.Quantity - mr.ReturnQuantity1 - mr.ReturnQuantity2), 0)), DrugInfo = new DrugInfo { DrugId = di.DrugId, DrugName = di.DrugName, DrugSpec = di.DrugSpec, Manufactory = di.Manufactory, PackUnit = di.PackUnit, }, Quantity = cs.Quantity }, true) .OrderBy(cs => cs.DrawerNo) .OrderBy(cs => cs.ColNo) .ToList() ; _ = Channels.Count; } //每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建 public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } //这个方法用于拦截请求 public void OnNavigatedFrom(NavigationContext navigationContext) { } } }