Created
June 24, 2015 17:36
-
-
Save julesx/022a65a59d2bf16d9304 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static long GetNewMessageStatusId(MessageStatus messageStatus) | |
{ | |
long messageStatusId = 1; | |
try | |
{ | |
string tableName = messageStatus == MessageStatus.Delivered ? "message_delivery_lookups" : "message_read_lookups"; | |
using (var dataContext = new MessageConnector()) | |
{ | |
dataContext.Database.ExecuteSqlCommand("LOCK TABLES " + tableName + " WRITE"); | |
var sql = "SELECT highest_id FROM " + tableName + " ORDER BY id DESC LIMIT 0, 1;"; | |
long? statusId = dataContext.Database.SqlQuery<long?>(sql).FirstOrDefault(); | |
string date = DateTime.Now.Year + "-" + DateTime.Now.Month.ToString("D2") + "-" + DateTime.Now.Day.ToString("D2"); | |
string column = messageStatus == MessageStatus.Delivered ? "message_delivery_date" : "message_read_date"; | |
if (statusId != null) | |
{ | |
messageStatusId = (long)statusId + 1; | |
sql = "UPDATE " + tableName + " SET highest_id = " + messageStatusId + " WHERE " + column + " = '" + date + "';"; | |
int rowsAffected = dataContext.Database.ExecuteSqlCommand(sql); | |
if (rowsAffected == 0) | |
{ | |
sql = "INSERT INTO " + tableName + " (" + column + ", highest_id) VALUES ('" + date + "', " + messageStatusId + ");"; | |
dataContext.Database.ExecuteSqlCommand(sql); | |
} | |
} | |
else | |
{ | |
sql = "INSERT INTO " + tableName + " (" + column + ", highest_id) VALUES ('" + date + "', " + messageStatusId + ");"; | |
dataContext.Database.ExecuteSqlCommand(sql); | |
} | |
dataContext.Database.ExecuteSqlCommand("UNLOCK TABLES"); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Logger.LogMessage("MessageModel:GetNewMessageStatusId ex: " + ex.Message); | |
} | |
return messageStatusId; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var transaction = new TransactionScope()) | |
{ | |
//GET THE HIGHEST RECEIPT ID THEN ADD 1 FOR OUR NEW UPDATE | |
receiptId = GetNewMessageStatusId(messageStatus); | |
transaction.Complete(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment