|
@@ -0,0 +1,157 @@
|
|
|
+package com.zhongzheng.modules.bank.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.lang.Validator;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.zhongzheng.common.exception.CustomException;
|
|
|
+import com.zhongzheng.common.utils.DateUtils;
|
|
|
+import com.zhongzheng.common.utils.ServletUtils;
|
|
|
+import com.zhongzheng.modules.bank.bo.*;
|
|
|
+import com.zhongzheng.modules.bank.domain.ExamQuestion;
|
|
|
+import com.zhongzheng.modules.bank.domain.ExamTemp;
|
|
|
+import com.zhongzheng.modules.bank.domain.ExamTempQuestion;
|
|
|
+import com.zhongzheng.modules.bank.mapper.ExamTempMapper;
|
|
|
+import com.zhongzheng.modules.bank.service.IExamTempQuestionService;
|
|
|
+import com.zhongzheng.modules.bank.service.IExamTempService;
|
|
|
+import com.zhongzheng.modules.bank.vo.ExamTempVo;
|
|
|
+import com.zhongzheng.modules.bank.vo.QuestionVo;
|
|
|
+import com.zhongzheng.modules.order.domain.OrderGoods;
|
|
|
+import com.zhongzheng.modules.order.service.IOrderGoodsService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.github.pagehelper.Page;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 随机生成试卷Service业务层处理
|
|
|
+ *
|
|
|
+ * @author hjl
|
|
|
+ * @date 2022-11-14
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ExamTempServiceImpl extends ServiceImpl<ExamTempMapper, ExamTemp> implements IExamTempService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IOrderGoodsService iOrderGoodsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IExamTempQuestionService iExamTempQuestionService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ExamTempVo queryById(Long examId){
|
|
|
+ ExamTemp db = this.baseMapper.selectById(examId);
|
|
|
+ return BeanUtil.toBean(db, ExamTempVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ExamTempVo> queryList(ExamTempQueryBo bo) {
|
|
|
+ LambdaQueryWrapper<ExamTemp> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.eq(bo.getStatus() != null, ExamTemp::getStatus, bo.getStatus());
|
|
|
+ lqw.like(StrUtil.isNotBlank(bo.getExamName()), ExamTemp::getExamName, bo.getExamName());
|
|
|
+ lqw.eq(bo.getGoodsId() != null, ExamTemp::getGoodsId, bo.getGoodsId());
|
|
|
+ lqw.eq(bo.getUserId() != null, ExamTemp::getUserId, bo.getUserId());
|
|
|
+ return entity2Vo(this.list(lqw));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<QuestionVo> getQuestionList(ExamTempQueryBo bo) {
|
|
|
+ return this.baseMapper.getQuestionList(bo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实体类转化成视图对象
|
|
|
+ *
|
|
|
+ * @param collection 实体类集合
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<ExamTempVo> entity2Vo(Collection<ExamTemp> collection) {
|
|
|
+ List<ExamTempVo> voList = collection.stream()
|
|
|
+ .map(any -> BeanUtil.toBean(any, ExamTempVo.class))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (collection instanceof Page) {
|
|
|
+ Page<ExamTemp> page = (Page<ExamTemp>)collection;
|
|
|
+ Page<ExamTempVo> pageVo = new Page<>();
|
|
|
+ BeanUtil.copyProperties(page,pageVo);
|
|
|
+ pageVo.addAll(voList);
|
|
|
+ voList = pageVo;
|
|
|
+ }
|
|
|
+ return voList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public ExamTempVo insertByAddBo(ExamTempAddBo bo) {
|
|
|
+ ExamTemp add = BeanUtil.toBean(bo, ExamTemp.class);
|
|
|
+ validEntityBeforeSave(add);
|
|
|
+ OrderGoods orderGoods = iOrderGoodsService.getOne(new LambdaQueryWrapper<OrderGoods>()
|
|
|
+ .eq(OrderGoods::getOrderGoodsId, bo.getOrderGoodsId()));
|
|
|
+ if(Validator.isEmpty(orderGoods)||!orderGoods.getGoodsId().equals(bo.getGoodsId())){
|
|
|
+ throw new CustomException("订单商品ID错误");
|
|
|
+ }
|
|
|
+ if(Validator.isEmpty(bo.getNumber())){
|
|
|
+ throw new CustomException("题目数量错误");
|
|
|
+ }
|
|
|
+ ExamTempQueryBo queryBo = new ExamTempQueryBo();
|
|
|
+ queryBo.setGoodsId(bo.getGoodsId());
|
|
|
+ queryBo.setOrderGoodsId(bo.getOrderGoodsId());
|
|
|
+ queryBo.setNumber(bo.getNumber());
|
|
|
+ List<QuestionVo> questionlist = getQuestionList(queryBo);
|
|
|
+
|
|
|
+ add.setExamName(ServletUtils.getEncoded("RD")+bo.getOrderGoodsId());
|
|
|
+
|
|
|
+ add.setCreateTime(DateUtils.getNowTime());
|
|
|
+ add.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ boolean result = this.save(add);
|
|
|
+ if(questionlist!=null){
|
|
|
+ Collection<ExamTempQuestion> coll = new HashSet<>();
|
|
|
+ for(int i=0;i<questionlist.size();i++){
|
|
|
+ QuestionVo questionVo = questionlist.get(i);
|
|
|
+ ExamTempQuestion addItem = new ExamTempQuestion();
|
|
|
+ addItem.setExamId(add.getExamId());
|
|
|
+ addItem.setQuestionId(questionVo.getQuestionId());
|
|
|
+ addItem.setSort(new Long(i));
|
|
|
+ coll.add(addItem);
|
|
|
+ }
|
|
|
+ if(!iExamTempQuestionService.saveBatch(coll)){
|
|
|
+ throw new CustomException("试卷绑定错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ExamTempVo examTempVo = BeanUtil.toBean(add, ExamTempVo.class);
|
|
|
+ examTempVo.setQuestionList(questionlist);
|
|
|
+ return examTempVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateByEditBo(ExamTempEditBo bo) {
|
|
|
+ ExamTemp update = BeanUtil.toBean(bo, ExamTemp.class);
|
|
|
+ validEntityBeforeSave(update);
|
|
|
+ update.setUpdateTime(DateUtils.getNowTime());
|
|
|
+ return this.updateById(update);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存前的数据校验
|
|
|
+ *
|
|
|
+ * @param entity 实体类数据
|
|
|
+ */
|
|
|
+ private void validEntityBeforeSave(ExamTemp entity){
|
|
|
+ //TODO 做一些数据校验,如唯一约束
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
|
|
+ if(isValid){
|
|
|
+ //TODO 做一些业务上的校验,判断是否需要校验
|
|
|
+ }
|
|
|
+ return this.removeByIds(ids);
|
|
|
+ }
|
|
|
+}
|