EPLAN_PROD_Plugin/Sinvo.EplanHpD.Plugin.WPFUI/Utils/MotorExcelHelper.cs

205 lines
8.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}
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>
/// 1. 100W,
/// 2. 100W带刹车,
/// 3. 200W,
/// 4. 200W带刹车,
/// 5. 400W低惯量,
/// 6. 400W低惯量带刹车,
/// 7. 750W低惯量,
/// 8. 750W低惯量带刹车,
/// 9. 1000W220V,
/// 10.1000W带刹车220V,
/// 11.1500W220V,
/// 12.1500带刹车220V,
/// </summary>
public List<ConfigItemModel> GetMotorPowers()
{
//return new List<ConfigItemModel>
//{
// new(){ ItemName = "100W", ItemValue = "100W"},
// new(){ ItemName = "100W带刹车", ItemValue = "100W带刹车"},
// new(){ ItemName = "200W", ItemValue = "200W"},
// new(){ ItemName = "200W带刹车", ItemValue = "200W带刹车"},
// new(){ ItemName = "400W低惯量", ItemValue = "400W低惯量"},
// new(){ ItemName = "400W低惯量带刹车", ItemValue = "400W低惯量带刹车"},
// new(){ ItemName = "750W低惯量", ItemValue = "750W低惯量"},
// new(){ ItemName = "750W低惯量带刹车", ItemValue = "750W低惯量带刹车"},
// new(){ ItemName = "1000W220V", ItemValue = "1000W220V"},
// new(){ ItemName = "1000W带刹车220V", ItemValue = "1000W带刹车220V"},
// new(){ ItemName = "1500W220V", ItemValue = "1500W220V"},
// new(){ ItemName = "1500带刹车220V", ItemValue = "1500带刹车220V"},
//};
var data = MiniExcel.Query(dataFileStream, sheetName: "电机驱动器", startCell: "A2", 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()
)).ToList();
}
/// <summary>
/// 1.动力线,
/// 2.编码器线,
/// 3.动力刹车线,
/// 4.编码器线+动力线,
/// 5.编码器线+动力刹车线,
/// </summary>
/// <returns></returns>
public List<ConfigItemModel> GetCableTypes()
{
//return new List<ConfigItemModel>
//{
// new(){ ItemName = "动力线", ItemValue = "动力线,"},
// new(){ ItemName = "编码器线", ItemValue = "编码器线"},
// new(){ ItemName = "动力刹车线", ItemValue = "动力刹车线"},
// new(){ ItemName = "编码器线+动力线", ItemValue = "编码器线+动力线"},
// new(){ ItemName = "编码器线+动力刹车线", ItemValue = "编码器线+动力刹车线"},
//};
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).ToList();
}
/// <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,
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();
}
}
}