Esempio n. 1
0
 public async Task Add(SerialNumberRecord record)
 {
     await _serialNumberRecordStore.Add(record);
 }
Esempio n. 2
0
 public async Task RefreashValue(SerialNumberRecord record)
 {
     await _serialNumberRecordStore.UpdateValue(record);
 }
        public async Task <string> GenerateSerialNumber(SerialNumberGeneratorConfiguration configuration, Dictionary <string, object> parameters)
        {
            var lcid = ContextContainer.GetValue <int>(ContextTypes.CurrentUserLcid);
            //使用模板服务获取前缀
            var strPrefix = await _templateService.Convert(configuration.PrefixTemplate, new TemplateContext(lcid, parameters));

            //查询序列号记录
            var record = await _serialNumberRecordRepository.QueryByPrefix(strPrefix);

            //如果不存在,则尝试新增,如果新增失败,并且错误Code为重复,则重新查询
            //如果发现仍不存在,则抛出错误
            long recordValue = 0;

            if (record == null)
            {
                record = new SerialNumberRecord()
                {
                    Prefix = strPrefix
                };

                bool needQueryAgain = false;
                try
                {
                    await record.Add();
                }
                catch (UtilityException ex)
                {
                    if (ex.Code == 314720331)
                    {
                        needQueryAgain = true;
                    }
                    else
                    {
                        throw;
                    }
                }

                if (needQueryAgain)
                {
                    record = await _serialNumberRecordRepository.QueryByPrefix(strPrefix);

                    if (record == null)
                    {
                        TextFragment fragment = new TextFragment()
                        {
                            Code = TextCodes.NotFoundSerialNumberRecordByPrefix,
                            DefaultFormatting = "前缀为{0}的序列号记录不存在",
                            ReplaceParameters = new List <object>()
                            {
                                strPrefix
                            }
                        };
                        throw new UtilityException((int)Errors.NotFoundSerialNumberRecordByPrefix, fragment);
                    }
                    await record.RefreashValue();
                }
            }
            else
            {
                await record.RefreashValue();
            }

            recordValue = record.Value;
            return($"{strPrefix}{recordValue.GetPrefixComplete(configuration.SerialLength)}");
        }