初步版本251204
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Globalization;
|
||||
|
||||
namespace FATrace.Com
|
||||
{
|
||||
@@ -17,7 +18,7 @@ namespace FATrace.Com
|
||||
/// </summary>
|
||||
/// <param name="configKey"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetVideoName(string basePath,string Code)
|
||||
public static string GetVideoPathName(string basePath, string Code)
|
||||
{
|
||||
// 清洗非法文件名字符,避免保存失败
|
||||
string safeCode = Code;
|
||||
@@ -25,11 +26,156 @@ namespace FATrace.Com
|
||||
{
|
||||
var invalid = System.IO.Path.GetInvalidFileNameChars();
|
||||
safeCode = new string(Code.Where(c => !invalid.Contains(c)).ToArray());
|
||||
if (string.IsNullOrWhiteSpace(safeCode)) safeCode = "CODE";
|
||||
if (string.IsNullOrWhiteSpace(safeCode)) safeCode = DateTime.Now.ToString("yyyy-MM-dd HHmmss");
|
||||
|
||||
}
|
||||
catch { }
|
||||
|
||||
return $"{basePath}\\{DateTime.Now.ToString("yyyy-MM-dd HHmmss")} {safeCode}.mp4";
|
||||
return $"{basePath}\\{safeCode}.mp4";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取视频名称
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetVideoName(string Code)
|
||||
{
|
||||
string safeCode = Code;
|
||||
try
|
||||
{
|
||||
var invalid = System.IO.Path.GetInvalidFileNameChars();
|
||||
safeCode = new string(Code.Where(c => !invalid.Contains(c)).ToArray());
|
||||
if (string.IsNullOrWhiteSpace(safeCode)) safeCode = DateTime.Now.ToString("yyyy-MM-dd HHmmss");
|
||||
|
||||
}
|
||||
catch { }
|
||||
return $"{safeCode}.mp4";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析条码
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static (string RawCode, string Batch, string Weight) ParseCode(string Code)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Code))
|
||||
{
|
||||
return (string.Empty, string.Empty, string.Empty);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 标准化分隔符并切分:支持 英文逗号/中文逗号/分号/空格
|
||||
var normalized = Code.Trim()
|
||||
.Replace(',', ',')
|
||||
.Replace(';', ',')
|
||||
.Replace(';', ',');
|
||||
var parts = normalized
|
||||
.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(p => p.Trim())
|
||||
.ToArray();
|
||||
|
||||
string rawCode = parts.Length > 0 ? parts[0] : string.Empty;
|
||||
|
||||
// 批号:通常为 8 位日期 yyyyMMdd;保留前 8 位数字
|
||||
string batch = string.Empty;
|
||||
if (parts.Length > 1)
|
||||
{
|
||||
var digits = new string(parts[1].Where(char.IsDigit).ToArray());
|
||||
if (digits.Length >= 8) batch = digits.Substring(0, 8);
|
||||
else batch = digits; // 若不足 8 位,原样返回可供上层判定
|
||||
}
|
||||
|
||||
// 重量:3/4 位数字,最后一位为小数位(例:802 => 80.2g)。
|
||||
string weight = string.Empty;
|
||||
if (parts.Length > 2)
|
||||
{
|
||||
var digits = new string(parts[2].Where(char.IsDigit).ToArray());
|
||||
if (digits.Length >= 2)
|
||||
{
|
||||
// 在最后一位前插入小数点,如 802 => 80.2,1234 => 123.4
|
||||
weight = string.Concat(digits.AsSpan(0, digits.Length - 1), ".", digits.AsSpan(digits.Length - 1));
|
||||
}
|
||||
else if (digits.Length == 1)
|
||||
{
|
||||
weight = "0." + digits; // 兜底:1 位数字视为 0.x
|
||||
}
|
||||
}
|
||||
|
||||
return (rawCode, batch, weight);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return (string.Empty, string.Empty, string.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public static ParsedCodeInfo ParseCodeFull(string code)
|
||||
{
|
||||
var result = new ParsedCodeInfo();
|
||||
if (string.IsNullOrWhiteSpace(code)) return result;
|
||||
result.Code = code;
|
||||
|
||||
try
|
||||
{
|
||||
var normalized = code.Trim()
|
||||
.Replace(',', ',')
|
||||
.Replace(';', ',')
|
||||
.Replace(';', ',');
|
||||
var parts = normalized
|
||||
.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(p => p.Trim())
|
||||
.ToArray();
|
||||
|
||||
result.RawCode = parts.Length > 0 ? parts[0] : string.Empty;
|
||||
|
||||
if (parts.Length > 1)
|
||||
{
|
||||
var digits = new string(parts[1].Where(char.IsDigit).ToArray());
|
||||
result.Batch = digits.Length >= 8 ? digits.Substring(0, 8) : digits;
|
||||
}
|
||||
|
||||
if (parts.Length > 2)
|
||||
{
|
||||
var digits = new string(parts[2].Where(char.IsDigit).ToArray());
|
||||
string weightStr = string.Empty;
|
||||
if (digits.Length >= 2)
|
||||
{
|
||||
weightStr = string.Concat(digits.AsSpan(0, digits.Length - 1), ".", digits.AsSpan(digits.Length - 1));
|
||||
}
|
||||
else if (digits.Length == 1)
|
||||
{
|
||||
weightStr = "0." + digits;
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(weightStr))
|
||||
{
|
||||
if (decimal.TryParse(weightStr, NumberStyles.Any, CultureInfo.InvariantCulture, out var w))
|
||||
result.Weight = w;
|
||||
}
|
||||
}
|
||||
|
||||
if (parts.Length > 3)
|
||||
{
|
||||
var digits = new string(parts[3].Where(char.IsDigit).ToArray());
|
||||
if (int.TryParse(digits, out var m)) result.ShelfLifeMonths = m;
|
||||
}
|
||||
|
||||
if (parts.Length > 4)
|
||||
{
|
||||
var digits = new string(parts[4].Where(char.IsDigit).ToArray());
|
||||
result.RegionCode = digits;
|
||||
result.RegionName = digits == "01" ? "国内" : (digits == "02" ? "日本" : "未知");
|
||||
}
|
||||
|
||||
if (parts.Length > 5)
|
||||
{
|
||||
var digits = new string(parts[5].Where(char.IsDigit).ToArray());
|
||||
if (int.TryParse(digits, out var c)) result.Count = c;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
26
FATrace.Com/ParsedCodeInfo.cs
Normal file
26
FATrace.Com/ParsedCodeInfo.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FATrace.Com
|
||||
{
|
||||
public class ParsedCodeInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 直接的条码
|
||||
/// 从这个条码中解析出其他信息
|
||||
/// </summary>
|
||||
public string Code { get; set; } = string.Empty;
|
||||
|
||||
public string RawCode { get; set; } = string.Empty;
|
||||
public string RawName { get; set; } = string.Empty;
|
||||
public string Batch { get; set; } = string.Empty;
|
||||
public decimal Weight { get; set; }
|
||||
public int ShelfLifeMonths { get; set; }
|
||||
public string RegionCode { get; set; } = string.Empty;
|
||||
public string RegionName { get; set; } = string.Empty;
|
||||
public int Count { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user