451 lines
18 KiB
C#
451 lines
18 KiB
C#
using NPOI.XSSF.UserModel;
|
||
using Ramitta;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
|
||
using static Ramitta.Excel;
|
||
using static Ramitta.lib.Basic;
|
||
using static Ramitta.lib.CryptoHelper;
|
||
using static Ramitta.SQLite;
|
||
using static Ramitta.winDataGrid;
|
||
|
||
namespace template
|
||
{
|
||
public partial class MainWindow
|
||
{
|
||
Neo4jService neo4jService = new Neo4jService("bolt://localhost:7687", "neo4j", "AE1BC6D3");
|
||
|
||
public async void 数据升天计划neo4j()
|
||
{
|
||
await neo4jService.DeleteAllNodesAndRelationshipsAsync();
|
||
|
||
var 文件夹名单 = GetFilesAndDirectories(inputPath(null,null), true,
|
||
outputFormat: dir => System.IO.Path.GetFileNameWithoutExtension(dir));
|
||
|
||
|
||
foreach (var 文件夹 in 文件夹名单)
|
||
{
|
||
var 文件名单 = GetFilesAndDirectories(inputPath(null, null) + 文件夹, false,
|
||
outputFormat: dir => System.IO.Path.GetFileNameWithoutExtension(dir));
|
||
|
||
await neo4jService.CreateNodeAsync("文件夹", new Dictionary<string, object>() { { "name", 文件夹 } });
|
||
|
||
foreach (var 文件 in 文件名单)
|
||
{
|
||
var name = Guid.NewGuid().ToString();
|
||
var pair = new Dictionary<string, object>() {
|
||
{ "name", name },{ "文件夹",文件夹} };
|
||
|
||
if (文件夹.Contains("边墙沉重管") ||
|
||
文件夹.Contains("边墙基座固定") ||
|
||
文件夹.Contains("杂件"))
|
||
{
|
||
}
|
||
else
|
||
{
|
||
pair.Add("类型", Regex.Match(文件, @"([^-]+)").Groups[1].Value);
|
||
}
|
||
|
||
if (
|
||
文件夹.Contains("边墙沉重管") ||
|
||
文件夹.Contains("承重柱") ||
|
||
文件夹.Contains("弧形拉伸杆") ||
|
||
文件夹.Contains("山墙侧柱套件") ||
|
||
文件夹.Contains("山墙沉重管") ||
|
||
文件夹.Contains("屋脊连接角") ||
|
||
文件夹.Contains("屋面方管") ||
|
||
文件夹.Contains("屋檐方管") ||
|
||
文件夹.Contains("斜梁") ||
|
||
文件夹.Contains("右悬挂管") ||
|
||
文件夹.Contains("左悬挂管"))
|
||
{
|
||
pair.Add("型号", Regex.Match(文件, @"^(?:[^-]+-){2}([^-\r\n]+)").Groups[1].Value);
|
||
}
|
||
|
||
if (文件.Contains("跨度"))
|
||
{
|
||
pair.Add("跨度", Regex.Match(文件, @"跨度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
if (文件.Contains("高度"))
|
||
{
|
||
pair.Add("高度", Regex.Match(文件, @"高度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
if (文件.Contains("长度"))
|
||
{
|
||
pair.Add("长度", Regex.Match(文件, @"长度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
if (文件.Contains("宽度"))
|
||
{
|
||
pair.Add("宽度", Regex.Match(文件, @"宽度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
await neo4jService.CreateNodeAsync("文件", pair);
|
||
await neo4jService.CreateRelationshipAsync("拥有",
|
||
"文件夹","name", 文件夹, "文件", "name", name);
|
||
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
Func<string?, string?, string> inputPath = (n, f) =>
|
||
{
|
||
// 如果 n 和 f 都为空,返回文件路径的根目录
|
||
if (string.IsNullOrEmpty(n) && string.IsNullOrEmpty(f))
|
||
return @"C:\Users\Xeroly\Desktop\组件库\";
|
||
|
||
// 如果 n 为空,返回路径不包含文件夹
|
||
if (string.IsNullOrEmpty(n))
|
||
return @"C:\Users\Xeroly\Desktop\组件库\" + f + ".xlsx";
|
||
|
||
// 如果 f 为空,返回路径不包含文件名
|
||
if (string.IsNullOrEmpty(f))
|
||
return @"C:\Users\Xeroly\Desktop\组件库\" + n + @"\";
|
||
|
||
// 如果 n 和 f 都不为空,返回完整路径
|
||
return @"C:\Users\Xeroly\Desktop\组件库\" + n + @"\" + f + ".xlsx";
|
||
};
|
||
Func<string, string> outPath = (n) => @"C:\Users\Xeroly\Desktop\Outnm\" + n +".xlsx";
|
||
|
||
public void CreateXlsxMember(
|
||
Dictionary<string, object> pair,
|
||
string? fileUUID = null,
|
||
Dictionary<string, List<string>>? dict = null)
|
||
{
|
||
// 如果没有传入 fileUUID,使用pair中的"name"作为文件名
|
||
if (fileUUID == null)
|
||
fileUUID = pair["name"].ToString();
|
||
|
||
// 创建一个新的工作簿
|
||
var workbook = new XSSFWorkbook();
|
||
var sheet1 = workbook.CreateSheet("data");
|
||
var sheet2 = workbook.CreateSheet("info");
|
||
var sheet3 = workbook.CreateSheet("link");
|
||
|
||
// 将字典pair内容竖着写入sheet2(即键在第一行,值在第二行)
|
||
int columnIndex = 0;
|
||
var headerRow = sheet2.CreateRow(0); // 第一行用作标题行(键)
|
||
var valueRow = sheet2.CreateRow(1); // 第二行用作数据行(值)
|
||
|
||
foreach (var entry in pair)
|
||
{
|
||
// 将键写入第一行的相应单元格
|
||
headerRow.CreateCell(columnIndex).SetCellValue(entry.Key);
|
||
|
||
// 将值写入第二行的相应单元格
|
||
valueRow.CreateCell(columnIndex).SetCellValue(entry.Value?.ToString() ?? ""); // 防止null值导致异常
|
||
|
||
columnIndex++;
|
||
}
|
||
|
||
// 如果dict不为空,将其内容竖着写入sheet1
|
||
if (dict != null && dict.Any())
|
||
{
|
||
int rowIndex = 0;
|
||
|
||
// 先写入表头(字典的键)
|
||
var dictHeaderRow = sheet1.CreateRow(rowIndex++);
|
||
int dictColumnIndex = 0;
|
||
foreach (var key in dict.Keys)
|
||
{
|
||
dictHeaderRow.CreateCell(dictColumnIndex++).SetCellValue(key); // 写入字典的键作为列名
|
||
}
|
||
|
||
// 写入每一列数据(将List<string>的数据填充到各自的列)
|
||
int maxRows = dict.Values.Max(list => list.Count); // 获取最多的行数
|
||
|
||
for (int i = 0; i < maxRows; i++)
|
||
{
|
||
var row = sheet1.CreateRow(rowIndex++);
|
||
|
||
int colIndex = 0;
|
||
foreach (var key in dict.Keys)
|
||
{
|
||
var list = dict[key];
|
||
|
||
// 如果list中有对应的项,写入,否则写入空字符串
|
||
row.CreateCell(colIndex++).SetCellValue(i < list.Count ? list[i] : string.Empty);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 将数据写入到文件
|
||
using (var fs = new FileStream(outPath(fileUUID), FileMode.Create, FileAccess.Write))
|
||
{
|
||
workbook.Write(fs);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
public async void 表升天() {
|
||
|
||
var 文件夹名单 = GetFilesAndDirectories(inputPath(null, null), true,
|
||
outputFormat: dir => Path.GetFileNameWithoutExtension(dir));
|
||
|
||
foreach (var 文件夹 in 文件夹名单)
|
||
{
|
||
var 文件名单 = GetFilesAndDirectories(inputPath(null, null) + 文件夹, false,
|
||
outputFormat: dir => Path.GetFileNameWithoutExtension(dir));
|
||
|
||
foreach (var 文件 in 文件名单)
|
||
{
|
||
var name = Guid.NewGuid().ToString();
|
||
var pair = new Dictionary<string, object>() {
|
||
{"name", name },{"文件夹", 文件夹 } };
|
||
|
||
if (文件夹.Contains("边墙沉重管") ||
|
||
文件夹.Contains("边墙基座固定") ||
|
||
文件夹.Contains("杂件"))
|
||
{
|
||
}
|
||
else
|
||
{
|
||
pair.Add("类型", Regex.Match(文件, @"([^-]+)").Groups[1].Value);
|
||
}
|
||
|
||
if (
|
||
文件夹.Contains("边墙沉重管") ||
|
||
文件夹.Contains("承重柱") ||
|
||
文件夹.Contains("弧形拉伸杆") ||
|
||
文件夹.Contains("山墙侧柱套件") ||
|
||
文件夹.Contains("山墙沉重管") ||
|
||
文件夹.Contains("屋脊连接角") ||
|
||
文件夹.Contains("屋面方管") ||
|
||
文件夹.Contains("屋檐方管") ||
|
||
文件夹.Contains("斜梁") ||
|
||
文件夹.Contains("右悬挂管") ||
|
||
文件夹.Contains("左悬挂管"))
|
||
{
|
||
pair.Add("型号", Regex.Match(文件, @"^(?:[^-]+-){2}([^-\r\n]+)").Groups[1].Value);
|
||
}
|
||
|
||
if (文件.Contains("跨度"))
|
||
{
|
||
pair.Add("跨度", Regex.Match(文件, @"跨度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
if (文件.Contains("高度"))
|
||
{
|
||
pair.Add("高度", Regex.Match(文件, @"高度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
if (文件.Contains("长度"))
|
||
{
|
||
pair.Add("长度", Regex.Match(文件, @"长度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
if (文件.Contains("宽度"))
|
||
{
|
||
pair.Add("宽度", Regex.Match(文件, @"宽度(\d+)").Groups[1].Value);
|
||
}
|
||
if (文件.Contains("边高"))
|
||
{
|
||
pair.Add("边高", Regex.Match(文件, @"边高(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
Dictionary<string, List<string>> dict =
|
||
ReadExcelAsDictCol(inputPath(文件夹, 文件),headerInit:new List<string>() {
|
||
"料号","名称","数量","标志","备注" });
|
||
|
||
// 删除每个列表中第一个元素(如果列表不为空)
|
||
foreach (var key in dict.Keys.ToList()) // 使用ToList()来避免修改集合时的枚举问题
|
||
{
|
||
if (dict[key].Any())
|
||
{
|
||
dict[key].RemoveAt(0); // 删除列表中的第一个元素
|
||
}
|
||
}
|
||
|
||
CreateXlsxMember(pair,
|
||
dict:dict);
|
||
}
|
||
}
|
||
}
|
||
|
||
public async void 新建关系(Dictionary<string, object> pair, Dictionary<string, List<string>> dict,string tap,string rela="属于") {
|
||
if (pair.ContainsKey(tap))
|
||
{
|
||
await neo4jService.MergeNodeAsync(tap, new Dictionary<string, object>() {
|
||
{ "name", dict[tap][0]} });
|
||
|
||
await neo4jService.CreateRelationshipAsync(rela,
|
||
tap, "name", dict[tap][0],
|
||
"文件", "name", dict["name"][0]);
|
||
}
|
||
}
|
||
|
||
public async void 库表升天() {
|
||
await neo4jService.DeleteAllNodesAndRelationshipsAsync();
|
||
var 文件夹路径 = @"C:\Users\Xeroly\Desktop\Outnm\";
|
||
|
||
var 文件名单 = GetFilesAndDirectories(
|
||
文件夹路径,false,
|
||
outputFormat: dir => System.IO.Path.GetFileNameWithoutExtension(dir));
|
||
|
||
foreach (var 文件 in 文件名单) {
|
||
var dict = ReadExcelAsDictCol(文件夹路径+文件+".xlsx",sheetName:"info");
|
||
|
||
// 创建新的字典pair
|
||
var pair = new Dictionary<string, object>();
|
||
|
||
// 遍历dict,将每个key对应的第一个value添加到新的pair字典中
|
||
foreach (var entry in dict)
|
||
{
|
||
if (entry.Value.Any()) // 确保每个列表至少有一个元素
|
||
{
|
||
pair.Add(entry.Key, entry.Value[0]); // 只取第一个元素
|
||
}
|
||
}
|
||
|
||
await neo4jService.MergeNodeAsync("文件夹", new Dictionary<string, object>() {
|
||
{ "name", dict["文件夹"][0] } });
|
||
|
||
await neo4jService.MergeNodeAsync("文件", pair);
|
||
|
||
await neo4jService.CreateRelationshipAsync("拥有",
|
||
"文件夹", "name", dict["文件夹"][0],
|
||
"文件", "name", dict["name"][0]);
|
||
|
||
|
||
新建关系(pair, dict, "跨度", "跨度");
|
||
新建关系(pair, dict, "高度","高度");
|
||
新建关系(pair, dict, "宽度","宽度");
|
||
新建关系(pair, dict, "长度","长度");
|
||
新建关系(pair, dict, "型号","型号");
|
||
新建关系(pair, dict, "边高", "边高");
|
||
}
|
||
}
|
||
|
||
public async void 数据升天计划sqlite() {
|
||
string filename = @"C:\Users\Xeroly\Desktop\琦亚\Kia.db";
|
||
SQLite db = new SQLite($"Data Source={filename};Version=3;");
|
||
db.DropAllTables();
|
||
|
||
var basic = @"C:\Users\Xeroly\Desktop\组件库\";
|
||
|
||
var list = GetFilesAndDirectories(basic, true, null,
|
||
outputFormat: dir => System.IO.Path.GetFileNameWithoutExtension(dir));
|
||
|
||
foreach (var file in list)
|
||
{
|
||
var listson = GetFilesAndDirectories(basic + file, false, null,
|
||
outputFormat: dir => System.IO.Path.GetFileNameWithoutExtension(dir));
|
||
|
||
db.CreateTable(file, new Dictionary<string, string> {
|
||
{ "料号", "TEXT" },
|
||
{ "名称", "TEXT" },
|
||
{ "数量", "TEXT" },
|
||
{ "备注", "TEXT" },
|
||
{ "标志", "TEXT" } });
|
||
|
||
foreach (var fileson in listson)
|
||
{
|
||
var form = ReadExcelAsDictRow(
|
||
$"{basic}{file}\\{fileson}.xlsx",
|
||
headerInit: new List<string> { "料号", "名称", "数量", "标志", "备注" });
|
||
form.RemoveAt(0);
|
||
|
||
|
||
db.AddColumn(file, "类型", "TEXT");
|
||
|
||
if (
|
||
file.Contains("边墙沉重管") ||
|
||
file.Contains("承重柱") ||
|
||
file.Contains("弧形拉伸杆") ||
|
||
file.Contains("山墙侧柱套件") ||
|
||
file.Contains("山墙沉重管") ||
|
||
file.Contains("屋脊连接角") ||
|
||
file.Contains("屋面方管") ||
|
||
file.Contains("屋檐方管") ||
|
||
file.Contains("斜梁") ||
|
||
file.Contains("右悬挂管") ||
|
||
file.Contains("左悬挂管"))
|
||
{
|
||
db.AddColumn(file, "型号", "TEXT");
|
||
}
|
||
if (fileson.Contains("跨度"))
|
||
{
|
||
db.AddColumn(file, "跨度", "TEXT");
|
||
}
|
||
if (fileson.Contains("高度"))
|
||
{
|
||
db.AddColumn(file, "高度", "TEXT");
|
||
}
|
||
if (fileson.Contains("长度"))
|
||
{
|
||
db.AddColumn(file, "长度", "TEXT");
|
||
}
|
||
if (fileson.Contains("宽度"))
|
||
{
|
||
db.AddColumn(file, "宽度", "TEXT");
|
||
}
|
||
|
||
|
||
foreach (var formson in form)
|
||
{
|
||
var data = new Dictionary<string, object>();
|
||
data.Add("料号", formson["料号"]);
|
||
data.Add("名称", formson["名称"]);
|
||
data.Add("数量", formson["数量"]);
|
||
data.Add("备注", formson["备注"]);
|
||
data.Add("标志", formson["标志"]);
|
||
|
||
data.Add("类型", Regex.Match(fileson, @"([^-]+)").Groups[1].Value);
|
||
|
||
if (
|
||
file.Contains("边墙沉重管") ||
|
||
file.Contains("承重柱") ||
|
||
file.Contains("弧形拉伸杆") ||
|
||
file.Contains("山墙侧柱套件") ||
|
||
file.Contains("山墙沉重管") ||
|
||
file.Contains("屋脊连接角") ||
|
||
file.Contains("屋面方管") ||
|
||
file.Contains("屋檐方管") ||
|
||
file.Contains("斜梁") ||
|
||
file.Contains("右悬挂管") ||
|
||
file.Contains("左悬挂管"))
|
||
{
|
||
data.Add("型号", Regex.Match(fileson, @"^(?:[^-]+-){2}([^-\r\n]+)").Groups[1].Value);
|
||
}
|
||
|
||
if (fileson.Contains("跨度"))
|
||
{
|
||
data.Add("跨度", Regex.Match(fileson, @"跨度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
if (fileson.Contains("高度"))
|
||
{
|
||
data.Add("高度", Regex.Match(fileson, @"高度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
if (fileson.Contains("长度"))
|
||
{
|
||
data.Add("长度", Regex.Match(fileson, @"长度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
if (fileson.Contains("宽度"))
|
||
{
|
||
data.Add("宽度", Regex.Match(fileson, @"宽度(\d+)").Groups[1].Value);
|
||
}
|
||
|
||
db.InsertData(file, data);
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|