using Admin.Core.IService;
using Admin.Core.Model.ViewModels;
using Aucma.Core.PrintTo.Models;
using Aucma.Core.PrintTo.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using log4net;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
/*
* 首页信息
*
*/
namespace Aucma.Core.PrintTo.ViewModels
{
public partial class IndexPageViewModel : ObservableObject
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(IndexPageViewModel));
protected readonly IBaseOrderInfoServices _baseOrderInfoServices;
///
/// 打印类型
///
public int printType { get; set; }
public IndexPageViewModel()
{
_baseOrderInfoServices = App.ServiceProvider.GetService();
PrintIsEnabled = "True";
WeakReferenceMessenger.Default.Register(this, Recive);
LoadData();
}
#region 加载DataGrid数据
private async void LoadData()
{
try
{
Datalist.Clear();
List list = await _baseOrderInfoServices.QueryPrintInfo();
if (list == null) return;
list= list.OrderBy(d => d.CreateTime).ToList();
foreach (var item in list)
{
OrderInfo order = new OrderInfo();
order.CreatedTime = item.CreateTime;
order.OrderCode = item.OrderCode;
order.ProductCode = item.ProductCode;
order.ProductName = item.ProductName;
order.PlanAmount = item.PlanAmount;
order.CompleteAmount = item.CompleteAmount;
order.MaterialCode = item.MaterialCode;
order.MaterialName = item.MaterialName;
order.StandardAmount = item.StandardAmount;
order.LinerAmount = item.LinerAmount;
order.BoxAmount = item.StandardAmount;
order.ErrorNum = item.ErrorAmount;
order.LinerCodeNum = item.LinerAmount;
order.BoxCodeNum = item.BoxAmount;
order.PrintName = item.PrintName;
datalist.Add(order);
}
}
catch (Exception ex)
{
log.Error(ex.Message);
}
}
private async void LoadData(string queryStr)
{
Datalist.Clear();
List list = await _baseOrderInfoServices.QueryPrintInfo();
if (list.Count() == 0) return;
List pintList = list.Where(d => d.OrderCode.Contains(queryStr) || d.ProductCode.Contains(queryStr) || d.ProductName.Contains(queryStr)).ToList();
foreach (var item in pintList)
{
OrderInfo order = new OrderInfo();
order.CreatedTime = item.CreateTime;
order.OrderCode = item.OrderCode;
order.ProductCode = item.ProductCode;
order.ProductName = item.ProductName;
order.PlanAmount = item.PlanAmount;
order.CompleteAmount = item.CompleteAmount;
order.MaterialCode = item.MaterialCode;
order.MaterialName = item.MaterialName;
order.StandardAmount = item.StandardAmount;
order.LinerAmount = item.LinerAmount;
order.BoxAmount = item.StandardAmount;
order.ErrorNum = item.ErrorAmount;
order.LinerCodeNum = item.LinerAmount;
order.BoxCodeNum = item.BoxAmount;
order.PrintName = item.PrintName;
datalist.Add(order);
}
}
#endregion
#region 查询按钮
///
/// 查询
///
[RelayCommand]
private void QueryString(string queryStr)
{
List orderList = null;
try
{
if (string.IsNullOrEmpty(queryStr))
{
LoadData();
return;
}
System.Windows.Application.Current.Dispatcher.Invoke((Action)(async () =>
{
Datalist.Clear();
LoadData(queryStr);
//Datalist.Insert(0, Datalist[Datalist.Count - 1]);
//Datalist.RemoveAt(Datalist.Count - 1);
}));
}
catch (Exception ex)
{
log.Error("QueryString方法出现异常");
}
}
#endregion
#region radio查询按钮
///
/// 查询
///
[RelayCommand]
private void RadioQuery(string type)
{
if (type== "内胆")
{
printType = 200;
}
if (type == "箱体")
{
printType = 500;
}
}
#endregion
#region 打印
///
/// 打印
///
[RelayCommand]
private void Print(string obj)
{
if (string.IsNullOrEmpty(obj))
{
MessageBox.Show("请选中需要打印的产品!","系统提醒");
return;
}
if (string.IsNullOrEmpty(SelectedCells.PrintName))
{
MessageBox.Show("打印名称不可为空!", "系统提醒");
return;
}
if (string.IsNullOrEmpty(SelectedCells.PrintName))
{
MessageBox.Show("打印名称不可为空!", "系统提醒");
return;
}
try
{
string order_code = SelectedCells.OrderCode;
string product_code = SelectedCells.ProductCode;
string product_name = SelectedCells.ProductName;
//string material_code = SelectedCells.MaterialCode;
//string material_name = SelectedCells.MaterialName;
int standard_amount = SelectedCells.StandardAmount;
string printName = SelectedCells.PrintName;
PrintIsEnabled = "False";
PrintToDevView printToDev = new PrintToDevView(order_code,product_code, product_name, standard_amount, printName,printType);
printToDev.ShowDialog();
}
catch (Exception ex)
{
PrintIsEnabled = "True";
log.Error(ex.Message);
}
}
#endregion
#region 补打
///
///补打
///
[RelayCommand]
private void SupplementPrint(string obj)
{
if (string.IsNullOrEmpty(obj))
{
MessageBox.Show("请选中要补打的数据!", "系统提醒");
return;
}
string order_code = SelectedCells.OrderCode;
string product_code = SelectedCells.ProductCode;
string product_name = SelectedCells.ProductName;
string material_code = SelectedCells.MaterialCode;
string material_name = SelectedCells.MaterialName;
int standard_amount = SelectedCells.StandardAmount;
SupplementPrintPageView printToDev = new SupplementPrintPageView(order_code,product_code, material_code, material_name);
printToDev.ShowDialog();
}
#endregion
#region 追打
///
///追打
///
[RelayCommand]
private void AppendPrintPrint(string obj)
{
if (string.IsNullOrEmpty(obj))
{
MessageBox.Show("请选中需要打印的产品!", "系统提醒");
return;
}
//if (!CheckPrintPassword())
//{
// return;
//}
try
{
string order_code = SelectedCells.OrderCode;
string product_code = SelectedCells.ProductCode;
string product_name = SelectedCells.ProductName;
string material_code = SelectedCells.MaterialCode;
string material_name = SelectedCells.MaterialName;
int standard_amount = SelectedCells.StandardAmount;
string printName = SelectedCells.PrintName;
PrintIsEnabled = "False";
PrintToDevView printToDev = new PrintToDevView(order_code, product_code, product_name, standard_amount, printName, printType);
printToDev.ShowDialog();
}
catch (Exception ex)
{
PrintIsEnabled = "True";
log.Error(ex.Message);
}
}
#endregion
#region 特殊打印
///
///特殊打印
///
[RelayCommand]
private Task SpecialPrint()
{
try
{
SpecialPrintView print = new SpecialPrintView();
print.ShowDialog();
}
catch (Exception ex)
{
log.Error(ex.Message);
}
return Task.CompletedTask;
}
#endregion
#region 初始化datagrid
private ObservableCollection datalist = new ObservableCollection();
public ObservableCollection Datalist
{
get { return datalist; }
set
{
datalist = value;
OnPropertyChanged();//属性通知
}
}
#endregion
#region 获取当前行数据 赋值到textbox
private OrderInfo selectedCells;
public OrderInfo SelectedCells
{
get { return selectedCells; }
set
{
selectedCells = value;
SetProperty(ref selectedCells, value);
}
}
#endregion
#region 常规打印按钮状态
private string _printIsEnabled;
public string PrintIsEnabled
{
get { return _printIsEnabled; }
set
{
_printIsEnabled = value;
SetProperty(ref _printIsEnabled, value);//属性通知
}
}
#endregion
#region 刷新列表-其他界面刷新该方法
///
/// 刷新列表
///
///
///
private void Recive(object recipient, string message)
{
if (message == "Refresh")
{
PrintIsEnabled = "True";
Datalist.Clear();
LoadData();
}
}
#endregion
}
}