Commit befb0951 by 郑云飞

题库存到redis缓存中

parent d226227a
......@@ -147,15 +147,7 @@ public class DbQuestionBankServiceImpl implements IDbQuestionBankService {
public Boolean updateByBo(DbQuestionBankBo bo) {
DbQuestionBank update = BeanUtil.toBean(bo, DbQuestionBank.class);
validEntityBeforeSave(update);
boolean upd = baseMapper.updateById(update) > 0;
if (upd) {
// 将新增的题目添加到redis题库缓存中
List<DbQuestionBank> questionBanks = JsonUtils.parseArray(RedisUtils.getCacheObject(DEFAULT_QUESTIOBN_BANK_KEY), DbQuestionBank.class);
questionBanks.removeIf(dbQuestionBank -> dbQuestionBank.getId().equals(update.getId()));
questionBanks.add(update);
RedisUtils.setCacheObject(DEFAULT_QUESTIOBN_BANK_KEY, JsonUtils.toJsonString(questionBanks));
}
return false;
return baseMapper.updateById(update) > 0;
}
/**
......@@ -169,20 +161,28 @@ public class DbQuestionBankServiceImpl implements IDbQuestionBankService {
boolean questionUpdateFlag = baseMapper.updateById(updateRecord) > 0;
// 若为选择题则同步插入至选项答案中
boolean answerUpdateFlag = false;
if (questionUpdateFlag && updateRecord.getSpecies() == 2 && CollUtil.isNotEmpty(bo.getOptions())) {
// 删除选择题旧有数据
LambdaQueryWrapper<DbQuestionBankAnswer> wrapper = new LambdaQueryWrapper<>();
Objects.requireNonNull(bo.getId(), "题目ID不能为空");
wrapper.eq(DbQuestionBankAnswer::getQuestionBankId, bo.getId());
questionBankAnswerMapper.delete(wrapper);
// 插入新值
List<DbQuestionBankAnswer> options = new ArrayList<>(bo.getOptions().size());
for (DbQuestionBankAnswerBo option : bo.getOptions()) {
DbQuestionBankAnswer bankAnswer = BeanUtil.toBean(option, DbQuestionBankAnswer.class);
bankAnswer.setQuestionBankId(updateRecord.getId());
options.add(bankAnswer);
if (questionUpdateFlag) {
// 将新增的题目添加到redis题库缓存中
List<DbQuestionBank> questionBanks = JsonUtils.parseArray(RedisUtils.getCacheObject(DEFAULT_QUESTIOBN_BANK_KEY), DbQuestionBank.class);
questionBanks.removeIf(dbQuestionBank -> dbQuestionBank.getId().equals(updateRecord.getId()));
questionBanks.add(updateRecord);
RedisUtils.setCacheObject(DEFAULT_QUESTIOBN_BANK_KEY, JsonUtils.toJsonString(questionBanks));
if (updateRecord.getSpecies() == 2 && CollUtil.isNotEmpty(bo.getOptions())) {
// 删除选择题旧有数据
LambdaQueryWrapper<DbQuestionBankAnswer> wrapper = new LambdaQueryWrapper<>();
Objects.requireNonNull(bo.getId(), "题目ID不能为空");
wrapper.eq(DbQuestionBankAnswer::getQuestionBankId, bo.getId());
questionBankAnswerMapper.delete(wrapper);
// 插入新值
List<DbQuestionBankAnswer> options = new ArrayList<>(bo.getOptions().size());
for (DbQuestionBankAnswerBo option : bo.getOptions()) {
DbQuestionBankAnswer bankAnswer = BeanUtil.toBean(option, DbQuestionBankAnswer.class);
bankAnswer.setQuestionBankId(updateRecord.getId());
options.add(bankAnswer);
}
answerUpdateFlag = questionBankAnswerMapper.insertBatch(options);
}
answerUpdateFlag = questionBankAnswerMapper.insertBatch(options);
}
return questionUpdateFlag && answerUpdateFlag;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment