'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 SettingService extends CrudService { constructor(ctx) { super(ctx, 'setting'); this.model = this.ctx.model.Setting; } async findone() { return await this.model.findOne(); } async update({ id }, { planyearid, planid, termid, user_email, auth_code, am_start, am_end, pm_start, pm_end, bd_start, bd_end, ques_setting, template_term }) { assert(id, '缺少部分信息项'); const setting = await this.model.findById(id); if (!setting) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '设置信息不存在'); } if (planyearid || planyearid === '') { setting.planyearid = planyearid; } if (planid || planid === '') { setting.planid = planid; } if (termid || termid === '') { setting.termid = termid; } if (user_email) { setting.user_email = user_email; } if (auth_code) { setting.auth_code = auth_code; } if (am_start) { setting.am_start = am_start; } if (am_end) { setting.am_end = am_end; } if (pm_start) { setting.pm_start = pm_start; } if (pm_end) { setting.pm_end = pm_end; } if (bd_start) { setting.bd_start = bd_start; } if (bd_end) { setting.bd_end = bd_end; } if (ques_setting) { setting.ques_setting = ques_setting; } if (template_term) { setting.template_term = template_term; } await setting.save(); return setting; } async termList() { let res = await this.ctx.model.Trainplan.find(); if (res && _.isArray(res)) { res = res.map(i => { if (_.isArray(i.termnum)) { i.termnum = i.termnum.map(t => { return { term: t.term * 1, _id: t._id }; }); i.termnum = _.orderBy(i.termnum, [ 'term' ], [ 'asc' ]); } return i.termnum; }); } return res.flat(); } } module.exports = SettingService;