完成附件功能

This commit is contained in:
FalconFly 2024-07-23 14:45:54 +08:00
parent 8ea15b2ab7
commit e7f8aaed1f
10 changed files with 299 additions and 27 deletions

View File

@ -21,6 +21,8 @@ namespace CalendarNotepad.Extends
public void DbInit() {
using var db = new DbContext();
db.CodeFirst.InitTables<WorkUnit>();
db.CodeFirst.InitTables<PlusFileUnit>();
db.CodeFirst.InitTables<WorkUnitPlusFile>();
}
}
}

View File

@ -0,0 +1,66 @@
using System.Security.Cryptography;
using System.Text;
namespace CalendarNotepad.Extends
{
/// <summary>
/// 附件操作
/// </summary>
public static class PlusFile
{
/// <summary>
/// 获取MD5码
/// </summary>
public static string GetMd5(string fileContent) {
var ibs = Encoding.UTF8.GetBytes(fileContent);
return GetMd5(ibs);
}
/// <summary>
/// 获取MD5码
/// </summary>
public static string GetMd5(byte[] content) {
using var md5 = MD5.Create();
var md5bs = md5.ComputeHash(content);
StringBuilder sb = new(md5bs.Length * 2);
for(int i = 0;i < md5bs.Length;i++) {
sb.Append(md5bs[i].ToString("X2"));
}
return sb.ToString();
}
/// <summary>
/// 读取文件内容
/// </summary>
/// <param name="fullFilePath"></param>
/// <returns></returns>
public static byte[] LoadFile(string fullFilePath) {
if(!File.Exists(fullFilePath)) {
throw new FileNotFoundException();
}
var fileInfo = new FileInfo(fullFilePath);
var fileSize = fileInfo.Length;
if(fileSize > 4 * 1024 * 1024) {
throw new OutOfMemoryException();
}
using FileStream fs = fileInfo.OpenRead();
using BinaryReader br = new BinaryReader(fs);
var length = (int)fs.Length;
var buf = br.ReadBytes(length);
return buf;
}
/// <summary>
/// 写入文件内容
/// </summary>
/// <param name="fullFilePath"></param>
/// <param name="content"></param>
public static void SaveFile(string fullFilePath,byte[] content) {
if(File.Exists(fullFilePath)) {
File.Delete(fullFilePath);
}
using var fs = new FileStream(fullFilePath,FileMode.CreateNew,FileAccess.Write);
using var bw = new BinaryWriter(fs);
bw.Write(content);
bw.Flush();
bw.Close();
}
}
}

View File

