295 lines
13 KiB
JavaScript
295 lines
13 KiB
JavaScript
'use strict';
|
|
|
|
const Service = require('../core/base_service');
|
|
|
|
const { Op } = require('sequelize');
|
|
|
|
const moment = require('moment');
|
|
|
|
const fileName = "ContestMsg";
|
|
|
|
class ContestMsgService extends Service {
|
|
// 管理系统检索竞赛 uid
|
|
async searchContestUid(uid, body) {
|
|
const { ctx } = this;
|
|
try {
|
|
const result = await ctx.model4.Contest.findAll({ where: { uid: { [Op.substring]: body.contest_uid } } });
|
|
return result.length == 0 ? false : result;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "searchContestUid";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 管理系统创建竞赛信息
|
|
async addContest(form) {
|
|
const { ctx } = this;
|
|
try {
|
|
const result = await ctx.model4.Contest.bulkCreate(form);
|
|
return result.length == 0 ? false : result;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "addContest";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 管理系统查看竞赛信息
|
|
async showContest(body) {
|
|
const { ctx } = this;
|
|
try {
|
|
let { offset, limit, searchOptions, sortOptions } = body;
|
|
offset = (offset - 1) * limit;
|
|
let attributes = ['uid', 'contest_name', 'contest_organizers', 'contest_term', 'type', 'status', 'online_start', 'online_end', 'offline_start', 'offline_end', 'contest_start', 'contest_end', 'created_at'],
|
|
where = {}, order = [], dbOptions,
|
|
joinSubstring = [],
|
|
substring = ['uid', 'contest_name', 'contest_organizers', 'contest_term'],
|
|
key = ['type', 'status'],
|
|
between = ['online_start', 'online_end', 'offline_start', 'offline_end', 'contest_start', 'contest_end', '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, offset, limit, where, order };
|
|
const result = await ctx.model4.Contest.findAndCountAll(dbOptions);
|
|
return result.rows.length == 0 ? false : result;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "showContest";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 管理系统编辑竞赛信息
|
|
async editContest(uid, modelType, updateMsg, requestMsg) {
|
|
const { ctx } = this;
|
|
try {
|
|
const result = await ctx.model3.Insiders.findOne({ where: { uid } });
|
|
if (!result || result.dataValues.role < 6) return false;
|
|
let where = {};
|
|
if (modelType == "single") {
|
|
const { uid, contest_name, contest_organizers, contest_term, type, status, online_start, online_end, offline_start, offline_end, contest_start, contest_end, created_at } = requestMsg;
|
|
where = { uid, contest_name, contest_organizers, contest_term, type, status, online_start, online_end, offline_start, offline_end, contest_start, contest_end, created_at };
|
|
} else if (modelType == "multiple") {
|
|
let uid = [], contest_name = [], contest_organizers = [], contest_term = [], type = [], status = [], online_start = [], online_end = [], offline_start = [], offline_end = [], contest_start = [], contest_end = [], created_at = [];
|
|
for (const key in requestMsg) {
|
|
requestMsg[key].forEach(element => {
|
|
uid.push(element.uid),
|
|
contest_name.push(element.contest_name),
|
|
contest_organizers.push(element.contest_organizers),
|
|
contest_term.push(element.contest_term),
|
|
type.push(element.type),
|
|
status.push(element.status),
|
|
online_start.push(element.online_start),
|
|
online_end.push(element.online_end),
|
|
offline_start.push(element.offline_start),
|
|
offline_end.push(element.offline_end),
|
|
contest_start.push(element.contest_start),
|
|
contest_end.push(element.contest_end),
|
|
created_at.push(element.created_at)
|
|
});
|
|
}
|
|
where = { uid, contest_name, contest_organizers, contest_term, type, status, online_start, online_end, offline_start, offline_end, contest_start, contest_end, created_at };
|
|
}
|
|
const resultList = await ctx.model4.Contest.update(updateMsg, { where });
|
|
return resultList[0] == 0 ? false : resultList;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "editContest";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 管理系统删除竞赛信息
|
|
async delContest(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.model3.Insiders.findOne({ where: { uid } });
|
|
if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt)) || result.dataValues.role < 9) {
|
|
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 < 9) systemMonitoring.status = 2;
|
|
await ctx.model3.Monitoring.create(systemMonitoring);
|
|
return false;
|
|
};
|
|
let where = {};
|
|
if (modelType == "single") {
|
|
const { uid, contest_name, contest_organizers, contest_term, type, status, online_start, online_end, offline_start, offline_end, contest_start, contest_end, created_at } = requestMsg;
|
|
where = { uid, contest_name, contest_organizers, contest_term, type, status, online_start, online_end, offline_start, offline_end, contest_start, contest_end, created_at };
|
|
} else if (modelType == "multiple") {
|
|
let uid = [], contest_name = [], contest_organizers = [], contest_term = [], type = [], status = [], online_start = [], online_end = [], offline_start = [], offline_end = [], contest_start = [], contest_end = [], created_at = [];
|
|
for (const key in requestMsg) {
|
|
requestMsg[key].forEach(element => {
|
|
uid.push(element.uid),
|
|
contest_name.push(element.contest_name),
|
|
contest_organizers.push(element.contest_organizers),
|
|
contest_term.push(element.contest_term),
|
|
type.push(element.type),
|
|
status.push(element.status),
|
|
online_start.push(element.online_start),
|
|
online_end.push(element.online_end),
|
|
offline_start.push(element.offline_start),
|
|
offline_end.push(element.offline_end),
|
|
contest_start.push(element.contest_start),
|
|
contest_end.push(element.contest_end),
|
|
created_at.push(element.created_at)
|
|
});
|
|
}
|
|
where = { uid, contest_name, contest_organizers, contest_term, type, status, online_start, online_end, offline_start, offline_end, contest_start, contest_end, created_at };
|
|
}
|
|
const resultList = await ctx.model4.Contest.destroy({ where });
|
|
systemMonitoring.status = resultList == 0 ? 3 : 4;
|
|
systemMonitoring.affect_entries = resultList;
|
|
await ctx.model3.Monitoring.create(systemMonitoring);
|
|
return resultList == 0 ? false : true;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "delContest";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 管理系统导出竞赛信息
|
|
async downloadContest(uid, searchMsg) {
|
|
const { ctx } = this;
|
|
try {
|
|
const user = await ctx.model3.Insiders.findOne({ where: { uid } });
|
|
if (!user || user.dataValues.role < 3) return false;
|
|
let { searchOptions, sortOptions } = searchMsg;
|
|
let attributes = ['uid', 'contest_name', 'contest_organizers', 'contest_term', 'type', 'status', 'online_start', 'online_end', 'offline_start', 'offline_end', 'contest_start', 'contest_end', 'created_at'],
|
|
where = {}, order = [], dbOptions,
|
|
joinSubstring = [],
|
|
substring = ['uid', 'contest_name', 'contest_organizers', 'contest_term'],
|
|
key = ['type', 'status'],
|
|
between = ['online_start', 'online_end', 'offline_start', 'offline_end', 'contest_start', 'contest_end', '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.model4.Contest.findAll(dbOptions);
|
|
if (result.length == 0) return { code: false };
|
|
let data = [];
|
|
const typeOptions = [
|
|
{
|
|
value: "0",
|
|
label: "线上赛"
|
|
},
|
|
{
|
|
value: "1",
|
|
label: "线下赛"
|
|
}
|
|
];
|
|
const statusOptions = [
|
|
{
|
|
value: "0",
|
|
label: "正常进行"
|
|
},
|
|
{
|
|
value: "1",
|
|
label: "停止举办"
|
|
}
|
|
];
|
|
result.forEach(element => {
|
|
typeOptions.forEach((item) => {
|
|
if (item.value == element.dataValues["type"]) {
|
|
element.dataValues["type"] = item.label;
|
|
}
|
|
});
|
|
statusOptions.forEach((item) => {
|
|
if (item.value == element.dataValues["status"]) {
|
|
element.dataValues["status"] = item.label;
|
|
}
|
|
});
|
|
element.dataValues.online_start = moment
|
|
.utc(element.dataValues.online_start)
|
|
.add(8, "hours")
|
|
.format("YYYY年MM月DD日HH时mm分ss秒");
|
|
element.dataValues.online_end = moment
|
|
.utc(element.dataValues.online_end)
|
|
.add(8, "hours")
|
|
.format("YYYY年MM月DD日HH时mm分ss秒");
|
|
element.dataValues.offline_start = moment
|
|
.utc(element.dataValues.offline_start)
|
|
.add(8, "hours")
|
|
.format("YYYY年MM月DD日HH时mm分ss秒");
|
|
element.dataValues.offline_end = moment
|
|
.utc(element.dataValues.offline_end)
|
|
.add(8, "hours")
|
|
.format("YYYY年MM月DD日HH时mm分ss秒");
|
|
element.dataValues.contest_start = moment
|
|
.utc(element.dataValues.contest_start)
|
|
.add(8, "hours")
|
|
.format("YYYY年MM月DD日HH时mm分ss秒");
|
|
element.dataValues.contest_end = moment
|
|
.utc(element.dataValues.contest_end)
|
|
.add(8, "hours")
|
|
.format("YYYY年MM月DD日HH时mm分ss秒");
|
|
element.dataValues.created_at = moment
|
|
.utc(element.dataValues.created_at)
|
|
.add(8, "hours")
|
|
.format("YYYY年MM月DD日HH时mm分ss秒");
|
|
data.push(element.dataValues);
|
|
});
|
|
return { code: true, data };
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "downloadContest";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = ContestMsgService; |