'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).skip(Number(skip)).limit(Number(limit)); const data = []; for (const elm of res) { const _elm = _.cloneDeep(JSON.parse(JSON.stringify(elm))); 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;