1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 'use strict';
- const Service = require('egg').Service;
- class BaseService extends Service {
- async one(id, pop) {
- return pop ? await this.tag().findById(id).populate(pop)
- : await this.tag().findById(id);
- }
- async oneData(data, pop) {
- const sort = data.sort;
- delete data.sort;
- if (pop) {
- return sort ? await this.tag().findOne(data).populate(pop)
- .sort(sort)
- : await this.tag().findOne(data).populate(pop);
- }
- return sort ? await this.tag().findOne(data).sort(sort)
- : await this.tag().findOne(data);
- }
- async list(data, pop) {
- const sort = data.sort;
- delete data.sort;
- if (pop) {
- return sort ? await this.tag().find(data).populate(pop)
- .sort(sort)
- : await this.tag().find(data).populate(pop);
- }
- return sort ? await this.tag().find(data).sort(sort)
- : await this.tag().find(data);
- }
- async listForPage(data, pop) {
- const page = data.page || 1;
- const rows = Number.parseInt(data.rows)
- || this.app.config.defaultPageSize;
- const sort = data.sort;
- delete data.page;
- delete data.rows;
- delete data.sort;
- if (pop) {
- const total = await this.tag().find(data).countDocuments();
- const result = (sort ? await this.tag().find(data).populate(pop)
- .sort(sort)
- .skip((page - 1) * rows)
- .limit(rows)
- : await this.tag().find(data).populate(pop)
- .skip((page - 1) * rows)
- .limit(rows));
- return {
- count: total,
- list: result,
- };
- }
- const total = await this.tag().find(data).countDocuments();
- const result = (sort ? await this.tag().find(data).sort(sort)
- .skip((page - 1) * rows)
- .limit(rows)
- : await this.tag().find(data).skip((page - 1) * rows)
- .limit(rows));
- return {
- count: total,
- list: result,
- };
- }
- async add(data) {
- return await this.tag().create(data);
- }
- async update(id, data) {
- return await this.tag().updateOne({ _id: id }, data);
- }
- async updateAll(cond, data) {
- return await this.tag().update(cond, data);
- }
- async delete(id) {
- return await this.tag().deleteOne({ _id: id });
- }
- async deleteAll(cond) {
- return await this.tag().delete(cond);
- }
- }
- module.exports = BaseService;
|