integralLog.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const assert = require('assert');
  3. const Service = require('egg').Service;
  4. const moment = require('moment');
  5. class integralLogService extends Service {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.model = this.ctx.model.IntegralLog;
  9. }
  10. async create({ openid, name, integral }) {
  11. assert(name, '姓名不存在');
  12. assert(openid, 'openid不存在');
  13. assert(integral, '积分不存在');
  14. try {
  15. const createTime = moment().valueOf();
  16. const res = await this.model.create({ openid, name, integral, createTime });
  17. return { errcode: 0, errmsg: 'ok', data: res };
  18. } catch (error) {
  19. throw error;
  20. }
  21. }
  22. async update({ id, openid, name, integral }) {
  23. assert(id, 'id不存在');
  24. try {
  25. await this.model.updateOne({ _id: id }, { openid, name, integral });
  26. return { errcode: 0, errmsg: 'ok', data: '' };
  27. } catch (error) {
  28. throw error;
  29. }
  30. }
  31. async delete({ id }) {
  32. assert(id, 'id不存在');
  33. try {
  34. await this.model.deleteOne({ _id: id });
  35. return { errcode: 0, errmsg: 'ok', data: '' };
  36. } catch (error) {
  37. throw error;
  38. }
  39. }
  40. async query({ skip, limit, openid, name, integral }) {
  41. const filter = {};
  42. const arr = { openid, name, integral };
  43. for (const e in arr) {
  44. let datas;
  45. if (e === 'list') {
  46. datas = `{ "${e}": { "$elemMatch": { "$eq": "${arr[e]}" } } }`;
  47. } else {
  48. datas = `{ "${e}": { "$regex": "${arr[e]}" } }`;
  49. }
  50. if (arr[e]) {
  51. filter.$or = [];
  52. filter.$or.push(JSON.parse(datas));
  53. }
  54. }
  55. try {
  56. const total = await this.model.find({ ...filter });
  57. let res;
  58. if (skip && limit) {
  59. res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  60. } else {
  61. res = await this.model.find({ ...filter });
  62. }
  63. return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
  64. } catch (error) {
  65. throw error;
  66. }
  67. }
  68. }
  69. module.exports = integralLogService;