Files
wcs/货架标准上位机/ViewModels/DataListWarnInfoViewModel.cs
2024-10-17 12:59:41 +08:00

207 lines
7.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HandyControl.Controls;
using MiniExcelLibs;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using SqlSugar;
using HandyControl.Data;
using System.Windows;
namespace WCS管理系统.ViewModel
{
public class DataListWarnInfoViewModel : BindableBase
{
private List<WarnInfoItemDb> dataList = new List<WarnInfoItemDb>();
/// <summary>
/// 列表数据
/// </summary>
public List<WarnInfoItemDb> DataList { get => dataList; set { SetProperty(ref dataList, value); } }
#region
private bool isLoad1 = false;
public bool IsLoad1 { get => isLoad1; set { SetProperty(ref isLoad1, value); } }
private bool isLoad2 = false;
public bool IsLoad2 { get => isLoad2; set { SetProperty(ref isLoad2, value); } }
#endregion
#region
private DateTime? timeGo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
/// <summary>
/// 开始时间(默认只显示今天开始的数据)
/// </summary>
public DateTime? TimeGo { get => timeGo; set { SetProperty(ref timeGo, value); } }
private DateTime? timeTo;
/// <summary>
/// 结束时间
/// </summary>
public DateTime? TimeTo { get => timeTo; set { SetProperty(ref timeTo, value); } }
private string info1;
public string Info1 { get => info1; set { SetProperty(ref info1, value); } }
private string info2;
public string Info2 { get => info2; set { SetProperty(ref info2, value); } }
#endregion
#region
private int maxPageCount;
/// <summary>
/// 最大页数
/// </summary>
public int MaxPageCount { get => maxPageCount; set { SetProperty(ref maxPageCount, value); } }
private int pageIndex = 1;
/// <summary>
/// 当前页
/// </summary>
public int PageIndex { get => pageIndex; set { SetProperty(ref pageIndex, value); } }
private int dataCountPerPage = 20;
/// <summary>
/// 每页的数据量
/// </summary>
public int DataCountPerPage { get => dataCountPerPage; set { SetProperty(ref dataCountPerPage, value); } }
public ICommand PageUpdateCommand { get => new DelegateCommand<FunctionEventArgs<int>>(PageUpdate); }
/// <summary>
/// 页码更新
/// </summary>
public void PageUpdate(FunctionEventArgs<int> page)
{
PageIndex = page.Info;
UpdateList();
}
#endregion
public ICommand ExportExcelCommand { get => new DelegateCommand(ExportExcel); }
/// <summary>
/// 导出为excel
/// </summary>
public async void ExportExcel()
{
if (IsLoad2)
{
Growl.Info("有任务正在进行,请稍等片刻。");
return;
}
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Filter = ".xlsx文件(*.xlsx)|*.xlsx";
sfd.FileName = "警告信息记录信息";
sfd.OverwritePrompt = true;
if (sfd.ShowDialog() != true)
{
IsLoad1 = false;
return;
}
string path = sfd.FileName;
try
{
//1.查询数据库,加入筛选条件,并按照时间倒序排序,并导出指定列
var dbData = WarnInfoDb.db.Queryable<WarnInfoItemDb>()
.WhereIF(TimeGo != null, o => o.TimeGo > TimeGo)
.WhereIF(TimeTo != null, o => o.TimeGo < TimeTo)
.WhereIF(!string.IsNullOrWhiteSpace(Info1), o => o.Source.StartsWith(Info1.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(Info2), o => o.Level.StartsWith(Info2.Trim()))
.OrderByDescending(o => o.TimeGo)
.Select(o => new
{
= o.Source,
= o.Text,
= o.Level,
= o.Solution,
= o.WarnType,
= o.TimeGo.ToString("yyyy-MM-dd HH:mm:ss"),
= o.TimeTo.HasValue ? o.TimeTo.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
= o.DuraTime,
});
//2.导出为Excel使用内存缓存的方式防止数据量过大导致内存泄露
var sqlkv = dbData.ToSql();
var dataReader = await DataDb.db.Ado.GetDataReaderAsync(sqlkv.Key, sqlkv.Value);
await MiniExcel.SaveAsAsync(path, dataReader, overwriteFile: true);
dataReader.Close();
Growl.Success("导出成功。");
}
catch (Exception ex)
{
Growl.Error("导出失败:" + ex.Message);
}
finally
{
IsLoad1 = false;
}
}
public ICommand UpdateListCommand { get => new DelegateCommand(UpdateList); }
/// <summary>
/// 更新信息
/// </summary>
public async void UpdateList()
{
if (IsLoad1)
{
Growl.Info("有任务正在进行,请稍等片刻。");
return;
}
try
{
await Task.Delay(200);
//1.查询数据库,加入筛选条件,并按照时间倒序排序
var dbData = WarnInfoDb.db.Queryable<WarnInfoItemDb>()
.WhereIF(TimeGo != null, o => o.TimeGo > TimeGo)
.WhereIF(TimeTo != null, o => o.TimeGo < TimeTo)
.WhereIF(!string.IsNullOrWhiteSpace(Info1), o => o.Source.StartsWith(Info1.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(Info2), o => o.Level.StartsWith(Info2.Trim()))
.OrderByDescending(o => o.TimeGo);
//2.开始分页(如模型不一样使用‘.Select<Model>()’来转换)
RefAsync<int> totalNumber = 0;
RefAsync<int> totalPage = 0;
DataList = await dbData.ToPageListAsync(PageIndex, DataCountPerPage, totalNumber, totalPage);
MaxPageCount = totalPage.Value;
}
catch (Exception ex)
{
Growl.Error("加载数据失败:" + ex.Message);
}
finally
{
IsLoad2 = false;
}
}
public ICommand SeeCommand { get => new DelegateCommand<MyTestTable>(See); }
/// <summary>
/// 查看详情
/// </summary>
public void See(MyTestTable obj)
{
if (IsLoad1 || IsLoad2)
{
Growl.Info("有任务正在进行,请稍等片刻。");
return;
}
Growl.Info("信息:" + Newtonsoft.Json.JsonConvert.SerializeObject(obj));
}
}
}