北斗应用技术培训考试管理系统
This commit is contained in:
493
app/service/Download.js
Normal file
493
app/service/Download.js
Normal file
@@ -0,0 +1,493 @@
|
||||
'use strict';
|
||||
|
||||
const Service = require('../core/base_service');
|
||||
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
const fileName = "Download";
|
||||
|
||||
class DownloadService extends Service {
|
||||
// 导出小鹅通导入用户
|
||||
async importXiaoeTech(uid, searchMsg, importForm) {
|
||||
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', 'result'],
|
||||
where = {}, order = [], dbOptions,
|
||||
joinSubstring = [],
|
||||
substring = ['exam_uid', 'result'],
|
||||
key = ['status'],
|
||||
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)) {
|
||||
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 include = [
|
||||
{
|
||||
model: ctx.model.Examinee,
|
||||
attributes: ['user_name', 'identity_card', 'workplace', 'address', 'phone', 'email'],
|
||||
}
|
||||
];
|
||||
ctx.model.Application.belongsTo(ctx.model.Examinee, { foreignKey: 'user_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 = [];
|
||||
result.forEach(element => {
|
||||
let identity_card = element.dataValues.examinee.dataValues.identity_card,
|
||||
gender = parseInt(identity_card[16], 10) % 2 == 0 ? '女' : '男',
|
||||
birthday = new Date(identity_card.substring(6, 14).substring(0, 4), identity_card.substring(6, 14).substring(4, 6) - 1, identity_card.substring(6, 14).substring(6, 8)),
|
||||
age = new Date().getFullYear() - birthday.getFullYear();
|
||||
const m = new Date().getMonth() - birthday.getMonth();
|
||||
if (m < 0 || (m === 0 && new Date().getDate() < birthday.getDate())) {
|
||||
age--;
|
||||
}
|
||||
data.push({
|
||||
phone: element.dataValues.examinee.dataValues.phone,
|
||||
user_name: element.dataValues.examinee.dataValues.user_name,
|
||||
remark: "",
|
||||
gender,
|
||||
age,
|
||||
birthday,
|
||||
province: "",
|
||||
city: "",
|
||||
address: element.dataValues.examinee.dataValues.address,
|
||||
email: element.dataValues.examinee.dataValues.email,
|
||||
industry: "",
|
||||
company: element.dataValues.examinee.dataValues.workplace,
|
||||
position: "",
|
||||
description: "",
|
||||
tags: "",
|
||||
});
|
||||
});
|
||||
return { code: true, data };
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "importXiaoeTech";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 导出小鹅通开课学员
|
||||
async commencementXiaoeTech(uid, searchMsg, importForm) {
|
||||
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', 'result'],
|
||||
where = {}, order = [], dbOptions,
|
||||
joinSubstring = [],
|
||||
substring = ['exam_uid', 'result'],
|
||||
key = ['status'],
|
||||
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)) {
|
||||
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 include = [
|
||||
{
|
||||
model: ctx.model.Examinee,
|
||||
attributes: ['phone'],
|
||||
}
|
||||
];
|
||||
ctx.model.Application.belongsTo(ctx.model.Examinee, { foreignKey: 'user_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 = [];
|
||||
result.forEach(element => {
|
||||
data.push({
|
||||
phone: element.dataValues.examinee.dataValues.phone,
|
||||
expired_at: importForm.expired_at.replace(/_/g, ' ').replace(/:/g, ':'),
|
||||
});
|
||||
});
|
||||
return { code: true, data };
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "commencementXiaoeTech";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 导出批量报名导入 excel 表
|
||||
async importApplicationData(uid, searchMsg, importForm) {
|
||||
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_name', 'identity_card', 'workplace', 'created_at'],
|
||||
where = {}, order = [], dbOptions,
|
||||
joinSubstring = [],
|
||||
substring = ['workplace'],
|
||||
key = [],
|
||||
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']);
|
||||
}
|
||||
});
|
||||
dbOptions = { attributes, where, order };
|
||||
const result = await ctx.model.Examinee.findAll(dbOptions);
|
||||
if (result.length == 0) return { code: false };
|
||||
let data = [];
|
||||
result.forEach(element => {
|
||||
data.push({
|
||||
identity_card: element.dataValues.identity_card,
|
||||
user_name: element.dataValues.user_name,
|
||||
exam_uid: importForm.exam_uid,
|
||||
source: importForm.source,
|
||||
status: "1",
|
||||
pay_status: "1",
|
||||
result: importForm.result
|
||||
});
|
||||
});
|
||||
return { code: true, data };
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "importApplicationData";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 导出考试星学员导入 excel 表
|
||||
async importExamStars(uid, searchMsg, importForm) {
|
||||
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 = ['exam_uid', 'status'],
|
||||
where = {}, order = [], dbOptions,
|
||||
joinSubstring = [],
|
||||
substring = ['exam_uid'],
|
||||
key = ['status'],
|
||||
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)) {
|
||||
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 include = [
|
||||
{
|
||||
model: ctx.model.Examinee,
|
||||
attributes: ['user_name', 'identity_card'],
|
||||
}
|
||||
];
|
||||
ctx.model.Application.belongsTo(ctx.model.Examinee, { foreignKey: 'user_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 = [];
|
||||
result.forEach(element => {
|
||||
data.push({
|
||||
account: element.dataValues.examinee.dataValues.identity_card,
|
||||
password: element.dataValues.examinee.dataValues.identity_card.substr(-6),
|
||||
name: element.dataValues.examinee.dataValues.user_name,
|
||||
department: "",
|
||||
});
|
||||
});
|
||||
return { code: true, data };
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "importExamStars";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 导出培训证书发放用户导入 excel 表
|
||||
async importIssuance(uid, searchMsg, importForm) {
|
||||
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 = ['exam_uid', 'status'],
|
||||
where = {}, order = [], dbOptions,
|
||||
joinSubstring = [],
|
||||
substring = ['exam_uid'],
|
||||
key = ['status'],
|
||||
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)) {
|
||||
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 include = [
|
||||
{
|
||||
model: ctx.model.Examinee,
|
||||
attributes: ['user_name', 'identity_card', 'workplace', 'address', 'phone'],
|
||||
}
|
||||
];
|
||||
ctx.model.Application.belongsTo(ctx.model.Examinee, { foreignKey: 'user_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 = [];
|
||||
result.forEach((element, index) => {
|
||||
data.push({
|
||||
id: index + 1,
|
||||
user_name: element.dataValues.examinee.dataValues.user_name,
|
||||
identity_card: element.dataValues.examinee.dataValues.identity_card,
|
||||
workplace: element.dataValues.examinee.dataValues.workplace,
|
||||
address: element.dataValues.examinee.dataValues.address,
|
||||
phone: element.dataValues.examinee.dataValues.phone,
|
||||
start: "",
|
||||
end: "",
|
||||
place: "",
|
||||
way: "",
|
||||
score: "",
|
||||
certificate_name: "",
|
||||
level: "",
|
||||
});
|
||||
});
|
||||
return { code: true, data };
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "importIssuance";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 导出培训证书发放用户照片文件夹
|
||||
async importUserPhoto(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 = ['exam_uid', 'status'],
|
||||
where = {}, order = [], dbOptions,
|
||||
joinSubstring = [],
|
||||
substring = ['exam_uid'],
|
||||
key = ['status'],
|
||||
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)) {
|
||||
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 include = [
|
||||
{
|
||||
model: ctx.model.Examinee,
|
||||
attributes: ['identity_card'],
|
||||
}
|
||||
];
|
||||
ctx.model.Application.belongsTo(ctx.model.Examinee, { foreignKey: 'user_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 = [];
|
||||
result.forEach(element => {
|
||||
data.push({
|
||||
identity_card: element.dataValues.examinee.dataValues.identity_card,
|
||||
});
|
||||
});
|
||||
return { code: true, data };
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "importUserPhoto";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 导出考试成绩导入 excel 表
|
||||
async importGradesData(uid, formClone, dataType, passingScore, gradesType) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const user = await ctx.model.Manager.findOne({ where: { uid } });
|
||||
if (!user || user.dataValues.role < 1) return false;
|
||||
const data = formClone.map(item => ({
|
||||
user_name: item.user_name,
|
||||
identity_card: item.identity_card,
|
||||
exam_uid: "",
|
||||
status: "",
|
||||
grades: item.grades,
|
||||
pass_type: "",
|
||||
}));
|
||||
const identityCards = data.map(item => item.identity_card);
|
||||
const userNames = data.map(item => item.user_name);
|
||||
const examineeUid = await ctx.model.Examinee.findAll({ attributes: ['uid', 'identity_card', 'user_name'], where: { identity_card: identityCards, user_name: userNames } });
|
||||
if (examineeUid.length == 0) return { code: false };
|
||||
const uidMap = new Map();
|
||||
const userUids = [];
|
||||
examineeUid.forEach(element => {
|
||||
uidMap.set(`${element.dataValues.identity_card}_${element.dataValues.user_name}`, element.dataValues.uid);
|
||||
userUids.push(element.dataValues.uid);
|
||||
});
|
||||
data.forEach(item => {
|
||||
const key = `${item.identity_card}_${item.user_name}`;
|
||||
if (uidMap.has(key)) {
|
||||
item.user_uid = uidMap.get(key);
|
||||
}
|
||||
});
|
||||
const applicationUid = await ctx.model.Application.findAll({ attributes: ['user_uid', 'exam_uid'], where: { user_uid: userUids, status: 1 }, order: [['created_at', 'DESC']], limit: userUids.length });
|
||||
if (applicationUid.length == 0) return { code: false };
|
||||
const applicationUidMap = new Map(applicationUid.map(item => [item.dataValues.user_uid, item.dataValues.exam_uid]));
|
||||
data.forEach(item => {
|
||||
if (applicationUidMap.has(item.user_uid)) {
|
||||
item.exam_uid = applicationUidMap.get(item.user_uid);
|
||||
}
|
||||
});
|
||||
const examUids = [...new Set(applicationUid.map(item => item.dataValues.exam_uid))];
|
||||
let include = [
|
||||
{
|
||||
model: ctx.model.Examinee,
|
||||
attributes: ['user_name', 'identity_card'],
|
||||
},
|
||||
];
|
||||
if (gradesType == '0') {
|
||||
ctx.model.Application.belongsTo(ctx.model.Examinee, { foreignKey: 'user_uid', targetKey: 'uid' });
|
||||
const application = await ctx.model.Application.findAll({ attributes: ['user_uid', 'exam_uid'], where: { user_uid: { [Op.notIn]: userUids }, exam_uid: examUids, status: 1 }, include });
|
||||
application.forEach(element => {
|
||||
data.push({
|
||||
identity_card: element.dataValues.examinee.dataValues.identity_card,
|
||||
user_name: element.dataValues.examinee.dataValues.user_name,
|
||||
user_uid: element.dataValues.user_uid,
|
||||
exam_uid: element.dataValues.exam_uid,
|
||||
grades: null,
|
||||
});
|
||||
});
|
||||
}
|
||||
data.forEach(item => {
|
||||
delete item.user_uid;
|
||||
item.status = dataType == '0' ? 1 : (item.grades == null ? 0 : 1);
|
||||
item.pass_type = item.grades == null
|
||||
? (dataType == '0' ? 0 : 2)
|
||||
: (dataType == '1' && item.grades < passingScore ? 0 : 1);
|
||||
});
|
||||
return { code: true, data };
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "importGradesData";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DownloadService;
|
||||
Reference in New Issue
Block a user