'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class UploadtaskService extends CrudService { constructor(ctx) { super(ctx, 'uploadtask'); this.model = this.ctx.model.Uploadtask; this.stumodel = this.ctx.model.Student; } async query({ skip, limit, ...info }) { const total = await this.model.count(info); const res = await this.model .find(info) .populate([ { path: 'studentid', model: 'Student', select: 'name', }, ]) .skip(Number(skip)) .limit(Number(limit)); const data = []; for (const elm of res) { const _elm = _.cloneDeep(JSON.parse(JSON.stringify(elm))); if (_.isObject(elm.studentid)) { const { _id: studentid, name: stuname } = elm.studentid; _elm.studentid = studentid; if (!stuname) continue; _elm.stuname = stuname; } // const stu = await this.stumodel.findById(_elm.studentid); // if (stu)_elm.stuname = stu.name; // else { // console.error(`学生id:${_elm.studentid}/数据错误`); // } data.push(_elm); } return { data, total }; } async create(data) { const { studentid, lessonid } = data; let res = await this.model.findOne({ studentid, lessonid }); if (res) { // 存在数据,但是页面还是创建提交,需要进行修改 if (_.get(data, 'picurl')) res.picurl = _.get(data, 'picurl'); if (_.get(data, 'taskid')) res.taskid = _.get(data, 'taskid'); if (_.get(data, 'answers')) res.picurl = _.get(data, 'answers'); res = await res.save(); } else { res = await this.model.create(data); } return res; } } module.exports = UploadtaskService;