Files
bd-contest/app/service/Topic.js

113 lines
5.6 KiB
JavaScript
Raw Normal View History

'use strict';
const Service = require('../core/base_service');
const { Op } = require('sequelize');
const fileName = "Topic";
class TopicService extends Service {
// 学生抽取/读取题目
async topic(uid, answerId) {
const { ctx } = this;
try {
let contestTopic = await ctx.model4.Examination.findOne({ where: { user_uid: uid } });
if (!contestTopic) {
const result = await ctx.model4.ChoiceTopic.findAndCountAll();
// 选择题一共10道,首先获取数据数目
let bdCardMsg = [];
// 若不满10道题,则直接将题目 id 放入数组中
if (result.rows.length < 10 || result.rows.length == 10) {
bdCardMsg = result.rows.map(item => item.id);
} else {
// 创建一个空数组来存储随机数
let randomNumbers = [];
// 循环,直到数组中的元素数量达到十个
while (randomNumbers.length < 10) {
let arrNum;
do {
// 获取一个随机数
arrNum = Math.floor(Math.random() * result.count);
} while (randomNumbers.includes(arrNum)); // 检查随机数是否已存在,如果存在则重新抽取
// 将随机数添加到数组中
randomNumbers.push(arrNum);
// 将随机数作为下标,向 bdCardMsg 数组中添加对应题目数据
bdCardMsg.push(result.rows[arrNum].id);
}
}
const hanzi = "北斗航天科学教育科普导航定位通信";
const randomCharIndex = Math.floor(Math.random() * hanzi.length);
const randomChar = hanzi.charAt(randomCharIndex);
const number = "0123456789";
const randomNumIndex = Math.floor(Math.random() * number.length);
const randomNum = number.charAt(randomNumIndex);
const sensor = ["温度", "湿度", "超声波传感器", "光线"];
const randomArrCharIndex = Math.floor(Math.random() * sensor.length);
const randomArrChar = sensor[randomArrCharIndex];
let sensorData, num;
if (randomArrChar == "温度") {
num = Math.floor(Math.random() * 899) + 100;
num = num.toString().slice(0, -1) + "." + num.toString().slice(-1);
sensorData = "温度" + num + "度";
} else if (randomArrChar == "湿度") {
num = Math.floor(Math.random() * 899) + 100;
num = num.toString().slice(0, -1) + "." + num.toString().slice(-1);
sensorData = "湿度" + num + "%";
} else if (randomArrChar == "超声波传感器") {
num = Math.floor(Math.random() * 89) + 10;
sensorData = "距离" + num + "厘米";
} else if (randomArrChar == "光线") {
num = Math.floor(Math.random() * 3095) + 1000;
sensorData = "光线强度" + num;
}
const user = await ctx.model4.Account.findOne({ attributes: ['contest_uid'], where: { uid } });
if (!user) return { code: 404 };
const contest = await ctx.model4.Contest.findOne({ attributes: ['type'], where: { uid: user.dataValues.contest_uid, status: 0 } });
if (!contest) return { code: 404 };
const form = {
user_uid: uid,
contest_uid: user.dataValues.contest_uid,
exam_type: contest.dataValues.type,
topic1: randomChar,
topic2: randomNum,
topic3: bdCardMsg.join(","),
topic5: sensorData,
created_at: new Date(),
};
contestTopic = await ctx.model4.Examination.create(form, { silent: true });
if (!contestTopic) return { code: 404 };
}
contestTopic.dataValues.topic3.match(/[^a-zA-Z]/g).join('');
let idArr;
if (contestTopic.dataValues.updated_at != null) {
idArr = contestTopic.dataValues.answer.split(",");
} else {
idArr = answerId;
}
let attributes = ['id', 'card_id', 'encode_type', 'msg_data', 'created_at'],
where = { id: idArr, user_uid: uid },
dbOptions = { attributes, where };
const result = await ctx.model4.TShortMsg.findAndCountAll(dbOptions);
return { code: 200, examination: contestTopic.dataValues, shortMsg: result.rows };
} catch (error) {
const blockName = fileName + "--" + "topic";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 系统读取单选题
async radio(radioArr) {
const { ctx } = this;
try {
const Radio = await ctx.model4.ChoiceTopic.findAll({ attributes: ['id', 'topic', 'options_a', 'options_b', 'options_c', 'options_d'], where: { id: radioArr } });
return Radio.length == 0 ? false : Radio;
} catch (error) {
const blockName = fileName + "--" + "radio";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = TopicService;