|
|
|
@ -1,27 +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.Page; |
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 论文管理接口 |
|
|
|
@ -41,12 +43,20 @@ 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())) { |
|
|
|
|
Paper paper = uploadPaper.toPaper(); |
|
|
|
|
paper.setUserName(username); |
|
|
|
|
paperRepository.save(paper); |
|
|
|
|
if (paperRepository.existsById(paper.getId())) { |
|
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
@ -64,9 +74,7 @@ public class PaperController { |
|
|
|
|
public ResponseData list(QueryPaper queryPaper, @CookieValue("user") String username) { |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
Page<Paper> list=new Page<>(); |
|
|
|
|
list.setTotal(paperRepository.count()); |
|
|
|
|
List<Paper> papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { |
|
|
|
|
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() + "%")); |
|
|
|
@ -90,14 +98,15 @@ public class PaperController { |
|
|
|
|
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(); |
|
|
|
|
}); |
|
|
|
|
return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); |
|
|
|
|
}, PageRequest.of(queryPaper.getPage() - 1, 10, Sort.Direction.DESC, "createTime")); |
|
|
|
|
papers.forEach(paper -> { |
|
|
|
|
paper.setHtmlContent(null); |
|
|
|
|
paper.setPdfContent(null); |
|
|
|
|
paper.setTxtContent(null); |
|
|
|
|
paper.setHasCollect(collectRepository.existsByPaperIdAndUserName(paper.getId(), username)); |
|
|
|
|
}); |
|
|
|
|
list.setData(papers); |
|
|
|
|
return ResponseDataUtil.buildSuccess(list); |
|
|
|
|
return ResponseDataUtil.buildSuccess(papers); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
logger.error(String.valueOf(e)); |
|
|
|
@ -108,12 +117,48 @@ public class PaperController { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 论文推荐 |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@GetMapping("recommend") |
|
|
|
|
public ResponseData recommend(){ |
|
|
|
|
List<Paper> papers=paperRepository.recommend(); |
|
|
|
|
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") |
|
|
|
@ -144,4 +189,15 @@ public class PaperController { |
|
|
|
|
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(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|