|
|
@ -1,28 +1,29 @@ |
|
|
|
package com.bupt.note.Controller; |
|
|
|
package com.bupt.note.Controller; |
|
|
|
|
|
|
|
|
|
|
|
import com.bupt.note.Model.Paper; |
|
|
|
import com.bupt.note.Model.*; |
|
|
|
import com.bupt.note.Model.Rating; |
|
|
|
import com.bupt.note.Repository.*; |
|
|
|
import com.bupt.note.Repository.CollectRepository; |
|
|
|
|
|
|
|
import com.bupt.note.Repository.PaperRepository; |
|
|
|
|
|
|
|
import com.bupt.note.Repository.RatingRepository; |
|
|
|
|
|
|
|
import com.bupt.note.ResponseData.ResponseData; |
|
|
|
import com.bupt.note.ResponseData.ResponseData; |
|
|
|
import com.bupt.note.ResponseData.ResponseDataUtil; |
|
|
|
import com.bupt.note.ResponseData.ResponseDataUtil; |
|
|
|
import com.bupt.note.dto.QueryPaper; |
|
|
|
import com.bupt.note.dto.QueryPaper; |
|
|
|
import com.bupt.note.dto.RatingForm; |
|
|
|
import com.bupt.note.dto.RatingForm; |
|
|
|
import com.bupt.note.dto.UploadPaper; |
|
|
|
import com.bupt.note.dto.UploadPaper; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.hibernate.query.criteria.internal.OrderImpl; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
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.Page; |
|
|
|
import org.springframework.data.domain.PageRequest; |
|
|
|
import org.springframework.data.domain.PageRequest; |
|
|
|
|
|
|
|
import org.springframework.data.domain.Sort; |
|
|
|
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
|
|
|
|
import javax.persistence.criteria.Order; |
|
|
|
|
|
|
|
import javax.persistence.criteria.Predicate; |
|
|
|
import javax.persistence.criteria.Predicate; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Collection; |
|
|
|
import java.util.List; |
|
|
|
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") |
|
|
|
@RequestMapping(value = "/v1/api/paper") |
|
|
|
public class PaperController { |
|
|
|
public class PaperController { |
|
|
|
|
|
|
|
|
|
|
|
private Logger logger= LoggerFactory.getLogger(PaperController.class); |
|
|
|
private Logger logger = LoggerFactory.getLogger(PaperController.class); |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private PaperRepository paperRepository; |
|
|
|
private PaperRepository paperRepository; |
|
|
@ -42,59 +43,68 @@ public class PaperController { |
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private CollectRepository collectRepository; |
|
|
|
private CollectRepository collectRepository; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
|
|
private PaperNoteRepository paperNoteRepository; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
|
|
private SimilarityRepository similarityRepository; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${score}") |
|
|
|
|
|
|
|
private Integer score; |
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("upload") |
|
|
|
@PostMapping("upload") |
|
|
|
public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) { |
|
|
|
public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) { |
|
|
|
if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(),uploadPaper.getType(), |
|
|
|
if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(), uploadPaper.getType(), |
|
|
|
uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){ |
|
|
|
uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(), uploadPaper.getTag())) { |
|
|
|
Paper paper = uploadPaper.toPaper(); |
|
|
|
Paper paper = uploadPaper.toPaper(); |
|
|
|
paperRepository.save(paper); |
|
|
|
paperRepository.save(paper); |
|
|
|
if(paperRepository.existsById(paper.getId())){ |
|
|
|
if (paperRepository.existsById(paper.getId())) { |
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
}else{ |
|
|
|
} else { |
|
|
|
logger.error("论文上传失败"); |
|
|
|
logger.error("论文上传失败"); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
} |
|
|
|
} |
|
|
|
}else{ |
|
|
|
} else { |
|
|
|
logger.error("论文表单校验失败"); |
|
|
|
logger.error("论文表单校验失败"); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("list") |
|
|
|
@GetMapping("list") |
|
|
|
public ResponseData list(QueryPaper queryPaper,@CookieValue("user") String username){ |
|
|
|
public ResponseData list(QueryPaper queryPaper, @CookieValue("user") String username) { |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
// Page<Paper> list=new Page<>();
|
|
|
|
|
|
|
|
// list.setTotal(paperRepository.count());
|
|
|
|
|
|
|
|
Page<Paper> papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { |
|
|
|
Page<Paper> papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { |
|
|
|
List<Predicate> predicates = new ArrayList<>(); |
|
|
|
List<Predicate> predicates = new ArrayList<>(); |
|
|
|
if(StringUtils.isNotEmpty(queryPaper.getTitle())){ |
|
|
|
if (StringUtils.isNotEmpty(queryPaper.getTitle())) { |
|
|
|
predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%")); |
|
|
|
predicates.add(criteriaBuilder.like(root.get("title"), "%" + queryPaper.getTitle() + "%")); |
|
|
|
} |
|
|
|
} |
|
|
|
if(StringUtils.isNotEmpty(queryPaper.getAuthor())){ |
|
|
|
if (StringUtils.isNotEmpty(queryPaper.getAuthor())) { |
|
|
|
predicates.add(criteriaBuilder.like(root.get("author"),"%"+queryPaper.getAuthor()+"%")); |
|
|
|
predicates.add(criteriaBuilder.like(root.get("author"), "%" + queryPaper.getAuthor() + "%")); |
|
|
|
} |
|
|
|
} |
|
|
|
if(StringUtils.isNotEmpty(queryPaper.getTag())){ |
|
|
|
if (StringUtils.isNotEmpty(queryPaper.getTag())) { |
|
|
|
predicates.add(criteriaBuilder.like(root.get("tag"),"%"+queryPaper.getTag()+"%")); |
|
|
|
predicates.add(criteriaBuilder.like(root.get("tag"), "%" + queryPaper.getTag() + "%")); |
|
|
|
} |
|
|
|
} |
|
|
|
if(StringUtils.isNotEmpty(queryPaper.getProfession())){ |
|
|
|
if (StringUtils.isNotEmpty(queryPaper.getProfession())) { |
|
|
|
predicates.add(criteriaBuilder.like(root.get("profession"),"%"+queryPaper.getTag()+"%")); |
|
|
|
predicates.add(criteriaBuilder.like(root.get("profession"), "%" + queryPaper.getTag() + "%")); |
|
|
|
} |
|
|
|
} |
|
|
|
if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()==null){ |
|
|
|
if (queryPaper.getStartYear() != null && queryPaper.getEndYear() == null) { |
|
|
|
predicates.add(criteriaBuilder.ge(root.get("year"),queryPaper.getStartYear())); |
|
|
|
predicates.add(criteriaBuilder.ge(root.get("year"), queryPaper.getStartYear())); |
|
|
|
}else if(queryPaper.getStartYear()==null&&queryPaper.getEndYear()!=null){ |
|
|
|
} else if (queryPaper.getStartYear() == null && queryPaper.getEndYear() != null) { |
|
|
|
predicates.add(criteriaBuilder.le(root.get("year"),queryPaper.getEndYear())); |
|
|
|
predicates.add(criteriaBuilder.le(root.get("year"), queryPaper.getEndYear())); |
|
|
|
}else if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()!=null){ |
|
|
|
} else if (queryPaper.getStartYear() != null && queryPaper.getEndYear() != null) { |
|
|
|
predicates.add(criteriaBuilder.between(root.get("year"),queryPaper.getStartYear(),queryPaper.getEndYear())); |
|
|
|
predicates.add(criteriaBuilder.between(root.get("year"), queryPaper.getStartYear(), queryPaper.getEndYear())); |
|
|
|
} |
|
|
|
} |
|
|
|
if(queryPaper.getOwn()){ |
|
|
|
if (queryPaper.getOwn()) { |
|
|
|
predicates.add(criteriaBuilder.equal(root.get("userName"),username)); |
|
|
|
predicates.add(criteriaBuilder.equal(root.get("userName"), username)); |
|
|
|
} |
|
|
|
} |
|
|
|
Order order=new OrderImpl(root.get("createTime"),false); |
|
|
|
return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); |
|
|
|
return criteriaQuery.where(predicates.toArray(new Predicate[0])).orderBy(order).getRestriction(); |
|
|
|
}, PageRequest.of(queryPaper.getPage() - 1, 10, Sort.Direction.DESC, "createTime")); |
|
|
|
}, PageRequest.of(queryPaper.getPage()-1,10)); |
|
|
|
|
|
|
|
papers.forEach(paper -> { |
|
|
|
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); |
|
|
|
return ResponseDataUtil.buildSuccess(papers); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
@ -107,40 +117,87 @@ public class PaperController { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 论文推荐 |
|
|
|
* 论文推荐 |
|
|
|
|
|
|
|
* |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@GetMapping("recommend") |
|
|
|
@GetMapping("recommend") |
|
|
|
public ResponseData recommend(){ |
|
|
|
public ResponseData recommend(@CookieValue("user") String userName) { |
|
|
|
List<Paper> papers=paperRepository.recommend(); |
|
|
|
// 查询已收藏论文
|
|
|
|
|
|
|
|
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); |
|
|
|
return ResponseDataUtil.buildSuccess(papers); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return ResponseDataUtil.buildSuccess("暂无推荐数据"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("rating") |
|
|
|
@PostMapping("rating") |
|
|
|
public ResponseData rating(@RequestBody RatingForm ratingForm,@CookieValue("user") String userName) { |
|
|
|
public ResponseData rating(@RequestBody RatingForm ratingForm, @CookieValue("user") String userName) { |
|
|
|
if(ratingForm.getPaperId()!=null&&ratingForm.getScore1()!=null&&ratingForm.getScore2()!=null&&ratingForm.getScore3()!=null){ |
|
|
|
if (ratingForm.getPaperId() != null && ratingForm.getScore1() != null && ratingForm.getScore2() != null && ratingForm.getScore3() != null) { |
|
|
|
Rating rating=ratingForm.toRating(); |
|
|
|
Rating rating = ratingForm.toRating(); |
|
|
|
rating.setUserName(userName); |
|
|
|
rating.setUserName(userName); |
|
|
|
ratingRepository.save(rating); |
|
|
|
ratingRepository.save(rating); |
|
|
|
if(ratingRepository.existsById(rating.getId())){ |
|
|
|
if (ratingRepository.existsById(rating.getId())) { |
|
|
|
return ResponseDataUtil.buildSuccess(rating); |
|
|
|
return ResponseDataUtil.buildSuccess(rating); |
|
|
|
}else{ |
|
|
|
} else { |
|
|
|
logger.warn("论文保存失败"); |
|
|
|
logger.warn("论文保存失败"); |
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
} |
|
|
|
} |
|
|
|
}else{ |
|
|
|
} else { |
|
|
|
logger.error("论文表单校验失败"); |
|
|
|
logger.error("论文表单校验失败"); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("findRating") |
|
|
|
@GetMapping("findRating") |
|
|
|
public ResponseData findRating(Long paperId,@CookieValue("user") String userName){ |
|
|
|
public ResponseData findRating(Long paperId, @CookieValue("user") String userName) { |
|
|
|
if(paperId!=null&&StringUtils.isNotEmpty(userName)){ |
|
|
|
if (paperId != null && StringUtils.isNotEmpty(userName)) { |
|
|
|
Rating rating=ratingRepository.findByPaperIdAndUserName(paperId,userName); |
|
|
|
Rating rating = ratingRepository.findByPaperIdAndUserName(paperId, userName); |
|
|
|
return ResponseDataUtil.buildSuccess(rating); |
|
|
|
return ResponseDataUtil.buildSuccess(rating); |
|
|
|
}else{ |
|
|
|
} else { |
|
|
|
logger.error("论文评价表单校验失败"); |
|
|
|
logger.error("论文评价表单校验失败"); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
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(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|