'use strict'; const assert = require('assert'); const Service = require('egg').Service; class LntactService extends Service { constructor(ctx) { super(ctx); this.model = this.ctx.model.Intact; } async create({ thumbnail, name, describe, status, url, date, column, type }) { assert(name, '名称不存在'); assert(date, '发布时间不存在'); assert(status, '状态不存在'); try { const res = await this.model.create({ thumbnail, name, describe, status, url, date, column, type }); return { errcode: 0, errmsg: 'ok', data: res }; } catch (error) { throw error; } } async update({ id, thumbnail, name, describe, status, url, date, column, type }) { assert(id, 'id不存在'); try { await this.model.updateOne({ _id: id }, { thumbnail, name, describe, status, url, date, column, type }); 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, name, status, url, date, column, type }) { const filter = {}; const arr = { name, status, url, date, column, type }; 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 }, { url: false }).skip(Number(skip) * Number(limit)).limit(Number(limit)); } else { res = await this.model.find({ ...filter }, { url: false }); } return { errcode: 0, errmsg: 'ok', data: res, total: total.length }; } catch (error) { throw error; } } async fetch({ id }) { assert(id, 'id不存在'); try { const res = await this.model.findOne({ _id: id }); return { errcode: 0, errmsg: 'ok', data: res }; } catch (error) { throw error; } } } module.exports = LntactService;