using Rhea.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static Tiger.Business.WMS.Barcode;
namespace Tiger.Business.WMS
{
///
/// 条码分析基类
///
public class BarcodeAnalysis
{
public Barcode Barcode;
///
/// 条码分析
///
///
public virtual Result Analyse(Barcode barcode)
{
var result = new Result(Result.Flags.Failed);
Barcode = barcode;
if (!string.IsNullOrEmpty(Barcode.MetaSn))
{
Barcode.QtyStr = "1";
Barcode.Type = Types.Other;
Barcode.ItemType = ItemTypes.Unknown;
result.Flag = Result.Flags.Success;
}
result.Data = Barcode;
return result;
}
///
/// 根据字符串返回流水号
///
///
///
public static int? GetSerialNo(string str)
{
int serialNo;
try
{
serialNo = Convert.ToInt32(new Regex(@"[^\d]*").Replace(str, ""));
}
catch
{
return null;
}
return serialNo;
}
///
/// 根据字符串返回重打流水号
///
///
///
public static int? GetReprintNo(string str)
{
int qty;
try
{
qty = Convert.ToInt32(str, 16);
}
catch
{
return null;
}
return qty;
}
///
/// 根据字符串返回数量
///
///
///
public static int? GetInt32(string str)
{
int qty;
try
{
qty = Convert.ToInt32(str);
}
catch
{
return null;
}
return qty;
}
///
/// 根据字符串返回数量
///
///
///
public static double? GetDouble(string str)
{
double qty;
try
{
qty = Convert.ToDouble(str);
}
catch
{
return null;
}
return qty;
}
///
/// 根据日期编码返回生产日期
///
///
///
public static DateTime? GetDateTime(string dateStr)
{
DateTime proddate;
dateStr = dateStr ?? "";
if (dateStr.Length == 8)
{
try
{
//年月日
proddate = DateTime.ParseExact(dateStr, "yyyyMMdd", new CultureInfo("zh-CN", true));
}
catch
{
return null;
}
}
else if (dateStr.Length == 6)
{
try
{
//年月日
proddate = DateTime.ParseExact(dateStr, "yyMMdd", new CultureInfo("zh-CN", true));
}
catch
{
try
{
//年周
var firstDate = Convert.ToDateTime(dateStr.Substring(0, 4) + "-1-1");
int weekNum = Convert.ToInt32(dateStr.Substring(4, 2));
int daysOffset = DayOfWeek.Monday - firstDate.DayOfWeek;
DateTime firstSunday = firstDate.AddDays(daysOffset);
var calendar = CultureInfo.CurrentCulture.Calendar;
int firstWeek = calendar.GetWeekOfYear(firstDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
if (firstWeek <= 1)
{
weekNum -= 1;
}
proddate = firstSunday.AddDays(weekNum * 7 - 1);
if (proddate.Year < firstDate.Year)
{
proddate = firstDate;
}
if (proddate.Year > firstDate.Year)
{
proddate = firstDate.AddYears(1).AddDays(-1);
}
}
catch
{
try
{
//年月
proddate = DateTime.ParseExact(dateStr, "yyyyMM", new CultureInfo("zh-CN", true));
}
catch
{
try
{
proddate = Convert.ToDateTime(dateStr);
}
catch
{
//proddate = DateTime.Now;
return null;
}
}
}
}
}
else if (dateStr.Length == 4)
{
try
{
//年周
var firstDate = DateTime.ParseExact(dateStr.Substring(0, 2), "yy", CultureInfo.CurrentCulture);
int weekNum = Convert.ToInt32(dateStr.Substring(2, 2));
int daysOffset = DayOfWeek.Monday - firstDate.DayOfWeek;
DateTime firstSunday = firstDate.AddDays(daysOffset);
var calendar = CultureInfo.CurrentCulture.Calendar;
int firstWeek = calendar.GetWeekOfYear(firstDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
if (firstWeek <= 1)
{
weekNum -= 1;
}
proddate = firstSunday.AddDays(weekNum * 7 - 1);
if (proddate.Year < firstDate.Year)
{
proddate = firstDate;
}
if (proddate.Year > firstDate.Year)
{
proddate = firstDate.AddYears(1).AddDays(-1);
}
}
catch
{
try
{
//年月
proddate = DateTime.ParseExact(dateStr, "yyMM", new CultureInfo("zh-CN", true));
}
catch
{
try
{
//年
proddate = DateTime.ParseExact(dateStr, "yyyy", new CultureInfo("zh-CN", true));
}
catch
{
try
{
proddate = Convert.ToDateTime(dateStr);
}
catch
{
//无法识别则使用当前时间
//proddate = DateTime.Now;
return null;
}
}
}
}
}
else
{
try
{
proddate = Convert.ToDateTime(dateStr);
}
catch
{
//proddate = DateTime.Now;
return null;
}
}
return proddate;
}
///
/// 是否非法条码
///
///
///
public static bool IsIllegal(string sn)
{
return !new Barcode(sn).IsRegular;
}
///
/// 获取条码类型
///
///
///
public static Barcode.Types GetType(string sn)
{
return new Barcode(sn).Type;
}
///
/// 判断是否重打的条码
///
///
///
public static bool IsReprint(string sn)
{
return new Barcode(sn).IsReprintSn;
}
}//endClass
}