增加去重功能

This commit is contained in:
唐道远 2021-06-25 15:01:06 +08:00
parent 0792f7c5f6
commit 34b04bccc1
3 changed files with 70 additions and 0 deletions

View File

@ -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) => {

View File

@ -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<string>();
const removeIdsSet = new Set<string>();
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<Crontab[]> {
let query = {};
if (searchText) {

View File

@ -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 = () => {
<Button type="primary" style={{ marginBottom: 5 }} onClick={delCrons}>
</Button>
<Button
type="primary"
style={{ marginLeft: 8, marginBottom: 5 }}
onClick={removeDuplicate}
>
</Button>
<Button
type="primary"
onClick={() => operateCrons(0)}