Example #1
0
        public async Task SaveData(PB_BarCodeSerial data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_BarCodeSerialBus.AddDataAsync(data);
            }
            else
            {
                await _pB_BarCodeSerialBus.UpdateDataAsync(data);
            }
        }
        private string GenerateByRule(List <PB_BarCodeSerial> listSerial, PB_BarCodeRule rule, Dictionary <string, string> para = null)
        {
            var now  = DateTime.Now;
            var code = "";

            switch (rule.Type)
            {
            case "Const": code = rule.Rule; break;

            case "Date": code = now.ToString(rule.Rule.IsNullOrEmpty() ? "YYYYMMDD" : rule.Rule); break;

            case "Serial":
            {
                var serial = listSerial.SingleOrDefault(w => w.ParaName == "Serial");
                if (serial == null)
                {
                    serial = new PB_BarCodeSerial()
                    {
                        ParaName  = "Serial",
                        ParaValue = "",
                        SerialNum = 0
                    };
                    listSerial.Add(serial);
                }
                else
                {
                    serial.TypeId = null;
                }
                var seq = serial.SerialNum + 1;
                code = seq.ToString();
                if (rule.length.HasValue)
                {
                    code = code.PadLeft(rule.length.Value, '0');
                }
                serial.SerialNum = seq;
            }
            break;

            case "Daily":
            {
                var serial = listSerial.SingleOrDefault(w => w.ParaName == "Daily");
                if (serial == null)
                {
                    serial = new PB_BarCodeSerial()
                    {
                        ParaName  = "Daily",
                        ParaValue = now.Date.ToString("yyyy-MM-dd HH:mm:ss"),
                        SerialNum = 0
                    };
                    listSerial.Add(serial);
                }
                else
                {
                    serial.TypeId = null;
                }
                var seqDate = serial.ParaValue.ToDateTime();
                if ((now - seqDate).Days == 0)
                {
                    var seq = serial.SerialNum + 1;
                    code = seq.ToString();
                    if (rule.length.HasValue)
                    {
                        code = code.PadLeft(rule.length.Value, '0');
                    }
                    serial.SerialNum = seq;
                }
                else
                {
                    var seq = 1;
                    code = seq.ToString();
                    if (rule.length.HasValue)
                    {
                        code = code.PadLeft(rule.length.Value, '0');
                    }
                    serial.SerialNum = seq;
                    serial.ParaValue = now.Date.ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
            break;

            case "PerMonth":
            {
                var serial = listSerial.SingleOrDefault(w => w.ParaName == "PerMonth");
                if (serial == null)
                {
                    serial = new PB_BarCodeSerial()
                    {
                        ParaName  = "PerMonth",
                        ParaValue = now.Date.ToString("yyyy-MM-dd HH:mm:ss"),
                        SerialNum = 0
                    };
                    listSerial.Add(serial);
                }
                else
                {
                    serial.TypeId = null;
                }
                var seqDate = serial.ParaValue.ToDateTime();
                if (now.Year == seqDate.Year && now.Month == seqDate.Month)
                {
                    var seq = serial.SerialNum + 1;
                    code = seq.ToString();
                    if (rule.length.HasValue)
                    {
                        code = code.PadLeft(rule.length.Value, '0');
                    }
                    serial.SerialNum = seq;
                }
                else
                {
                    var seq = 1;
                    code = seq.ToString();
                    if (rule.length.HasValue)
                    {
                        code = code.PadLeft(rule.length.Value, '0');
                    }
                    serial.SerialNum = seq;
                    serial.ParaValue = now.Date.ToString("yyyy-MM-dd HH:mm:ss");
                }
            };
                break;

            case "PerYear":
            {
                var serial = listSerial.SingleOrDefault(w => w.ParaName == "PerYear");
                if (serial == null)
                {
                    serial = new PB_BarCodeSerial()
                    {
                        ParaName  = "PerYear",
                        ParaValue = now.Date.ToString("yyyy-MM-dd HH:mm:ss"),
                        SerialNum = 0
                    };
                    listSerial.Add(serial);
                }
                else
                {
                    serial.TypeId = null;
                }
                var seqDate = serial.ParaValue.ToDateTime();
                if (now.Year == seqDate.Year)
                {
                    var seq = serial.SerialNum + 1;
                    code = seq.ToString();
                    if (rule.length.HasValue)
                    {
                        code = code.PadLeft(rule.length.Value, '0');
                    }
                    serial.SerialNum = seq;
                }
                else
                {
                    var seq = 1;
                    code = seq.ToString();
                    if (rule.length.HasValue)
                    {
                        code = code.PadLeft(rule.length.Value, '0');
                    }
                    serial.SerialNum = seq;
                    serial.ParaValue = now.Date.ToString("yyyy-MM-dd HH:mm:ss");
                }
            };
                break;

            case "Random":
            {
                var newId   = IdHelper.GetLongId();
                var buf     = new char[32];
                var binLen  = 33;
                int charPos = 32;
                while ((newId / binLen) > 0)
                {
                    int ind = (int)(newId % binLen);
                    buf[--charPos] = r[ind];
                    newId         /= binLen;
                }
                buf[--charPos] = r[(int)(newId % binLen)];
                code           = new string(buf, charPos, (32 - charPos));
                if (rule.length.HasValue)
                {
                    code = code.PadLeft(rule.length.Value, '0');
                }
            }
            break;

            case "Parameter":
            {
                if (para.ContainsKey(rule.Rule))
                {
                    code = para[rule.Rule];
                }
                else
                {
                    code = "";
                }
                if (rule.length.HasValue)
                {
                    code = code.PadLeft(rule.length.Value, '0');
                }
            }
            break;

            case "ParaSerial":
            {
                if (para.ContainsKey(rule.Rule))
                {
                    var serial = listSerial.SingleOrDefault(w => w.ParaName == rule.Rule && w.ParaValue == para[rule.Rule]);
                    if (serial == null)
                    {
                        serial = new PB_BarCodeSerial()
                        {
                            ParaName  = rule.Rule,
                            ParaValue = para[rule.Rule],
                            SerialNum = 0
                        };
                        listSerial.Add(serial);
                    }
                    else
                    {
                        serial.TypeId = null;
                    }
                    var seq = serial.SerialNum + 1;
                    code = seq.ToString();
                    if (rule.length.HasValue)
                    {
                        code = code.PadLeft(rule.length.Value, '0');
                    }
                    serial.SerialNum = seq;
                }
            }
            break;

            default: break;
            }
            return(code);
        }