251 lines
11 KiB
C#
251 lines
11 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 KPExhCoverSealTestViewModel : ViewModelBase
|
|
{
|
|
private static Logger _Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
private string CurrentMachineName = "KP排气盖板安装气密检测";
|
|
|
|
private string CurrentMachineNameTemplate = "KPExhCoverSealTestTemplate.xlsx";
|
|
|
|
private IMapper autoMapper;
|
|
|
|
public KPExhCoverSealTestViewModel()
|
|
{
|
|
ListModelDto = new ObservableCollection<KPExhCoverSealTestDto>();
|
|
SearchStartDate = DateTime.Now.AddDays(-1).ToShortDateString();
|
|
SearchEndDate = DateTime.Now.AddDays(2).ToShortDateString();
|
|
MachineName = CurrentMachineName + " - 搜索条件";
|
|
|
|
var config = new MapperConfiguration(cfg => cfg.CreateMap<KPExhCoverSealTest, KPExhCoverSealTestDto>()
|
|
.ForMember(dest => dest.Result, opt => opt.MapFrom(src => src.Result.ToString())));
|
|
autoMapper = config.CreateMapper();
|
|
}
|
|
|
|
private ObservableCollection<KPExhCoverSealTestDto> _ListModelDto;
|
|
|
|
public ObservableCollection<KPExhCoverSealTestDto> 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<KPExhCoverSealTest>();
|
|
|
|
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<KPExhCoverSealTestDto>(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].FinalTorque5.ToString());
|
|
row.CreateCell(15).SetCellValue(listData[i].FinalAngle5.ToString());
|
|
row.CreateCell(16).SetCellValue(listData[i].FinalTorque6.ToString());
|
|
row.CreateCell(17).SetCellValue(listData[i].FinalAngle6.ToString());
|
|
row.CreateCell(18).SetCellValue(listData[i].FinalTorque7.ToString());
|
|
row.CreateCell(19).SetCellValue(listData[i].FinalAngle7.ToString());
|
|
row.CreateCell(20).SetCellValue(listData[i].FinalTorque8.ToString());
|
|
row.CreateCell(21).SetCellValue(listData[i].FinalAngle8.ToString());
|
|
row.CreateCell(22).SetCellValue(listData[i].FinalTorque9.ToString());
|
|
row.CreateCell(23).SetCellValue(listData[i].FinalAngle9.ToString());
|
|
row.CreateCell(24).SetCellValue(listData[i].FinalTorque10.ToString());
|
|
row.CreateCell(25).SetCellValue(listData[i].FinalAngle10.ToString());
|
|
row.CreateCell(26).SetCellValue(listData[i].FinalTorque11.ToString());
|
|
row.CreateCell(27).SetCellValue(listData[i].FinalAngle11.ToString());
|
|
row.CreateCell(28).SetCellValue(listData[i].FinalTorque12.ToString());
|
|
row.CreateCell(29).SetCellValue(listData[i].FinalAngle12.ToString());
|
|
row.CreateCell(30).SetCellValue(listData[i].OperatorNo.ToString());
|
|
row.CreateCell(31).SetCellValue(listData[i].CreateTime.ToString("yyyy-MM-dd HH:mm"));
|
|
|
|
style0.DataFormat = dataformat.GetFormat("yyyy-MM-dd HH:mm:ss");
|
|
row.GetCell(31).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));
|
|
}
|
|
}
|
|
}
|
|
}
|