uploadtask.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class UploadtaskService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'uploadtask');
  10. this.model = this.ctx.model.Uploadtask;
  11. this.stumodel = this.ctx.model.Student;
  12. }
  13. async query({ skip, limit, ...info }) {
  14. const total = await this.model.count(info);
  15. const res = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  16. const data = [];
  17. for (const elm of res) {
  18. const _elm = _.cloneDeep(JSON.parse(JSON.stringify(elm)));
  19. const stu = await this.stumodel.findById(_elm.studentid);
  20. if (stu)_elm.stuname = stu.name;
  21. else {
  22. console.error(`学生id:${_elm.studentid}/数据错误`);
  23. }
  24. data.push(_elm);
  25. }
  26. return { data, total };
  27. }
  28. async create(data) {
  29. const { studentid, lessonid } = data;
  30. let res = await this.model.findOne({ studentid, lessonid });
  31. if (res) {
  32. // 存在数据,但是页面还是创建提交,需要进行修改
  33. if (_.get(data, 'picurl')) res.picurl = _.get(data, 'picurl');
  34. if (_.get(data, 'taskid')) res.taskid = _.get(data, 'taskid');
  35. if (_.get(data, 'answers')) res.picurl = _.get(data, 'answers');
  36. res = await res.save();
  37. } else {
  38. res = await this.model.create(data);
  39. }
  40. return res;
  41. }
  42. }
  43. module.exports = UploadtaskService;