|
@@ -9,6 +9,7 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
const moment = require('moment');
|
|
|
const XLSX = require('xlsx');
|
|
|
const Excel = require('exceljs');
|
|
|
+const { sep } = require('path');
|
|
|
|
|
|
class SchoolService extends CrudService {
|
|
|
constructor(ctx) {
|
|
@@ -52,10 +53,10 @@ class SchoolService extends CrudService {
|
|
|
let res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
|
|
|
if (res && res.length > 0) {
|
|
|
res = JSON.parse(JSON.stringify(res));
|
|
|
- const ids = res.map((i) => i._id);
|
|
|
+ const ids = res.map(i => i._id);
|
|
|
const users = await this.umodel.find({ uid: { $in: ids } }, '+passwd');
|
|
|
for (const tea of res) {
|
|
|
- const r = users.find((f) => f.uid === tea._id);
|
|
|
+ const r = users.find(f => f.uid === tea._id);
|
|
|
if (r) {
|
|
|
const passwd = _.get(r.passwd, 'secret');
|
|
|
if (passwd) tea.passwd = passwd;
|
|
@@ -90,12 +91,13 @@ class SchoolService extends CrudService {
|
|
|
let dbStuList = await this.ctx.model.Student.find({ termid, batchid, schid });
|
|
|
if (dbStuList.length > 0) {
|
|
|
// 查这个学校的这期学生是否修改过班级 或 寝室
|
|
|
- const is_change = dbStuList.find((f) => f.classid || f.bedroomid);
|
|
|
- if (is_change)
|
|
|
+ const is_change = dbStuList.find(f => f.classid || f.bedroomid);
|
|
|
+ if (is_change) {
|
|
|
throw new BusinessError(
|
|
|
ErrorCode.BUSINESS,
|
|
|
'上报过的学生已经安排班级或寝室!若需要替换学生,让同性别的学生直接来和班主任说,修改信息即可.若还是有疑问,请和中心负责人联系(最好联系下)'
|
|
|
);
|
|
|
+ }
|
|
|
}
|
|
|
// 2021-06-07 如果学生已经绑定,那也不允许修改名单了
|
|
|
const countOpenid = await this.ctx.model.Student.count({ termid, batchid, schid, openid: { $exists: true } });
|
|
@@ -163,7 +165,7 @@ class SchoolService extends CrudService {
|
|
|
// 复制,删除,添加
|
|
|
if (dbStuList.length > 0) {
|
|
|
dbStuList = JSON.parse(JSON.stringify(dbStuList));
|
|
|
- dbStuList = dbStuList.map((i) => {
|
|
|
+ dbStuList = dbStuList.map(i => {
|
|
|
delete i.meta;
|
|
|
i.studentid = _.clone(i._id);
|
|
|
delete i.id;
|
|
@@ -210,7 +212,7 @@ class SchoolService extends CrudService {
|
|
|
async getschnum(plan, schid, batchid) {
|
|
|
const schtime = await this.schmodel.findOne({ schid, planid: plan.id });
|
|
|
const { arrange } = schtime;
|
|
|
- const r = arrange.find((f) => f.batchid === batchid);
|
|
|
+ const r = arrange.find(f => f.batchid === batchid);
|
|
|
if (!r) {
|
|
|
throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到该学校的计划');
|
|
|
}
|
|
@@ -239,9 +241,9 @@ class SchoolService extends CrudService {
|
|
|
headRow.eachCell((cell, coli) => {
|
|
|
console.log(cell.value);
|
|
|
if (cell.value !== '序号') {
|
|
|
- const r = cols.find((f) => f.key === cell.value);
|
|
|
+ const r = cols.find(f => f.key === cell.value);
|
|
|
if (r) {
|
|
|
- const ri = cols.findIndex((f) => f.key === cell.value);
|
|
|
+ const ri = cols.findIndex(f => f.key === cell.value);
|
|
|
// 表头符合要求,做上标记
|
|
|
r.colIndex = coli;
|
|
|
cols[ri] = r;
|
|
@@ -251,14 +253,14 @@ class SchoolService extends CrudService {
|
|
|
}
|
|
|
});
|
|
|
// 检查表头结果,如果有没有 colIndex,说明表头里有不符合要求的,退回去
|
|
|
- const excelIsRigth = cols.every((f) => f.colIndex);
|
|
|
+ const excelIsRigth = cols.every(f => f.colIndex);
|
|
|
if (!excelIsRigth) throw new BusinessError(ErrorCode.DATA_INVALID, 'Excel表格格式不正确,请使用系统提供的模板,或重新下载模板!');
|
|
|
// 删除掉第一行 表头行,这不是数据
|
|
|
worksheet.spliceRows(0, 1);
|
|
|
const stuList = [];
|
|
|
- const noWhite = (str) => str.replace(/\s*/g, '');
|
|
|
+ const noWhite = str => str.replace(/\s*/g, '');
|
|
|
// 整理数据,根据检查合格的表头行,获取每个格子的数据,制成[object]格式
|
|
|
- worksheet.eachRow((row) => {
|
|
|
+ worksheet.eachRow(row => {
|
|
|
const stu = {};
|
|
|
for (let i = 0; i < cols.length; i++) {
|
|
|
const col = cols[i];
|
|
@@ -314,7 +316,7 @@ class SchoolService extends CrudService {
|
|
|
// error = true;
|
|
|
// msg = `${msg}学生已存在`;
|
|
|
// }
|
|
|
- const have_same = stuList.filter((f) => f.id_number === val && f.name !== name);
|
|
|
+ const have_same = stuList.filter(f => f.id_number === val && f.name !== name);
|
|
|
if (have_same.length > 0) {
|
|
|
error = true;
|
|
|
const h = _.head(have_same);
|
|
@@ -338,7 +340,7 @@ class SchoolService extends CrudService {
|
|
|
// error = true;
|
|
|
// msg = `${msg}学生库中已有该手机号,请检查手机号是否正确,若无误,请联系中心负责人`;
|
|
|
// }
|
|
|
- const have_same = stuList.filter((f) => f.phone === val && f.name !== name);
|
|
|
+ const have_same = stuList.filter(f => f.phone === val && f.name !== name);
|
|
|
if (have_same.length > 0) {
|
|
|
error = true;
|
|
|
const h = _.head(have_same);
|
|
@@ -403,15 +405,15 @@ class SchoolService extends CrudService {
|
|
|
// 最后整合数据
|
|
|
lastSetData(stuList, data) {
|
|
|
const cols = this.getStucolumn();
|
|
|
- const needChange = cols.filter((f) => f.change);
|
|
|
- stuList = stuList.map((i) => {
|
|
|
+ const needChange = cols.filter(f => f.change);
|
|
|
+ stuList = stuList.map(i => {
|
|
|
const d = { ...i, ...data };
|
|
|
for (const col of needChange) {
|
|
|
const { column, change } = col;
|
|
|
if (!column && change && _.isArray(change)) continue;
|
|
|
const val = _.get(d, column);
|
|
|
if (!val) continue;
|
|
|
- const r = change.find((f) => f.key === val);
|
|
|
+ const r = change.find(f => f.key === val);
|
|
|
if (!r) continue;
|
|
|
const { value } = r;
|
|
|
d[column] = value;
|
|
@@ -458,7 +460,7 @@ class SchoolService extends CrudService {
|
|
|
return arr;
|
|
|
}
|
|
|
|
|
|
- // 导出学校名单 TODO:bug
|
|
|
+ // 导出学校名单
|
|
|
async exportSchool({ trainplanId }) {
|
|
|
// 批次期次都在这里面
|
|
|
const trainplan = await this.tmodel.find({ _id: trainplanId });
|
|
@@ -486,11 +488,13 @@ class SchoolService extends CrudService {
|
|
|
|
|
|
// 构建 workbook 对象
|
|
|
const nowDate = new Date().getTime();
|
|
|
- // TODO: 地址有问题,需要引用config变量
|
|
|
- const path = 'D:\\wwwroot\\service\\service-file\\upload\\train\\' + nowDate + '.xlsx';
|
|
|
- const respath = 'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx';
|
|
|
+ // 地址有问题,需要引用config变量
|
|
|
+ const { repos_root_path: rp } = this.ctx.app.config.cdn;
|
|
|
+ const { baseUrl: bu } = this.ctx.app.config;
|
|
|
+ const path = `${rp}${sep}train${sep}${nowDate}.xlsx`;
|
|
|
+ const respath = `${bu}/files/train/${nowDate}.xlsx`;
|
|
|
const wb = {
|
|
|
- SheetNames: ['sheet0'],
|
|
|
+ SheetNames: [ 'sheet0' ],
|
|
|
Sheets: { sheet0: Object.assign({}, output, { '!ref': ref }) },
|
|
|
};
|
|
|
// 导出 Excel
|
|
@@ -591,9 +595,9 @@ class SchoolService extends CrudService {
|
|
|
code = code.split('');
|
|
|
// ∑(ai×Wi)(mod 11)
|
|
|
// 加权因子
|
|
|
- const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
|
|
|
+ const factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
|
|
|
// 校验位
|
|
|
- const parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
|
|
|
+ const parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
|
|
|
let sum = 0;
|
|
|
let ai = 0;
|
|
|
let wi = 0;
|