'use strict'; const Service = require('../core/base_service'); const { Op } = require('sequelize'); const moment = require('moment'); const fileName = "TopicWhitelist"; class TopicWhitelistService extends Service { // 管理系统新增题库白名单 async addTopicWhitelist(userMsgObj, form) { const { ctx } = this; try { let attributes = ['uid', 'phone'], where = userMsgObj, dbOptions = { attributes, where }; const userUid = await ctx.model.Examinee.findAll(dbOptions); form.forEach(async item => { item.user_uid = 'none'; userUid.forEach(element => { if (item.phone == element.dataValues.phone) { item.user_uid = element.dataValues.uid; } }); delete item.phone; }); const result = await ctx.model.Whitelist.bulkCreate(form, { ignoreDuplicates: true }); return result.length == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "addTopicWhitelist"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统查看题库白名单 async showTopicWhitelist(uid, body) { const { ctx } = this; try { let { offset, limit, searchOptions, sortOptions } = body; offset = (offset - 1) * limit; let attributes = ['user_uid', 'type', 'expired_at'], where = {}, order = [], dbOptions, joinSubstring = [], substring = ['user_uid', 'user_name', 'identity_card'], key = ['type'], between = ['expired_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 examinee = ['user_name', 'identity_card'], examineeWhere = {}, examineeOrder = [], examineeJoinSubstring = [], examineeSubstring = ['user_name', 'identity_card'], 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'], where: examineeWhere, order: examineeOrder } ]; ctx.model.Whitelist.belongsTo(ctx.model.Examinee, { foreignKey: 'user_uid', targetKey: 'uid' }); dbOptions = { attributes, offset, limit, where, order, include }; const result = await ctx.model.Whitelist.findAndCountAll(dbOptions); return result.rows.length == 0 ? false : result; } catch (error) { const blockName = fileName + "--" + "showTopicWhitelist"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统编辑题库白名单 async editTopicWhitelist(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, type, expired_at } = requestMsg; where = { user_uid, type, expired_at }; } else if (modelType == "multiple") { let user_uid = [], type = [], expired_at = []; for (const key in requestMsg) { requestMsg[key].forEach(element => { user_uid.push(element.user_uid); type.push(element.type); expired_at.push(element.expired_at); }); } where = { user_uid, type, expired_at }; } updateMsg.type = Number(updateMsg.type); if (updateMsg.expired_at) { updateMsg.expired_at = new Date(updateMsg.expired_at); } else { updateMsg.expired_at = null; } const resultList = await ctx.model.Whitelist.update(updateMsg, { where }); return resultList[0] == 0 ? false : resultList; } catch (error) { const blockName = fileName + "--" + "editTopicWhitelist"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统删除题库白名单 async deleteTopicWhitelist(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 { type, expired_at } = requestMsg; where = { type, expired_at }; } else if (modelType == "multiple") { let type = [], expired_at = []; for (const key in requestMsg) { requestMsg[key].forEach(element => { type.push(element.type); expired_at.push(element.expired_at); }); } where = { type, expired_at }; } const resultList = await ctx.model.Whitelist.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 + "--" + "deleteTopicWhitelist"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } // 管理系统导出题库白名单 async downloadTopicWhitelistData(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', 'type', 'expired_at'], where = {}, order = [], dbOptions, joinSubstring = [], substring = ['user_uid'], key = ['type'], between = ['expired_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 examinee = ['user_name', 'identity_card'], examineeWhere = {}, examineeOrder = [], examineeJoinSubstring = [], examineeSubstring = ['user_name', 'identity_card'], 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'], where: examineeWhere, order: examineeOrder } ]; ctx.model.Whitelist.belongsTo(ctx.model.Examinee, { foreignKey: 'user_uid', targetKey: 'uid' }); dbOptions = { attributes, where, order, include }; const result = await ctx.model.Whitelist.findAll(dbOptions); if (result.length == 0) return { code: false }; let data = []; const typeOptions = [ { value: "0", label: "永久白名单" }, { value: "1", label: "临时白名单" }, ]; result.forEach(element => { typeOptions.forEach((item) => { if (item.value == element.dataValues["type"]) { element.dataValues["type"] = item.label; } }); element.dataValues.user_name = element.dataValues.examinee.dataValues.user_name; element.dataValues.identity_card = element.dataValues.examinee.dataValues.identity_card; element.dataValues.expired_at = moment .utc(element.dataValues.examination.dataValues.expired_at) .add(8, "hours") .format("YYYY年MM月DD日HH时mm分ss秒"); delete element.dataValues.examinee; let elementClone = { user_uid: element.dataValues.user_uid, user_name: element.dataValues.user_name, identity_card: element.dataValues.identity_card, type: element.dataValues.type, expired_at: element.dataValues.expired_at, } data.push(elementClone); }); return { code: true, data }; } catch (error) { const blockName = fileName + "--" + "downloadTopicWhitelistData"; ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error); return 'error'; } } } module.exports = TopicWhitelistService;