/// <summary> /// 执行队列 /// </summary> public void ExecQueue() { Database database = CreateDAO(); try { database.OpenSharedConnection(); //将CountQueue中的数据更新至计数表 foreach (string key in CountQueue.Keys) { Queue <CountQueueItem> queue = null; bool removed = CountQueue.TryRemove(key, out queue); if (!removed || queue == null) { continue; } string countTableName = GetTableName_Counts(key); while (queue.Count > 0) { var item = queue.Dequeue(); //需要判断数据库中是否存在记录 var sql = PetaPoco.Sql.Builder; sql.Select("CountId") .From(countTableName) .Where("ObjectId=@0 and CountType=@1", item.ObjectId, item.CountType); CountEntity countEntity = database.FirstOrDefault <CountEntity>(sql); if (countEntity == null) { database.Insert(countTableName, "CountId", item.AsCountEntity()); } else { sql = PetaPoco.Sql.Builder; sql.Append("Update " + countTableName) .Append("Set StatisticsCount = StatisticsCount + @0 ", item.StatisticsCount) .Where("ObjectId=@0", item.ObjectId) .Where("CountType=@0", item.CountType); database.Execute(sql); } } } //将CountPerDayQueue中的数据更新每日计数表 foreach (string key in CountPerDayQueue.Keys) { Queue <CountQueueItem> queue = null; bool removed = CountPerDayQueue.TryRemove(key, out queue); if (!removed || queue == null) { continue; } string countPerDayTableName = GetTableName_CountsPerDay(key); while (queue.Count > 0) { var item = queue.Dequeue(); var sql = PetaPoco.Sql.Builder; sql.Select("Id") .From(countPerDayTableName) .Where("ObjectId=@0 and CountType=@1 and ReferenceYear=@2 and ReferenceMonth=@3 and ReferenceDay=@4", item.ObjectId, item.CountType, DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day); CountPerDayEntity countPerDayEntity = database.FirstOrDefault <CountPerDayEntity>(sql); if (countPerDayEntity == null) { database.Insert(countPerDayTableName, "Id", item.AsCountPerDayEntity()); } else { sql = PetaPoco.Sql.Builder; sql.Append("Update " + countPerDayTableName) .Append("Set StatisticsCount = StatisticsCount + @0 ", item.StatisticsCount) .Where("ObjectId=@0", item.ObjectId) .Where("CountType=@0", item.CountType) .Where("ReferenceYear=@0 and ReferenceMonth=@1 and ReferenceDay=@2", DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day); database.Execute(sql); } } } } finally { database.CloseSharedConnection(); } }
/// <summary> /// 新建实体时使用 /// </summary> public static CountEntity New() { CountEntity countEntity = new CountEntity(); return(countEntity); }