'use strict'; const Service = require('../core/base_service'); const { Op } = require('sequelize'); const fileName = "ContestExamination"; class ContestExaminationService extends Service { // 管理系统查看账号答题情况 async showExamination(body) { const { ctx } = this; try { let { offset, limit, searchOptions, sortOptions } = body; offset = (offset - 1) * limit; let attributes = ['user_uid', 'contest_uid', 'exam_type', 'topic_type', 'score', 'created_at', 'updated_at'], where = {}, order = [], dbOptions, joinSubstring = [], substring = ['user_uid', 'contest_uid', 'score'], key = ['exam_type', 'topic_type'], between = ['created_at', 'updated_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.Examination.findAndCountAll(dbOptions); return result.rows.length == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "showExamination"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统编辑账号答题情况 async editExamination(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 { user_uid, contest_uid, exam_type, topic_type, score, created_at, updated_at } = requestMsg; where = { user_uid, contest_uid, exam_type, topic_type, score, created_at, updated_at }; } else if (modelType == "multiple") { let user_uid = [], contest_uid = [], exam_type = [], topic_type = [], score = [], created_at = [], updated_at = []; for (const key in requestMsg) { requestMsg[key].forEach(element => { user_uid.push(element.user_uid), contest_uid.push(element.contest_uid), exam_type.push(element.exam_type), topic_type.push(element.topic_type), score.push(element.score), created_at.push(element.created_at), updated_at.push(element.updated_at) }); } where = { user_uid, contest_uid, exam_type, topic_type, score, created_at, updated_at }; } updateMsg.updated_at = new Date(); const resultList = await ctx.model4.Examination.update(updateMsg, { where }); return resultList[0] == 0 ? false : resultList; } catch (error) { const blockName = fileName + "--" + "editExamination"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统删除账号答题情况 async delExamination(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 { user_uid, contest_uid, exam_type, topic_type, score, created_at, updated_at } = requestMsg; where = { user_uid, contest_uid, exam_type, topic_type, score, created_at, updated_at }; } else if (modelType == "multiple") { let user_uid = [], contest_uid = [], exam_type = [], topic_type = [], score = [], created_at = [], updated_at = []; for (const key in requestMsg) { requestMsg[key].forEach(element => { user_uid.push(element.user_uid), contest_uid.push(element.contest_uid), exam_type.push(element.exam_type), topic_type.push(element.topic_type), score.push(element.score), created_at.push(element.created_at), updated_at.push(element.updated_at) }); } where = { user_uid, contest_uid, exam_type, topic_type, score, created_at, updated_at }; } const resultList = await ctx.model4.Examination.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 + "--" + "delExamination"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统导出账号答题情况 async downloadExamination(uid, body) { const { ctx } = this; try { console.log(uid); console.log(body); // 使用分数和时间排序后导出 // const result = await ctx.model4.Examination.findAll({ where: { body } }); // console.log(result); // return result.length == 0 ? false : result; return true; } catch (error) { const blockName = fileName + "--" + "downloadExamination"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } } module.exports = ContestExaminationService;