using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using EMIS.DataLogic.SystemDAL;
namespace EMIS.CommonLogic.SystemServices
{
public class SerialNumberServices : BaseServices, ISerialNumberServices
{
public SerialNumberDAL SerialNumberDAL { get; set; }
///
/// 获取下一个序列号带key(可指定序列号位数)
///
///
///
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 = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
if (entity != null)
{
result = entity.Number + 1;
}
return key + text + result.ToString().PadLeft(postfixLength, '0');
}
///
/// 获取下一个序列号带key同时更新(可指定序列号位数)
///
///
///
public string SetSN(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 = 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();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (!isInTransaction)
{
scope.Dispose();
}
}
return key + result.ToString().PadLeft(postfixLength, '0');
}
///
/// 获取下一位序列号不带key
///
///
///
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;
}
///
/// 设置下一个序列号,同时更新(带skip)
///
///
///
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 = 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();
if (!isInTransaction)
{
scope.Complete();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (!isInTransaction)
{
scope.Dispose();
}
}
}
///
/// 获取下一个序列号带key同时更新(可指定序列号位数)
///
///
///
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 = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
if (entity != null)
{
if (entity.ResetTime.Date != DateTime.Today)
{
result = 1;
}
else
{
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();
if (!isInTransaction)
{
scope.Complete();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (!isInTransaction)
{
scope.Dispose();
}
}
return key + result.ToString().PadLeft(postfixLength, '0');
}
}
}