123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- using YLShipBuildLandMap.Entity;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Transactions;
- namespace YLShipBuildLandMap.Services.SystemSetting
- {
- public class SNServices : ISNServices
- {
- private YLShipBuildLandMapContext DbContext { get; set; }
- public SNServices(YLShipBuildLandMapContext dbContext)
- {
- DbContext = dbContext;
- }
- /// <summary>
- /// 获取下一个序列号带key(可指定序列号位数)
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string GetSN(string key, int postfixLength = 2, string text = "")
- {
- if (key == null)
- {
- throw new ArgumentNullException("key");
- }
- postfixLength = postfixLength < 0 ? 0 : postfixLength;
- var result = 1;
- var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
- if (entity != null)
- {
- result = entity.Number + 1;
- }
- return key + text + result.ToString().PadLeft(postfixLength, '0');
- }
- /// <summary>
- /// 获取下一个序列号带key同时更新(可指定序列号位数)
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string SetSN(string key, int postfixLength = 2, string text = "")
- {
- if (key == null)
- {
- throw new ArgumentNullException("key");
- }
- postfixLength = postfixLength < 0 ? 0 : postfixLength;
- var result = 1;
- /*var isInTransaction = Transaction.Current != null;
- TransactionScope scope = null;
- if (!isInTransaction)
- {
- scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
- }*/
- try
- {
- var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
- if (entity != null)
- {
- result = entity.Number + 1;
- }
- else
- {
- entity = new SysSerialNumber();
- entity.SerialNumberKey = key;
- this.DbContext.SysSerialNumber.Add(entity);
- }
- entity.Number = result;
- entity.ResetTime = DateTime.Now;
- this.DbContext.SaveChanges();
- /*if (!isInTransaction)
- {
- scope.Complete();
- }*/
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- /*if (!isInTransaction)
- {
- scope.Dispose();
- }*/
- }
- return key + text + result.ToString().PadLeft(postfixLength, '0');
- }
- /// <summary>
- /// 获取下一位序列号不带key(可指定序列号位数)
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string GetSNValue(string key, int postfixLength = 2)
- {
- if (key == null)
- {
- throw new ArgumentNullException("key");
- }
- var result = 1;
- postfixLength = postfixLength < 0 ? 0 : postfixLength;
- var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
- if (entity != null)
- {
- result = entity.Number + 1;
- }
- else
- {
- entity = new SysSerialNumber();
- entity.SerialNumberKey = key;
- entity.Number = 0;
- entity.ResetTime = DateTime.Now;
- DbContext.Add(entity);
- DbContext.SaveChanges();
- }
- return result.ToString().PadLeft(postfixLength, '0');
- }
- /// <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 isInTransaction = Transaction.Current != null;
- TransactionScope scope = null;
- if (!isInTransaction)
- {
- scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
- }
- try
- {
- var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
- var result = 1;
- if (entity != null)
- {
- result = entity.Number;
- }
- else
- {
- entity = new SysSerialNumber();
- entity.SerialNumberKey = key;
- DbContext.Add(entity);
- }
- entity.Number = result + skipCount;
- entity.ResetTime = DateTime.Now;
- DbContext.SaveChanges();
- if (!isInTransaction)
- {
- scope.Complete();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (!isInTransaction)
- {
- scope.Dispose();
- }
- }
- }
- /// <summary>
- /// 获取下一个序列号带key同时更新(可指定序列号位数)
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string SetDialySN(string key, int postfixLength = 2)
- {
- if (key == null)
- {
- throw new ArgumentNullException("key");
- }
- postfixLength = postfixLength < 0 ? 0 : postfixLength;
- var result = 1;
- var isInTransaction = Transaction.Current != null;
- TransactionScope scope = null;
- if (!isInTransaction)
- {
- scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
- }
- try
- {
- var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
- if (entity != null)
- {
- if (entity.ResetTime.Date != DateTime.Today)
- {
- result = 1;
- }
- else
- {
- result = entity.Number + 1;
- }
- }
- else
- {
- entity = new SysSerialNumber();
- entity.SerialNumberKey = key;
- DbContext.Add(entity);
- }
- entity.Number = result;
- entity.ResetTime = DateTime.Now;
- DbContext.SaveChanges();
- if (!isInTransaction)
- {
- scope.Complete();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (!isInTransaction)
- {
- scope.Dispose();
- }
- }
- return key + DateTime.Now.ToString("yyyyMMdd") + result.ToString().PadLeft(postfixLength, '0');
- }
- /// <summary>
- /// 获取下一个序列号带key同时更新,不带key(可指定序列号位数)
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string SetSNValue(string key, int postfixLength = 2, string text = "")
- {
- if (key == null)
- {
- throw new ArgumentNullException("key");
- }
- postfixLength = postfixLength < 0 ? 0 : postfixLength;
- var result = 1;
- var isInTransaction = Transaction.Current != null;
- TransactionScope scope = null;
- if (!isInTransaction)
- {
- scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
- }
- try
- {
- var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
- if (entity != null)
- {
- result = entity.Number + 1;
- }
- else
- {
- entity = new SysSerialNumber();
- entity.SerialNumberKey = key;
- this.DbContext.SysSerialNumber.Add(entity);
- }
- entity.Number = result;
- entity.ResetTime = DateTime.Now;
- this.DbContext.SaveChanges();
- if (!isInTransaction)
- {
- scope.Complete();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (!isInTransaction)
- {
- scope.Dispose();
- }
- }
- return text + result.ToString().PadLeft(postfixLength, '0');
- }
- }
- }
|