paper.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. const assert = require('assert');
  3. const Service = require('egg').Service;
  4. class PaperService extends Service {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.model = this.ctx.model.Paper;
  8. this.bankModel = this.ctx.model.Bank;
  9. }
  10. async create({ userName, userId, list }) {
  11. assert(userName, '用户名不存在');
  12. assert(userId, '用户ID不存在');
  13. assert(list, '内容不存在');
  14. try {
  15. let fraction = 0;
  16. for (let i = 0; i < list.length; i++) {
  17. const data = await this.bankModel.findById({ _id: list[i]._id });
  18. if (data.answer === list[i].answer) {
  19. fraction += data.fraction;
  20. }
  21. }
  22. await this.model.create({ userName, userId, list, fraction });
  23. return { errcode: 0, errmsg: 'ok', data: { fraction } };
  24. } catch (error) {
  25. throw error;
  26. }
  27. }
  28. async update({ id, userName, userId, list, fraction }) {
  29. assert(id, 'id不存在');
  30. try {
  31. await this.model.updateOne({ _id: id }, { userName, userId, list, fraction });
  32. return { errcode: 0, errmsg: 'ok', data: '' };
  33. } catch (error) {
  34. throw error;
  35. }
  36. }
  37. async delete({ id }) {
  38. assert(id, 'id不存在');
  39. try {
  40. await this.model.deleteOne({ _id: id });
  41. return { errcode: 0, errmsg: 'ok', data: '' };
  42. } catch (error) {
  43. throw error;
  44. }
  45. }
  46. async query({ skip, limit, userName, fraction }) {
  47. const filter = {};
  48. const arr = { userName, fraction };
  49. for (const e in arr) {
  50. const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
  51. if (arr[e]) {
  52. filter.$or = [];
  53. filter.$or.push(JSON.parse(data));
  54. }
  55. }
  56. try {
  57. const total = await this.model.find({ ...filter });
  58. let res;
  59. if (skip && limit) {
  60. res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  61. } else {
  62. res = await this.model.find({ ...filter });
  63. }
  64. return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
  65. } catch (error) {
  66. throw error;
  67. }
  68. }
  69. async setTopic({ isRandom, total }) {
  70. try {
  71. let data;
  72. const list = [];
  73. if (!isRandom) {
  74. // 不随机
  75. data = await this.bankModel.find({}, { answer: false }).skip(0).limit(Number(total));
  76. } else {
  77. // 随机
  78. const totals = await this.bankModel.find();
  79. const arr = [];
  80. for (let i = 0; i < total; i++) {
  81. const num = Math.floor(Math.random() * totals.length);
  82. if (!arr.includes(num)) {
  83. arr.push(num);
  84. const res = await this.bankModel.find({}, { answer: false }).skip(Number(num)).limit(1);
  85. list.push(...res);
  86. }
  87. }
  88. }
  89. return { errcode: 0, errmsg: 'ok', data: !isRandom ? data : list, total };
  90. } catch (error) {
  91. throw error;
  92. }
  93. }
  94. }
  95. module.exports = PaperService;