Exemplo n.º 1
0
        public void ReturnBarcode(string progId, string prefix, string barcode)
        {
            string key      = string.Format("{0}/t{1}", progId, prefix);
            object lockItem = lockObjDic.GetOrAdd(key, new object());

            lock (lockItem)
            {
                BarcodeValue barcodeValue = this.Get <BarcodeValue>(key);
                if (barcodeValue != null)
                {
                    barcodeValue.Unused.Enqueue(barcode);
                }
            }
        }
Exemplo n.º 2
0
        public IList <string> GetBarcode(string progId, string fieldName, string prefix, int serialLen, int number = 1)
        {
            IList <string> list     = new List <string>();
            string         key      = string.Format("{0}/t{1}", progId, prefix);
            object         lockItem = lockObjDic.GetOrAdd(key, new object());

            lock (lockItem)
            {
                BarcodeValue barcodeValue = this.Get <BarcodeValue>(key);

                if (barcodeValue == null)
                {
                    string        curSerial  = string.Empty;
                    int           len        = prefix.Length + serialLen;
                    SqlBuilder    sqlBuilder = new SqlBuilder(progId);
                    string        sql        = sqlBuilder.GetQuerySql(0, string.Format("A.{0}", fieldName), string.Format("A.{0} Like '{1}%'", fieldName, prefix), string.Format("A.{0} DESC", fieldName));
                    LibDataAccess dataAccess = new LibDataAccess();
                    int           serial     = 0;
                    using (IDataReader reader = dataAccess.ExecuteDataReader(sql))
                    {
                        while (reader.Read())
                        {
                            string temp = reader.GetString(0);
                            if (temp.Length != len)
                            {
                                continue;
                            }
                            if (!int.TryParse(temp.Substring(prefix.Length, serialLen), out serial))
                            {
                                continue;
                            }
                            curSerial = temp;
                            break;
                        }
                    }
                    int value = 0;
                    if (!string.IsNullOrEmpty(curSerial))
                    {
                        int.TryParse(curSerial.Substring(prefix.Length, serialLen), out value);
                    }
                    barcodeValue = new BarcodeValue()
                    {
                        MaxValue = value
                    };
                }
                if (barcodeValue.Unused.Count >= number)
                {
                    for (int i = 0; i < number; i++)
                    {
                        list.Add(barcodeValue.Unused.Dequeue());
                    }
                }
                else
                {
                    for (int i = 0; i < number; i++)
                    {
                        string maxValue = (++barcodeValue.MaxValue).ToString();
                        list.Add(string.Format("{0}{1}", prefix, maxValue.PadLeft(serialLen, '0')));
                    }

                    this.Set(key, barcodeValue, new TimeSpan(0, 180, 0));
                }
            }
            return(list);
        }