public SignatureDto GetMailboxSignature(int mailboxId, string user, int tenant)
        {
            using (var db = GetDb())
            {
                CheckMailboxOwnage(mailboxId, user, tenant, db);

                var signatureDal = new SignatureDal(db);
                return signatureDal.GetSignature(mailboxId, tenant);
            }
        }
        public long RemoveMailBox(MailBox mailBox, DbManager db)
        {
            if (mailBox.MailBoxId <= 0)
                throw new Exception("MailBox id is 0");

            db.ExecuteNonQuery(
                new SqlUpdate(MailboxTable.name)
                    .Set(MailboxTable.Columns.is_removed, true)
                    .Where(MailboxTable.Columns.id, mailBox.MailBoxId));

            db.ExecuteNonQuery(
                new SqlDelete(ChainTable.name)
                    .Where(GetUserWhere(mailBox.UserId, mailBox.TenantId))
                    .Where(ChainTable.Columns.id_mailbox, mailBox.MailBoxId));


            db.ExecuteNonQuery(
                new SqlUpdate(MailTable.name)
                    .Set(MailTable.Columns.is_removed, true)
                    .Where(MailTable.Columns.id_mailbox, mailBox.MailBoxId)
                    .Where(GetUserWhere(mailBox.UserId, mailBox.TenantId)));

            var totalAttachmentsSize = db.ExecuteScalar<long>(
                string.Format(
                    "select sum(a.size) from {0} a inner join {1} m on a.{2} = m.{3} where m.{4} = @mailbox_id and m.{5} = @tid and a.{5} = @tid and a.{6} != @need_remove",
                    AttachmentTable.name,
                    MailTable.name,
                    AttachmentTable.Columns.id_mail,
                    MailTable.Columns.id,
                    MailTable.Columns.id_mailbox,
                    MailTable.Columns.id_tenant,
                    AttachmentTable.Columns.need_remove), new { tid = mailBox.TenantId, need_remove = true, mailbox_id = mailBox.MailBoxId });

            var query = string.Format("update {0} a inner join {1} m on a.{2} = m.{3} set a.{4} = @need_remove where m.{5} = @mailbox_id",
                    AttachmentTable.name, MailTable.name, AttachmentTable.Columns.id_mail, MailTable.Columns.id, AttachmentTable.Columns.need_remove, MailTable.Columns.id_mailbox);

            db.ExecuteNonQuery(query, new { need_remove = true, mailbox_id = mailBox.MailBoxId });


            query = string.Format("select t.{0} from {1} t inner join {2} m on t.{3} = m.{4} where m.{5} = @mailbox_id",
                TagMailTable.Columns.id_tag, TagMailTable.name, MailTable.name, TagMailTable.Columns.id_mail, MailTable.Columns.id, MailTable.Columns.id_mailbox);

            var affectedTags = db.ExecuteList(query, new { mailbox_id = mailBox.MailBoxId })
                .ConvertAll(r => Convert.ToInt32(r[0]))
                .Distinct();

            query = string.Format("delete t from {0} t inner join {1} m on t.{2} = m.{3} where m.{4} = @mailbox_id",
                                  TagMailTable.name, MailTable.name, TagMailTable.Columns.id_mail, MailTable.Columns.id, MailTable.Columns.id_mailbox);

            db.ExecuteNonQuery(query, new { mailbox_id = mailBox.MailBoxId });

            UpdateTagsCount(db, mailBox.TenantId, mailBox.UserId, affectedTags);

            RecalculateFolders(db, mailBox.TenantId, mailBox.UserId);

            var signatureManager = new SignatureDal(db);

            signatureManager.DeleteSignature(mailBox.MailBoxId, mailBox.TenantId);

            return totalAttachmentsSize;
        }
        public SignatureDto UpdateOrCreateMailboxSignature(int mailboxId, string user, int tenant, string html, bool isActive)
        {
            using (var db = GetDb())
            {
                CheckMailboxOwnage(mailboxId, user, tenant, db);

                var signature = new SignatureDto(mailboxId, tenant, html, isActive);
                var signatureDal = new SignatureDal(db);
                signatureDal.UpdateOrCreateSignature(signature);
                return signature;
            }
        }
 private static List<SignatureDto> GetMailboxesSignatures(List<int> mailboxIds, int tenant, DbManager db)
 {
     var signatureDal = new SignatureDal(db);
     return signatureDal.GetSignatures(mailboxIds, tenant);
 }
 public List<SignatureDto> GetMailboxesSignatures(List<int> mailbox_ids, string user_id, int tenant)
 {
     using (var db = GetDb())
     {
         CheckMailboxesOwnage(mailbox_ids, user_id, tenant, db);
         
         var signature_dal = new SignatureDal(db);
         return signature_dal.GetSignatures(mailbox_ids, tenant);
     }
 }