uploadtask.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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
  16. .find(info)
  17. .populate([
  18. {
  19. path: 'studentid',
  20. model: 'Student',
  21. select: 'name',
  22. },
  23. ])
  24. .skip(Number(skip))
  25. .limit(Number(limit));
  26. const data = [];
  27. for (const elm of res) {
  28. const _elm = _.cloneDeep(JSON.parse(JSON.stringify(elm)));
  29. if (_.isObject(elm.studentid)) {
  30. const { _id: studentid, name: stuname } = elm.studentid;
  31. _elm.studentid = studentid;
  32. if (!stuname) continue;
  33. _elm.stuname = stuname;
  34. }
  35. // const stu = await this.stumodel.findById(_elm.studentid);
  36. // if (stu)_elm.stuname = stu.name;
  37. // else {
  38. // console.error(`学生id:${_elm.studentid}/数据错误`);
  39. // }
  40. data.push(_elm);
  41. }
  42. return { data, total };
  43. }
  44. async create(data) {
  45. const { studentid, lessonid } = data;
  46. let res = await this.model.findOne({ studentid, lessonid });
  47. if (res) {
  48. // 存在数据,但是页面还是创建提交,需要进行修改
  49. if (_.get(data, 'picurl')) res.picurl = _.get(data, 'picurl');
  50. if (_.get(data, 'taskid')) res.taskid = _.get(data, 'taskid');
  51. if (_.get(data, 'answers')) res.picurl = _.get(data, 'answers');
  52. res = await res.save();
  53. } else {
  54. res = await this.model.create(data);
  55. }
  56. return res;
  57. }
  58. }
  59. module.exports = UploadtaskService;