|
|
|
@ -1,28 +1,29 @@ |
|
|
|
|
package com.bupt.note.Controller; |
|
|
|
|
|
|
|
|
|
import com.bupt.note.Model.Paper; |
|
|
|
|
import com.bupt.note.Model.Rating; |
|
|
|
|
import com.bupt.note.Repository.CollectRepository; |
|
|
|
|
import com.bupt.note.Repository.PaperRepository; |
|
|
|
|
import com.bupt.note.Repository.RatingRepository; |
|
|
|
|
import com.bupt.note.Model.*; |
|
|
|
|
import com.bupt.note.Repository.*; |
|
|
|
|
import com.bupt.note.ResponseData.ResponseData; |
|
|
|
|
import com.bupt.note.ResponseData.ResponseDataUtil; |
|
|
|
|
import com.bupt.note.dto.QueryPaper; |
|
|
|
|
import com.bupt.note.dto.RatingForm; |
|
|
|
|
import com.bupt.note.dto.UploadPaper; |
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
import org.hibernate.query.criteria.internal.OrderImpl; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
import org.springframework.data.domain.Page; |
|
|
|
|
import org.springframework.data.domain.PageRequest; |
|
|
|
|
import org.springframework.data.domain.Sort; |
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
|
|
import javax.persistence.criteria.Order; |
|
|
|
|
import javax.persistence.criteria.Predicate; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 论文管理接口 |
|
|
|
@ -31,7 +32,7 @@ import java.util.List; |
|
|
|
|
@RequestMapping(value = "/v1/api/paper") |
|
|
|
|
public class PaperController { |
|
|
|
|
|
|
|
|
|
private Logger logger= LoggerFactory.getLogger(PaperController.class); |
|
|
|
|
private Logger logger = LoggerFactory.getLogger(PaperController.class); |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private PaperRepository paperRepository; |
|
|
|
@ -42,59 +43,68 @@ public class PaperController { |
|
|
|
|
@Autowired |
|
|
|
|
private CollectRepository collectRepository; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private PaperNoteRepository paperNoteRepository; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private SimilarityRepository similarityRepository; |
|
|
|
|
|
|
|
|
|
@Value("${score}") |
|
|
|
|
private Integer score; |
|
|
|
|
|
|
|
|
|
@PostMapping("upload") |
|
|
|
|
public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) { |
|
|
|
|
if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(),uploadPaper.getType(), |
|
|
|
|
uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){ |
|
|
|
|
public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) { |
|
|
|
|
if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(), uploadPaper.getType(), |
|
|
|
|
uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(), uploadPaper.getTag())) { |
|
|
|
|
Paper paper = uploadPaper.toPaper(); |
|
|
|
|
paperRepository.save(paper); |
|
|
|
|
if(paperRepository.existsById(paper.getId())){ |
|
|
|
|
if (paperRepository.existsById(paper.getId())) { |
|
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
|
}else{ |
|
|
|
|
} else { |
|
|
|
|
logger.error("论文上传失败"); |
|
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
|
} |
|
|
|
|
}else{ |
|
|
|
|
} else { |
|
|
|
|
logger.error("论文表单校验失败"); |
|
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@GetMapping("list") |
|
|
|
|
public ResponseData list(QueryPaper queryPaper,@CookieValue("user") String username){ |
|
|
|
|
public ResponseData list(QueryPaper queryPaper, @CookieValue("user") String username) { |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
// Page<Paper> list=new Page<>();
|
|
|
|
|
// list.setTotal(paperRepository.count());
|
|
|
|
|
Page<Paper> papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { |
|
|
|
|
List<Predicate> predicates = new ArrayList<>(); |
|
|
|
|
if(StringUtils.isNotEmpty(queryPaper.getTitle())){ |
|
|
|
|
predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%")); |
|
|
|
|
if (StringUtils.isNotEmpty(queryPaper.getTitle())) { |
|
|
|
|
predicates.add(criteriaBuilder.like(root.get("title"), "%" + queryPaper.getTitle() + "%")); |
|
|
|
|
} |
|
|
|
|
if(StringUtils.isNotEmpty(queryPaper.getAuthor())){ |
|
|
|
|
predicates.add(criteriaBuilder.like(root.get("author"),"%"+queryPaper.getAuthor()+"%")); |
|
|
|
|
if (StringUtils.isNotEmpty(queryPaper.getAuthor())) { |
|
|
|
|
predicates.add(criteriaBuilder.like(root.get("author"), "%" + queryPaper.getAuthor() + "%")); |
|
|
|
|
} |
|
|
|
|
if(StringUtils.isNotEmpty(queryPaper.getTag())){ |
|
|
|
|
predicates.add(criteriaBuilder.like(root.get("tag"),"%"+queryPaper.getTag()+"%")); |
|
|
|
|
if (StringUtils.isNotEmpty(queryPaper.getTag())) { |
|
|
|
|
predicates.add(criteriaBuilder.like(root.get("tag"), "%" + queryPaper.getTag() + "%")); |
|
|
|
|
} |
|
|
|
|
if(StringUtils.isNotEmpty(queryPaper.getProfession())){ |
|
|
|
|
predicates.add(criteriaBuilder.like(root.get("profession"),"%"+queryPaper.getTag()+"%")); |
|
|
|
|
if (StringUtils.isNotEmpty(queryPaper.getProfession())) { |
|
|
|
|
predicates.add(criteriaBuilder.like(root.get("profession"), "%" + queryPaper.getTag() + "%")); |
|
|
|
|
} |
|
|
|
|
if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()==null){ |
|
|
|
|
predicates.add(criteriaBuilder.ge(root.get("year"),queryPaper.getStartYear())); |
|
|
|
|
}else if(queryPaper.getStartYear()==null&&queryPaper.getEndYear()!=null){ |
|
|
|
|
predicates.add(criteriaBuilder.le(root.get("year"),queryPaper.getEndYear())); |
|
|
|
|
}else if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()!=null){ |
|
|
|
|
predicates.add(criteriaBuilder.between(root.get("year"),queryPaper.getStartYear(),queryPaper.getEndYear())); |
|
|
|
|
if (queryPaper.getStartYear() != null && queryPaper.getEndYear() == null) { |
|
|
|
|
predicates.add(criteriaBuilder.ge(root.get("year"), queryPaper.getStartYear())); |
|
|
|
|
} else if (queryPaper.getStartYear() == null && queryPaper.getEndYear() != null) { |
|
|
|
|
predicates.add(criteriaBuilder.le(root.get("year"), queryPaper.getEndYear())); |
|
|
|
|
} else if (queryPaper.getStartYear() != null && queryPaper.getEndYear() != null) { |
|
|
|
|
predicates.add(criteriaBuilder.between(root.get("year"), queryPaper.getStartYear(), queryPaper.getEndYear())); |
|
|
|
|
} |
|
|
|
|
if(queryPaper.getOwn()){ |
|
|
|
|
predicates.add(criteriaBuilder.equal(root.get("userName"),username)); |
|
|
|
|
if (queryPaper.getOwn()) { |
|
|
|
|
predicates.add(criteriaBuilder.equal(root.get("userName"), username)); |
|
|
|
|
} |
|
|
|
|
Order order=new OrderImpl(root.get("createTime"),false); |
|
|
|
|
return criteriaQuery.where(predicates.toArray(new Predicate[0])).orderBy(order).getRestriction(); |
|
|
|
|
}, PageRequest.of(queryPaper.getPage()-1,10)); |
|
|
|
|
return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); |
|
|
|
|
}, PageRequest.of(queryPaper.getPage() - 1, 10, Sort.Direction.DESC, "createTime")); |
|
|
|
|
papers.forEach(paper -> { |
|
|
|
|
paper.setHasCollect(collectRepository.existsByPaperIdAndUserName(paper.getId(),username)); |
|
|
|
|
paper.setHtmlContent(null); |
|
|
|
|
paper.setPdfContent(null); |
|
|
|
|
paper.setTxtContent(null); |
|
|
|
|
paper.setHasCollect(collectRepository.existsByPaperIdAndUserName(paper.getId(), username)); |
|
|
|
|
}); |
|
|
|
|
return ResponseDataUtil.buildSuccess(papers); |
|
|
|
|
} catch (Exception e) { |
|
|
|
@ -107,40 +117,87 @@ public class PaperController { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 论文推荐 |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@GetMapping("recommend") |
|
|
|
|
public ResponseData recommend(){ |
|
|
|
|
List<Paper> papers=paperRepository.recommend(); |
|
|
|
|
return ResponseDataUtil.buildSuccess(papers); |
|
|
|
|
public ResponseData recommend(@CookieValue("user") String userName) { |
|
|
|
|
// 查询已收藏论文
|
|
|
|
|
List<Long> collectIds = collectRepository.findByUserName(userName).stream().map(Collect::getPaperId).collect(Collectors.toList()); |
|
|
|
|
//查询喜欢论文
|
|
|
|
|
List<Long> likeIds = ratingRepository.findLike(score); |
|
|
|
|
List<Long> ids = Stream.of(collectIds, likeIds).flatMap(Collection::stream).distinct().collect(Collectors.toList()); |
|
|
|
|
logger.info(String.format("找到论文id数%d", ids.size())); |
|
|
|
|
if (ids.size() > 0) { |
|
|
|
|
|
|
|
|
|
Page<Similarity> similarities = similarityRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { |
|
|
|
|
List<Predicate> predicates = new ArrayList<>(); |
|
|
|
|
predicates.add(criteriaBuilder.or(criteriaBuilder.and(root.get("id").get("sourcePaper").in(ids), |
|
|
|
|
criteriaBuilder.not(root.get("id").get("targetPaper").in(ids))), |
|
|
|
|
criteriaBuilder.and(root.get("id").get("targetPaper").in(ids), |
|
|
|
|
criteriaBuilder.not(root.get("id").get("sourcePaper").in(ids))))); |
|
|
|
|
return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); |
|
|
|
|
}, PageRequest.of(0, 10, Sort.Direction.DESC, "similarity")); |
|
|
|
|
|
|
|
|
|
logger.info(String.format("找到论文数%d", similarities.getTotalElements())); |
|
|
|
|
List<Long> paperIds=similarities.stream().map(similarity -> { |
|
|
|
|
if (ids.contains(similarity.getId().getSourcePaper())) { |
|
|
|
|
return similarity.getId().getTargetPaper(); |
|
|
|
|
} else { |
|
|
|
|
return similarity.getId().getSourcePaper(); |
|
|
|
|
} |
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Paper> papers = paperRepository.recommendWithSim(paperIds); |
|
|
|
|
papers.forEach(paper -> { |
|
|
|
|
paper.setTxtContent(null); |
|
|
|
|
paper.setPdfContent(null); |
|
|
|
|
paper.setHtmlContent(null); |
|
|
|
|
}); |
|
|
|
|
return ResponseDataUtil.buildSuccess(papers); |
|
|
|
|
} else { |
|
|
|
|
return ResponseDataUtil.buildSuccess("暂无推荐数据"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@PostMapping("rating") |
|
|
|
|
public ResponseData rating(@RequestBody RatingForm ratingForm,@CookieValue("user") String userName) { |
|
|
|
|
if(ratingForm.getPaperId()!=null&&ratingForm.getScore1()!=null&&ratingForm.getScore2()!=null&&ratingForm.getScore3()!=null){ |
|
|
|
|
Rating rating=ratingForm.toRating(); |
|
|
|
|
public ResponseData rating(@RequestBody RatingForm ratingForm, @CookieValue("user") String userName) { |
|
|
|
|
if (ratingForm.getPaperId() != null && ratingForm.getScore1() != null && ratingForm.getScore2() != null && ratingForm.getScore3() != null) { |
|
|
|
|
Rating rating = ratingForm.toRating(); |
|
|
|
|
rating.setUserName(userName); |
|
|
|
|
ratingRepository.save(rating); |
|
|
|
|
if(ratingRepository.existsById(rating.getId())){ |
|
|
|
|
if (ratingRepository.existsById(rating.getId())) { |
|
|
|
|
return ResponseDataUtil.buildSuccess(rating); |
|
|
|
|
}else{ |
|
|
|
|
} else { |
|
|
|
|
logger.warn("论文保存失败"); |
|
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
|
} |
|
|
|
|
}else{ |
|
|
|
|
} else { |
|
|
|
|
logger.error("论文表单校验失败"); |
|
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@GetMapping("findRating") |
|
|
|
|
public ResponseData findRating(Long paperId,@CookieValue("user") String userName){ |
|
|
|
|
if(paperId!=null&&StringUtils.isNotEmpty(userName)){ |
|
|
|
|
Rating rating=ratingRepository.findByPaperIdAndUserName(paperId,userName); |
|
|
|
|
public ResponseData findRating(Long paperId, @CookieValue("user") String userName) { |
|
|
|
|
if (paperId != null && StringUtils.isNotEmpty(userName)) { |
|
|
|
|
Rating rating = ratingRepository.findByPaperIdAndUserName(paperId, userName); |
|
|
|
|
return ResponseDataUtil.buildSuccess(rating); |
|
|
|
|
}else{ |
|
|
|
|
} else { |
|
|
|
|
logger.error("论文评价表单校验失败"); |
|
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@GetMapping(value = "content/{id}", produces = MediaType.TEXT_PLAIN_VALUE) |
|
|
|
|
public String findContent(@PathVariable("id") Long paperId, @CookieValue("user") String userName) { |
|
|
|
|
if (paperNoteRepository.existsByPaperIdAndUserName(paperId, userName)) { |
|
|
|
|
PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(paperId, userName); |
|
|
|
|
return paperNote.getTxtContent(); |
|
|
|
|
} else { |
|
|
|
|
Paper paper = paperRepository.getOne(paperId); |
|
|
|
|
return paper.getTxtContent(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|