新增批量删除邮件

This commit is contained in:
eoao
2025-08-28 22:11:00 +08:00
parent 344389cf0f
commit 375613c219
11 changed files with 417 additions and 122 deletions

View File

@@ -160,6 +160,33 @@ const attService = {
eq(att.type, attConst.type.ATT)
))
.all();
},
async deleteByEmailIds(c, emailIds) {
const queryAttSql = emailIds.map(emailId => c.env.db.prepare(`SELECT key,att_id FROM attachments WHERE email_id = ${emailId} GROUP BY key HAVING COUNT(*) = 1;`))
const attListResult = await c.env.db.batch(queryAttSql);
const delKeyList = attListResult.flatMap(r => r.results.map(row => row.key));
if (delKeyList.length > 0) {
await this.batchDelete(c, delKeyList)
}
const delAttSql = emailIds.map(emailId => c.env.db.prepare(`DELETE FROM attachments WHERE email_id = ${emailId}`))
await c.env.db.batch(delAttSql);
},
async batchDelete(c, keys) {
if (!keys.length) return;
const BATCH_SIZE = 1000;
for (let i = 0; i < keys.length; i += BATCH_SIZE) {
const batch = keys.slice(i, i + BATCH_SIZE);
await r2Service.delete(c, batch);
}
}
};