123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using EMIS.DataLogic.SystemDAL;
- namespace EMIS.CommonLogic.SystemServices
- {
- public class SerialNumberServices : BaseServices, ISerialNumberServices
- {
- public SerialNumberDAL SerialNumberDAL { get; set; }
- /// <summary>
- /// 获取下一个序列号带key(可指定序列号位数)
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string GetSN(string key, int postfixLength = 2)
- {
- if (key == null)
- {
- throw new ArgumentNullException("key");
- }
- postfixLength = postfixLength < 0 ? 0 : postfixLength;
- var result = 1;
- var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
- if (entity != null)
- {
- result = entity.Number + 1;
- }
- return key + result.ToString().PadLeft(postfixLength, '0');
- }
- /// <summary>
- /// 获取下一个序列号带key同时更新(可指定序列号位数)
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string SetSN(string key, int postfixLength = 2)
- {
- if (key == null)
- {
- throw new ArgumentNullException("key");
- }
- postfixLength = postfixLength < 0 ? 0 : postfixLength;
- var result = 1;
- var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
- if (entity != null)
- {
- result = entity.Number + 1;
- }
- else
- {
- entity = new Entities.Sys_SerialNumber();
- entity.ID = key;
- UnitOfWork.Add(entity);
- }
- entity.Number = result;
- entity.ResetTime = DateTime.Now;
- UnitOfWork.Commit();
- return key + result.ToString().PadLeft(postfixLength, '0');
- }
- /// <summary>
- /// 获取下一位序列号不带key
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public int GetSNValue(string key)
- {
- if (key == null)
- {
- throw new ArgumentNullException("key");
- }
- var result = 1;
- var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
- if (entity != null)
- {
- result = entity.Number + 1;
- }
- return result;
- }
- /// <summary>
- /// 设置下一个序列号,同时更新(带skip)
- /// </summary>
- /// <param name="key"></param>
- /// <param name="skipCount"></param>
- public void SkipSN(string key, int skipCount)
- {
- if (key == null)
- {
- throw new ArgumentNullException("key");
- }
- var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
- var result = 1;
- if (entity != null)
- {
- result = entity.Number;
- }
- else
- {
- entity = new Entities.Sys_SerialNumber();
- entity.ID = key;
- UnitOfWork.Add(entity);
- }
- entity.Number = result + skipCount;
- entity.ResetTime = DateTime.Now;
- UnitOfWork.Commit();
- }
- }
- }
|