修改定时任务分页数据

This commit is contained in:
whyour
2022-08-20 20:05:35 +08:00
parent f3de8435f1
commit 102e447f78
3 changed files with 70 additions and 32 deletions
+22 -11
View File
@@ -9,17 +9,28 @@ const route = Router();
export default (app: Router) => {
app.use('/crons', route);
route.get('/', async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cronService = Container.get(CronService);
const data = await cronService.crontabs(req.query.searchValue as string);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
});
route.get(
'/',
celebrate({
query: Joi.object({
searchText: Joi.string().required().allow(''),
page: Joi.string().required(),
size: Joi.string().required(),
t: 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.crontabs(req.query as any);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.post(
'/',
+21 -7
View File
@@ -113,7 +113,15 @@ export default class CronService {
}
}
public async crontabs(searchText?: string): Promise<Crontab[]> {
public async crontabs(params?: {
searchText: string;
page: string;
size: string;
}): Promise<{ data: Crontab[]; total: number }> {
const searchText = params?.searchText;
const page = Number(params?.page || '0');
const size = Number(params?.size || '0');
let query = {};
if (searchText) {
const textArray = searchText.split(':');
@@ -158,12 +166,18 @@ export default class CronService {
break;
}
}
let condition: any = {
where: query,
order: [['createdAt', 'DESC']],
};
if (page && size) {
condition.offset = (page - 1) * size;
condition.limit = size;
}
try {
const result = await CrontabModel.findAll({
where: query,
order: [['createdAt', 'DESC']],
});
return result as any;
const result = await CrontabModel.findAll(condition);
const count = await CrontabModel.count();
return { data: result, total: count };
} catch (error) {
throw error;
}
@@ -441,7 +455,7 @@ export default class CronService {
private async set_crontab(needReloadSchedule: boolean = false) {
const tabs = await this.crontabs();
var crontab_string = '';
tabs.forEach((tab) => {
tabs.data.forEach((tab) => {
const _schedule = tab.schedule && tab.schedule.split(/ +/);
if (tab.isDisabled === 1 || _schedule!.length !== 5) {
crontab_string += '# ';