Files
bd-training-exam-manager/app/service/Grades.js

468 lines
21 KiB
JavaScript

'use strict';
const Service = require('../core/base_service');
const { Op } = require('sequelize');
const moment = require('moment');
const fileName = "Grades";
class GradesService extends Service {
// 管理系统新增用户考试结果
async addGrades(userMsgObj, form) {
const { ctx } = this;
try {
let attributes = ['uid', 'user_name', 'identity_card'],
where = userMsgObj,
dbOptions = { attributes, where };
const userUid = await ctx.model.Examinee.findAll(dbOptions);
if(userUid.length == 0) {
let errorForm = [];
form.forEach(item => {
errorForm.push({
user_name: item.user_name,
identity_card: item.identity_card,
exam_uid: item.exam_uid,
type: 1,
status: 0,
created_at: new Date()
});
});
await ctx.model.Importerror.bulkCreate(errorForm);
return { code: 400 };
};
let errorMark = false;
form.forEach(async item => {
item.user_uid = 'none';
userUid.forEach(element => {
if (item.user_name == element.dataValues.user_name && item.identity_card == element.dataValues.identity_card) {
item.user_uid = element.dataValues.uid;
}
});
delete item.user_name;
delete item.identity_card;
if (item.user_uid == 'none') {
let errorForm = {
user_name: item.user_name,
identity_card: item.identity_card,
exam_uid: item.exam_uid,
type: 1,
status: 0,
created_at: new Date()
};
errorMark = true;
await ctx.model.Importerror.create(errorForm);
}
});
const result = await ctx.model.Grades.bulkCreate(form, { ignoreDuplicates: true });
return result.length == 0 ? false : errorMark ? { code: 300, data: result } : { code: 200, data: result };
} catch (error) {
const blockName = fileName + "--" + "addGrades";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 管理系统查看用户考试结果
async showGrades(uid, body) {
const { ctx } = this;
try {
let { offset, limit, searchOptions, sortOptions } = body;
offset = (offset - 1) * limit;
let attributes = ['user_uid', 'exam_uid', 'status', 'grades', 'pass_type'],
where = {}, order = [], dbOptions,
joinSubstring = ['grades'],
substring = ['user_uid', 'exam_uid'],
key = ['status', 'pass_type'],
between = [];
attributes.forEach(element => {
if (searchOptions[element]) {
if (searchOptions[element] == '*空') {
if (joinSubstring.includes(element)) {
where[element] = null;
} else {
where[element] = [null, ''];
}
} else if (joinSubstring.includes(element)) {
where[element] = { [Op.substring]: searchOptions[element] };
} else if (substring.includes(element)) {
where[element] = { [Op.substring]: searchOptions[element] };
} else if (key.includes(element)) {
where[element] = searchOptions[element];
} else if (between.includes(element)) {
where[element] = { [Op.between]: [searchOptions[element][0], searchOptions[element][1]] };
}
}
if (sortOptions[element] == true) {
order.push([element, 'ASC']);
} else if (sortOptions[element] == false) {
order.push([element, 'DESC']);
}
});
let examination = ['exam_name', 'exam_start', 'exam_end'],
examinationWhere = {},
examinationOrder = [],
examinationJoinSubstring = [],
examinationSubstring = ['exam_name'],
examinationKey = [],
examinationBetween = ['exam_start', 'exam_end'];
examination.forEach(item => {
if (searchOptions[item]) {
if (searchOptions[item] == '*空') {
if (examinationJoinSubstring.includes(item)) {
examinationWhere[item] = null;
} else {
examinationWhere[item] = [null, ''];
}
} else if (examinationSubstring.includes(item)) {
examinationWhere[item] = { [Op.substring]: searchOptions[item] };
} else if (examinationKey.includes(item)) {
examinationWhere[item] = searchOptions[item];
} else if (examinationBetween.includes(item)) {
examinationWhere[item] = { [Op.between]: [searchOptions[item][0], searchOptions[item][1]] };
}
}
if (sortOptions[item] == true) {
examinationOrder.push([item, 'ASC']);
} else if (sortOptions[item] == false) {
examinationOrder.push([item, 'DESC']);
}
});
let examinee = ['user_name', 'identity_card', 'workplace', 'phone'],
examineeWhere = {},
examineeOrder = [],
examineeJoinSubstring = [],
examineeSubstring = ['user_name', 'identity_card', 'workplace', 'phone'],
examineeKey = [],
examineeBetween = [];
examinee.forEach(item => {
if (searchOptions[item]) {
if (searchOptions[item] == '*空') {
if (examineeJoinSubstring.includes(item)) {
examineeWhere[item] = null;
} else {
examineeWhere[item] = [null, ''];
}
} else if (examineeSubstring.includes(item)) {
examineeWhere[item] = { [Op.substring]: searchOptions[item] };
} else if (examineeKey.includes(item)) {
examineeWhere[item] = searchOptions[item];
} else if (examineeBetween.includes(item)) {
examineeWhere[item] = { [Op.between]: [searchOptions[item][0], searchOptions[item][1]] };
}
}
if (sortOptions[item] == true) {
examineeOrder.push([item, 'ASC']);
} else if (sortOptions[item] == false) {
examineeOrder.push([item, 'DESC']);
}
});
let include = [
{
model: ctx.model.Examinee,
attributes: ['user_name', 'identity_card', 'workplace', 'phone'],
where: examineeWhere,
order: examineeOrder
},
{
model: ctx.model.Examination,
attributes: ['exam_name', 'exam_start', 'exam_end'],
where: examinationWhere,
order: examinationOrder
}
];
ctx.model.Grades.belongsTo(ctx.model.Examinee, { foreignKey: 'user_uid', targetKey: 'uid' });
ctx.model.Grades.belongsTo(ctx.model.Examination, { foreignKey: 'exam_uid', targetKey: 'uid' });
dbOptions = { attributes, offset, limit, where, order, include };
const result = await ctx.model.Grades.findAndCountAll(dbOptions);
return result.rows.length == 0 ? false : result;
} catch (error) {
const blockName = fileName + "--" + "showGrades";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 管理系统编辑用户考试结果
async editGrades(uid, modelType, updateMsg, requestMsg) {
const { ctx } = this;
try {
const result = await ctx.model.Manager.findOne({ where: { uid } });
if (!result || result.dataValues.role < 1) return false;
let where = {};
if (modelType == "single") {
const { user_uid, exam_uid, status, grades, pass_type } = requestMsg;
where = { user_uid, exam_uid, status, grades, pass_type };
} else if (modelType == "multiple") {
let user_uid = [], exam_uid = [], status = [], pass_type = [];
for (const key in requestMsg) {
requestMsg[key].forEach(element => {
user_uid.push(element.user_uid);
exam_uid.push(element.exam_uid);
status.push(element.status);
pass_type.push(element.pass_type);
});
}
where = { user_uid, exam_uid, status, pass_type };
}
const resultList = await ctx.model.Grades.update(updateMsg, { where });
return resultList[0] == 0 ? false : resultList;
} catch (error) {
const blockName = fileName + "--" + "editGrades";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 管理系统删除考生考试成绩
async deleteGrades(uid, modelType, password, requestMsg) {
const { ctx } = this;
const systemMonitoring = {
operate: 2,
operate_msg: '删除考生考试成绩信息',
type: 0,
status: 0,
affect_entries: 0,
user: uid,
created_at: new Date()
};
try {
const result = await ctx.model.Manager.findOne({ where: { uid } });
if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt)) || result.dataValues.role < 2) {
if (!result) systemMonitoring.status = 0;
else if (result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) systemMonitoring.status = 1;
else if (result.dataValues.role < 2) systemMonitoring.status = 2;
await ctx.model.Monitoring.create(systemMonitoring);
return false;
};
let where = {};
if (modelType == "single") {
const { user_uid, exam_uid, status, grades, pass_type } = requestMsg;
where = { user_uid, exam_uid, status, grades, pass_type };
} else if (modelType == "multiple") {
let user_uid = [], exam_uid = [], status = [], grades = [], pass_type = [];
for (const key in requestMsg) {
requestMsg[key].forEach(element => {
user_uid.push(element.user_uid);
exam_uid.push(element.exam_uid);
status.push(element.status);
grades.push(element.grades);
pass_type.push(element.pass_type);
});
}
where = { user_uid, exam_uid, status, grades, pass_type };
}
const resultList = await ctx.model.Grades.destroy({ where });
systemMonitoring.status = resultList == 0 ? 3 : 4;
systemMonitoring.affect_entries = resultList;
await ctx.model.Monitoring.create(systemMonitoring);
return resultList == 0 ? false : true;
} catch (error) {
systemMonitoring.status = 5;
await ctx.model.Monitoring.create(systemMonitoring);
const blockName = fileName + "--" + "deleteGrades";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 管理系统导出考生考试结果数据
async downloadGradesData(uid, searchMsg) {
const { ctx } = this;
try {
const user = await ctx.model.Manager.findOne({ where: { uid } });
if (!user || user.dataValues.role < 1) return false;
let { searchOptions, sortOptions } = searchMsg;
let attributes = ['user_uid', 'exam_uid', 'status', 'grades', 'pass_type'],
where = {}, order = [], dbOptions,
joinSubstring = ['grades'],
substring = ['user_uid', 'exam_uid'],
key = ['status', 'pass_type'],
between = [];
attributes.forEach(element => {
if (searchOptions[element]) {
if (searchOptions[element] == '*空') {
if (joinSubstring.includes(element)) {
where[element] = null;
} else {
where[element] = [null, ''];
}
} else if (joinSubstring.includes(element)) {
where[element] = { [Op.substring]: searchOptions[element] };
} else if (substring.includes(element)) {
where[element] = { [Op.substring]: searchOptions[element] };
} else if (key.includes(element)) {
where[element] = searchOptions[element];
} else if (between.includes(element)) {
where[element] = { [Op.between]: [searchOptions[element][0], searchOptions[element][1]] };
}
}
if (sortOptions[element] == true) {
order.push([element, 'ASC']);
} else if (sortOptions[element] == false) {
order.push([element, 'DESC']);
}
});
let examination = ['exam_name', 'exam_start', 'exam_end'],
examinationWhere = {},
examinationOrder = [],
examinationJoinSubstring = [],
examinationSubstring = ['exam_name'],
examinationKey = [],
examinationBetween = ['exam_start', 'exam_end'];
examination.forEach(item => {
if (searchOptions[item]) {
if (searchOptions[item] == '*空') {
if (examinationJoinSubstring.includes(item)) {
examinationWhere[item] = null;
} else {
examinationWhere[item] = [null, ''];
}
} else if (examinationSubstring.includes(item)) {
examinationWhere[item] = { [Op.substring]: searchOptions[item] };
} else if (examinationKey.includes(item)) {
examinationWhere[item] = searchOptions[item];
} else if (examinationBetween.includes(item)) {
examinationWhere[item] = { [Op.between]: [searchOptions[item][0], searchOptions[item][1]] };
}
}
if (sortOptions[item] == true) {
examinationOrder.push([item, 'ASC']);
} else if (sortOptions[item] == false) {
examinationOrder.push([item, 'DESC']);
}
});
let examinee = ['user_name', 'identity_card', 'workplace', 'phone'],
examineeWhere = {},
examineeOrder = [],
examineeJoinSubstring = [],
examineeSubstring = ['user_name', 'identity_card', 'workplace', 'phone'],
examineeKey = [],
examineeBetween = [];
examinee.forEach(item => {
if (searchOptions[item]) {
if (searchOptions[item] == '*空') {
if (examineeJoinSubstring.includes(item)) {
examineeWhere[item] = null;
} else {
examineeWhere[item] = [null, ''];
}
} else if (examineeSubstring.includes(item)) {
examineeWhere[item] = { [Op.substring]: searchOptions[item] };
} else if (examineeKey.includes(item)) {
examineeWhere[item] = searchOptions[item];
} else if (examineeBetween.includes(item)) {
examineeWhere[item] = { [Op.between]: [searchOptions[item][0], searchOptions[item][1]] };
}
}
if (sortOptions[item] == true) {
examineeOrder.push([item, 'ASC']);
} else if (sortOptions[item] == false) {
examineeOrder.push([item, 'DESC']);
}
});
let include = [
{
model: ctx.model.Examinee,
attributes: ['user_name', 'identity_card', 'workplace', 'phone'],
where: examineeWhere,
order: examineeOrder
},
{
model: ctx.model.Examination,
attributes: ['exam_name', 'exam_start', 'exam_end'],
where: examinationWhere,
order: examinationOrder
}
];
ctx.model.Grades.belongsTo(ctx.model.Examinee, { foreignKey: 'user_uid', targetKey: 'uid' });
ctx.model.Grades.belongsTo(ctx.model.Examination, { foreignKey: 'exam_uid', targetKey: 'uid' });
dbOptions = { attributes, where, order, include };
const result = await ctx.model.Grades.findAll(dbOptions);
if (result.length == 0) return { code: false };
let data = [];
const statusOptions = [
{
value: "0",
label: "旷考"
},
{
value: "1",
label: "正常参与"
},
{
value: "2",
label: "考试作弊"
},
];
const passTypeOptions = [
{
value: "0",
label: "成绩不及格"
},
{
value: "1",
label: "成绩及格"
},
{
value: "2",
label: "无成绩"
},
];
result.forEach(element => {
statusOptions.forEach((item) => {
if (item.value == element.dataValues["status"]) {
element.dataValues["status"] = item.label;
}
});
passTypeOptions.forEach((item) => {
if (item.value == element.dataValues["pass_type"]) {
element.dataValues["pass_type"] = item.label;
}
});
element.dataValues.user_name = element.dataValues.examinee.dataValues.user_name;
element.dataValues.identity_card = element.dataValues.examinee.dataValues.identity_card;
element.dataValues.exam_name = element.dataValues.examination.dataValues.exam_name;
element.dataValues.exam_start = moment
.utc(element.dataValues.examination.dataValues.exam_start)
.add(8, "hours")
.format("YYYY年MM月DD日HH时mm分ss秒");
element.dataValues.exam_end = moment
.utc(element.dataValues.examination.dataValues.exam_end)
.add(8, "hours")
.format("YYYY年MM月DD日HH时mm分ss秒");
delete element.dataValues.examinee;
delete element.dataValues.examination;
let elementClone = {
user_uid: element.dataValues.user_uid,
user_name: element.dataValues.user_name,
identity_card: element.dataValues.identity_card,
exam_uid: element.dataValues.exam_uid,
exam_name: element.dataValues.exam_name,
status: element.dataValues.status,
grades: element.dataValues.grades,
pass_type: element.dataValues.pass_type,
exam_start: element.dataValues.exam_start,
exam_end: element.dataValues.exam_end,
}
data.push(elementClone);
});
return { code: true, data };
} catch (error) {
const blockName = fileName + "--" + "downloadGradesData";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = GradesService;