WuHan/DM_Weight/ViewModels/ReturnEmptyWindowViewModel.cs

333 lines
13 KiB
C#
Raw Normal View History

2023-11-13 14:33:38 +08:00
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;
2023-11-13 14:33:38 +08:00
namespace DM_Weight.ViewModels
{
public class ReturnEmptyWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
{
2023-11-13 14:33:38 +08:00
private List<ChannelStock>? _channelStocks;
public List<ChannelStock>? 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<IDialogResult> RequestClose;
2023-11-13 14:33:38 +08:00
IDialogService _dialogService;
IEventAggregator _eventAggregator;
public ReturnEmptyWindowViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
2023-11-13 14:33:38 +08:00
{
_dialogService = dialogService;
_eventAggregator = eventAggregator;
2023-11-13 14:33:38 +08:00
}
public DelegateCommand RowSelected
{
get => new DelegateCommand(() =>
{
if (Channel != null && Channel.DrugId != null)
2023-11-13 14:33:38 +08:00
{
Channels = Channels.Select(x =>
{
if (x.Id == Channel.Id)
{
x.IsSelected = !x.IsSelected;
}
return x;
}).ToList();
}
else if(Channel != null && Channel.DrugId == null)
{
2023-11-13 14:33:38 +08:00
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<ChannelStock> 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
{
2024-06-28 10:37:26 +08:00
Message = "请选择一条且可归还数量不为0的数据进行归还",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
else
{
DialogParameters dialogParameters = new DialogParameters();
dialogParameters.Add("channel", records.Single(rs => rs.CanReturnQuantity > 0));
DialogServiceExtensions.ShowDialogHost(_dialogService, "ReturnEmptyDialog", dialogParameters, DoDialogResult, "RootDialog");
}
}
else
2023-11-13 14:33:38 +08:00
{
AlertMsg alertMsg = new AlertMsg
{
Message = "请选择要还的空瓶",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
2023-11-13 14:33:38 +08:00
}
});
}
private List<ChannelStock> csList=new List<ChannelStock>();
//销毁
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<string> msg = new List<string>();
//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<MachineRecord> machines = SqlSugarHelper.Db.Queryable<MachineRecord>()
//.InnerJoin<ChannelStock>((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<SnackbarEvent>().Publish(alertMsg);
}
if (!f.IsSuccess)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "销毁失败!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "未勾选要销毁的数据或勾选的数据库存为0",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "没有要操作销毁的数据",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().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<MachineRecord> machines = SqlSugarHelper.Db.Queryable<MachineRecord>()
.InnerJoin<ChannelStock>((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<SnackbarEvent>().Publish(alertMsg);
}
if (!f.IsSuccess)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "销毁失败!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
}
2023-11-13 14:33:38 +08:00
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<bool> continuationCallback)
{
continuationCallback(true);
}
//接收导航传过来的参数
public void OnNavigatedTo(NavigationContext navigationContext)
{
2024-06-28 10:37:26 +08:00
//查询表格数据
RequestData();
2023-11-13 14:33:38 +08:00
}
void RequestData()
{
Channels = SqlSugarHelper.Db.Queryable<ChannelStock>()
.LeftJoin<DrugInfo>((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<MachineRecord>().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)),
2023-11-13 14:33:38 +08:00
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)
{
}
}
}