From 34b04bccc1ffa157d449bc7c29cfed0aea04013b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E9=81=93=E8=BF=9C?= Date: Fri, 25 Jun 2021 15:01:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=BB=E9=87=8D=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/cron.ts | 18 ++++++++++++++++++ back/services/cron.ts | 20 ++++++++++++++++++++ src/pages/crontab/index.tsx | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/back/api/cron.ts b/back/api/cron.ts index 5206c6b6..c3aa6481 100644 --- a/back/api/cron.ts +++ b/back/api/cron.ts @@ -191,6 +191,24 @@ export default (app: Router) => { }, ); + route.delete( + '/crons/remove-duplicate', + celebrate({ + body: Joi.array().items(Joi.string().required()), + }), + async (req: Request, res: Response, next: NextFunction) => { + const logger: Logger = Container.get('logger'); + try { + const cronService = Container.get(CronService); + const data = await cronService.removeDuplicate(req.body); + return res.send({ code: 200, data }); + } catch (e) { + logger.error('🔥 error: %o', e); + return next(e); + } + }, + ); + route.get( '/crons/import', async (req: Request, res: Response, next: NextFunction) => { diff --git a/back/services/cron.ts b/back/services/cron.ts index 4dcd522a..ee761544 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -107,6 +107,26 @@ export default class CronService { }); } + public async removeDuplicate(ids: string[]) { + return new Promise((resolve: any) => { + { + this.cronDb.find({ _id: { $in: ids } }).exec((err, docs: Crontab[]) => { + const nameSet = new Set(); + const removeIdsSet = new Set(); + docs.forEach((item) => { + if (nameSet.has(item.name)) { + removeIdsSet.add(item._id); + } else { + nameSet.add(item.name); + } + }); + const removeIds: string[] = Array.from(removeIdsSet); + this.remove(removeIds).then(resolve); + }); + } + }); + } + public async crontabs(searchText?: string): Promise { let query = {}; if (searchText) { diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index 90203d18..f0f103c7 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -492,6 +492,31 @@ const Crontab = () => { }); }; + const removeDuplicate = () => { + Modal.confirm({ + title: '确认去重', + content: <>确认删除选中的重复定时任务吗, + onOk() { + request + .delete(`${config.apiPrefix}crons/remove-duplicate`, { + data: selectedRowIds, + }) + .then((data: any) => { + if (data.code === 200) { + message.success('批量删除成功'); + setSelectedRowIds([]); + getCrons(); + } else { + message.error(data); + } + }); + }, + onCancel() { + console.log('Cancel'); + }, + }); + }; + const operateCrons = (operationStatus: number) => { Modal.confirm({ title: `确认${OperationName[operationStatus]}`, @@ -579,6 +604,13 @@ const Crontab = () => { +