'use strict'; const Service = require('../core/base_service'); const { Op } = require('sequelize'); const moment = require('moment'); const fileName = "ContestChoiceTopic"; class ContestChoiceTopicService extends Service { // 管理系统导入选择题 async importChoiceTopic(form) { const { ctx } = this; try { const result = await ctx.model4.ChoiceTopic.bulkCreate(form); return result.length == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "importChoiceTopic"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统创建选择题 async addChoiceTopic(form) { const { ctx } = this; try { const result = await ctx.model4.ChoiceTopic.bulkCreate(form); return result.length == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "addChoiceTopic"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统查看选择题 async showChoiceTopic(body) { const { ctx } = this; try { let { offset, limit, searchOptions, sortOptions } = body; offset = (offset - 1) * limit; let attributes = ['topic', 'options_a', 'options_b', 'options_c', 'options_d', 'answer'], where = {}, order = [], dbOptions, joinSubstring = [], substring = ['topic', 'options_a', 'options_b', 'options_c', 'options_d'], key = ['answer'], 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']); } }); dbOptions = { attributes, offset, limit, where, order }; const result = await ctx.model4.ChoiceTopic.findAndCountAll(dbOptions); return result.rows.length == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "showChoiceTopic"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统编辑选择题 async editChoiceTopic(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 { topic, options_a, options_b, options_c, options_d, answer } = requestMsg; where = { topic, options_a, options_b, options_c, options_d, answer }; } else if (modelType == "multiple") { let topic = [], options_a = [], options_b = [], options_c = [], options_d = [], answer = []; for (const key in requestMsg) { requestMsg[key].forEach(element => { topic.push(element.topic), options_a.push(element.options_a), options_b.push(element.options_b), options_c.push(element.options_c), options_d.push(element.options_d), answer.push(element.answer) }); } where = { topic, options_a, options_b, options_c, options_d, answer }; } updateMsg.updated_at = new Date(); const resultList = await ctx.model4.ChoiceTopic.update(updateMsg, { where }); return resultList[0] == 0 ? false : resultList; } catch (error) { const blockName = fileName + "--" + "editChoiceTopic"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统删除选择题 async delChoiceTopic(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 { topic, options_a, options_b, options_c, options_d, answer } = requestMsg; where = { topic, options_a, options_b, options_c, options_d, answer }; } else if (modelType == "multiple") { let topic = [], options_a = [], options_b = [], options_c = [], options_d = [], answer = []; for (const key in requestMsg) { requestMsg[key].forEach(element => { topic.push(element.topic), options_a.push(element.options_a), options_b.push(element.options_b), options_c.push(element.options_c), options_d.push(element.options_d), answer.push(element.answer) }); } where = { topic, options_a, options_b, options_c, options_d, answer }; } const resultList = await ctx.model4.ChoiceTopic.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 + "--" + "delChoiceTopic"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统导出选择题 async downloadChoiceTopic(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 = ['topic', 'options_a', 'options_b', 'options_c', 'options_d', 'answer'], where = {}, order = [], dbOptions, joinSubstring = [], substring = ['topic', 'options_a', 'options_b', 'options_c', 'options_d', 'answer'], key = [], 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']); } }); dbOptions = { attributes, where, order }; const result = await ctx.model4.ChoiceTopic.findAll(dbOptions); if (result.length == 0) return { code: false }; let data = []; result.forEach(element => { data.push(element.dataValues); }); return { code: true, data }; } catch (error) { const blockName = fileName + "--" + "downloadChoiceTopic"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } } module.exports = ContestChoiceTopicService;