setting.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 SettingService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'setting');
  10. this.model = this.ctx.model.Setting;
  11. }
  12. async findone() {
  13. return await this.model.findOne();
  14. }
  15. 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 }) {
  16. assert(id, '缺少部分信息项');
  17. const setting = await this.model.findById(id);
  18. if (!setting) {
  19. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '设置信息不存在');
  20. }
  21. if (planyearid || planyearid === '') {
  22. setting.planyearid = planyearid;
  23. }
  24. if (planid || planid === '') {
  25. setting.planid = planid;
  26. }
  27. if (termid || termid === '') {
  28. setting.termid = termid;
  29. }
  30. if (user_email) {
  31. setting.user_email = user_email;
  32. }
  33. if (auth_code) {
  34. setting.auth_code = auth_code;
  35. }
  36. if (am_start) {
  37. setting.am_start = am_start;
  38. }
  39. if (am_end) {
  40. setting.am_end = am_end;
  41. }
  42. if (pm_start) {
  43. setting.pm_start = pm_start;
  44. }
  45. if (pm_end) {
  46. setting.pm_end = pm_end;
  47. }
  48. if (bd_start) {
  49. setting.bd_start = bd_start;
  50. }
  51. if (bd_end) {
  52. setting.bd_end = bd_end;
  53. }
  54. if (ques_setting) {
  55. setting.ques_setting = ques_setting;
  56. }
  57. if (template_term) {
  58. setting.template_term = template_term;
  59. }
  60. await setting.save();
  61. return setting;
  62. }
  63. async termList() {
  64. let res = await this.ctx.model.Trainplan.find();
  65. if (res && _.isArray(res)) {
  66. res = res.map(i => {
  67. if (_.isArray(i.termnum)) {
  68. i.termnum = i.termnum.map(t => {
  69. return { term: t.term * 1, _id: t._id };
  70. });
  71. i.termnum = _.orderBy(i.termnum, [ 'term' ], [ 'asc' ]);
  72. }
  73. return i.termnum;
  74. });
  75. }
  76. return res.flat();
  77. }
  78. }
  79. module.exports = SettingService;