'use strict'; const assert = require('assert'); const Service = require('egg').Service; const moment = require('moment'); class integralLogService extends Service { constructor(ctx) { super(ctx); this.model = this.ctx.model.IntegralLog; } async create({ openid, name, integral }) { assert(name, '姓名不存在'); assert(openid, 'openid不存在'); assert(integral, '积分不存在'); try { const createTime = moment().valueOf(); const res = await this.model.create({ openid, name, integral, createTime }); return { errcode: 0, errmsg: 'ok', data: res }; } catch (error) { throw error; } } async update({ id, openid, name, integral }) { assert(id, 'id不存在'); try { await this.model.updateOne({ _id: id }, { openid, name, integral }); 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, openid, name, integral }) { const filter = {}; const arr = { openid, name, integral }; for (const e in arr) { let datas; if (e === 'list') { datas = `{ "${e}": { "$elemMatch": { "$eq": "${arr[e]}" } } }`; } else { datas = `{ "${e}": { "$regex": "${arr[e]}" } }`; } if (arr[e]) { filter.$or = []; filter.$or.push(JSON.parse(datas)); } } 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 = integralLogService;