Files
bd-training-exam/app/service/Exam.js

279 lines
13 KiB
JavaScript

'use strict';
const Service = require('../core/base_service');
const { Op } = require('sequelize');
const fileName = "Exam";
class ExamService extends Service {
// 用户报名接口
async application(uid, form) {
const { ctx } = this;
try {
let where = { uid }
const user = await ctx.model.Examinee.findOne({ where });
if (!user) return false;
where = { user_uid: uid, exam_uid: form.exam_uid }
const application = await ctx.model.Application.findOne({ where });
let result;
if (!application) {
result = await ctx.model.Application.create(form);
return result ? true : false;
} else {
result = await ctx.model.Application.update(form, { where });
return result[0] != 0;
};
} catch (error) {
const blockName = fileName + "--" + "application";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 用户取消报名接口
async cancelApplication(uid, exam_uid) {
const { ctx } = this;
try {
const result = await ctx.model.Application.update({ status: 3, result: "用户已取消报名" }, { where: { user_uid: uid, exam_uid } });
return result[0] != 0;
} catch (error) {
const blockName = fileName + "--" + "cancelApplication";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 用户查看所有考试接口
async examList(uid, body) {
const { ctx } = this;
try {
let { offset, limit, searchOptions, sortOptions } = body;
offset = (offset - 1) * limit;
let attributes = ['uid', 'exam_name', 'status', 'application_end', 'exam_start', 'exam_end'],
where = {}, order = [], dbOptions, joinSubstring = [],
substring = ['uid', 'exam_name'], key = ['status'], between = ['application_end', 'exam_start', 'exam_end'];
attributes.forEach(element => {
if (searchOptions[element]) {
if (searchOptions[element] == '*空') {
if (joinSubstring.includes(element)) {
where[element] = null;
} else {
where[element] = [null, ''];
}
} 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']);
}
});
dbOptions = { attributes, offset, limit, where, order };
const result = await ctx.model.Examination.findAndCountAll(dbOptions);
if (result.rows.length == 0) return false;
const examUids = result.rows.map(exam => exam.dataValues.uid);
const applicationStatus = await ctx.model.Application.findAll({ where: { user_uid: uid, exam_uid: { [Op.in]: examUids } } });
const statusMap = new Map();
applicationStatus.forEach(item => {
statusMap.set(item.dataValues.exam_uid, item.dataValues.status);
});
result.rows.forEach(exam => {
statusMap.get(exam.dataValues.uid) == undefined ?
exam.dataValues.applicationStatus = 4 :
exam.dataValues.applicationStatus = statusMap.get(exam.dataValues.uid);
});
let passExamArr = result.rows.map(exam => exam.dataValues.status == 0 ? exam.dataValues.uid : '').filter(item => item !== '')
const applicationCheck = await ctx.model.Application.findAll({ attributes: ['exam_uid', 'status'], where: { user_uid: uid, exam_uid: passExamArr, status: { [Op.in]: [0, 1] } } });
let applicationCheckSwitch = false;
result.rows.forEach((item) => {
item.dataValues.applicationCheck = 0;
item.dataValues.applicationCheckSwitch = false;
applicationCheck.forEach(element => {
if (element.dataValues.exam_uid == item.dataValues.uid) {
if (element.dataValues.status == 0 || element.dataValues.status == 1) {
item.dataValues.applicationCheck = 1;
applicationCheckSwitch = true;
}
}
})
});
if (applicationCheckSwitch) {
result.rows.forEach((items) => {
items.dataValues.applicationCheckSwitch = true;
});
}
return result;
} catch (error) {
const blockName = fileName + "--" + "examList";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 用户查看考试结果接口
async gradesList(user_uid, body) {
const { ctx } = this;
try {
let { offset, limit, searchOptions, sortOptions } = body;
offset = (offset - 1) * limit;
let attributes = ['exam_uid', 'status', 'grades', 'pass_type'],
where = { user_uid }, order = [], dbOptions, joinSubstring = [],
substring = ['exam_uid'], key = ['status', 'grades', '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 (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'],
examinationWhere = {},
examinationOrder = [],
examinationJoinSubstring = [],
examinationSubstring = ['exam_name'],
examinationKey = [],
examinationBetween = [];
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 include = [
{
model: ctx.model.Examination,
attributes: ['exam_name', 'status'],
where: examinationWhere,
order: examinationOrder
}
];
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 + "--" + "gradesList";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 用户查看报名历史接口
async applicationList(user_uid, body) {
const { ctx } = this;
try {
let { offset, limit, searchOptions, sortOptions } = body;
offset = (offset - 1) * limit;
let attributes = ['exam_uid', 'source', 'status', 'pay_status', 'result', 'created_at'],
where = { user_uid }, order = [], dbOptions, joinSubstring = [],
substring = ['exam_uid', 'source', 'result'], key = ['status', 'pay_status'], between = ['created_at', 'application_end', 'exam_start', 'exam_end'];
attributes.forEach(element => {
if (searchOptions[element]) {
if (searchOptions[element] == '*空') {
if (joinSubstring.includes(element)) {
where[element] = null;
} else {
where[element] = [null, ''];
}
} 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', 'application_end', 'exam_start', 'exam_end'],
examinationWhere = {},
examinationOrder = [],
examinationJoinSubstring = [],
examinationSubstring = ['exam_name'],
examinationKey = [],
examinationBetween = ['application_end', '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 include = [
{
model: ctx.model.Examination,
attributes: ['exam_name', 'status', 'application_end', 'exam_start', 'exam_end'],
where: examinationWhere,
order: examinationOrder
}
];
ctx.model.Application.belongsTo(ctx.model.Examination, { foreignKey: 'exam_uid', targetKey: 'uid' });
dbOptions = { attributes, offset, limit, where, order, include };
const result = await ctx.model.Application.findAndCountAll(dbOptions);
return result.rows.length == 0 ? false : result;
} catch (error) {
const blockName = fileName + "--" + "applicationList";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = ExamService;