123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- package com.qxgmat.service.inline;
- import com.github.pagehelper.Page;
- import com.nuliji.tools.AbstractService;
- import com.nuliji.tools.PageResult;
- import com.nuliji.tools.Transform;
- import com.nuliji.tools.exception.ParameterException;
- import com.nuliji.tools.exception.SystemException;
- import com.nuliji.tools.mybatis.Example;
- import com.qxgmat.data.constants.enums.module.StructModule;
- import com.qxgmat.data.constants.enums.status.DirectionStatus;
- import com.qxgmat.data.dao.QuestionNoMapper;
- import com.qxgmat.data.dao.entity.Question;
- import com.qxgmat.data.dao.entity.QuestionNo;
- import com.qxgmat.data.dao.entity.UserQuestion;
- import com.qxgmat.data.inline.PaperStat;
- import com.qxgmat.data.relation.QuestionNoRelationMapper;
- import com.qxgmat.data.relation.entity.QuestionNoRelation;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- public class QuestionNoService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(QuestionNoService.class);
- protected boolean SOFT_FLAG = true;
- final static String formatSet = "FIND_IN_SET('%d', module_struct)";
- @Resource
- private QuestionNoMapper questionNoMapper;
- @Resource
- private QuestionNoRelationMapper questionNoRelationMapper;
- @Resource
- private QuestionService questionService;
- /**
- * 根据题干搜索相似题目: 相似度80%
- * @param page
- * @param size
- * @param stem
- * @return
- */
- public PageResult<QuestionNoRelation> searchStem(int page, int size, String stem){
- // String[] stems = stem.replaceAll("\\.?,?!?:?;?\\??", "").split(" ");
- Page<QuestionNo> p = page(()->{
- questionNoRelationMapper.searchStem(stem);
- }, page, size);
- Collection ids = Transform.getIds(p, QuestionNo.class, "id");
- // 获取详细数据
- List<QuestionNo> list = select(ids);
- return new PageResult<>(relation(list), p.getTotal());
- }
- /**
- * 根据题目编号搜索相似题目
- * @param page
- * @param size
- * @param keyword
- * @param module
- * @return
- */
- public PageResult<QuestionNoRelation> searchNo(int page, int size, String keyword, String module){
- logger.info("SearchNo: {}, {}", page, size);
- Example example = new Example(QuestionNo.class);
- if(keyword != null)
- example.and(
- example.createCriteria()
- .andLike("title", "%"+keyword+"%")
- );
- if (module != null)
- example.and(
- example.createCriteria()
- .andEqualTo("module", module)
- );
- example.orderBy("id").asc();
- Page<QuestionNo> p = page(()->select(questionNoMapper, example), page, size);
- logger.info("SearchNo result: {}", p.toString());
- return new PageResult<>(relation(p), p.getTotal());
- }
- /**
- * 获取结构模块下的题目列表: 按序号排列
- * @param module
- * @param structId
- * @return
- */
- public List<QuestionNoRelation> listByStruct(StructModule module, Integer structId){
- Example example = new Example(QuestionNo.class);
- example.and(
- example.createCriteria()
- .andEqualTo("module", module.key)
- .andCondition(String.format(formatSet, structId))
- );
- example.orderBy("no").asc();
- return relation(select(questionNoMapper, example));
- }
- /**
- * 根据题目标号列表获取题目
- * @param nos
- * @param module
- * @return
- */
- public List<QuestionNoRelation> listWithRelationByNos(String[] nos, String module){
- if (nos.length == 0) return new ArrayList<>();
- Example example = new Example(QuestionNo.class);
- example.and(
- example.createCriteria()
- .andIn("no", Arrays.stream(nos).collect(Collectors.toList()))
- );
- if (module != null)
- example.and(
- example.createCriteria()
- .andEqualTo("module", module)
- );
- List<QuestionNo> p = select(questionNoMapper, example);
- return relation(p);
- }
- /**
- * 根据题目获取关联的题目编号
- * @param questionId
- * @return
- */
- public List<QuestionNo> listByQuestion(Number questionId){
- Example example = new Example(QuestionNo.class);
- example.and(
- example.createCriteria()
- .andEqualTo("questionId", questionId)
- );
- return select(questionNoMapper, example);
- }
- /**
- * 根据题目编号id列表获取关联题目
- * @param ids
- * @return
- */
- public List<QuestionNoRelation> listWithRelationByIds(Number[] ids){
- List<QuestionNo> p = select(questionNoMapper, ids);
- return relation(p);
- }
- /**
- * 根据题目编号id列表获取关联题目
- * @param ids
- * @return
- */
- public Map<Number, QuestionNoRelation> mapWithRelationByIds(Number[] ids){
- List<QuestionNo> p = select(questionNoMapper, ids);
- List<QuestionNoRelation> list = relation(p);
- Map<Number, QuestionNoRelation> map = new HashMap<>();
- for(QuestionNoRelation relation : list){
- map.put(relation.getId(), relation);
- }
- return map;
- }
- /**
- * 根据题目编号id获取关联题目
- * @param id
- * @return
- */
- public QuestionNoRelation getWithRelation(Number id){
- QuestionNo questionNo = get(id);
- return relation(questionNo);
- }
- /**
- * 绑定no和question
- * @param ids
- * @param questionId
- * @return
- */
- public Boolean bindQuestion(Integer[] ids, Integer questionId){
- if (ids.length == 0) return false;
- Example example = new Example(QuestionNo.class);
- example.and(
- example.createCriteria()
- .andIn("id", Arrays.stream(ids).collect(Collectors.toList()))
- );
- int result = update(questionNoMapper, example, QuestionNo.builder().questionId(questionId).deleteTime(null).build());
- return result > 0;
- }
- /**
- * 设定编号关联关系
- * @param ids
- * @return
- */
- public Boolean relationQuestion(int[] ids){
- if (ids.length == 0) return false;
- Example example = new Example(QuestionNo.class);
- example.and(
- example.createCriteria()
- .andIn("id", Arrays.stream(ids).boxed().collect(Collectors.toList()))
- );
- int result = update(questionNoMapper, example, QuestionNo.builder().relationQuestion(ids).deleteTime(null).build());
- return result > 0;
- }
- /**
- * 根据题目获取总试卷统计信息
- * @param questionNoList
- * @return
- */
- public PaperStat statPaper(List<QuestionNo> questionNoList){
- PaperStat stat = new PaperStat();
- Integer totalTime = 0;
- Integer totalNumber = 0;
- Integer totalCorrect = 0;
- for(QuestionNo questionNo : questionNoList){
- totalTime += questionNo.getTotalTime();
- totalNumber += questionNo.getTotalNumber();
- totalCorrect += questionNo.getTotalCorrect();
- }
- stat.setTotalCorrect(totalCorrect);
- stat.setTotalNumber(totalNumber);
- stat.setTotalTime(totalTime);
- return stat;
- }
- /**
- * 累加做题记录到questionNo
- * @param question
- */
- public void accumulation(UserQuestion question){
- questionNoRelationMapper.accumulation(question.getQuestionNoId(), 1, question.getTime(), question.getIsCorrect());
- }
- /**
- * 根据试卷分组获取统计信息
- * @param questionNoIdsMap
- * @return
- */
- public Map<Integer, PaperStat> statPaperMap(Map<Integer, Integer[]> questionNoIdsMap){
- Map<Integer, PaperStat> relationMap = new HashMap<>();
- List<Integer> ids = new ArrayList<>();
- for(Integer[] questionNoIds : questionNoIdsMap.values()){
- ids.addAll(Arrays.stream(questionNoIds).collect(Collectors.toList()));
- }
- List<QuestionNo> questionNoList = select(ids);
- Map questionNoMap = Transform.getMap(questionNoList, QuestionNo.class, "id");
- List<QuestionNo> l = new ArrayList<>();
- for(Integer k: questionNoIdsMap.keySet()){
- l.clear();
- for (Integer questionNoId : questionNoIdsMap.get(k)){
- l.add((QuestionNo)questionNoMap.get(questionNoId));
- }
- relationMap.put(k, statPaper(l));
- }
- return relationMap;
- }
- public List<QuestionNoRelation> relation(List<QuestionNo> p){
- List<QuestionNoRelation> relationList = Transform.convert(p, QuestionNoRelation.class);
- Collection questionIds = Transform.getIds(p, QuestionNo.class, "questionId");
- List<Question> questions = questionService.select(questionIds);
- Transform.combine(relationList, questions, QuestionNoRelation.class, "questionId", "question", Question.class, "id");
- return relationList;
- }
- public QuestionNoRelation relation(QuestionNo p){
- QuestionNoRelation relation = Transform.convert(p, QuestionNoRelation.class);
- Question question = questionService.get(p.getQuestionId());
- relation.setQuestion(question);
- return relation;
- }
- /**
- * 通过题目编号获取
- * @param title
- * @return
- */
- public QuestionNo getByNo(String title, String module){
- return one(questionNoMapper, QuestionNo.builder().title(title).module(module).build());
- }
- @Transactional
- public QuestionNo add(QuestionNo question){
- QuestionNo in = getByNo(question.getTitle(), question.getModule());
- if (in != null){
- return in;
- }
- int result = insert(questionNoMapper, question);
- question = one(questionNoMapper, question.getId());
- if(question == null){
- throw new SystemException("题目添加失败");
- }
- return question;
- }
- @Deprecated
- public QuestionNo edit(QuestionNo question){
- QuestionNo in = one(questionNoMapper, question.getId());
- if(in == null){
- throw new ParameterException("题目不存在");
- }
- int result = update(questionNoMapper, question);
- return question;
- }
- public boolean delete(Number id){
- QuestionNo in = one(questionNoMapper, id);
- if(in == null){
- throw new ParameterException("题目不存在");
- }
- int result = delete(questionNoMapper, id, SOFT_FLAG);
- return result > 0;
- }
- public QuestionNo get(Number id){
- QuestionNo in = one(questionNoMapper, id);
- if(in == null){
- throw new ParameterException("题目不存在");
- }
- return in;
- }
- public Page<QuestionNo> select(int page, int pageSize){
- return select(questionNoMapper, page, pageSize);
- }
- public List<QuestionNo> select(Collection ids){
- return select(questionNoMapper, ids);
- }
- }
|