using DevExpress.XtraGrid.Columns; using ReportTemplateTool.Models; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Forms; namespace ReportTool.Views { public partial class LisReportFileForm : BaseView { private string _reportFileCode = ""; private List _reportFiles = new List(); public LisReportFileForm(string code) { InitializeComponent(); Load += (s, e) => { this.GetReportFileAllData(); }; this._reportFileCode = code; UploadBtn.Click += UploadBtnClick; DownloadBtn.Click += DownloadBtnClick; DelBtn.Click += DelBtnClick; SaveBtn.Click += SaveBtnClick; } private void GetReportFileAllData() { _reportFiles = _db.DictReportFile.Where(p => p.Code == _reportFileCode).ToList(); if (this._reportFileCode == "Lis") { foreach (DictReportFile file in _reportFiles) { file.SampleCodes = string.Join(",", _db.DictReport.Where(p => (p.FileName + ".frx") == file.FileName).Select(p => p.SampleCode).ToList()); } } Invoke(new Action(() => { ReportFileControl.DataSource = _reportFiles; })); } private void UploadBtnClick(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = "D:\\ProgramCode\\company\\LisWeb\\liswebsvn\\LisWeb\\ReportFiles"; openFileDialog.RestoreDirectory = true; openFileDialog.Multiselect = true; openFileDialog.Title = "选择多个文件"; openFileDialog.Filter = "模版文件 (*.frx)|*.frx"; openFileDialog.FilterIndex = 2; //openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (openFileDialog.ShowDialog() == DialogResult.OK) { List _addReportFiles = new List(); List _updateReportFiles = new List(); foreach (string filePath in openFileDialog.FileNames) { FileInfo fileInfo = new FileInfo(filePath); DictReportFile _reportFile = _reportFiles.FirstOrDefault(p => p.FileName == fileInfo.Name); if (_reportFile != null) { _reportFile.File = File.ReadAllBytes(filePath); _reportFile.UpdateTime = DateTime.Now; _updateReportFiles.Add(_reportFile); } else { _addReportFiles.Add(new DictReportFile { Code = _reportFileCode, FileName = fileInfo.Name, File = File.ReadAllBytes(filePath), UpdateTime = DateTime.Now }); } } string message = $"新上传文件{_addReportFiles.Count}个"; if (_updateReportFiles.Count > 0) { message += $",{string.Join(",", _updateReportFiles.Select(p => p.FileName).ToList())}等已存储文件将会被更新,是否确认上传?"; } DialogResult result = MessageBox.Show( message, "操作确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { _db.DictReportFile.AddRange(_addReportFiles); _db.SaveChanges(); } this.GetReportFileAllData(); } } } private void DownloadBtnClick(object sender, EventArgs e) { if (ReportFileGrid.SelectedRowsCount > 0) { using (FolderBrowserDialog folderDialog = new FolderBrowserDialog()) { List failFile = new List(); int successCount = ReportFileGrid.SelectedRowsCount; if (folderDialog.ShowDialog() == DialogResult.OK) { foreach (var file in _reportFiles.Where(p => ReportFileGrid.GetSelectedRows().Contains(_reportFiles.IndexOf(p)))) { if (file != null) { bool saveStatus = DownloadFile(file, folderDialog.SelectedPath); if (!saveStatus) { failFile.Add(file.FileName); successCount--; } } } MessageBox.Show($"选择文件{ReportFileGrid.SelectedRowsCount}个,下载成功{successCount}个,下载失败{failFile.Count}个!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show("请先选择要下载的文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private bool DownloadFile(DictReportFile file, string savePath) { try { string _filename = file.FileName; if (!_filename.Contains(".frx")) _filename += ".frx"; File.WriteAllBytes(Path.Combine(savePath, _filename), file.File); return true; } catch (Exception ex) { return false; } } private void DelBtnClick(object sender, EventArgs e) { if (ReportFileGrid.SelectedRowsCount == 0) return; DialogResult result = MessageBox.Show($"是否删除所选中的{ReportFileGrid.SelectedRowsCount}个报告单模版文件数据?", "操作确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { foreach (var file in _reportFiles.Where(p => ReportFileGrid.GetSelectedRows().Contains(_reportFiles.IndexOf(p)))) { _db.DictReportFile.Remove(file); } _db.SaveChanges(); this.GetReportFileAllData(); } } private void SaveBtnClick(object sender, EventArgs e) { for (int i = 0; i < ReportFileGrid.DataRowCount; i++) { long ID = Convert.ToInt64(ReportFileGrid.GetRowCellValue(i, "ID").ToString()); string fileName = (ReportFileGrid.GetRowCellValue(i, "FileName").ToString() as string); _db.Database.ExecuteSqlCommand($"update Dict_ReportFile set FileName = '{fileName}', UpdateTime = '{DateTime.Now}' where ID = {ID} and FileName <> '{fileName}'"); } this.GetReportFileAllData(); } } }