فهرست منبع

新建资料表与资料评分表,在新建资料评分数据时更新资料表中的评分

reloaded 5 سال پیش
والد
کامیت
1bc92b1671

+ 43 - 0
app/controller/.material.js

@@ -0,0 +1,43 @@
+module.exports = {
+  create: {
+    requestBody: [
+      '!url',
+      '!type',
+      'score'
+    ]
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete'
+  },
+  update: {
+    params: ['!id'],
+    requestBody: [
+      'url',
+      'type',
+      'score'
+    ]
+  },
+  show: {
+    parameters: {
+      params: ['!id']
+    },
+    service: 'fetch'
+  },
+  index: {
+    parameters: {
+      query: {
+        url :'url',
+        type :'type',
+        score :'score'
+      }
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true
+    }
+  },
+};

+ 46 - 0
app/controller/.materialscore.js

@@ -0,0 +1,46 @@
+module.exports = {
+  create: {
+    requestBody: [
+      '!materialid',
+      '!uid',
+      '!score',
+      'remark'
+    ]
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete'
+  },
+  update: {
+    params: ['!id'],
+    requestBody: [
+      'materialid',
+      'uid',
+      'score',
+      'remark'
+    ]
+  },
+  show: {
+    parameters: {
+      params: ['!id']
+    },
+    service: 'fetch'
+  },
+  index: {
+    parameters: {
+      query: {
+        materialid:'materialid',
+        uid :'uid',
+        score :'score',
+        remark:'remark'
+      }
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true
+    }
+  },
+};

+ 18 - 0
app/controller/material.js

@@ -0,0 +1,18 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.material.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 上传资料表管理
+class MaterialController extends Controller {
+
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.material;
+  }
+
+}
+
+module.exports = CrudController(MaterialController, meta);

+ 18 - 0
app/controller/materialscore.js

@@ -0,0 +1,18 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.materialscore.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 资料评分管理
+class MaterialscoreController extends Controller {
+
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.materialscore;
+  }
+
+}
+
+module.exports = CrudController(MaterialscoreController, meta);

+ 20 - 0
app/model/material.js

@@ -0,0 +1,20 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+// 上传资料表
+const MaterialSchema = {
+  url: { type: String, required: true, maxLength: 200 }, // 资料链接
+  type: { type: String, required: true, maxLength: 200 }, // 资料类型,0-学生学习资料,1-教师学习资料
+  score: { type: String, required: false, maxLength: 200 }, // 资料得分
+};
+
+
+const schema = new Schema(MaterialSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Material', schema, 'material');
+};

+ 21 - 0
app/model/materialscore.js

@@ -0,0 +1,21 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+// 资料评分表
+const MaterialscoreSchema = {
+  materialid: { type: String, required: true, maxLength: 200 }, // 资料id
+  uid: { type: String, required: true, maxLength: 200 }, // 评价人id
+  score: { type: String, required: true, maxLength: 200 }, // 评分
+  remark: { type: String, required: false, maxLength: 2000 }, // 评价内容
+};
+
+
+const schema = new Schema(MaterialscoreSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Materialscore', schema, 'materialscore');
+};

+ 8 - 0
app/router.js

@@ -148,4 +148,12 @@ module.exports = app => {
   // 评分表设置路由
   router.resources('score', '/api/train/score', controller.score); // index、create、show、destroy
   router.post('score', '/api/train/score/update/:id', controller.score.update);
+
+  // 上传资料表设置路由
+  router.resources('material', '/api/train/material', controller.material); // index、create、show、destroy
+  router.post('material', '/api/train/material/update/:id', controller.material.update);
+
+  // 资料评分表设置路由
+  router.resources('materialscore', '/api/train/materialscore', controller.materialscore); // index、create、show、destroy
+  router.post('materialscore', '/api/train/materialscore/update/:id', controller.materialscore.update);
 };

+ 18 - 0
app/service/material.js

@@ -0,0 +1,18 @@
+'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 MaterialService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'material');
+    this.model = this.ctx.model.Material;
+  }
+
+}
+
+module.exports = MaterialService;

+ 36 - 0
app/service/materialscore.js

@@ -0,0 +1,36 @@
+'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 MaterialscoreService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'materialscore');
+    this.model = this.ctx.model.Materialscore;
+    this.mmodel = this.ctx.model.Material;
+  }
+
+  async create(data) {
+    const { materialid, score } = data;
+    const material = await this.mmodel.findById(materialid);
+    let newscore;
+    if (!material.score) {
+      newscore = score;
+    } else {
+      const _score = material.score;
+      const number = await this.model.count({ materialid });
+      newscore = (Number(_score) * number + Number(score)) / (number + 1);
+
+    }
+    material.score = newscore;
+    await material.save();
+    return await this.model.create(data);
+  }
+
+}
+
+module.exports = MaterialscoreService;

+ 4 - 0
app/service/teaplan.js

@@ -15,12 +15,16 @@ class TeaplanService extends CrudService {
   }
 
   async findteacher({ batchid }) {
+    // 查询所有班主任信息
     const headteachers = await this.hmodel.find();
     const newheadteachers = [];
+    // 遍历班主任信息
     for (const headteacher of headteachers) {
+      // 查询某班主任对应的班主任全年计划表
       const teaplan = await this.model.findOne({ headteacherid: headteacher.id });
       if (teaplan) {
         const nobatchids = teaplan.nobatchid;
+        // 如果有对应的全年计划表并且该计划表中的不能上课的批次包含指定批次,则添加status='0'的标记
         if (nobatchids.includes(batchid)) {
           newheadteachers.push({ ...JSON.parse(JSON.stringify(headteacher)), status: '0' });
         } else {