Files
SCRGroupLine/GroupLine.App/ViewModel/KPReliefValveAsmViewModel.cs
2026-03-19 17:29:32 +08:00

235 lines
9.1 KiB
C#

using AutoMapper;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GroupLine.App.ModelDto;
using GroupLine.Model;
using NLog;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using MessageBox = System.Windows.MessageBox;
namespace GroupLine.App.ViewModel
{
public class KPReliefValveAsmViewModel : ViewModelBase
{
private static Logger _Logger = LogManager.GetCurrentClassLogger();
private string CurrentMachineName = "KP溢流阀组装机";
private string CurrentMachineNameTemplate = "KPReliefValveAsmTemplate.xlsx";
private IMapper autoMapper;
public KPReliefValveAsmViewModel()
{
ListModelDto = new ObservableCollection<KPReliefValveAsmDto>();
SearchStartDate = DateTime.Now.AddDays(-1).ToShortDateString();
SearchEndDate = DateTime.Now.AddDays(2).ToShortDateString();
MachineName = CurrentMachineName + " - 搜索条件";
var config = new MapperConfiguration(cfg => cfg.CreateMap<KPReliefValveAsm, KPReliefValveAsmDto>()
.ForMember(dest => dest.Result, opt => opt.MapFrom(src => src.Result.ToString())));
autoMapper = config.CreateMapper();
}
private ObservableCollection<KPReliefValveAsmDto> _ListModelDto;
public ObservableCollection<KPReliefValveAsmDto> ListModelDto
{
get { return _ListModelDto; }
set { _ListModelDto = value; }
}
private string machineName;
public string MachineName
{
get { return machineName; }
set { machineName = value; RaisePropertyChanged(() => MachineName); }
}
private string searchMvDiscNo;
public string SearchMvDiscNo
{
get { return searchMvDiscNo; }
set { searchMvDiscNo = value; RaisePropertyChanged(() => SearchMvDiscNo); }
}
private string searchStartDate;
public string SearchStartDate
{
get { return searchStartDate; }
set { searchStartDate = value; RaisePropertyChanged(() => SearchStartDate); }
}
private string searchEndDate;
public string SearchEndDate
{
get { return searchEndDate; }
set { searchEndDate = value; RaisePropertyChanged(() => SearchEndDate); }
}
private RelayCommand searchCmd;
public RelayCommand SearchCmd
{
get
{
if (searchCmd == null) return new RelayCommand(() => Search());
return searchCmd;
}
set { searchCmd = value; }
}
private void Search()
{
try
{
var mulConQueryable = FSqlContext.FDb.Select<KPReliefValveAsm>();
if (!string.IsNullOrEmpty(SearchMvDiscNo))
{
mulConQueryable = mulConQueryable.Where(t => t.MvDiscNo.Contains(SearchMvDiscNo));
}
if (!string.IsNullOrEmpty(SearchStartDate))
{
mulConQueryable = mulConQueryable.Where(t => t.CreateTime.Date >= Convert.ToDateTime(SearchStartDate));
}
if (!string.IsNullOrEmpty(SearchEndDate))
{
mulConQueryable = mulConQueryable.Where(t => t.CreateTime.Date < Convert.ToDateTime(SearchEndDate).AddDays(1));
}
var listData = mulConQueryable.OrderByDescending(a => a.CreateTime).ToList();
ListModelDto.Clear();
foreach (var item in listData)
{
ListModelDto.Add(autoMapper.Map<KPReliefValveAsmDto>(item));
}
}
catch (Exception ex)
{
_Logger.Error(string.Format("ErrSource : {0} ErrMsg : {1}", ex.StackTrace, ex.Message));
}
}
private RelayCommand _OutputDataCmd;
public RelayCommand OutputDataCmd
{
get
{
if (_OutputDataCmd == null) return new RelayCommand(() => OutputDataAction());
return _OutputDataCmd;
}
set { _OutputDataCmd = value; }
}
private void OutputDataAction()
{
try
{
if (ListModelDto != null && ListModelDto.Count > 0)
{
string filePath = string.Empty;
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件夹";
dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(dialog.SelectedPath))
{
MessageBox.Show("文件夹路径不能为空", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
filePath = dialog.SelectedPath;
}
var listData = ListModelDto.ToList();
string templateFilePath = Environment.CurrentDirectory;
string tPath = templateFilePath + @"\ReportFile\" + CurrentMachineNameTemplate;
XSSFWorkbook wk;
using (FileStream fs = File.Open(tPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
wk = new XSSFWorkbook(fs);
fs.Close();
}
ISheet sheet = wk.GetSheetAt(0);
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++)
{
IRow row = sheet.CreateRow(i + 1);
row.CreateCell(0).SetCellValue(listData[i].ModelName.ToString());
row.CreateCell(1).SetCellValue(listData[i].MvDiscNo.ToString());
row.CreateCell(2).SetCellValue(listData[i].StDiscNo.ToString());
row.CreateCell(3).SetCellValue(listData[i].CycleTime.ToString());
row.CreateCell(4).SetCellValue(listData[i].ScrewCount.ToString());
row.CreateCell(5).SetCellValue(listData[i].Result.ToString());
row.CreateCell(6).SetCellValue(listData[i].FinalTorque1.ToString());
row.CreateCell(7).SetCellValue(listData[i].FinalAngle1.ToString());
row.CreateCell(8).SetCellValue(listData[i].FinalTorque2.ToString());
row.CreateCell(9).SetCellValue(listData[i].FinalAngle2.ToString());
row.CreateCell(10).SetCellValue(listData[i].FinalTorque3.ToString());
row.CreateCell(11).SetCellValue(listData[i].FinalAngle3.ToString());
row.CreateCell(12).SetCellValue(listData[i].FinalTorque4.ToString());
row.CreateCell(13).SetCellValue(listData[i].FinalAngle4.ToString());
row.CreateCell(14).SetCellValue(listData[i].OperatorNo.ToString());
row.CreateCell(15).SetCellValue(listData[i].CreateTime.ToString("yyyy-MM-dd HH:mm"));
style0.DataFormat = dataformat.GetFormat("yyyy-MM-dd HH:mm:ss");
row.GetCell(15).CellStyle = style0;
}
using (MemoryStream ms = new MemoryStream())
{
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
filePath = filePath + @"\" + Convert.ToDateTime(SearchStartDate).ToString("yyyy-MM-dd") + "-" + Convert.ToDateTime(SearchEndDate).ToString("yyyy-MM-dd") + "-" + CurrentMachineName + ".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();
}
}
MessageBox.Show("生成成功");
}
else
{
MessageBox.Show("请先搜索数据后再导出", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
_Logger.Error(string.Format("ErrSource : {0} ErrMsg : {1}", ex.StackTrace, ex.Message));
}
}
}
}