You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
3.1 KiB
C#
76 lines
3.1 KiB
C#
using Dapper;
|
|
using HandyControl.Controls;
|
|
using HandyControl.Themes;
|
|
using MySql.Data.MySqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Xml.Linq;
|
|
using Txgy.FilesWatcher.DBModels;
|
|
|
|
namespace Txgy.FilesWatcher.model
|
|
{
|
|
public class UploadMseedFile
|
|
{
|
|
|
|
public string FileName { get; set; }
|
|
|
|
public static void UploadMSeedOnce(Action<string> mseedAction, string path, int workAreaId=1)
|
|
{
|
|
try
|
|
{
|
|
var currentTime = DateTime.Now;
|
|
var Dfiles = new DirectoryInfo(path).GetFiles("*.mseed");
|
|
string mes = string.Empty;
|
|
foreach (var DFile in Dfiles)
|
|
{
|
|
string file = DFile.Name;
|
|
string tbname = file.Substring(3, 10);
|
|
tbname = tbname.Replace("-", "");
|
|
string uploadedtbname = $"uploaded{tbname}";
|
|
|
|
string sqlNumber = $"SELECT COUNT(*) FROM {uploadedtbname} WHERE filename = '{file}'";
|
|
//检查文件是否已经上传
|
|
using (var conn = DataBaseConnect.GetInstance.OpenConnection())
|
|
{
|
|
MySqlCommand cmd = new MySqlCommand(sqlNumber, conn);
|
|
var a = conn.QueryFirst<int>(sqlNumber);
|
|
if (a == 0)
|
|
{
|
|
//WorkAreaId = 1,更新
|
|
mes += file.Substring(3, 3);
|
|
FileStream fs = new FileStream(DFile.FullName, FileMode.Open, FileAccess.Read);
|
|
BinaryReader mbr = new BinaryReader(fs);
|
|
Byte[] mseedDatas = mbr.ReadBytes((int)fs.Length);
|
|
fs.Close();
|
|
string WaveTime = Path.GetFileNameWithoutExtension(file).Substring(7); //tbname.Substring(3);
|
|
WaveTime = WaveTime.Insert(4, "-").Insert(7, "-").Insert(10, "T").Insert(13, ":").Insert(16, ":00");
|
|
|
|
sqlNumber = $"INSERT INTO {tbname} (WaveTime, WorkAreaID, WaveData) VALUES('{WaveTime}', '{workAreaId}', @mseedD)";
|
|
var res = conn.Execute(sqlNumber, new { mseedD = mseedDatas });
|
|
//更新上传文件记录
|
|
sqlNumber = $"INSERT INTO {uploadedtbname}(filename, uploadtime) VALUES('{file}', '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
|
|
res = conn.Execute(sqlNumber);
|
|
}
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(mes))
|
|
{
|
|
string notifyMes = $"共{Dfiles.Length}个数据上传成功,{mes}";// {string.Join(",", Dfiles.Select(f => f.Name.Substring(3, 3)))}";
|
|
mseedAction(notifyMes);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// System.Windows.MessageBox.Show(ex.ToString());
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|