public static Int64 AddNewMessage(Int64 bidId, Int64 supplierId, long originalSupplierId = 0, string state = START_STAGE)
 {
     using (ConnectorBase conn = ConnectorBase.NewInstance())
     {
         //BidMessage message = get_message_by_bid_and_supplier(bidId, supplierId);
         // if (message != null) return 0;
         BidMessage message = new BidMessage();
         message.BidId              = bidId;
         message.SupplierId         = supplierId;
         message.Stage              = state;
         message.IsActive           = true;
         message.ExpirationTime     = DateTime.Now;
         message.SendingTime        = DateTime.Now;
         message.OriginalSupplierId = originalSupplierId == 0? supplierId : originalSupplierId;
         message.Save(conn);
         return(message.MessageId);
     }
 }
        internal static void SendMessages(List <BidMessage> messages, DateTime now)
        {
            foreach (var msg in messages)
            {
                switch (msg.Stage)
                {
                case START_STAGE:
                    WaitingEmails.Enqueue(msg);
                    SupplierNotification.SendNotificationNewBidToSupplier(msg.BidId, new List <Int64> {
                        msg.SupplierId
                    });
                    msg.ExpirationTime = now.AddMinutes(Settings.GetSettingInt64(Settings.Keys.MESSAGE_EXPIRATION_SUPPLIER, 60));
                    msg.Stage          = SUPPLIER_STAGE;
                    msg.Save();
                    break;

                case PREMIUM_STAGE:
                case SUPPLIER_STAGE:
                    long supplierId = get_primium_supplier(msg);
                    if (supplierId == 0)
                    {
                        goto case SPECIAL_DEAL_STAGE;
                    }
                    SupplierNotification.SendNotificationNewBidToPremiumSupplier(msg.BidId, supplierId);
                    var newPremMessage = new BidMessage(msg);
                    newPremMessage.Stage          = PREMIUM_STAGE;
                    newPremMessage.SupplierId     = supplierId;
                    newPremMessage.ExpirationTime = now.AddMinutes(Settings.GetSettingInt64(Settings.Keys.MESSAGE_EXPIRATION_PREMIUM, 20));
                    newPremMessage.Save();
                    WaitingEmails.Enqueue(newPremMessage);
                    goto default;

                case SPECIAL_DEAL_STAGE:
                    var supplierList = get_relevant_suppliers(msg);
                    if (supplierList.Count <= 0)
                    {
                        var bid = Bid.FetchByID(msg.BidId);
                        if (!bid.IsActive)
                        {
                            goto default;
                        }
                        goto case ADMIN_STAGE;
                    }
                    foreach (int sId in supplierList)
                    {
                        var newSpecMessage = new BidMessage(msg);
                        newSpecMessage.SupplierId     = sId;
                        newSpecMessage.ExpirationTime = now.AddMinutes(Settings.GetSettingInt64(Settings.Keys.MESSAGE_EXPIRATION_SPECIAL_DEAL, 10));
                        newSpecMessage.Stage          = SPECIAL_DEAL_STAGE;
                        newSpecMessage.IsActive       = false;
                        newSpecMessage.Save();
                        SupplierNotification.SendNotificationNewBidToPremiumSupplier(msg.BidId, sId);
                        WaitingEmails.Enqueue(newSpecMessage);
                    }
                    var forAdminMessage = new BidMessage(msg);
                    forAdminMessage.ExpirationTime = now.AddMinutes(Settings.GetSettingInt64(Settings.Keys.MESSAGE_EXPIRATION_SPECIAL_DEAL, 10));
                    forAdminMessage.Stage          = SPECIAL_DEAL_STAGE;
                    forAdminMessage.IsActive       = true;
                    forAdminMessage.SupplierId     = 1;
                    forAdminMessage.Save();
                    goto default;

                case ADMIN_STAGE:
                    var expiredDid = Bid.FetchByID(msg.BidId);
                    expiredDid.IsActive = false;
                    expiredDid.Save();
                    msg.Stage = ADMIN_STAGE;
                    send_message_to_admin(msg);
                    goto default;

                default:
                    msg.IsActive = false;
                    msg.Save();
                    break;
                }
            }
        }