Files
quinn-bbc/src/main/java/com/quinn/controller/SourceController.java
2022-05-07 01:52:25 +08:00

170 lines
5.8 KiB
Java

package com.quinn.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quinn.common.QuinnConstant;
import com.quinn.common.SourceType;
import com.quinn.pojo.*;
import com.quinn.pojo.param.QuerySource;
import com.quinn.service.SourceCategoryService;
import com.quinn.service.SourceCommentService;
import com.quinn.service.SourceService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
@Controller
public class SourceController {
@Resource
SourceService sourceService;
@Resource
SourceCategoryService sourceCategoryService;
@Resource
SourceCommentService sourceCommentService;
// 列表展示
@GetMapping("/source")
public String sourceList(Model model){
Page<Source> pageParam = new Page<>(1, 10);
QueryWrapper<Source> sourceQuery = new QueryWrapper<>();
sourceQuery.orderByDesc("gmt_create");
sourceService.page(pageParam,sourceQuery);
// 结果
List<Source> sourceList = pageParam.getRecords();
if (!CollectionUtils.isEmpty(sourceList)){
sourceList.forEach(x ->{
x.setSourceLink(QuinnConstant.GUN);
x.setKeyWord1(QuinnConstant.GUN);
x.setKeyWord2(QuinnConstant.GUN);
x.setKeyWord3(QuinnConstant.GUN);
x.setSourceContent(QuinnConstant.GUN);
});
}
model.addAttribute("sourceList",sourceList);
model.addAttribute("pageParam",pageParam);
// 分类信息
List<SourceCategory> categoryList = sourceCategoryService.list(null);
model.addAttribute("categoryList",categoryList);
return "source/list";
}
@PostMapping("/source/page")
public String blogListPage(QuerySource querySource, Model model){
int page = querySource.getPageNum();
int limit = querySource.getLimit();
if (querySource.getPageNum() < 1){
page = 1;
}
Page<Source> pageParam = new Page<>(page, limit);
QueryWrapper<Source> sourceQuery = new QueryWrapper<>();
addParam(sourceQuery,querySource.getName(),querySource.getCategory());
sourceQuery.orderByDesc("gmt_create");
sourceService.page(pageParam,sourceQuery);
// 结果
List<Source> blogList = pageParam.getRecords();
model.addAttribute("sourceList",blogList);
model.addAttribute("pageParam",pageParam);
return "source/list::s_table_refresh";
}
// 列表展示
@GetMapping("/hotspot")
public String sourceHotPot(Model model){
Page<Source> pageParam = new Page<>(1, 9);
QueryWrapper<Source> sourceQuery = new QueryWrapper<>();
sourceQuery.orderByDesc("down_record");
sourceService.page(pageParam,sourceQuery);
// 结果
List<Source> sourceList = pageParam.getRecords();
if (!CollectionUtils.isEmpty(sourceList)){
sourceList.forEach(x ->{
x.setSourceLink(QuinnConstant.GUN);
x.setKeyWord1(QuinnConstant.GUN);
x.setKeyWord2(QuinnConstant.GUN);
x.setKeyWord3(QuinnConstant.GUN);
x.setSourceContent(QuinnConstant.GUN);
});
}
model.addAttribute("sourceList",sourceList);
return "source/hotspot";
}
/**
* 查看下载资源的详情
* @param sid
* @param model
* @return
*/
@GetMapping("/source/view/{sid}")
public String read(@PathVariable("sid") String sid, Model model){
Source source = sourceService.view(sid);
if(source != null){
source.setSourceLink(QuinnConstant.GUN);
source.setKeyWord1(QuinnConstant.GUN);
source.setKeyWord2(QuinnConstant.GUN);
source.setKeyWord3(QuinnConstant.GUN);
source.setSourceType(QuinnConstant.GUN);
}
model.addAttribute("source",source);
List<SourceComment> commentList = sourceCommentService.list(new QueryWrapper<SourceComment>().eq("topic_id", sid).orderByDesc("gmt_create"));
model.addAttribute("commentList",commentList);
return "source/view";
}
/**
* 下载具体的文件
* @param response
* @param sid
* @throws IOException
*/
@PostMapping("/source/download/{sid}")
public void download(HttpServletResponse response, @PathVariable("sid") String sid) throws IOException {
Source source = sourceService.getOne(new QueryWrapper<Source>().eq("sid", sid));
if (source!=null){
if (SourceType.OSS.name().equals(source.getSourceType())){
//通知浏览器以附件形式下载
response.setHeader("Content-Disposition",
"attachment;filename=" + source.getEnName() + QuinnConstant.LINK_SUFFIX + source.getFileType());
this.sourceService.downloadSource(response,source);
}else {
this.sourceService.downloadForBaidu(response,source);
}
}
}
private void addParam(QueryWrapper<Source> sourceQuery, String name, int category) {
if (!StringUtils.isEmpty(name)){
sourceQuery.like("source_name",name);
}
if (category > 0){
sourceQuery.eq("category_id",category);
}
}
}