CPS条码生成原理
系列目录
前言
本节主要介绍CPS中产品条码、批次号的生成原理。
代码实现
CPS中条码生成主要涉及四个表:
PRD_SERIAL_BOOK(标签序列号流水记录表)
CFG_LABEL_RULE(条码标签规则)
CFG_LABEL_TEMPLATE(标签模板维护)
CFG_LABEL_TEMPLATE_PARAMETER(模板参数维护)
CFG_LABEL_PARAMETER(标签参数维护)
,
其中 PRD_SERIAL_BOOK和CFG_LABEL_RULE作用是生产条码,CFG_LABEL_TEMPLATE和CFG_LABEL_PARAMETER是打印标签。
根据条码规则和流水记录表生成唯一码,如下:
/// <summary>
/// CPS页面生产流水号公共方法
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public string GenerateCommonSerialNumber(CommonLotNumberInput input)
{
try
{
lock (SerialLock)
{
if (String.IsNullOrEmpty(input.RuleCode))
{
throw new UserFriendlyException("条码规则不能为空");
}
LabelRule labelRule = _labelRuleRepository.FirstOrDefault(r => r.RuleCode == input.RuleCode);
if (labelRule == null)
{
throw new UserFriendlyException(string.Format("该[{0}]条码规则不存在", input.RuleCode));
}
int n = labelRule.SerialLenth;
string format = string.Empty;
while (n > 0)
{
format += "0";
n--;
}
DateTime dtBusiness = Convert.ToDateTime(DateTime.Now);
GregorianCalendar gc = new GregorianCalendar();
SerialBook serialBook = _serialBookRepository.FirstOrDefault(s => s.SNType == labelRule.RuleCode && s.GenerateCondition == labelRule.Prefix);
if (serialBook == null)
{
serialBook = new SerialBook
{
TenantId = AbpSession.TenantId,
OrgId = AbpSession.OrgId,
SNType = labelRule.RuleCode,
Prefix = labelRule.Prefix,
CreationTime = DateTime.Now,
Year = dtBusiness.Year,
Quarter = GetQuarter(dtBusiness.Month),
Month = dtBusiness.Month,
Week = gc.GetWeekOfYear(dtBusiness, CalendarWeekRule.FirstDay, DayOfWeek.Monday),
Date = dtBusiness.Day,
MaxSerial = 1.ToString(format),
GenerateCondition = labelRule.Prefix,
};
serialBook = _serialBookRepository.Insert(serialBook);
CurrentUnitOfWork.SaveChanges();
}
else
{
serialBook.MaxSerial = (serialBook.MaxSerial.ToInt32() + 1).ToString(format);
serialBook = _serialBookRepository.Update(serialBook);
}
if (serialBook.MaxSerial.Length > labelRule.SerialLenth)
{
throw new UserFriendlyException(L("SerialLenthTooLarge", labelRule.RuleCode));
}
//生成条码 前缀+年+月+日+流水号
string barcode = labelRule.Prefix;
if (labelRule.Year)
{
barcode += dtBusiness.Year.ToString().Remove(0, 2);
}
if (labelRule.Month)
{
barcode += dtBusiness.Month.ToString("00");
}
if (labelRule.Date)
{
barcode += dtBusiness.Day.ToString("00");
}
barcode += serialBook.MaxSerial;
return barcode;
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}