@ -45,7 +45,6 @@ namespace CalendarNotepad
private void rtbMsg_TextChanged(object sender,EventArgs e) {
var data = new WorkUnit {
DayRange = GetDateRange(),
WorkDay = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd"),
WorkMessage = this.rtbMsg.Text,
};

View File

@ -0,0 +1,17 @@
namespace CalendarNotepad.Models
{
/// <summary>
/// 附件文件单元
/// </summary>
public class PlusFileUnit
{
/// <summary>
/// 文件指纹ID
/// </summary>
public string? FileId { get; set; }
/// <summary>
/// 文件内容
/// </summary>
public byte[]? FileContent { get; set; }
}
}

View File

@ -27,7 +27,7 @@ namespace CalendarNotepad.Models
public void AddOrReplace(WorkUnit unit) {
lock(_lockObj) {
var qu = this.WorkList.Where(a => a.WorkDay == unit.WorkDay && a.DayRange == unit.DayRange);
var qu = this.WorkList.Where(a => a.WorkDay == unit.WorkDay);
if(qu.Any() && unit.WorkMessage.IsNullOrEmpty()) {
qu.ToList().ForEach(a => this.WorkList.Remove(a));
return;
@ -53,8 +53,8 @@ namespace CalendarNotepad.Models
/// <param name="workRange">工作范围</param>
/// <returns></returns>
public WorkUnit GetUnit(string workDay,string workRange) {
var qu = this.WorkList.Where(a => a.WorkDay == workDay && a.DayRange == workRange);
return qu.Any() ? qu.First() : new WorkUnit { DayRange = workRange,WorkDay = workDay,WorkMessage = "" };
var qu = this.WorkList.Where(a => a.WorkDay == workDay);
return qu.Any() ? qu.First() : new WorkUnit { WorkDay = workDay,WorkMessage = "" };
}
public void SaveToFile() {

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CalendarNotepad.Models
{
@ -11,12 +12,9 @@ namespace CalendarNotepad.Models
/// <summary>
/// 工作日期
/// </summary>
[Key]
public string? WorkDay { get; set; }
/// <summary>
/// 时间段
/// </summary>
public string? DayRange { get; set; }
/// <summary>
/// 内容
/// </summary>
public string? WorkMessage { get; set; }

View File

@ -0,0 +1,21 @@
namespace CalendarNotepad.Models
{
/// <summary>
/// 工作单元附件对应关系
/// </summary>
public class WorkUnitPlusFile
{
/// <summary>
/// 工作日期
/// </summary>
public string? WorkDay { get; set; }
/// <summary>
/// 文件指纹
/// </summary>
public string? FileKey { get; set; }
/// <summary>
/// 文件名称
/// </summary>
public string? FileName { get; set; }
}
}

View File

@ -25,6 +25,7 @@
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WorkMain));
tabControl1 = new TabControl();
tpWorkManage = new TabPage();
@ -47,9 +48,15 @@
radioButton5 = new RadioButton();
mcWorkDay = new MonthCalendar();
rtbMsg = new RichTextBox();
FileListView = new ListView();
cms_FileList = new ContextMenuStrip(components);
ToolStripMenuItem = new ToolStripMenuItem();
ToolStripMenuItem = new ToolStripMenuItem();
toolStrip1 = new ToolStrip();
tsp_Copy = new ToolStripButton();
tsb_palse = new ToolStripButton();
OpenFileDlg = new OpenFileDialog();
ToolStripMenuItem = new ToolStripMenuItem();
tabControl1.SuspendLayout();
tpWorkManage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
@ -61,6 +68,7 @@
flowLayoutPanel1.SuspendLayout();
gbWorkTypes.SuspendLayout();
C_WorkTypes.SuspendLayout();
cms_FileList.SuspendLayout();
toolStrip1.SuspendLayout();
SuspendLayout();
//
@ -71,7 +79,7 @@
tabControl1.Location = new Point(0,0);
tabControl1.Name = "tabControl1";
tabControl1.SelectedIndex = 0;
tabControl1.Size = new Size(695,490);
tabControl1.Size = new Size(717,443);
tabControl1.TabIndex = 0;
//
// tpWorkManage
@ -80,7 +88,7 @@
tpWorkManage.Location = new Point(4,26);
tpWorkManage.Name = "tpWorkManage";
tpWorkManage.Padding = new Padding(3);
tpWorkManage.Size = new Size(687,460);
tpWorkManage.Size = new Size(709,413);
tpWorkManage.TabIndex = 1;
tpWorkManage.Text = "录入记录";
tpWorkManage.UseVisualStyleBackColor = true;
@ -102,8 +110,9 @@
// splitContainer1.Panel2
//
splitContainer1.Panel2.Controls.Add(rtbMsg);
splitContainer1.Panel2.Controls.Add(FileListView);
splitContainer1.Panel2.Controls.Add(toolStrip1);
splitContainer1.Size = new Size(681,454);
splitContainer1.Size = new Size(703,407);
splitContainer1.SplitterDistance = 236;
splitContainer1.TabIndex = 9;
//
@ -178,7 +187,7 @@
flowLayoutPanel1.AutoSize = true;
flowLayoutPanel1.Controls.Add(btSave);
flowLayoutPanel1.Controls.Add(btReset);
flowLayoutPanel1.Location = new Point(6,420);
flowLayoutPanel1.Location = new Point(6,373);
flowLayoutPanel1.Name = "flowLayoutPanel1";
flowLayoutPanel1.Size = new Size(220,29);
flowLayoutPanel1.TabIndex = 8;
@ -297,17 +306,51 @@
rtbMsg.EnableAutoDragDrop = true;
rtbMsg.Location = new Point(0,25);
rtbMsg.Name = "rtbMsg";
rtbMsg.Size = new Size(441,429);
rtbMsg.Size = new Size(463,297);
rtbMsg.TabIndex = 7;
rtbMsg.Text = "";
rtbMsg.LinkClicked += rtbMsg_LinkClicked;
rtbMsg.TextChanged += rtbMsg_TextChanged_1;
//
// FileListView
//
FileListView.ContextMenuStrip = cms_FileList;
FileListView.Dock = DockStyle.Bottom;
FileListView.FullRowSelect = true;
FileListView.GridLines = true;
FileListView.Location = new Point(0,322);
FileListView.Name = "FileListView";
FileListView.Size = new Size(463,85);
FileListView.TabIndex = 9;
FileListView.UseCompatibleStateImageBehavior = false;
FileListView.View = View.Details;
//
// cms_FileList
//
cms_FileList.Items.AddRange(new ToolStripItem[] { ToolStripMenuItem,ToolStripMenuItem,ToolStripMenuItem });
cms_FileList.Name = "contextMenuStrip1";
cms_FileList.Size = new Size(181,92);
//
// 删除ToolStripMenuItem
//
ToolStripMenuItem.Name = "删除ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Text = "删除";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// 导入ToolStripMenuItem
//
ToolStripMenuItem.Name = "导入ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Text = "导入";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// toolStrip1
//
toolStrip1.Items.AddRange(new ToolStripItem[] { tsp_Copy,tsb_palse });
toolStrip1.Location = new Point(0,0);
toolStrip1.Name = "toolStrip1";
toolStrip1.Size = new Size(441,25);
toolStrip1.Size = new Size(463,25);
toolStrip1.TabIndex = 8;
toolStrip1.Text = "toolStrip1";
//
@ -331,11 +374,22 @@
tsb_palse.Text = "粘贴";
tsb_palse.Click += tsb_palse_Click;
//
// OpenFileDlg
//
OpenFileDlg.Multiselect = true;
//
// 下载ToolStripMenuItem
//
ToolStripMenuItem.Name = "下载ToolStripMenuItem";
ToolStripMenuItem.Size = new Size(180,22);
ToolStripMenuItem.Text = "下载";
ToolStripMenuItem.Click += ToolStripMenuItem_Click;
//
// WorkMain
//
AutoScaleDimensions = new SizeF(7F,17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(695,490);
ClientSize = new Size(717,443);
Controls.Add(tabControl1);
Name = "WorkMain";
StartPosition = FormStartPosition.CenterScreen;
@ -357,6 +411,7 @@
gbWorkTypes.ResumeLayout(false);
C_WorkTypes.ResumeLayout(false);
C_WorkTypes.PerformLayout();
cms_FileList.ResumeLayout(false);
toolStrip1.ResumeLayout(false);
toolStrip1.PerformLayout();
ResumeLayout(false);
@ -388,5 +443,11 @@
private ToolStrip toolStrip1;
private ToolStripButton tsp_Copy;
private ToolStripButton tsb_palse;
private ListView FileListView;
private ContextMenuStrip cms_FileList;
private ToolStripMenuItem ToolStripMenuItem;
private ToolStripMenuItem ToolStripMenuItem;
private OpenFileDialog OpenFileDlg;
private ToolStripMenuItem ToolStripMenuItem;
}
}

View File

@ -25,6 +25,16 @@ namespace CalendarNotepad
rab.CheckedChanged += Rab_CheckedChanged;
}
}
var fieViewWidth = this.FileListView.Width;
this.FileListView.Columns.Add(new ColumnHeader {
Text = "附件",Width = fieViewWidth - 200,TextAlign = HorizontalAlignment.Left,
});
this.FileListView.Columns.Add(new ColumnHeader {
Text = "类型",Width = 100,TextAlign = HorizontalAlignment.Left,
});
this.FileListView.Columns.Add(new ColumnHeader {
Text = "文件指纹",Width = 100,TextAlign = HorizontalAlignment.Left,
});
}
private void Rab_CheckedChanged(object? sender,EventArgs e) {
@ -44,18 +54,28 @@ namespace CalendarNotepad
private void mcWorkDay_DateChanged(object sender,DateRangeEventArgs e) {
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
var sr = "全天";
var qu = this.Db.Queryable<WorkUnit>().Where(a => a.DayRange == sr && a.WorkDay == wd).ToList();
this.FileListView.Items.Clear();
var qu = this.Db.Queryable<WorkUnit>().Where(a => a.WorkDay == wd).ToList();
if(!qu.Any()) {
this.rtbMsg.Text = "";
this.SetWorkType("休假");
this.SetWorkOut("无");
}
if(qu.Any()) {
var fir = qu.First();
this.rtbMsg.Text = fir.WorkMessage;
this.SetWorkType(fir.WorkType);
this.SetWorkOut(fir.Workout ?? "无");
var plsq = this.Db.Queryable<WorkUnitPlusFile>().Where(a => a.WorkDay == wd).ToList();
if(plsq.Any()) {
foreach(var i in plsq) {
var lit1 = new ListViewItem(i.FileName);
lit1.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "已载入" });
lit1.SubItems.Add(new ListViewItem.ListViewSubItem { Text = i.FileKey });
this.FileListView.Items.Add(lit1);
}
}
else {
this.rtbMsg.Text = "";
this.SetWorkType("休假");
this.SetWorkOut("无");
}
this.DataReset();
Task.Factory.StartNew(() => {
@ -116,11 +136,10 @@ namespace CalendarNotepad
private void btSave_Click(object sender,EventArgs e) {
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
var dr = "全天";
var msg = this.rtbMsg.Text;
var wt = GetWorkType();
var wo = this.GetWorkOut();
var exp = Expressionable.Create<WorkUnit>().And(a => a.DayRange == dr && a.WorkDay == wd).ToExpression();
var exp = Expressionable.Create<WorkUnit>().And(a => a.WorkDay == wd).ToExpression();
var qu = this.Db.Queryable<WorkUnit>().Where(exp).ToList();
if(qu.Any() && msg.IsNullOrEmpty()) {
this.Db.Deleteable<WorkUnit>().Where(exp).ExecuteCommand();
@ -138,7 +157,7 @@ namespace CalendarNotepad
}
if(!qu.Any() && msg.IsNotNullOrEmpty()) {
this.Db.Insertable<WorkUnit>(new WorkUnit {
DayRange = dr,WorkDay = wd,
WorkDay = wd,
WorkMessage = msg,WorkType = wt,Workout = wo,
}).ExecuteCommand();
this.DataReset();
@ -206,5 +225,85 @@ namespace CalendarNotepad
private void tsb_palse_Click(object sender,EventArgs e) {
this.rtbMsg.AppendText(Clipboard.GetText());
}
private void rtbMsg_TextChanged_1(object sender,EventArgs e) {
}
private void ToolStripMenuItem_Click(object sender,EventArgs e) {
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
var items = this.FileListView.SelectedItems;
if(items.Count == 0) {
MessageBox.Show("没有选择列表项!");
return;
}
if(MessageBox.Show($"共选择了{items.Count}项,是否全部删除?","警告",MessageBoxButtons.OKCancel) == DialogResult.Cancel) {
return;
}
foreach(ListViewItem i in items) {
this.FileListView.Items.Remove(i);
this.Db.Deleteable<WorkUnitPlusFile>()
.Where(a => a.WorkDay == wd && a.FileKey == i.SubItems[2].Text)
.ExecuteCommand();
}
}
private void ToolStripMenuItem_Click(object sender,EventArgs e) {
var dlgFile = OpenFileDlg.ShowDialog();
if(dlgFile == DialogResult.Cancel) {
return;
}
if(OpenFileDlg.SafeFileNames.Length == 0) {
return;
}
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
foreach(var f in OpenFileDlg.FileNames) {
var lit1 = new ListViewItem(Path.GetFileName(f));
lit1.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "导入中" });
lit1.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "" });
this.FileListView.Items.Add(lit1);
var fc = PlusFile.LoadFile(f);
var fk = PlusFile.GetMd5(fc);
lit1.SubItems[2].Text = fk;
var pfu = new PlusFileUnit { FileContent = fc,FileId = fk };
var qu = this.Db.Queryable<PlusFileUnit>().Where(a => a.FileId == fk);
if(!qu.Any()) {
this.Db.Insertable(pfu).ExecuteCommand();
}
this.Db.Insertable(new WorkUnitPlusFile {
FileKey = fk,
WorkDay = wd,
FileName = Path.GetFileName(f),
}).ExecuteCommand();
}
}
private void ToolStripMenuItem_Click(object sender,EventArgs e) {
var items = this.FileListView.SelectedItems;
if(items.Count == 0) {
MessageBox.Show("没有选择文件");
return;
}
foreach(ListViewItem i in items) {
var sdlg = new SaveFileDialog {
Title = "保存附件",
FileName = Path.GetFileName(i.Text),
};
var dresult = sdlg.ShowDialog();
if(dresult != DialogResult.OK) {
return;
}
var filePath = sdlg.FileName;
var fileKey = i.SubItems[2].Text;
var qu = this.Db.Queryable<PlusFileUnit>().Where(a => a.FileId == fileKey).ToList();
if(qu.Any()) {
var fir = qu.First();
if(fir.FileContent != null) {
PlusFile.SaveFile(filePath,fir.FileContent);
}
}
}
}
}
}

View File

@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="cms_FileList.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>127, 17</value>
</metadata>
<metadata name="toolStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
@ -130,4 +133,10 @@
AAAAAElFTkSuQmCC
</value>
</data>
<metadata name="OpenFileDlg.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>248, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>36</value>
</metadata>
</root>