|
@@ -0,0 +1,67 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const Service = require('egg').Service;
|
|
|
+class ReaderService extends Service {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx);
|
|
|
+ this.model = this.ctx.model.Reader;
|
|
|
+ }
|
|
|
+ async create({ source, target }) {
|
|
|
+ assert(source, 'source不存在');
|
|
|
+ assert(target, 'target不存在');
|
|
|
+ try {
|
|
|
+ const total = await this.model.find({ source, target });
|
|
|
+ if (total.length > 0) {
|
|
|
+ return { errcode: -1001, errmsg: '不能重复添加' };
|
|
|
+ }
|
|
|
+ const res = await this.model.create({ source, target });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: res };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async update({ id, source, target }) {
|
|
|
+ assert(id, 'id不存在');
|
|
|
+ try {
|
|
|
+ await this.model.updateOne({ _id: id }, { source, target });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: '' };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async delete({ id }) {
|
|
|
+ assert(id, 'id不存在');
|
|
|
+ try {
|
|
|
+ await this.model.deleteOne({ _id: id });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: '' };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async query({ skip, limit, source, target }) {
|
|
|
+ const filter = {};
|
|
|
+ const arr = { source, target };
|
|
|
+ for (const e in arr) {
|
|
|
+ const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
|
|
|
+ if (arr[e]) {
|
|
|
+ filter.$or = [];
|
|
|
+ filter.$or.push(JSON.parse(data));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ const total = await this.model.find({ ...filter });
|
|
|
+ let res;
|
|
|
+ if (skip && limit) {
|
|
|
+ res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
|
|
|
+ } else {
|
|
|
+ res = await this.model.find({ ...filter });
|
|
|
+ }
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = ReaderService;
|