123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- 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.UserCollectQuestion;
- 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.QuestionDifficultRelation;
- 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 = false;
- @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, Integer[] ids){
- 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)
- );
- if (ids != null)
- example.and(
- example.createCriteria()
- .andIn("id", Arrays.stream(ids).collect(Collectors.toList()))
- );
- example.and(
- example.createCriteria()
- .andIsNull("deleteTime")
- );
- 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 page
- * @param size
- * @param keyword
- * @return
- */
- public Page<QuestionNoRelation> searchStemFulltext(int page, int size, String keyword, String[] questionTypes, String module, Integer[] structIds, String place, String difficult, Integer qxCatId, String order, DirectionStatus direction){
- if (direction == null){
- direction = DirectionStatus.DESC;
- }
- String finalOrder = order;
- DirectionStatus finalDirection = direction;
- Page<QuestionNoRelation> p = page(()->{
- questionNoRelationMapper.searchStemFulltext(keyword, questionTypes, module, structIds, place, difficult, qxCatId, finalOrder, finalDirection.key);
- }, page, size);
- Collection ids = Transform.getIds(p, QuestionNo.class, "id");
- // 获取详细数据
- List<QuestionNo> list = select(ids);
- List<QuestionNoRelation> relationList = relation(list);
- Transform.replace(p, relationList, QuestionNoRelation.class, "id");
- return p;
- }
- /**
- * 根据题目编号搜索相似题目
- * @param page
- * @param size
- * @param keyword
- * @return
- */
- public Page<QuestionNo> searchNoFulltext(int page, int size, String keyword, String module, Integer qxCatId){
- Page<QuestionNo> p = page(()->{
- questionNoRelationMapper.searchNoFulltext(keyword, module, qxCatId);
- }, page, size);
- Collection ids = Transform.getIds(p, QuestionNo.class, "id");
- // 获取详细数据
- List<QuestionNo> list = select(ids);
- Transform.replace(p, list, QuestionNo.class, "id");
- return p;
- }
- /**
- * 获取结构模块下的题目列表: 按序号排列
- * @param module
- * @param structId
- * @return
- */
- public List<QuestionNo> listByStruct(StructModule module, Integer structId){
- Example example = new Example(QuestionNo.class);
- example.and(
- example.createCriteria()
- .andEqualTo("module", module.key)
- .andGreaterThan("questionId", 0)
- .andCondition(String.format(formatSet, structId, "module_struct"))
- );
- example.and(
- example.createCriteria()
- .andIsNull("deleteTime")
- );
- example.orderBy("no").asc();
- return select(questionNoMapper, example);
- }
- /**
- * 获取结构模块下的题目列表: 按序号排列
- * @param module
- * @param structIds
- * @return
- */
- public List<QuestionNo> listByStruct(StructModule module, Integer[] structIds){
- Example example = new Example(QuestionNo.class);
- example.and(
- example.createCriteria()
- .andEqualTo("module", module.key)
- .andGreaterThan("questionId", 0)
- );
- if (structIds != null){
- Example.Criteria criteria = example.createCriteria();
- for(Integer structId : structIds){
- criteria.orCondition(String.format(formatSet, structId, "module_struct"));
- }
- example.and(criteria);
- }
- example.and(
- example.createCriteria()
- .andIsNull("deleteTime")
- );
- example.orderBy("no").asc();
- return select(questionNoMapper, example);
- }
- /**
- * 获取结构模块下的题目列表: 按序号排列
- * @param module
- * @param structId
- * @return
- */
- public List<QuestionNoRelation> listWithRelationByStruct(StructModule module, Integer structId){
- return relation(listByStruct(module, structId));
- }
- /**
- * 根据题目标号列表获取题目
- * @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)
- );
- example.and(
- example.createCriteria()
- .andIsNull("deleteTime")
- );
- 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)
- );
- example.and(
- example.createCriteria()
- .andIsNull("deleteTime")
- );
- 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);
- }
- /**
- * 随机获取对应模块下的试题:排除已做试题
- * @param structId
- * @param filterIds
- * @return
- */
- public Integer randomExamination(Integer structId, Collection targetTypes, Collection filterIds){
- List<QuestionNo> questionNoList = questionNoRelationMapper.randomExamination(structId, targetTypes, filterIds, 1);
- if (questionNoList.size() > 0){
- return questionNoList.get(0).getId();
- }else{
- return null;
- }
- }
- /**
- * 随机批量获取对应模块下的试题:排除已做试题
- * @param structId
- * @param targetTypes
- * @param filterIds
- * @param size
- * @return
- */
- public Integer[] randomExaminationList(Integer structId, Collection targetTypes, Collection filterIds, Integer size){
- List<QuestionNo> questionNoList = questionNoRelationMapper.randomExamination(structId, targetTypes, filterIds, size);
- if (questionNoList.size() > 0){
- Integer[] ids = new Integer[questionNoList.size()];
- for(int i = 0; i< questionNoList.size(); i++){
- ids[i] = questionNoList.get(i).getId();
- }
- return ids;
- }else{
- return new Integer[0];
- }
- }
- /**
- * 获取该struct下指定类型的所有题目信息
- * @param structId
- * @param targetTypes
- * @return
- */
- public List<QuestionDifficultRelation> allExaminationByType(Integer structId, Collection targetTypes){
- return questionNoRelationMapper.allExaminationByType(structId, targetTypes);
- }
- /**
- * 随机查找模考阅读题
- * @param structId
- * @param number
- * @param filterIds
- * @return
- */
- public Integer[] randomExaminationRc(Integer structId, Integer number, Collection filterIds){
- Example example = new Example(QuestionNo.class);
- example.and(
- example.createCriteria()
- .andGreaterThan("questionId", 0)
- .andCondition(String.format(formatSet, structId, "module_struct"))
- .andEqualTo("relationNumber", number)
- .andNotIn("id", filterIds)
- );
- example.and(
- example.createCriteria()
- .andIsNull("deleteTime")
- );
- example.setOrderByClause("RAND()");
- QuestionNo questionNo = one(questionNoMapper, example);
- if (questionNo == null){
- throw new ParameterException("阅读题查找失败");
- }
- return Arrays.stream(questionNo.getRelationQuestion()).boxed().toArray(Integer[]::new);
- }
- /**
- * 绑定no和question
- * @param ids
- * @param questionId
- * @return
- */
- public Boolean bindQuestion(Integer[] ids, Integer questionId){
- if (ids == null || 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)
- .relationNumber(ids.length)
- .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){
- if (questionNo == null) continue;
- 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.getUserTime(), question.getIsCorrect());
- }
- /**
- * 累加收藏记录到questionNo
- * @param question
- */
- public void accumulationCollect(UserCollectQuestion question, int collect){
- questionNoRelationMapper.accumulationCollect(question.getQuestionNoId(), collect);
- }
- /**
- * 根据试卷分组获取统计信息
- * @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;
- }
- /**
- * 根据试卷分组获取统计信息
- * @param questionNoIdsMap
- * @return
- */
- public Map<Integer, List<String>> questionTypeMap(Map<Integer, Integer[]> questionNoIdsMap){
- Map<Integer, List<String>> 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);
- List<QuestionNoRelation> relationList = relation(questionNoList);
- Map questionNoMap = Transform.getMap(relationList, QuestionNoRelation.class, "id");
- for(Integer k: questionNoIdsMap.keySet()){
- List<String> l = new ArrayList<>();
- for (Integer questionNoId : questionNoIdsMap.get(k)){
- QuestionNoRelation relation = (QuestionNoRelation)questionNoMap.get(questionNoId);
- if (relation == null) continue;
- if (relation.getQuestion() == null) continue;
- if (!l.contains(relation.getQuestion().getQuestionType())){
- l.add(relation.getQuestion().getQuestionType());
- }
- }
- relationMap.put(k, 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){
- if (in.getQuestionId() == 0){
- return in;
- }
- throw new ParameterException("题目已经存在");
- }
- int result = insert(questionNoMapper, question);
- question = one(questionNoMapper, question.getId());
- if(question == null){
- throw new SystemException("题目添加失败");
- }
- return question;
- }
- @Transactional
- 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, true);
- 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 Page<QuestionNo> select(Integer[] ids){
- return page(()->select(questionNoMapper, ids), 1, ids.length);
- }
- public List<QuestionNo> select(Collection ids){
- return select(questionNoMapper, ids);
- }
- }
|