public static int Add(CySequenceCounterEntity entity, DbConnection conn)
 {
     const string sql = @"INSERT INTO [dbo].[CySequenceCounter]
                         ([SequenceKey]
                         ,[CounterId]
                         ,UpdateTime)
                         VALUES
                         (@SequenceKey
                         ,@CounterId
                         ,@UpdateTime)";
     try
     {
         int result = conn.Execute(sql, entity);
         return result;
     }
     catch (Exception ex)
     {
         throw ;
     }
 }
        public static int Update(CySequenceCounterEntity entity, DbConnection conn)
        {
            const string sql = @"UPDATE  [dbo].[CySequenceCounter]
                                SET     CounterId = @CounterId,UpdateTime=@UpdateTime
                                WHERE   SequenceKey = @SequenceKey";
            try
            {
                int result = conn.Execute(sql, entity);
                return result;
            }
            catch (Exception ex)
            {

                throw ;
            }
        }
        /// <summary>
        /// 根据Key获取一个递增指定数量的序列号 by chenggang
        /// </summary>
        /// <param name="sequenceKey">Key</param>
        /// <param name="count">递增指定数量</param>
        /// <returns>序列号</returns>
        public int GetNextNumCounterId(string sequenceKey, int count)
        {
            int counterId = 1;
            try
            {
                lock (MutexObj1)
                {
                    using (var conn = DbHelper.CreateOpenConnection(DbConnectionStringConfig.CyMainDbConnectionStringName))
                    {
                        var model = GetCounterIdByKey(sequenceKey, conn);
                        if (model == null)
                        {
                            var addModel = new CySequenceCounterEntity()
                            {
                                SequenceKey = sequenceKey,
                                CounterId = counterId + count,
                                UpdateTime = DateTime.Now
                            };
                            int addRow = Add(addModel, conn);
                        }
                        else
                        {
                            counterId = model.CounterId + count;
                            var updateModel = new CySequenceCounterEntity()
                            {
                                SequenceKey = sequenceKey,
                                CounterId = counterId,
                                UpdateTime = DateTime.Now
                            };
                            int pdateRow = Update(updateModel, conn);
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                throw ;
            }
            return counterId;
        }
 /// <summary>
 /// 根据日期获取一个新的自增序列编号(每天从1开始重新计数)  by chenggang
 /// </summary>
 /// <param name="sequenceKey">Key</param>
 /// <returns>日期加自增序列组合的编号</returns>
 public long GetNewNoOfEveryDayReset(string sequenceKey)
 {
     try
     {
         int counterId = 1;
         string newNo = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00");
         lock (MutexObj2)
         {
             using (var conn = DbHelper.CreateOpenConnection(DbConnectionStringConfig.CyMainDbConnectionStringName))
             {
                 var model = GetCounterIdByKey(sequenceKey, conn);
                 if (model == null)
                 {
                     var addModel = new CySequenceCounterEntity()
                     {
                         SequenceKey = sequenceKey,
                         CounterId = counterId,
                         UpdateTime = DateTime.Now
                     };
                     int addRow = Add(addModel, conn);
                     newNo += counterId.ToString("00000");
                 }
                 else
                 {
                     var currentDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                     var dateUpdate = Convert.ToDateTime(model.UpdateTime.ToString("yyyy-MM-dd"));
                     var result = DateTime.Compare(currentDate, dateUpdate);
                     if (result > 0)
                     {
                         var updateModel = new CySequenceCounterEntity()
                         {
                             SequenceKey = sequenceKey,
                             CounterId = counterId,
                             UpdateTime = DateTime.Now
                         };
                         int pdateRow = Update(updateModel, conn);
                         newNo += counterId.ToString("00000");
                     }
                     else
                     {
                         counterId = model.CounterId + 1;
                         var updateModel = new CySequenceCounterEntity()
                         {
                             SequenceKey = sequenceKey,
                             CounterId = counterId,
                             UpdateTime = DateTime.Now
                         };
                         int pdateRow = Update(updateModel, conn);
                         newNo += counterId.ToString("00000");
                     }
                 }
             }
             return long.Parse(newNo);
         }
     }
     catch (Exception ex)
     {
         throw ;
     }
 }