using Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.Model;
using Aucma.Core.Palletiz.Business;
using Aucma.Core.Palletiz.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Elasticsearch.Net;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
namespace Aucma.Core.Palletiz.ViewModels
{
public partial class StatisticsPageViewModel : ObservableObject
{
///
/// 入库数量统计
///
public delegate void CountInstore(int count);
public static event CountInstore? CountInstoreEvent;
// private static readonly Lazy lazy = new Lazy(() => new StatisticsPageViewModel());
// public static StatisticsPageViewModel Instance => lazy.Value;
public StatisticsPageViewModel()
{
MainWindowViewModel.RefreshInfoEvent += LoadData;
InStoreBusiness.AddDataEvent += AddData;
LoadData();
}
private readonly IRecordInStoreServices? _recordInstoreServices = App.ServiceProvider.GetService();
List materialComplateInfos = new List();
public string storeCodeA = Appsettings.app("StoreInfo", "PalletizStoreCodeA");
public string storeCodeB = Appsettings.app("StoreInfo", "PalletizStoreCodeB");
///
/// 入库时DataGrid添加一条记录
///
///
public void AddData(RecordInStore record)
{
App.Current.Dispatcher.Invoke(() =>
{
MaterialDataGrid.Add(record);
CountInstoreEvent?.Invoke(MaterialDataGrid.Count);
});
}
#region 加载DataGrid数据
private async Task LoadData()
{
await Task.Run(() =>
{
try
{
List list = _recordInstoreServices.QueryAsync(x => (x.StoreCode == storeCodeA || x.StoreCode == storeCodeB) && x.InStoreTime >= DateTime.Today).Result;
if (list != null && list.Count > 0)
{
App.Current.Dispatcher.Invoke(() =>
{
MaterialDataGrid.Clear();
CountInstoreEvent?.Invoke(list.Count);
list.OrderByDescending(x => x.InStoreTime);
foreach (RecordInStore record in list)
{
MaterialDataGrid.Add(record);
}
});
}
}
catch (Exception ex)
{
Console.WriteLine("" + ex.Message.ToString());
}
});
//MaterialDataGrid.Add(new MaterialComplateInfo() { No = 1, ProductPlanCode = "8659452123",MaterialCode = "8659452123", MaterialName = "SC-AUCMA-农夫山泉,SC", PlanAmount = 50, CompleteAmount = 10 });
}
#endregion
#region 初始化datagrid
private ObservableCollection materialDataGrid = new ObservableCollection();
public ObservableCollection MaterialDataGrid
{
get { return materialDataGrid; }
set
{
materialDataGrid = value;
OnPropertyChanged();//属性通知
}
}
#endregion
#region 查询
///
/// 查询
///
[RelayCommand]
private async Task ExecQuery(object obj)
{
List list = null;
await Task.Run(() =>
{
string result = (string)obj;
if(string.IsNullOrEmpty(result))
{
list = _recordInstoreServices.QueryAsync(x => (x.StoreCode == storeCodeA || x.StoreCode == storeCodeB) && x.InStoreTime >= DateTime.Today).Result;
}
else
{
list = _recordInstoreServices.QueryAsync(x => (x.StoreCode == storeCodeA || x.StoreCode == storeCodeB) && x.BarCodeCode.Contains(result)).Result;
}
if (list != null && list.Count > 0)
{
App.Current.Dispatcher.Invoke(() =>
{
MaterialDataGrid.Clear();
foreach (RecordInStore record in list)
{
MaterialDataGrid.Add(record);
}
});
}
});
}
#endregion
}
}