Browse Source

查询结尾不对,导致id没出来

lrf 10 tháng trước cách đây
mục cha
commit
25b03b417d

+ 1 - 0
app/service/headteacher.js

@@ -23,6 +23,7 @@ class HeadteacherService extends CrudService {
     }
     }
 
 
     const rs = await this.model.find(trimData(filter), projection, { skip, limit, sort }).lean();
     const rs = await this.model.find(trimData(filter), projection, { skip, limit, sort }).lean();
+    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();
     const uids = users.map(i => ObjectId(i._id).toString());
     const uids = users.map(i => ObjectId(i._id).toString());

+ 2 - 1
app/service/lesson.js

@@ -262,8 +262,9 @@ class LessonService extends CrudService {
       'lessons.teaid': teaid,
       'lessons.teaid': teaid,
     });
     });
     const classids = lessons.map(i => ObjectId(i.classid));
     const classids = lessons.map(i => ObjectId(i.classid));
-    let data = await this.ctx.model.Class.find({ _id: { $in: classids } }).lean();
+    let data = await this.ctx.model.Class.find({ _id: { $in: classids } }).exec();
     if (data.length <= 0) return;
     if (data.length <= 0) return;
+    data = JSON.parse(JSON.stringify(data));
     const planid = data[0].planid;
     const planid = data[0].planid;
     const trainPlan = await this.tmodel.findById(planid).lean();
     const trainPlan = await this.tmodel.findById(planid).lean();
     const { termnum = [] } = trainPlan;
     const { termnum = [] } = trainPlan;

+ 39 - 87
app/service/student.js

