using AutoMapper;
using CapMachine.Core;
using CapMachine.Model;
using CapMachine.Model.Alarm;
using CapMachine.Wpf.Dtos;
using CapMachine.Wpf.Models;
using CapMachine.Wpf.Services;
using Microsoft.Extensions.Logging;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Prism.Events;
using Prism.Services.Dialogs;
using Syncfusion.Windows.Shared;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace CapMachine.Wpf.ViewModels
{
public class ActionLogViewModel : NavigationViewModel
{
///
/// 实例化
///
///
///
///
public ActionLogViewModel(IDialogService dialogService, IFreeSql freeSql, IEventAggregator eventAggregator, IMapper mapper, ILogService logger)
{
DialogService = dialogService;
FreeSql = freeSql;
EventAggregator = eventAggregator;
Mapper = mapper;
Logger = logger;
CategoryComboBoxList = new List()
{
new ComboBoxModel(){Key="0",Text="系统日志" },
new ComboBoxModel(){Key="1",Text="程序步骤" },
new ComboBoxModel(){Key="2",Text="历史报警" },
};
}
private string CurrentName = "";
private string CurrentTemplate = "BurPlugInstallTemplate.xlsx";
public IDialogService DialogService { get; }
public IFreeSql FreeSql { get; }
public IEventAggregator EventAggregator { get; }
public IMapper Mapper { get; }
public ILogService Logger { get; }
//ActionLog
private List _CategoryComboBoxList;
///
/// 分类下拉框列表
///
public List CategoryComboBoxList
{
get { return _CategoryComboBoxList; }
set { _CategoryComboBoxList = value; RaisePropertyChanged(); }
}
private string _SearchCategory;
///
/// 搜索条件-分类
///
public string SearchCategory
{
get
{
return _SearchCategory;
}
set
{
_SearchCategory = value;
RaisePropertyChanged();
}
}
///
/// 列表集合
///
private ObservableCollection _ListModelDto = new ObservableCollection();
///
/// 列表集合
///
public ObservableCollection ListModelDto
{
get { return _ListModelDto; }
set { _ListModelDto = value; RaisePropertyChanged(); }
}
#region 搜索条件属性
///
/// 分类
///
private string _Category;
public string Category
{
get
{
return _Category;
}
set
{
_Category = value;
RaisePropertyChanged();
}
}
/////
///// 搜索条件-筒体编号
/////
//private string searchCylinderNo;
//public string SearchCylinderNo
//{
// get
// {
// return searchCylinderNo;
// }
// set
// {
// searchCylinderNo = value;
// RaisePropertyChanged();
// }
//}
///
/// 搜索条件-开始时间
///
private string _SearchStartDate;
public string SearchStartDate
{
get
{
return _SearchStartDate;
}
set
{
_SearchStartDate = value;
RaisePropertyChanged();
}
}
///
/// 搜索条件-结束时间
///
private string _SearchEndDate;
public string SearchEndDate
{
get
{
return _SearchEndDate;
}
set
{
_SearchEndDate = value;
RaisePropertyChanged();
}
}
#endregion
#region 搜索命令
///
/// 搜索命令
///
private DelegateCommand _SearchCmd;
public DelegateCommand SearchCmd
{
get
{
if (_SearchCmd == null) return new DelegateCommand((a) => Search());
return _SearchCmd;
}
set
{
_SearchCmd = value;
}
}
private void Search()
{
try
{
switch (SearchCategory)
{
case "历史报警":
var MulConHistoryAlarmQueryable = FreeSql.Select();
//多条件查询
if (!string.IsNullOrEmpty(SearchStartDate))
{
MulConHistoryAlarmQueryable = MulConHistoryAlarmQueryable.Where(t => t.CreateTime.Date >= Convert.ToDateTime(SearchStartDate));
}
//多条件查询
if (!string.IsNullOrEmpty(SearchEndDate))
{
MulConHistoryAlarmQueryable = MulConHistoryAlarmQueryable.Where(t => t.CreateTime.Date < Convert.ToDateTime(SearchEndDate).AddDays(1));
}
var ListHistoryAlarmDpI = MulConHistoryAlarmQueryable.OrderByDescending(a => a.CreateTime).ToList();//.Where(a => a.CreateTime >= DateTime.Now);
ListModelDto = new ObservableCollection(Mapper.Map>(ListHistoryAlarmDpI));
break;
case "系统日志":
//ActionLog
var MulConActionLogQueryable = FreeSql.Select();
//多条件查询
if (!string.IsNullOrEmpty(SearchStartDate))
{
MulConActionLogQueryable = MulConActionLogQueryable.Where(t => t.CreateTime.Date >= Convert.ToDateTime(SearchStartDate));
}
//多条件查询
if (!string.IsNullOrEmpty(SearchEndDate))
{
MulConActionLogQueryable = MulConActionLogQueryable.Where(t => t.CreateTime.Date < Convert.ToDateTime(SearchEndDate).AddDays(1));
}
var ListActionLogDpI = MulConActionLogQueryable.OrderByDescending(a => a.CreateTime).ToList();//.Where(a => a.CreateTime >= DateTime.Now);
ListModelDto = new ObservableCollection(Mapper.Map>(ListActionLogDpI));
break;
default:
break;
}
}
catch (Exception ex)
{
Logger.Error(String.Format("ErrSource : {0} ErrMsg : {1}", ex.StackTrace.ToString(), ex.Message.ToString()));
}
}
#endregion
#region "导出数据"
///
/// 搜索命令
///
private DelegateCommand _OutputDataCmd;
public DelegateCommand OutputDataCmd
{
set
{
_OutputDataCmd = value;
}
get
{
if (_OutputDataCmd == null)
{
_OutputDataCmd = new DelegateCommand(a => OutputDataAction());
}
return _OutputDataCmd;
}
}
private void OutputDataAction()
{
return;
try
{
if (ListModelDto != null && ListModelDto.Count > 0)
{
string FilePath = string.Empty;
//FolderBrowserDialog dialog = new FolderBrowserDialog();
//dialog.Description = "请选择文件夹";
//dialog.ShowNewFolderButton = true; // 显示 新建文件夹 按钮
// //dialog.SelectedPath = Environment.CurrentDirectory; // 设置 选择的路径 为 当前项目路径
//if (dialog.ShowDialog() == DialogResult.OK)
//{
// // 判空
// if (string.IsNullOrEmpty(dialog.SelectedPath)) { MessageBox.Show("文件夹路径不能为空", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information); return; }
// FilePath = dialog.SelectedPath; // 获取 选择的 文件夹 路径
//}
var ListData = ListModelDto.ToList();
/***********************
1.读取excel到workbook中
***********************/
string TemplateFilePath = System.Environment.CurrentDirectory;
string tPath = TemplateFilePath + @"\ReportFile\" + CurrentTemplate;
//string tPath = @"D:\ReportTemplate\注油Template.xlsx";
XSSFWorkbook wk = null;
using (FileStream fs = File.Open(tPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
wk = new XSSFWorkbook(fs);
fs.Close(); //把excel里的内容保存到workbook中之后,就可以关闭了
}
NPOI.SS.UserModel.ISheet sheet = wk.GetSheetAt(0); //获得wk中第一个sheet,保存到sheet中
/***********************
2.操作数据
***********************/
//2行 5行
IDataFormat dataformat = wk.CreateDataFormat();
ICellStyle style0 = wk.CreateCellStyle();
ICellStyle styleBag = wk.CreateCellStyle();
styleBag.FillForegroundColor = HSSFColor.Red.Index;
styleBag.FillPattern = FillPattern.SolidForeground;
for (int i = 0; i < ListData.Count; i++)// (int i = 2; i < 6; i += 3)
{
IRow row;
row = sheet.CreateRow(i + 1);
//row.CreateCell(0).SetCellValue(ListData[i].StaticDiskNo.ToString());
//row.CreateCell(1).SetCellValue(ListData[i].OpNo.ToString());
//row.CreateCell(2).SetCellValue(ListData[i].CreateTime.ToString("yyyy-MM-dd HH:mm"));
//row.GetCell(10).CellStyle = style0;
style0.DataFormat = dataformat.GetFormat("yyyy-MM-dd HH:mm:ss");
row.GetCell(2).CellStyle = style0;
//row.GetCell(26).CellStyle = style0;
//if (ListDat[i].UpperGroupStandDiffResult == "NG")
//{
// row.GetCell(4).CellStyle = styleBag;
//}
//if (ListDat[i].LowerGroupStandDiffResult == "NG")
//{
// row.GetCell(6).CellStyle = styleBag;
//}
//if (ListDat[i].GroupStandDiffResult == "NG")
//{
// row.GetCell(8).CellStyle = styleBag;
//}
//if (ListDat[i].FillReal >= ListDat[i].BeforLow && ListDat[i].FillReal <= ListDat[i].BeforUp)
//{
//}
//else
//{
// row.GetCell(7).CellStyle = styleBag;
//}
}
//DayDailyParameterInfo = null;
//NightDailyParameterInfo = null;
/***********************
3.修改完成,写入到客户端
***********************/
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
//tPath = @"D:\日立报表\";
//判断文件夹是否存在,不存在就创建
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
FilePath = FilePath + @"\" + Convert.ToDateTime(SearchStartDate).ToString("yyyy-MM-dd") + "-" + Convert.ToDateTime(SearchStartDate).ToString("yyyy-MM-dd") + "-" + CurrentName + ".xlsx";
wk.Write(ms);
using (FileStream fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
}
wk = null;
}
System.Windows.MessageBox.Show("生成成功");
}
else
{
MessageBox.Show("请先搜索数据后再导出", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
Logger.Error(String.Format("ErrSource : {0} ErrMsg : {1}", ex.StackTrace.ToString(), ex.Message.ToString()));
}
}
#endregion
}
}