172 lines
6.5 KiB
C#
172 lines
6.5 KiB
C#
using MiniExcelLibs;
|
|
using Sinvo.EplanHpD.Plugin.WPFUI.Datas;
|
|
using Sinvo.EplanHpD.Plugin.WPFUI.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace Sinvo.EplanHpD.Plugin.WPFUI.Utils
|
|
{
|
|
public class MotorExcelHelper
|
|
{
|
|
public static MotorExcelHelper Instance = new();
|
|
|
|
private Stream dataFileStream;
|
|
public void ReadDataToStream()
|
|
{
|
|
var file = Path.Combine(Consts.PLUGIN_DATA_FILE_PATH, Consts.PLUGIN_DATA_FILE_NAME);
|
|
if (File.Exists(file))
|
|
{
|
|
var bytes = File.ReadAllBytes(file);
|
|
dataFileStream = new MemoryStream(bytes);
|
|
}
|
|
else
|
|
{
|
|
throw new FileNotFoundException(file);
|
|
}
|
|
}
|
|
public List<LectotypeModel> GetCableDatas(string brand = Brands.SANLING_HK_KT)
|
|
{
|
|
if (dataFileStream == null || !dataFileStream.CanRead)
|
|
{
|
|
// 未初始化或是无法读取时,重试一次
|
|
ReadDataToStream();
|
|
}
|
|
//var filePath = "D:\\旧电脑文件\\Desktop\\Data\\三菱伺服HK-KT线材选型BOM表_按程序格式整理后.xlsx";
|
|
var data = MiniExcel.Query<LectotypeModel>(dataFileStream, brand).ToList();
|
|
//MiniExcel.Query()
|
|
return data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取电机功率
|
|
/// </summary>
|
|
public List<ConfigItemModel> GetMotorPowers()
|
|
{
|
|
var data = MiniExcel.Query(dataFileStream, sheetName: "电机功率", useHeaderRow: true).OrderBy(it => it.电机功率).Select(it => new ConfigItemModel
|
|
{
|
|
ItemName = it.电机功率,
|
|
ItemValue = it.电机功率,
|
|
ItemFlag = it.电机型号
|
|
}).ToList();
|
|
|
|
return [.. data.Distinct(new LambdaComparer<ConfigItemModel>(
|
|
(a, b) => a.ItemName == b.ItemName && a.ItemValue == b.ItemValue && b.ItemFlag == a.ItemFlag,
|
|
obj => obj.ToString().GetHashCode()
|
|
))];
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取线材类型
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<ConfigItemModel> GetCableTypes()
|
|
{
|
|
|
|
var data = MiniExcel.Query(dataFileStream, sheetName: "线材类型", useHeaderRow: true).Select(it => new ConfigItemModel
|
|
{
|
|
ItemName = it.线材类型,
|
|
ItemValue = it.线材类型,
|
|
ItemFlag = it.是否组合
|
|
}).OrderBy(it => it.ItemName).ToList();
|
|
|
|
return [.. data.Distinct(new LambdaComparer<ConfigItemModel>(
|
|
(a, b) => a.ItemName == b.ItemName && a.ItemValue == b.ItemValue,
|
|
obj => obj.ToString().GetHashCode()
|
|
)).OrderBy(it => it.ItemValue)];
|
|
}
|
|
/// <summary>
|
|
/// 获取直通线数量
|
|
/// </summary>
|
|
/// <param name="motorModel"></param>
|
|
/// <returns></returns>
|
|
public List<MotorPassthroughCable> GetPassthroughCableCount(string motorModel)
|
|
{
|
|
if (dataFileStream == null || !dataFileStream.CanRead)
|
|
{
|
|
// 未初始化或是无法读取时,重试一次
|
|
ReadDataToStream();
|
|
}
|
|
return MiniExcel.Query<MotorPassthroughCable>(dataFileStream, sheetName: "直通线数量").Where(it => it.MotorModel == motorModel).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取电机功率
|
|
/// </summary>
|
|
/// <param name="motorModel">电机型号</param>
|
|
/// <returns></returns>
|
|
public int GetMotorPower(string motorModel)
|
|
{
|
|
if (dataFileStream == null || !dataFileStream.CanRead)
|
|
{
|
|
// 未初始化或是无法读取时,重试一次
|
|
ReadDataToStream();
|
|
}
|
|
return MiniExcel.Query<MotorPowerModel>(dataFileStream, sheetName: "电机功率")
|
|
.Where(it => it.MotorModel == motorModel)
|
|
.FirstOrDefault()
|
|
?.MotorPower ?? 0;
|
|
}
|
|
public List<LineBomModel> GetBomList(string drawNo)
|
|
{
|
|
//var filePath = "D:\\Desktop\\Data\\BOM表.xlsx";
|
|
var filePath = Path.Combine(Consts.PLUGIN_DATA_FILE_PATH, Consts.PLUGIN_DATA_FILE_NAME_BOM);
|
|
var data = MiniExcel.Query<LineBomModel>(filePath).Where(it => it.DrawingNo == drawNo).ToList();
|
|
//MiniExcel.Query<>()
|
|
return data;
|
|
}
|
|
public void SaveBomListToExcel(string targetPath, List<LineBomItemModel> bomList, string cableModelNo)
|
|
{
|
|
MiniExcel.SaveAs(
|
|
targetPath,
|
|
bomList
|
|
);
|
|
}
|
|
public bool SaveLinesToExcel(string targetPath,string mechanismNo, List<LectotypeLineModel> lines)
|
|
{
|
|
try
|
|
{
|
|
var templatePath = Path.Combine(Consts.PLUGIN_DATA_FILE_PATH, Consts.PLUGIN_TEMPLATE_FILE_NAME);
|
|
var data = lines.Select(line => new
|
|
{
|
|
line.ItemSeqNo,
|
|
line.SeqNo,
|
|
line.MechanicalNo,
|
|
line.MechanicalName,
|
|
line.CableModelNo,
|
|
line.CableType,
|
|
line.AxisNo,
|
|
line.DrawingNo,
|
|
line.InsulationMCode,
|
|
line.InsulationModel,
|
|
line.InsulationCount,
|
|
EncoderLineLength = line.EncoderLineLength == 0 ? "" : line.EncoderLineLength.ToString(),
|
|
PowerLineLength = line.PowerLineLength == 0 ? "" : line.PowerLineLength.ToString(),
|
|
OrderDate = DateTime.Now.ToString("yyyy/M/d"),
|
|
DeliveryDate = DateTime.Now.AddDays(7).ToString("yyyy/M/d"), // 交期默认一周
|
|
}).ToList();
|
|
MiniExcel.SaveAsByTemplate(
|
|
$"{targetPath}\\{mechanismNo}线材下单_{DateTime.Now:yyyy_MM_dd}_{DateTime.Now.Ticks}.xlsx",
|
|
templatePath,
|
|
new { data },
|
|
new MiniExcelLibs.OpenXml.OpenXmlConfiguration
|
|
{
|
|
FastMode = true
|
|
}
|
|
);
|
|
return true;
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void CloseStream()
|
|
{
|
|
dataFileStream?.Close();
|
|
}
|
|
}
|
|
}
|