using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Globalization; namespace FATrace.Com { /// /// NVR 公共的类 /// public class NVRCom { /// /// 获取视频名称 /// /// /// public static string GetVideoPathName(string basePath, 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 $"{basePath}\\{safeCode}.mp4"; } /// /// 获取视频名称 /// /// 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"; } /// /// 解析条码 /// /// 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; } } }