Bladeren bron

修改学校查重

zs 10 maanden geleden
bovenliggende
commit
6b2dd6e0bd
4 gewijzigde bestanden met toevoegingen van 47 en 3 verwijderingen
  1. 5 2
      app/controller/school.js
  2. 1 0
      app/router.js
  3. 1 1
      app/service/headteacher.js
  4. 40 0
      app/service/school.js

+ 5 - 2
app/controller/school.js

@@ -7,12 +7,16 @@ const { CrudController } = require('naf-framework-mongoose/lib/controller');
 
 
 // 学校管理
 // 学校管理
 class SchoolController extends Controller {
 class SchoolController extends Controller {
-
   constructor(ctx) {
   constructor(ctx) {
     super(ctx);
     super(ctx);
     this.service = this.ctx.service.school;
     this.service = this.ctx.service.school;
   }
   }
 
 
+  // 去重查询学校
+  async findSchool() {
+    const data = await this.service.findSchool(this.ctx.query);
+    this.ctx.ok({ ...data });
+  }
   // 学生名单上报
   // 学生名单上报
   async stuimport() {
   async stuimport() {
     const res = await this.service.stuimport(this.ctx.request.body);
     const res = await this.service.stuimport(this.ctx.request.body);
@@ -22,7 +26,6 @@ class SchoolController extends Controller {
     const data = await this.service.exportSchool(this.ctx.request.body);
     const data = await this.service.exportSchool(this.ctx.request.body);
     this.ctx.ok({ data });
     this.ctx.ok({ data });
   }
   }
-
 }
 }
 
 
 module.exports = CrudController(SchoolController, meta);
 module.exports = CrudController(SchoolController, meta);

+ 1 - 0
app/router.js

@@ -209,6 +209,7 @@ module.exports = app => {
   router.post('attendance', '/api/train/attendance/attendancecreate', controller.attendance.attendancecreate);
   router.post('attendance', '/api/train/attendance/attendancecreate', controller.attendance.attendancecreate);
   router.post('attendance', '/api/train/attendance/attendancecreateList', controller.attendance.attendancecreateList);
   router.post('attendance', '/api/train/attendance/attendancecreateList', controller.attendance.attendancecreateList);
   // 学校上传学生名单
   // 学校上传学生名单
+  router.get('/api/train/school/findSchool', controller.school.findSchool);
   router.resources('school', '/api/train/school', controller.school); // index、create、show、destroy
   router.resources('school', '/api/train/school', controller.school); // index、create、show、destroy
   router.post('school', '/api/train/school/update/:id', controller.school.update);
   router.post('school', '/api/train/school/update/:id', controller.school.update);
   router.post('/api/train/school/import', controller.school.stuimport); // 名单上传
   router.post('/api/train/school/import', controller.school.stuimport); // 名单上传

+ 1 - 1
app/service/headteacher.js

@@ -22,7 +22,7 @@ class HeadteacherService extends CrudService {
       sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
       sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
     }
     }
 
 
-    const rs = await this.model.find(trimData(filter), projection, { skip, limit, sort }).lean();
+    let rs = await this.model.find(trimData(filter), projection, { skip, limit, sort }).lean();
     if (rs.length > 0) rs = JSON.parse(JSON.stringify(rs));
     if (rs.length > 0) rs = JSON.parse(JSON.stringify(rs));
     const ids = rs.map(i => ObjectId(i._id).toString());
     const ids = rs.map(i => ObjectId(i._id).toString());
     const users = await this.umodel.find({ uid: ids }).lean();
     const users = await this.umodel.find({ uid: ids }).lean();

+ 40 - 0
app/service/school.js

@@ -74,6 +74,46 @@ class SchoolService extends CrudService {
     return await this.model.count(query);
     return await this.model.count(query);
   }
   }
 
 
+  // 去重查询学校
+  async findSchool({ name, skip, limit, ...info }) {
+    let data = [];
+    const query = { ...info };
+    if (name) {
+      query.name = { $regex: name };
+    }
+    const AggregateInfo = [
+      // 这里可以添加其他查询条件,例如过滤特定字段等
+      { $match: query },
+      // 去除重复数据
+      { $group: { _id: '$code', uniqueData: { $first: '$$ROOT' } } },
+      { $replaceRoot: { newRoot: '$uniqueData' } },
+      { $addFields: { id: '$_id' } },
+      // 分页查询
+      { $skip: parseInt(skip) },
+    ];
+    if (limit) AggregateInfo.push({ $limit: parseInt(limit) });
+    const res = await this.model.aggregate(AggregateInfo);
+    if (res && res.length > 0) {
+      data = JSON.parse(JSON.stringify(res));
+      const ids = res.map(i => i._id);
+      const users = await this.umodel.find({ uid: { $in: ids } }, '+passwd').lean();
+      for (const tea of data) {
+        const r = users.find(f => f.uid === tea._id);
+        if (r) {
+          const passwd = _.get(r.passwd, 'secret');
+          if (passwd) tea.passwd = passwd;
+        }
+      }
+    }
+    const count = await this.model.aggregate([
+      { $match: query },
+      { $group: { _id: '$code' } },
+      { $count: 'distinctCount' },
+    ]);
+    const total = _.get(count[0], 'distinctCount') || 0;
+    return { total, data };
+  }
+
   async stuimport(data) {
   async stuimport(data) {
     const { filepath, termid, schid, type, batchid } = data;
     const { filepath, termid, schid, type, batchid } = data;
     assert(filepath, 'filepath不能为空');
     assert(filepath, 'filepath不能为空');