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 OilGuideMountViewModel : ViewModelBase
{
private static Logger _Logger = LogManager.GetCurrentClassLogger();
private string CurrentMachineName = "导油板敲入";
private string CurrentMachineNameTemplate = "OilGuideMountTemplate.xlsx";
private IMapper autoMapper;
///
/// 实例化函数
///
public OilGuideMountViewModel()
{
ListModelDto = new ObservableCollection();
SearchStartDate = DateTime.Now.AddDays(-1).ToShortDateString();
SearchEndDate = DateTime.Now.AddDays(2).ToShortDateString();
MachineName = CurrentMachineName + " - 搜索条件";
var config = new MapperConfiguration(cfg => cfg.CreateMap()
.ForMember(dest => dest.PlaceNoDamage, opt => opt.MapFrom(src => src.PlaceNoDamage.ToString())));
autoMapper = config.CreateMapper();
}
///
/// 列表集合
///
private ObservableCollection _ListModelDto;
///
/// 列表集合
///
public ObservableCollection ListModelDto
{
get { return _ListModelDto; }
set { _ListModelDto = value; }
}
///
/// 机器设备名称
///
private string machineName;
public string MachineName
{
get { return machineName; }
set { machineName = value; RaisePropertyChanged(() => MachineName); }
}
///
/// 搜索条件-曲轴编号
///
private string searchCrankshaftNo;
public string SearchCrankshaftNo
{
get { return searchCrankshaftNo; }
set { searchCrankshaftNo = value; RaisePropertyChanged(() => SearchCrankshaftNo); }
}
///
/// 搜索条件-开始时间
///
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();
if (!string.IsNullOrEmpty(SearchCrankshaftNo))
{
mulConQueryable = mulConQueryable.Where(t => t.CrankshaftNo.Contains(SearchCrankshaftNo));
}
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(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].CrankshaftNo.ToString());
row.CreateCell(1).SetCellValue(listData[i].BracketNo.ToString());
row.CreateCell(2).SetCellValue(listData[i].BracketCastingNo.ToString());
row.CreateCell(3).SetCellValue(listData[i].OperatorNo.ToString());
row.CreateCell(4).SetCellValue(listData[i].PlaceNoDamage.ToString());
row.CreateCell(5).SetCellValue(listData[i].OilGuidePlate.ToString());
row.CreateCell(6).SetCellValue(listData[i].FeelerGaugeCheck.ToString());
row.CreateCell(7).SetCellValue(listData[i].BuriedPlug.ToString());
row.CreateCell(8).SetCellValue(listData[i].HelicalScrew.ToString());
row.CreateCell(9).SetCellValue(listData[i].AirTightCheck.ToString());
row.CreateCell(10).SetCellValue(listData[i].CreateTime.ToString("yyyy-MM-dd HH:mm"));
style0.DataFormat = dataformat.GetFormat("yyyy-MM-dd HH:mm:ss");
row.GetCell(10).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));
}
}
}
}