Commit befb0951 by 郑云飞

题库存到redis缓存中

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