Files
shgx_tz_mes_backend_sync/ZR.Service/Utils/MaterialUtils.cs

383 lines
16 KiB
C#
Raw Normal View History

using System;
2024-08-14 14:58:48 +08:00
using System.Text.RegularExpressions;
using ZR.Model.MES.wms;
2024-06-07 11:04:26 +08:00
using ZR.Model.MES.wms.Dto;
namespace ZR.Service.Utils
{
2024-06-07 11:04:26 +08:00
public class MaterialUtils : BaseService<WmMaterial>
{
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
2024-08-14 14:58:48 +08:00
/// <summary>
/// 解析测试
/// </summary>
/// <param name="str">测试字符串</param>
/// <returns></returns>
public string ResolutionTestUtil(string str)
{
try
{
// 定义正则表达式模式
string partnumberPattern = @"ERP(\w+)PQ"; // 产品零件号
string quantityPattern = @"PQ(\d+)D"; // 产品数量
string batchCodePattern = @"D(\d+)S"; // 产品生产批次
// 使用正则表达式进行匹配
Match partnumberMatch = Regex.Match(str, partnumberPattern);
Match quantityMatch = Regex.Match(str, quantityPattern);
Match batchCodeMatch = Regex.Match(str, batchCodePattern);
// 创建接收
string partnumber = "";
string quantity = "";
string batchCode = "";
// 判断解析是否成功
if (!partnumberMatch.Success)
{
2024-08-14 14:58:48 +08:00
throw new Exception("解析零件号失败");
}
if (!quantityMatch.Success)
{
throw new Exception("解析产品数量失败");
}
if (!batchCodeMatch.Success)
{
throw new Exception("解析产品生产批次失败");
}
partnumber = partnumberMatch.Groups[1].Value;
quantity = quantityMatch.Groups[1].Value;
batchCode = batchCodeMatch.Groups[1].Value;
return batchCode;
}
catch (Exception e)
{
throw new Exception("解析失败" + e.Message);
}
}
//解析外箱标签码
public ResultionPackageCodeDto ResolutionPackage(string code)
{
// 德国大众
if (code.Contains("MX2D") && code.Contains("MLX"))
{
return ResolutionPackagecode3(code);
}
// 毛坯标签
if (code.Contains('/'))
{
// 初步进行解析检测,增加解析成功率
string[] splitstr = code.Split('/');
if (splitstr.Length < 1)
{
return null;
}
return ResolutionPackagecode4(code);
}
// 成品标签
2024-08-05 17:25:52 +08:00
if (code.Contains('^'))
{
2024-04-09 14:03:10 +08:00
// 初步进行解析检测,增加解析成功率
string[] splitstr = code.Split('^');
if (splitstr.Length < 1)
{
return null;
}
// 第一类
if (splitstr[0].Contains("Code="))
{
return ResolutionPackagecode1(code);
}
// 第二类
if (splitstr[3].Contains("Cd="))
{
return ResolutionPackagecode2(code);
}
}
2024-06-07 11:04:26 +08:00
// 解析失败
return null;
}
/// <summary>
/// 1-解析鲨鱼鳍外箱标签码
/// </summary>
/// <param name="packagecode"></param>
/// <returns></returns>
private ResultionPackageCodeDto ResolutionPackagecode1(string packagecode)
{
ResultionPackageCodeDto resultionPackageCode = new ResultionPackageCodeDto();
try
{
resultionPackageCode.originalCode = packagecode;
// 解析外箱标签码
string[] splitstr = packagecode.Split('^');
// 解析批次号
resultionPackageCode.PatchCode = splitstr[0].Substring(5);
// 解析零件号
string partnumber = splitstr[1].Substring(11);
// 有的零件号带了-FL,不是很标准
resultionPackageCode.PartNumner = partnumber;
// 解析工单号 工单号会带个W需要去掉
string workoderidid = splitstr[2].Substring(7);
resultionPackageCode.WorkoderID = workoderidid;
// 解析生产时间 工单号生产时间提取
resultionPackageCode.ProductionTime = string.Concat(
"20",
workoderidid.AsSpan(0, 6)
);
// 解析箱子中产品数量
string product_num = splitstr[3].Substring(4);
resultionPackageCode.Quantity = int.Parse(product_num);
// 解析产品描述 partnumber 从物料列表抓取数据
WmMaterial material = Context
.Queryable<WmMaterial>()
.Where(it => it.Partnumber == partnumber)
.First();
if (material == null)
{
resultionPackageCode.ProductionDescribe = "物料记录未录入此零件号信息!";
return resultionPackageCode;
}
string des1 = material.Description;
string des2 = material.ProductName;
if (!string.IsNullOrEmpty(des1))
{
resultionPackageCode.ProductionDescribe = des1;
}
else if (!string.IsNullOrEmpty(des2))
{
resultionPackageCode.ProductionDescribe = des2;
}
return resultionPackageCode;
}
catch (Exception ex)
{
throw new Exception($"外箱标签码,解析失败-1 {ex.Message}");
}
}
/// <summary>
/// 2-解析门把手
/// </summary>
/// <param name="packagecode"></param>
/// <returns></returns>
private ResultionPackageCodeDto ResolutionPackagecode2(string packagecode)
{
ResultionPackageCodeDto resultionPackageCode = new ResultionPackageCodeDto();
try
{
// 原始编码
resultionPackageCode.originalCode = packagecode;
// 解析外箱标签码
string[] splitstr = packagecode.Split('^');
// 解析批次号
resultionPackageCode.PatchCode = splitstr[3].Substring(3);
// 解析零件号
string partnumber = splitstr[5].Substring(3);
resultionPackageCode.PartNumner = partnumber;
// 解析工单号
2024-04-22 17:10:57 +08:00
string workoderidid = splitstr[3].Substring(6, 9);
resultionPackageCode.WorkoderID = workoderidid;
// 生产时间
resultionPackageCode.ProductionTime = "20" + workoderidid.Substring(0, 6);
//todo 解析箱子中产品数量
string product_num = splitstr[4].Substring(3);
resultionPackageCode.Quantity = int.Parse(product_num);
// 解析产品描述 partnumber 从物料列表抓取数据
WmMaterial material = Context
.Queryable<WmMaterial>()
.Where(it => it.Partnumber == partnumber)
.First();
if (material == null)
{
resultionPackageCode.ProductionDescribe = "物料记录未录入此零件号信息!";
return resultionPackageCode;
}
string des1 = material.Description;
string des2 = material.ProductName;
if (!string.IsNullOrEmpty(des1))
{
resultionPackageCode.ProductionDescribe = des1;
}
else if (!string.IsNullOrEmpty(des2))
{
resultionPackageCode.ProductionDescribe = des2;
}
else
{
resultionPackageCode.ProductionDescribe = "物料记录内未填写详情信息!";
}
return resultionPackageCode;
}
catch (Exception ex)
{
throw new Exception($"外箱标签码,解析失败-2 {ex.Message}");
}
}
2024-08-14 14:58:48 +08:00
/// <summary>
/// 3-解析德国大众标签
/// </summary>
/// <param name="packagecode">原始标签码</param>
/// <returns></returns>
private ResultionPackageCodeDto ResolutionPackagecode3(string packagecode)
{
try
{
// 定义正则表达式模式
string partnumberPattern = @"1P(\d+)Q"; // 产品零件号
string quantityPattern = @"Q(\d+)S"; // 产品数量
string batchCodePattern = @"S(\d+)13Q"; // 批次号(工单号)
string serialNumberPattern = @"13Q(\d+)B"; // 流水号
string productionTimePattern = @"12D(\d+)4L"; // 生产日期
// 使用正则表达式进行匹配
Match partnumberMatch = Regex.Match(packagecode, partnumberPattern);
Match quantityMatch = Regex.Match(packagecode, quantityPattern);
Match batchCodeMatch = Regex.Match(packagecode, batchCodePattern);
Match serialNumberMatch = Regex.Match(packagecode, serialNumberPattern);
Match productionTimeMatch = Regex.Match(packagecode, productionTimePattern);
// 创建接收
string partnumber = "";
string quantityStr = "";
string batchCode = "";
string serialNumber = "";
string productionTime = "";
// 判断解析是否成功
if (!partnumberMatch.Success)
{
throw new Exception("解析零件号失败");
}
if (!quantityMatch.Success)
{
throw new Exception("解析产品数量失败");
}
if (!batchCodeMatch.Success)
{
throw new Exception("解析产品批次号失败");
}
if (!serialNumberMatch.Success)
{
throw new Exception("解析产品流水号失败");
}
if (!productionTimeMatch.Success)
{
throw new Exception("解析产品生产日期失败");
}
partnumber = partnumberMatch.Groups[1].Value;
quantityStr = quantityMatch.Groups[1].Value.TrimStart('0');
batchCode = batchCodeMatch.Groups[1].Value.TrimStart('0');
serialNumber = serialNumberMatch.Groups[1].Value.TrimStart('0');
productionTime = productionTimeMatch.Groups[1].Value;
string PatchCode = "BNW" + batchCode + '_' + serialNumber;
string WorkoderID = batchCode;
string ProductionTime = productionTime;
bool isSuccess1 = int.TryParse(quantityStr, out int quantity);
if (!isSuccess1)
{
quantity = 0;
}
// 产品描述
string ProductionDescribe = "";
WmMaterial material = Context
.Queryable<WmMaterial>()
.Where(it => it.Partnumber == partnumber)
.Where(it => it.Type == 1)
.Where(it => it.Status == 1)
.First();
if (material != null)
{
ProductionDescribe = !string.IsNullOrEmpty(material.Description)
? material.Description
: material.ProductName;
}
ResultionPackageCodeDto resultionPackageCode =
new()
{
originalCode = packagecode,
PatchCode = PatchCode,
WorkoderID = WorkoderID,
PartNumner = partnumber,
ProductionTime = ProductionTime,
Quantity = quantity,
Team = "",
ProductionDescribe = ProductionDescribe,
Remark = "德国大众",
};
return resultionPackageCode;
}
catch (Exception e)
{
throw new Exception("标签解析失败-3" + e.Message);
}
}
/// <summary>
/// 4-解析毛坯标签码
/// </summary>
/// <param name="packagecode"></param>
/// <returns></returns>
private ResultionPackageCodeDto ResolutionPackagecode4(string packagecode)
{
ResultionPackageCodeDto resultionPackageCode = new ResultionPackageCodeDto();
try
{
resultionPackageCode.originalCode = packagecode;
// 解析外箱标签码
string[] splitstr = packagecode.Split('/');
// 解析批次号
resultionPackageCode.PatchCode = splitstr[0];
// 解析零件号
string partnumber = "";
// 零件号
resultionPackageCode.PartNumner = partnumber;
// 毛坯号
string _blankNumber = splitstr[4];
// 去掉毛坯号中带横杠的后缀(如-M2
if (!string.IsNullOrEmpty(_blankNumber) && _blankNumber.Contains('-'))
{
_blankNumber = _blankNumber.Split('-')[0];
}
resultionPackageCode.BlankNumber = _blankNumber;
// 解析工单号 工单号会带个W需要去掉
string workoderidid = "";
resultionPackageCode.WorkoderID = workoderidid;
// 解析生产时间 工单号生产时间提取
resultionPackageCode.ProductionTime = splitstr[0];
// 解析箱子中产品数量
string product_num = splitstr[3];
resultionPackageCode.Quantity = int.Parse(product_num);
// 解析产品描述 partnumber 从物料列表抓取数据
WmMaterial material = Context
.Queryable<WmMaterial>()
.Where(it => it.BlankNum == _blankNumber)
.Where(it => it.Type == 2)
.Where(it => it.Status == 1)
.First();
if (material == null)
{
resultionPackageCode.ProductionDescribe = "物料清单未录入此毛坯号信息!";
return resultionPackageCode;
}
string des1 = material.Description;
string des2 = material.ProductName;
if (!string.IsNullOrEmpty(des1))
{
resultionPackageCode.ProductionDescribe = des1;
}
else if (!string.IsNullOrEmpty(des2))
{
resultionPackageCode.ProductionDescribe = des2;
}
return resultionPackageCode;
}
catch (Exception ex)
{
throw new Exception($"标签码解析失败-4 {ex.Message}");
}
}
}
}