@@ -46,10 +46,7 @@ class StudentService extends CrudService {
 
 
   async delete({ id }) {
   async delete({ id }) {
     // 删除小组中的这个人,作业表,问卷表,评分,考勤,用户表,学生表
     // 删除小组中的这个人,作业表,问卷表,评分,考勤,用户表,学生表
-    await this.gmodel.update(
-      { 'students.stuid': id },
-      { $pull: { students: { stuid: id } } }
-    );
+    await this.gmodel.update({ 'students.stuid': id }, { $pull: { students: { stuid: id } } });
     await this.upmodel.deleteMany({ studentid: id });
     await this.upmodel.deleteMany({ studentid: id });
     await this.uqmodel.deleteMany({ studentid: id });
     await this.uqmodel.deleteMany({ studentid: id });
     await this.scoremodel.deleteMany({ stuid: id });
     await this.scoremodel.deleteMany({ stuid: id });
@@ -62,10 +59,7 @@ class StudentService extends CrudService {
     if (student) {
     if (student) {
       const { phone, name } = data;
       const { phone, name } = data;
       if (phone && name) {
       if (phone && name) {
-        await this.umodel.findOneAndUpdate(
-          { uid: id },
-          { mobile: phone, name }
-        );
+        await this.umodel.findOneAndUpdate({ uid: id }, { mobile: phone, name });
       }
       }
     }
     }
     return student;
     return student;
@@ -77,22 +71,22 @@ class StudentService extends CrudService {
     if (name) {
     if (name) {
       query.name = { $regex: name };
       query.name = { $regex: name };
     }
     }
-    let data = await this.model
-      .find(query)
-      .skip(parseInt(skip))
-      .limit(parseInt(limit));
-    const planids = _.uniq(data.map(i => ObjectId(i.planid)));
+    let data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
+    const planids = _.uniq(data.map((i) => ObjectId(i.planid)));
     const plan = await this.ctx.model.Trainplan.find({ _id: planids });
     const plan = await this.ctx.model.Trainplan.find({ _id: planids });
     data = JSON.parse(JSON.stringify(data));
     data = JSON.parse(JSON.stringify(data));
     for (const i of data) {
     for (const i of data) {
-      if (i.classid) { i.classid = await this.clamodel.findById(i.classid).lean(); }
+      if (i.classid) {
+        const d = await this.clamodel.findById(i.classid).exec();
+        if (d) i.classid = JSON.parse(JSON.stringify(d));
+      }
     }
     }
-    data = data.map(i => {
+    data = data.map((i) => {
       if (i.classid && _.isObject(i.classid)) {
       if (i.classid && _.isObject(i.classid)) {
         i.classname = _.get(i.classid, 'name');
         i.classname = _.get(i.classid, 'name');
         i.classid = _.get(i.classid, '_id');
         i.classid = _.get(i.classid, '_id');
       }
       }
-      const p = plan.find(f => ObjectId(f._id).equals(i.planid));
+      const p = plan.find((f) => ObjectId(f._id).equals(i.planid));
       if (p) {
       if (p) {
         const { termnum, title } = p;
         const { termnum, title } = p;
         i.plantitle = title;
         i.plantitle = title;
@@ -203,14 +197,7 @@ class StudentService extends CrudService {
       const detail = '你已被班主任设置为' + job + ',请及时登录查看';
       const detail = '你已被班主任设置为' + job + ',请及时登录查看';
       const remark = '感谢您的使用';
       const remark = '感谢您的使用';
       if (openid) {
       if (openid) {
-        this.ctx.service.weixin.sendTemplateMsg(
-          this.ctx.app.config.REVIEW_TEMPLATE_ID,
-          openid,
-          '您有一个新的通知',
-          detail,
-          date,
-          remark
-        );
+        this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
       }
       }
     }
     }
     return await student.save();
     return await student.save();
@@ -231,17 +218,12 @@ class StudentService extends CrudService {
   async findscore({ skip, limit, ...info }) {
   async findscore({ skip, limit, ...info }) {
     const { classid } = info;
     const { classid } = info;
     const total = await this.model.count(info);
     const total = await this.model.count(info);
-    const students = await this.model
-      .find(info)
-      .skip(Number(skip))
-      .limit(Number(limit));
+    const students = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
     const data = [];
     const data = [];
     const groups = await this.gmodel.find({ classid });
     const groups = await this.gmodel.find({ classid });
     for (const student of students) {
     for (const student of students) {
       const _student = JSON.parse(JSON.stringify(student));
       const _student = JSON.parse(JSON.stringify(student));
-      const group = groups.find(item =>
-        item.students.find(stuinfo => stuinfo.stuid === _student.id)
-      );
+      const group = groups.find((item) => item.students.find((stuinfo) => stuinfo.stuid === _student.id));
       if (group) {
       if (group) {
         _student.groupscore = group.score;
         _student.groupscore = group.score;
       }
       }
@@ -299,19 +281,15 @@ class StudentService extends CrudService {
     // 获取班级学生列表
     // 获取班级学生列表
     let studentList = await this.model.find({ classid });
     let studentList = await this.model.find({ classid });
     // 重置评优,干部全优秀
     // 重置评优,干部全优秀
-    studentList = studentList.map(i => {
+    studentList = studentList.map((i) => {
       if (i.job.includes('普通')) i.is_fine = '0';
       if (i.job.includes('普通')) i.is_fine = '0';
       else i.is_fine = '1';
       else i.is_fine = '1';
       return i;
       return i;
     });
     });
     // 初始化后取出不需要算的人,他们就这样,没必要算
     // 初始化后取出不需要算的人,他们就这样,没必要算
-    const reverseList = studentList.filter(
-      f => !(f.is_fine !== '2' && f.job.includes('普通'))
-    );
+    const reverseList = studentList.filter((f) => !(f.is_fine !== '2' && f.job.includes('普通')));
     // 过滤出取消评优资格的学生和干部;干部就是优秀;被取消资格就别凑热闹了
     // 过滤出取消评优资格的学生和干部;干部就是优秀;被取消资格就别凑热闹了
-    studentList = studentList.filter(
-      f => f.is_fine !== '2' && f.job.includes('普通')
-    );
+    studentList = studentList.filter((f) => f.is_fine !== '2' && f.job.includes('普通'));
     // 获取平时分
     // 获取平时分
     let dailyScoreList = await this.psmodel.find({ classid });
     let dailyScoreList = await this.psmodel.find({ classid });
     // 去重复
     // 去重复
@@ -324,21 +302,12 @@ class StudentService extends CrudService {
     // 获取小组分,小组
     // 获取小组分,小组
     const groupList = await this.gmodel.find({ classid });
     const groupList = await this.gmodel.find({ classid });
     const groupScoreList = await this.gsmodel.find({ classid });
     const groupScoreList = await this.gsmodel.find({ classid });
-    studentList = this.dealGroupScoreList(
-      groupList,
-      groupScoreList,
-      studentList
-    );
-    studentList = studentList.map(i => {
-      i.score = _.round(
-        (i.daily || 0) + (i.task || 0) + (i.groupscore || 0),
-        2
-      );
+    studentList = this.dealGroupScoreList(groupList, groupScoreList, studentList);
+    studentList = studentList.map((i) => {
+      i.score = _.round((i.daily || 0) + (i.task || 0) + (i.groupscore || 0), 2);
       return i;
       return i;
     });
     });
-    studentList = studentList.sort(
-      (a, b) => (b.score * 1 || 0) - (a.score * 1 || 0)
-    );
+    studentList = studentList.sort((a, b) => (b.score * 1 || 0) - (a.score * 1 || 0));
     // 排名
     // 排名
     // eslint-disable-next-line no-unused-vars
     // eslint-disable-next-line no-unused-vars
     let num = 0;
     let num = 0;
@@ -353,15 +322,11 @@ class StudentService extends CrudService {
       let plus = 1; // 这轮有多少人,到这了,这个人肯定是要改了,所以默认1
       let plus = 1; // 这轮有多少人,到这了,这个人肯定是要改了,所以默认1
       // 评优
       // 评优
       student.is_fine = '1';
       student.is_fine = '1';
-      const rlist = studentList.filter(
-        f => f.score === score && !ObjectId(f._id).equals(_id)
-      );
+      const rlist = studentList.filter((f) => f.score === score && !ObjectId(f._id).equals(_id));
       // 处理同分的人也都变成is_fine
       // 处理同分的人也都变成is_fine
       for (const stud of rlist) {
       for (const stud of rlist) {
         stud.is_fine = '1';
         stud.is_fine = '1';
-        const sindex = studentList.findIndex(f =>
-          ObjectId(stud._id).equals(f._id)
-        );
+        const sindex = studentList.findIndex((f) => ObjectId(stud._id).equals(f._id));
         if (sindex >= 0) {
         if (sindex >= 0) {
           studentList[sindex] = stud;
           studentList[sindex] = stud;
           plus++;
           plus++;
@@ -371,14 +336,11 @@ class StudentService extends CrudService {
       num = num + plus;
       num = num + plus;
     }
     }
     // 算完的和不用算的合并,提交
     // 算完的和不用算的合并,提交
-    const lastList = [ ...studentList, ...reverseList ];
+    const lastList = [...studentList, ...reverseList];
     for (const student of lastList) {
     for (const student of lastList) {
       // const res = await student.save();
       // const res = await student.save();
       // const { meta, ...data } = student;
       // const { meta, ...data } = student;
-      const r = await this.model.findByIdAndUpdate(
-        { _id: ObjectId(student._id) },
-        { score: student.score, is_fine: student.is_fine }
-      );
+      const r = await this.model.findByIdAndUpdate({ _id: ObjectId(student._id) }, { score: student.score, is_fine: student.is_fine });
     }
     }
   }
   }
   /**
   /**
@@ -416,7 +378,7 @@ class StudentService extends CrudService {
   dealGroupScoreList(groupList, scoreList, studentList) {
   dealGroupScoreList(groupList, scoreList, studentList) {
     scoreList = _.groupBy(scoreList, 'groupid');
     scoreList = _.groupBy(scoreList, 'groupid');
     // 算出每组的平均分,之后加给学生
     // 算出每组的平均分,之后加给学生
-    groupList = groupList.map(i => {
+    groupList = groupList.map((i) => {
       const { students } = i;
       const { students } = i;
       if (students.length > 0) {
       if (students.length > 0) {
         const slist = scoreList[i._id];
         const slist = scoreList[i._id];
@@ -429,10 +391,8 @@ class StudentService extends CrudService {
       return i;
       return i;
     });
     });
     // 每个学生加自己的组的平均分
     // 每个学生加自己的组的平均分
-    studentList = studentList.map(i => {
-      const r = groupList.find(f =>
-        f.students.find(sf => ObjectId(sf.stuid).equals(i._id))
-      );
+    studentList = studentList.map((i) => {
+      const r = groupList.find((f) => f.students.find((sf) => ObjectId(sf.stuid).equals(i._id)));
       if (r) {
       if (r) {
         // i.score = _.round((i.score * 1 || 0) + (r.score * 1 || 0), 2);
         // i.score = _.round((i.score * 1 || 0) + (r.score * 1 || 0), 2);
         i.groupscore = r.score * 1 || 0;
         i.groupscore = r.score * 1 || 0;
@@ -520,10 +480,10 @@ class StudentService extends CrudService {
     assert(missionid, '缺少任务信息,无法执行任务');
     assert(missionid, '缺少任务信息,无法执行任务');
     try {
     try {
       // 整理表头+生成excel
       // 整理表头+生成excel
-      const head = model.map(i => i.zh);
+      const head = model.map((i) => i.zh);
       let fn = await this.toGetFn(data, missionid);
       let fn = await this.toGetFn(data, missionid);
       let dp = null;
       let dp = null;
-      const { downloadPath, fn: nfn } = await this.ctx.service.util.toAsyncExcel([ head ], fn, dp);
+      const { downloadPath, fn: nfn } = await this.ctx.service.util.toAsyncExcel([head], fn, dp);
       dp = downloadPath;
       dp = downloadPath;
       fn = nfn;
       fn = nfn;
       // 整理数据(分页循环)+写入excel
       // 整理数据(分页循环)+写入excel
@@ -535,7 +495,7 @@ class StudentService extends CrudService {
       else if (termid.length > 0) query.termid = termid;
       else if (termid.length > 0) query.termid = termid;
       else if (planid) query.planid = planid;
       else if (planid) query.planid = planid;
       if (isComming === '0') query.isComming = '0';
       if (isComming === '0') query.isComming = '0';
-      else if (isComming !== '0')query.isComming = { $ne: '0' };
+      else if (isComming !== '0') query.isComming = { $ne: '0' };
       const total = await this.exportToGetTotal(query);
       const total = await this.exportToGetTotal(query);
       const times = Math.ceil(total / limit);
       const times = Math.ceil(total / limit);
       for (let i = 0; i < times; i++) {
       for (let i = 0; i < times; i++) {
@@ -543,7 +503,7 @@ class StudentService extends CrudService {
         if (data.length <= 0) break;
         if (data.length <= 0) break;
         skip = skip + limit;
         skip = skip + limit;
         data = await this.exportDealData(data);
         data = await this.exportDealData(data);
-        const dataList = data.map(i => {
+        const dataList = data.map((i) => {
           const obj = [];
           const obj = [];
           for (const m of model) {
           for (const m of model) {
             obj.push(i[m.model]);
             obj.push(i[m.model]);
@@ -619,27 +579,27 @@ class StudentService extends CrudService {
       {
       {
         model: 'schid',
         model: 'schid',
         type: 'get',
         type: 'get',
-        format: i => i.school_name,
+        format: (i) => i.school_name,
       },
       },
       {
       {
         model: 'termid',
         model: 'termid',
         type: 'get',
         type: 'get',
-        format: i => i.termname,
+        format: (i) => i.termname,
       },
       },
       {
       {
         model: 'batchid',
         model: 'batchid',
         type: 'get',
         type: 'get',
-        format: i => i.batchname,
+        format: (i) => i.batchname,
       },
       },
       {
       {
         model: 'classid',
         model: 'classid',
         type: 'get',
         type: 'get',
-        format: i => i.classname,
+        format: (i) => i.classname,
       },
       },
       {
       {
         model: 'bedroomid',
         model: 'bedroomid',
         type: 'get',
         type: 'get',
-        format: i => i.bedroom,
+        format: (i) => i.bedroom,
       },
       },
     ];
     ];
     // 需要请求的数据, 学校数据
     // 需要请求的数据, 学校数据
@@ -657,7 +617,7 @@ class StudentService extends CrudService {
             const option = _.head(options);
             const option = _.head(options);
             i[model] = option.to;
             i[model] = option.to;
           } else {
           } else {
-            const option = options.find(f => f.value === v);
+            const option = options.find((f) => f.value === v);
             if (option) i[model] = option.to;
             if (option) i[model] = option.to;
             else {
             else {
               // 没有默认第一个
               // 没有默认第一个
@@ -666,7 +626,6 @@ class StudentService extends CrudService {
             }
             }
           }
           }
         }
         }
-
       }
       }
     }
     }
     return data;
     return data;
@@ -729,20 +688,13 @@ class StudentService extends CrudService {
 
 
   // 确认学生打印证书
   // 确认学生打印证书
   async printCert({ ids }) {
   async printCert({ ids }) {
-    const res = await this.model.updateMany(
-      { _id: { $in: ids.map(i => ObjectId(i)) } },
-      { cert: '1' }
-    );
+    const res = await this.model.updateMany({ _id: { $in: ids.map((i) => ObjectId(i)) } }, { cert: '1' });
     return res;
     return res;
   }
   }
 
 
   // 根据计划id找到学校是否上传学生
   // 根据计划id找到学校是否上传学生
   async getSchoolStudent({ planid }) {
   async getSchoolStudent({ planid }) {
-    const res = await this.model.aggregate([
-      { $match: { planid } },
-      { $group: { _id: '$schid', sum: { $sum: 1 } } },
-      { $project: { _id: 0, schid: '$_id', schnum: '$sum' } },
-    ]);
+    const res = await this.model.aggregate([{ $match: { planid } }, { $group: { _id: '$schid', sum: { $sum: 1 } } }, { $project: { _id: 0, schid: '$_id', schnum: '$sum' } }]);
     return res;
     return res;
   }
   }
 }
 }

+ 10 - 9
app/service/teacher.js

@@ -25,19 +25,20 @@ class TeacherService extends CrudService {
     if (name) {
     if (name) {
       query.name = { $regex: name };
       query.name = { $regex: name };
     }
     }
-    const rs = await this.model.find(query, null, { skip, limit }).lean();
-    const ids = rs.map(i => ObjectId(i._id).toString());
+    let rs = await this.model.find(query, null, { skip, limit }).exec();
+    if (rs.length > 0) rs = JSON.parse(JSON.stringify(rs));
+    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();
-    const uids = users.map(i => ObjectId(i._id).toString());
+    const uids = users.map((i) => ObjectId(i._id).toString());
     const yjyUsers = await this.yjy.find({ suid: uids }).lean();
     const yjyUsers = await this.yjy.find({ suid: uids }).lean();
     for (const i of rs) {
     for (const i of rs) {
       const { _id: did } = i;
       const { _id: did } = i;
-      const user = users.find(f => ObjectId(did).equals(f.uid));
+      const user = users.find((f) => ObjectId(did).equals(f.uid));
       if (!user) {
       if (!user) {
         i.is_bind = false;
         i.is_bind = false;
         continue;
         continue;
       }
       }
-      const yjyUser = yjyUsers.find(f => ObjectId(user._id).equals(f.suid));
+      const yjyUser = yjyUsers.find((f) => ObjectId(user._id).equals(f.suid));
       if (yjyUser) i.is_bind = true;
       if (yjyUser) i.is_bind = true;
       else i.is_bind = false;
       else i.is_bind = false;
     }
     }
@@ -187,7 +188,7 @@ class TeacherService extends CrudService {
         xsscore: data[theadRule[3]],
         xsscore: data[theadRule[3]],
       });
       });
     }
     }
-    exceldata = [ ...exceldata, ..._datas ];
+    exceldata = [...exceldata, ..._datas];
     return exceldata;
     return exceldata;
   }
   }
 
 
@@ -250,7 +251,7 @@ class TeacherService extends CrudService {
   async export({ missionid, model }) {
   async export({ missionid, model }) {
     assert(missionid, '缺少任务信息,无法执行任务');
     assert(missionid, '缺少任务信息,无法执行任务');
     try {
     try {
-      const head = model.map(i => {
+      const head = model.map((i) => {
         // const { zh, model } = i;
         // const { zh, model } = i;
         // const headObj = { header: zh };
         // const headObj = { header: zh };
         // if (model) headObj.key = model;
         // if (model) headObj.key = model;
@@ -263,13 +264,13 @@ class TeacherService extends CrudService {
       const total = await this.count({});
       const total = await this.count({});
       const times = Math.ceil(total / this.export_limit);
       const times = Math.ceil(total / this.export_limit);
       let dp = null;
       let dp = null;
-      const { downloadPath, fn: nfn } = await this.ctx.service.util.toAsyncExcel([ head ], fn, dp);
+      const { downloadPath, fn: nfn } = await this.ctx.service.util.toAsyncExcel([head], fn, dp);
       dp = downloadPath;
       dp = downloadPath;
       fn = nfn;
       fn = nfn;
       for (let i = 0; i < times; i++) {
       for (let i = 0; i < times; i++) {
         const data = await this.query({}, { skip, limit: this.export_limit });
         const data = await this.query({}, { skip, limit: this.export_limit });
         if (data.length <= 0) break;
         if (data.length <= 0) break;
-        const dataList = data.map(i => {
+        const dataList = data.map((i) => {
           const obj = [];
           const obj = [];
           for (const m of model) {
           for (const m of model) {
             obj.push(i[m.model]);
             obj.push(i[m.model]);