integral.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. const assert = require('assert');
  3. const Service = require('egg').Service;
  4. class integralService extends Service {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.model = this.ctx.model.Integral;
  8. }
  9. async create({ openid, name, integral }) {
  10. assert(name, '姓名不存在');
  11. assert(openid, 'openid不存在');
  12. assert(integral, '积分不存在');
  13. await this.service.integralLog.create({ openid, name, integral });
  14. try {
  15. const res = await this.model.create({ openid, name, integral });
  16. return { errcode: 0, errmsg: 'ok', data: res };
  17. } catch (error) {
  18. throw error;
  19. }
  20. }
  21. async update({ id, openid, name, integral }) {
  22. assert(id, 'id不存在');
  23. assert(name, '姓名不存在');
  24. assert(openid, 'openid不存在');
  25. assert(integral, '积分不存在');
  26. await this.service.integralLog.create({ openid, name, integral });
  27. try {
  28. await this.model.updateOne({ _id: id }, { openid, name, integral });
  29. return { errcode: 0, errmsg: 'ok', data: '' };
  30. } catch (error) {
  31. throw error;
  32. }
  33. }
  34. async delete({ id }) {
  35. assert(id, 'id不存在');
  36. try {
  37. await this.model.deleteOne({ _id: id });
  38. return { errcode: 0, errmsg: 'ok', data: '' };
  39. } catch (error) {
  40. throw error;
  41. }
  42. }
  43. async query({ skip, limit, openid, name, integral }) {
  44. const filter = {};
  45. const arr = { openid, name, integral };
  46. for (const e in arr) {
  47. const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
  48. if (arr[e]) {
  49. filter.$or = [];
  50. filter.$or.push(JSON.parse(data));
  51. }
  52. }
  53. try {
  54. const total = await this.model.find({ ...filter });
  55. let res;
  56. if (skip && limit) {
  57. res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  58. } else {
  59. res = await this.model.find({ ...filter });
  60. }
  61. return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
  62. } catch (error) {
  63. throw error;
  64. }
  65. }
  66. async increase({ openid, name, integral }) {
  67. assert(openid, 'openid不存在');
  68. assert(name, '姓名不存在');
  69. assert(integral, '积分不存在');
  70. try {
  71. await this.service.integralLog.create({ openid, name, integral });
  72. const one = await this.model.findOne({ openid });
  73. if (one) {
  74. const total = Number(integral) + Number(one.integral);
  75. await this.model.updateOne({ openid }, { integral: total });
  76. return { errcode: 0, errmsg: 'ok', data: '' };
  77. }
  78. await this.model.create({ openid, name, integral });
  79. return { errcode: 0, errmsg: 'ok', data: '' };
  80. } catch (error) {
  81. throw error;
  82. }
  83. }
  84. async reduce({ openid, name, integral }) {
  85. assert(openid, 'openid不存在');
  86. assert(name, '姓名不存在');
  87. assert(integral, '积分不存在');
  88. try {
  89. const one = await this.model.findOne({ openid });
  90. if (one) {
  91. const total = Number(one.integral) - Number(integral);
  92. await this.model.updateOne({ openid }, { integral: total });
  93. await this.service.integralLog.create({ openid, name, integral });
  94. return { errcode: 0, errmsg: 'ok', data: '' };
  95. }
  96. return { errcode: -1001, errmsg: '未查询到用户', data: '' };
  97. } catch (error) {
  98. throw error;
  99. }
  100. }
  101. }
  102. module.exports = integralService;