using Common.Logging; using DM_Weight.Models; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.util; using log4net; using MaterialDesignThemes.Wpf; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Media; namespace DM_Weight.ViewModels { public class CheckStockWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime { private readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(CheckStockWindowViewModel)); private int _drawerNo = 0; public int DrawerNo { get => _drawerNo; set => SetProperty(ref _drawerNo, value); } private SolidColorBrush _colorBrush; public SolidColorBrush SnackbarBackground { get => _colorBrush; set => SetProperty(ref _colorBrush, value); } private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3)); public ISnackbarMessageQueue SnackbarMessageQueue { get => _snackbarMessageQueue; set => SetProperty(ref _snackbarMessageQueue, value); } private List _channelStocks = new(); public List ChannelStocks { get => _channelStocks; set => SetProperty(ref _channelStocks, value); } private ChannelStock _channelStock; public ChannelStock _ChannelStock { get => _channelStock; set => SetProperty(ref _channelStock, value); } private bool _isEnable = true; public bool IsEnable { get => _isEnable; set => SetProperty(ref _isEnable, value); } private int _status = 0; public int Status { get => _status; set { SetProperty(ref _status, value); } } private readonly IDialogService _dialogService; public bool KeepAlive => false; IEventAggregator _eventAggregator; public CheckStockWindowViewModel(IDialogService dialogService, IEventAggregator eventAggregator) { _dialogService = dialogService; _eventAggregator = eventAggregator; } private void RequestData() { List queryData = SqlSugarHelper.Db.Queryable() .Includes(cs => cs.DrugInfo) .Where(cs => cs.DrawerNo == DrawerNo + 1) .Where(cs => cs.DrugId != null) .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5")) .OrderBy(cs => cs.DrugId) .ToList(); ChannelStocks = queryData.Select(it => { it.CheckQuantity = it.Quantity; return it; }).ToList(); } public void OnNavigatedTo(NavigationContext navigationContext) { logger.Info("进入OnNavigatedTo"); //删除药品有批次但是库存为0的药品 List csList = SqlSugarHelper.Db.Queryable().Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cs.Quantity <= 0 && !string.IsNullOrEmpty(cs.ManuNo)).ToList(); if (csList != null && csList.Count > 0) { SqlSugarHelper.Db.Deleteable().Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cs.Quantity <= 0 && !string.IsNullOrEmpty(cs.ManuNo)).ExecuteCommand(); for (int i = 0; i < csList.Count; i++) { //如果channel_stock下无数据则channel_list同步删除 int iListCount= SqlSugarHelper.Db.Queryable().Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cs.DrawerNo==csList[i].DrawerNo&& cs.DrugId == csList[i].DrugId).Count(); if (iListCount <=0) SqlSugarHelper.Db.Deleteable().Where(cl => cl.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cl.Id == csList[i].Chnguid).ExecuteCommand(); } } RequestData(); logger.Info("结束RequestData"); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { // 取消消息订阅 //_eventAggregator.GetEvent().Unsubscribe(DoMyPrismEvent); } public DelegateCommand UpdateDrawerNo { get => new DelegateCommand((DrawerNo) => { this.DrawerNo = Convert.ToInt32(DrawerNo); RequestData(); } ); } private static readonly DateTime Jan1st1970 = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); public long CurrentTimeMillis() { return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds; } //保存修改的库存数 public DelegateCommand SaveCommand { get => new DelegateCommand(() => { List record = ChannelStocks.FindAll(it => it.Quantity != it.CheckQuantity).ToList(); if (record.Count > 0) { bool iFlag = true; string InvoiceId = "CHECK_" + CurrentTimeMillis(); var f = SqlSugarHelper.Db.UseTran(() => { for (int i = 0; i < record.Count; i++) { iFlag = true; ChannelStock it = record[i]; if (it.AddToJJNum > 0 || it.NeedNum > 0) { //有待取药或待入库药品,弹出提示,确认后保存 DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("ConfirmInfo", $"所选药品{it.DrugInfo.DrugName}存在待取药或待入库数据是否确认保存?"); DialogServiceExtensions.ShowDialogHost(_dialogService, "ConfirmDialog", dialogParameters, (IDialogResult dialogResult) => { if (dialogResult.Result == ButtonResult.OK) { iFlag = true; } else { iFlag = false; } }, "RootDialog"); } if (iFlag) { // 更新数据 库存信息 SqlSugarHelper.Db.Updateable(new ChannelStock() { Quantity = it.CheckQuantity, ManuNo = it.ManuNo, EffDate = it.EffDate, Id = it.Id, NeedNum = 0, AddToJJNum = 0 }).UpdateColumns(it => new { it.Quantity, it.ManuNo, it.EffDate, it.NeedNum, it.AddToJJNum }).ExecuteCommand(); SqlSugarHelper.Db.Updateable(new ChannelList() { State = 0, Id = it.Chnguid }).UpdateColumns(cl => new { cl.State }).ExecuteCommand(); // 保存数据 盘点记录 SqlSugarHelper.Db.Insertable(new MachineRecord() { MachineId = it.MachineId, DrawerNo = it.DrawerNo, ColNo = it.ColNo, DrugId = it.DrugId, ManuNo = it.ManuNo, EffDate = !String.IsNullOrEmpty(it.EffDate) ? DateTime.ParseExact(it.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, Operator = HomeWindowViewModel.Operator?.Id, Reviewer = HomeWindowViewModel.Reviewer?.Id, OperationTime = DateTime.Now, Quantity = it.CheckQuantity - it.Quantity, Type = 4, InvoiceId = $"{it.DrawerNo}{it.DrugInfo.DrugName}盘前库存{it.Quantity},后{it.CheckQuantity}" // InvoiceId //,StockQuantity = nowChannels.Sum(it => it.Quantity), //CheckQuantity = it.CheckQuantity }).ExecuteCommand(); logger.Info($"库存盘点->药箱号【{it.DrawerNo}】药品【{it.DrugInfo.DrugName}】盘点前库存【{it.Quantity}】,更改后【{it.CheckQuantity}】"); } else { return false; break; } } return true; }); if (!iFlag) { return; } 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); Status = 0; } }); } //刷新 public DelegateCommand Query { get => new DelegateCommand(RequestData); } public DelegateCommand RowSelected { get => new DelegateCommand(() => { if (_channelStock != null) { } }); } } }