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 DM_Weight.msg; using Prism.Events; using System.Configuration; using System.Reflection.PortableExecutable; using DM_Weight.Report; 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); } } private DateTime? _startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); public DateTime? StartDate { get => _startDate; set { if (value != null) { SetProperty(ref _startDate, new DateTime(value?.Year ?? 0, value?.Month ?? 0, value?.Day ?? 0)); } else { SetProperty(ref _startDate, value); } RequestData(); } } private DateTime? _endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59); public DateTime? EndDate { get => _endDate; set { if (value != null) { TimeSpan ershisi = new TimeSpan(23, 59, 59); SetProperty(ref _endDate, new DateTime(value?.Year ?? 0, value?.Month ?? 0, value?.Day ?? 0, 23, 59, 59)); } else { SetProperty(ref _endDate, value); } RequestData(); } } 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) { DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("DrawerNo", Channel.DrawerNo); DialogServiceExtensions.ShowDialogHost(_dialogService, "BindingChannelDialog", dialogParameters, DoDialogResult, "RootDialog"); } else if (Channel != null && Channel.CanReturnQuantity > 0) { Channels = Channels.Select(x => { if (x.Id == Channel.Id) { x.IsSelected = !x.IsSelected; } return x; }).ToList(); //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 (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"); } 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"); var f = SqlSugarHelper.Db.UseTran(() => { for (int i = 0; i < csList.Count; i++) { ChannelStock channelStock = csList[i]; channelStock.Quantity = 0; SqlSugarHelper.Db.Updateable(channelStock).ExecuteCommand(); MachineRecord 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.DrugId == channelStock.DrugId) .Where(mr => mr.Type == 32) .First(); SqlSugarHelper.Db.Updateable(machines).ReSetValue(mr => { mr.IsDestroy = 1; mr.TakeUser = HomeWindowViewModel.Operator?.Id.ToString(); mr.DestoryReviewerUser = HomeWindowViewModel.Reviewer?.Id.ToString(); }).ExecuteCommand(); } return true; }); //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); } }); } //空瓶销毁(根据还的空瓶) public DelegateCommand DestoryEmptyCommand { get => new DelegateCommand(DestoryEmptyAction); } private void DestoryEmptyAction() { //List records = Channels.FindAll(it => it.IsSelected).ToList(); //if (records.Count > 0) if (Channel != null && Channel.DrugId != null&&Channel.Quantity>0) { DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("channel", Channel); DialogServiceExtensions.ShowDialogHost(_dialogService, "DestoryEmptyDialog", dialogParameters, DoDialogResult, "RootDialog"); } else { 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 DelegateCommand DownloadRecordCommand { get => new DelegateCommand(() => { GridReportUtil.PrintEmptyDestoryReport(StartDate, EndDate); }); } 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).Where(cs => cs.MachineId == ConfigurationManager.AppSettings["machineId"]) .Select((cs, di) => new ChannelStock { CanReturnQuantity = SqlFunc.Subqueryable().Where(mr => mr.DrugId == cs.DrugId && mr.MachineId == ConfigurationManager.AppSettings["machineId"]).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) { } } }