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

483 lines
22 KiB
JavaScript

'use strict';
const Service = require('../core/base_service');
const { Op } = require('sequelize');
const moment = require('moment');
const fileName = "Application";
class ApplicationService extends Service {
// 管理系统新增用户报名信息
async addApplication(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: 0,
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: 0,
status: 0,
created_at: new Date()
};
errorMark = true;
await ctx.model.Importerror.create(errorForm);
}
});
const result = await ctx.model.Application.bulkCreate(form, { ignoreDuplicates: true });
return result.length == 0 ? false : errorMark ? { code: 300, data: result } : { code: 200, data: result };
} catch (error) {
const blockName = fileName + "--" + "addApplication";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 管理系统查看用户报名信息
async showApplication(uid, body) {
const { ctx } = this;
try {
let { offset, limit, searchOptions, sortOptions } = body;
offset = (offset - 1) * limit;
let attributes = ['user_uid', 'exam_uid', 'source', 'status', 'pay_status', 'result', 'created_at'],
where = {}, order = [], dbOptions,
joinSubstring = [],
substring = ['user_uid', 'exam_uid', 'source', 'result'],
key = ['status', 'pay_status'],
between = ['created_at'];
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)) {
let targetKeyArr = [];
where[element] = targetKeyArr.length > 0 ? targetKeyArr : '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_start', 'application_end'],
examinationWhere = {},
examinationOrder = [],
examinationJoinSubstring = [],
examinationSubstring = ['exam_name'],
examinationKey = [],
examinationBetween = ['application_start', 'application_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', 'application_start', 'application_end'],
where: examinationWhere,
order: examinationOrder
}
];
ctx.model.Application.belongsTo(ctx.model.Examinee, { foreignKey: 'user_uid', targetKey: 'uid' });
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 + "--" + "showApplication";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 管理系统编辑用户报名信息
async editApplication(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, source, status, pay_status, result } = requestMsg;
where = { user_uid, exam_uid, source, status, pay_status, result };
} else if (modelType == "multiple") {
let user_uid = [], exam_uid = [], source = [], status = [], pay_status = [], result = [];
for (const key in requestMsg) {
requestMsg[key].forEach(element => {
user_uid.push(element.user_uid);
exam_uid.push(element.exam_uid);
source.push(element.source);
status.push(element.status);
pay_status.push(element.pay_status);
result.push(element.result);
});
}
where = { user_uid, exam_uid, source, status, pay_status, result };
}
const resultList = await ctx.model.Application.update(updateMsg, { where });
return resultList[0] == 0 ? false : resultList;
} catch (error) {
const blockName = fileName + "--" + "editApplication";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 管理系统删除用户报名信息
async deleteApplication(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, source, status, pay_status, result } = requestMsg;
where = { user_uid, exam_uid, source, status, pay_status, result };
} else if (modelType == "multiple") {
let user_uid = [], exam_uid = [], source = [], status = [], pay_status = [], result = [];
for (const key in requestMsg) {
requestMsg[key].forEach(element => {
user_uid.push(element.user_uid);
exam_uid.push(element.exam_uid);
source.push(element.source);
status.push(element.status);
pay_status.push(element.pay_status);
result.push(element.result);
});
}
where = { user_uid, exam_uid, source, status, pay_status, result };
}
const resultList = await ctx.model.Application.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 + "--" + "deleteApplication";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 管理系统导出考生报名信息数据
async downloadApplicationData(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', 'source', 'status', 'pay_status', 'result', 'created_at'],
where = {}, order = [], dbOptions,
joinSubstring = [],
substring = ['user_uid', 'exam_uid', 'source', 'result'],
key = ['status', 'pay_status'],
between = ['created_at'];
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)) {
let targetKeyArr = [];
where[element] = targetKeyArr.length > 0 ? targetKeyArr : '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_start', 'application_end'],
examinationWhere = {},
examinationOrder = [],
examinationJoinSubstring = [],
examinationSubstring = ['exam_name'],
examinationKey = [],
examinationBetween = ['application_start', 'application_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', 'application_start', 'application_end'],
where: examinationWhere,
order: examinationOrder
}
];
ctx.model.Application.belongsTo(ctx.model.Examinee, { foreignKey: 'user_uid', targetKey: 'uid' });
ctx.model.Application.belongsTo(ctx.model.Examination, { foreignKey: 'exam_uid', targetKey: 'uid' });
dbOptions = { attributes, where, order, include };
const result = await ctx.model.Application.findAll(dbOptions);
if (result.length == 0) return { code: false };
let data = [];
const statusOptions = [
{
value: "0",
label: "审核中"
},
{
value: "1",
label: "报名成功"
},
{
value: "2",
label: "报名失败"
},
{
value: "3",
label: "取消报名"
},
];
const payStatusOptions = [
{
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;
}
});
payStatusOptions.forEach((item) => {
if (item.value == element.dataValues["pay_status"]) {
element.dataValues["pay_status"] = 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.created_at = moment
.utc(element.dataValues.created_at)
.add(8, "hours")
.format("YYYY年MM月DD日HH时mm分ss秒");
element.dataValues.application_start = moment
.utc(element.dataValues.examination.dataValues.application_start)
.add(8, "hours")
.format("YYYY年MM月DD日HH时mm分ss秒");
element.dataValues.application_end = moment
.utc(element.dataValues.examination.dataValues.application_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,
source: element.dataValues.source,
status: element.dataValues.status,
pay_status: element.dataValues.pay_status,
result: element.dataValues.result,
created_at: element.dataValues.created_at,
application_start: element.dataValues.application_start,
application_end: element.dataValues.application_end,
}
data.push(elementClone);
});
return { code: true, data };
} catch (error) {
const blockName = fileName + "--" + "downloadApplicationData";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = ApplicationService;