271 lines
11 KiB
Java
271 lines
11 KiB
Java
package com.zhangmeng.blog.controller;
|
|
|
|
import com.github.pagehelper.PageInfo;
|
|
import com.zhangmeng.api.service.blog.ArticleControllerApi;
|
|
import com.zhangmeng.blog.feign.SysConfigFeign;
|
|
import com.zhangmeng.blog.feign.UserFeign;
|
|
import com.zhangmeng.blog.service.ArticleService;
|
|
import com.zhangmeng.blog.service.CategoryService;
|
|
import com.zhangmeng.blog.tools.BlogTools;
|
|
import com.zhangmeng.model.base.baseController.BaseController;
|
|
import com.zhangmeng.model.base.baseUtil.CommonUtil;
|
|
import com.zhangmeng.model.dto.article.ArticleDto;
|
|
import com.zhangmeng.model.dto.article.ArticleModel;
|
|
import com.zhangmeng.model.dto.query.QueryParams;
|
|
import com.zhangmeng.model.entity.Article;
|
|
import com.zhangmeng.model.entity.Category;
|
|
import com.zhangmeng.model.entity.SysConfig;
|
|
import com.zhangmeng.model.entity.User;
|
|
import com.zhangmeng.model.vo.Result;
|
|
import com.zhangmeng.model.vo.StatusCode;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
import springfox.documentation.annotations.ApiIgnore;
|
|
import tk.mybatis.mapper.entity.Condition;
|
|
import tk.mybatis.mapper.entity.Example;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@RestController
|
|
@RequestMapping("/article")
|
|
public class ArticleController extends BaseController implements ArticleControllerApi {
|
|
|
|
@Autowired
|
|
private ArticleService articleService;
|
|
|
|
@Autowired
|
|
private CategoryService categoryService;
|
|
|
|
@Autowired
|
|
private UserFeign userFeign;
|
|
|
|
@Autowired
|
|
private SysConfigFeign sysConfigFeign;
|
|
|
|
@Autowired
|
|
private BlogTools blogTools;
|
|
|
|
@Override
|
|
@PostMapping("/save")
|
|
public Result save(@RequestParam @RequestBody Map<String, Object> parms) {
|
|
String articleId = CommonUtil.map_get_value(parms, "articleId");
|
|
Article article = null;
|
|
boolean flag = false;
|
|
if (CommonUtil.isNull(articleId)) {
|
|
article = new Article();
|
|
article.setAddTime(new Date());
|
|
article.setUpdateTime(new Date());
|
|
article.setDeleteStatus(false);
|
|
flag = true;
|
|
} else {
|
|
article = this.articleService.findById(Long.parseLong(articleId));
|
|
}
|
|
|
|
String title = CommonUtil.map_get_value(parms, "title");
|
|
if (CommonUtil.isNotNull(title)) {
|
|
article.setTitle(title);
|
|
}
|
|
|
|
String intro = CommonUtil.map_get_value(parms, "intro");
|
|
if (CommonUtil.isNotNull(intro)) {
|
|
article.setIntro(intro);
|
|
}
|
|
|
|
String categoryId = CommonUtil.map_get_value(parms, "categoryId");
|
|
if (CommonUtil.isNotNull(categoryId) && !categoryId.equals("0")) {
|
|
article.setCategory_id(Long.parseLong(categoryId));
|
|
Category category = this.categoryService.findById(Long.parseLong(categoryId));
|
|
article.setCategoryName(category.getCategoryName());
|
|
}
|
|
|
|
String content_editormd_markdown_doc = CommonUtil.map_get_value(parms, "content-editormd-markdown-doc");
|
|
if (CommonUtil.isNotNull(content_editormd_markdown_doc)) {
|
|
article.setContentEditormdMarkdownDoc(content_editormd_markdown_doc);
|
|
}
|
|
|
|
String content_editormd_html_code = CommonUtil.map_get_value(parms, "content-editormd-html-code");
|
|
if (CommonUtil.isNotNull(content_editormd_html_code)) {
|
|
article.setContent(content_editormd_html_code);
|
|
article.setContentEditormdHtmlCode(content_editormd_html_code);
|
|
}
|
|
|
|
String keyWords = CommonUtil.map_get_value(parms, "keyWords");
|
|
if (CommonUtil.isNotNull(keyWords)) {
|
|
article.setKeyWords(keyWords);
|
|
}
|
|
|
|
String status = CommonUtil.map_get_value(parms, "status");
|
|
if (CommonUtil.isNotNull(status)) {
|
|
Article.Status status1 = CommonUtil.getEnum(status, Article.Status.class);
|
|
article.setStatus(status1);
|
|
}
|
|
|
|
//article_upload_image
|
|
String article_upload_image = CommonUtil.map_get_value(parms, "article_upload_image");
|
|
String file_id = CommonUtil.map_get_value(parms, "file_id");
|
|
if (CommonUtil.isNotNull(article_upload_image)){
|
|
article.setPhotoPath(article_upload_image);
|
|
article.setPhoto_id(Long.parseLong(file_id));
|
|
}
|
|
if (flag){
|
|
article.setViews(0);
|
|
User currentUser = this.userFeign.getCurrentUser();
|
|
article.setUser_id(currentUser == null ? 1L : currentUser.getId());
|
|
article.setUsername(currentUser == null? "admin":currentUser.getUsername());
|
|
this.articleService.save(article);
|
|
//存放链接
|
|
article.setLinks("/blog/details/"+article.getId());
|
|
this.articleService.update(article);
|
|
}else {
|
|
this.articleService.update(article);
|
|
}
|
|
return this.success("保存成功");
|
|
}
|
|
|
|
@Override
|
|
@GetMapping("/list")
|
|
public Result list(Integer pageNum, Integer pageSize, String startTime, String endTime, String title,Long categoryId) {
|
|
Condition condition = new Condition(Article.class);
|
|
Example.Criteria criteria = condition.createCriteria();
|
|
if (startTime != null && !startTime.equals("") && endTime != null && !endTime.equals("")) {
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("");
|
|
criteria.andBetween("addTime", startTime, endTime);
|
|
}
|
|
if (title != null) {
|
|
criteria.andLike("title", "%" + title + "%");
|
|
}
|
|
if (categoryId != null && categoryId != 0L){
|
|
criteria.andEqualTo("category_id",categoryId);
|
|
}
|
|
criteria.andEqualTo("deleteStatus", false);
|
|
PageInfo<Article> pageInfo = this.articleService.findByCondition(new QueryParams(pageNum, pageSize, condition),true);
|
|
return new Result(true, StatusCode.OK, "查询成功", pageInfo.getTotal(),pageInfo.getList());
|
|
}
|
|
|
|
@ApiOperation("删除文章")
|
|
@PostMapping("/delete")
|
|
public Result delete( String articleId) {
|
|
if (CommonUtil.isNotNull(articleId)){
|
|
this.articleService.deleteById(Long.parseLong(articleId));
|
|
}
|
|
return new Result(true,StatusCode.OK,"删除成功");
|
|
}
|
|
|
|
@Override
|
|
@PostMapping("/batchRemove")
|
|
public Result batchRemove( String ids) {
|
|
String id_s = CommonUtil.firstLastComma(ids);
|
|
String[] strings = id_s.split(",");
|
|
if (strings.length > 0){
|
|
for (String id : strings) {
|
|
this.articleService.deleteById(Long.parseLong(id));
|
|
}
|
|
}
|
|
return new Result(true,StatusCode.OK,"删除成功");
|
|
}
|
|
|
|
@Override
|
|
@GetMapping("/selectCountEq")
|
|
public Integer selectCountEq(boolean deleteStatus){
|
|
return this.articleService.selectCountEq("deleteStatus",deleteStatus);
|
|
}
|
|
|
|
@Override
|
|
@GetMapping("/selectCountByDayOfPass")
|
|
public List<Integer> selectCountByDayOfPass(@RequestBody @RequestParam(value = "x_date") List<String> x_date){
|
|
return this.articleService.selectCountByDayOfPass(x_date);
|
|
}
|
|
|
|
@Override
|
|
@GetMapping("/findByCondition")
|
|
public List<Article> findByCondition(){
|
|
return this.articleService.findByCondition(new QueryParams(new Condition(Article.class)));
|
|
}
|
|
|
|
@Override
|
|
@GetMapping("/findById")
|
|
public Article findById(@RequestParam Long articleId){
|
|
return this.articleService.findById(articleId);
|
|
}
|
|
|
|
|
|
@Override
|
|
@GetMapping("/getListByPage")
|
|
public Map<String,Object> getListByPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
|
|
Map<String,Object> map = new HashMap<>();
|
|
ArticleModel articleModel = new ArticleModel();
|
|
//文章总数
|
|
Condition article_condition = new Condition(Article.class);
|
|
Example.Criteria article_criteria = article_condition.createCriteria();
|
|
article_criteria.andEqualTo("deleteStatus", false);
|
|
//文章列表
|
|
PageInfo<Article> pageInfo = this.articleService.findByCondition(new QueryParams(pageNum, pageSize, article_condition, "addTime desc"), true);
|
|
articleModel.setPrePage(pageInfo.getPrePage());
|
|
articleModel.setNextPage(pageInfo.getNextPage());
|
|
articleModel.setPageNum(pageInfo.getPageNum());
|
|
articleModel.setPageSize(pageInfo.getPageSize());
|
|
List<ArticleDto> article_dto_list = new ArrayList<>();
|
|
if (pageInfo.getList().size() > 0) {
|
|
for (Article article : pageInfo.getList()) {
|
|
ArticleDto articleDto = new ArticleDto();
|
|
BeanUtils.copyProperties(article, articleDto);
|
|
Long user_id = article.getUser_id();
|
|
User user = this.userFeign.findByUserId(user_id);
|
|
articleDto.setAvatar(user.getAvatar());
|
|
article_dto_list.add(articleDto);
|
|
}
|
|
}
|
|
articleModel.setArticleDto_list(article_dto_list);
|
|
|
|
SysConfig sysConfig = this.sysConfigFeign.getSysConfig();
|
|
if (sysConfig == null){
|
|
sysConfig = new SysConfig();
|
|
sysConfig.setAuthor("转身的背影在心底里沉沦");
|
|
}
|
|
if (sysConfig.getAuthor() == null){
|
|
sysConfig.setAuthor("转身的背影在心底里沉沦");
|
|
}
|
|
map.put("sysConfig",sysConfig);
|
|
map.put("articleModel", articleModel);
|
|
map.put("title",this.blogTools.title());
|
|
map.put("blog_count",blogTools.blog_count());
|
|
map.put("categoryList",blogTools.categoryList());
|
|
map.put("latest_recommended",blogTools.latest_recommended());
|
|
map.put("latest_blog",blogTools.latest_blog());
|
|
map.put("user",blogTools.userInfo());
|
|
map.put("message",blogTools.message());
|
|
return map;
|
|
}
|
|
|
|
@Override
|
|
@GetMapping("/blog_common_page")
|
|
public Map<String,Object> blog_common_page(){
|
|
Map<String,Object> map = new HashMap<>();
|
|
map.put("title",this.blogTools.title());
|
|
map.put("blog_count",blogTools.blog_count());
|
|
map.put("categoryList",blogTools.categoryList());
|
|
map.put("latest_recommended",blogTools.latest_recommended());
|
|
map.put("latest_blog",blogTools.latest_blog());
|
|
map.put("user",blogTools.userInfo());
|
|
map.put("message",blogTools.message());
|
|
map.put("tagsList",blogTools.tagsList());
|
|
return map;
|
|
}
|
|
|
|
@Override
|
|
@GetMapping("/comment_list")
|
|
public Map<String,Object> comment_list(@RequestParam("article_id") Long article_id){
|
|
Map<String,Object> map = new HashMap<>();
|
|
map.put("commentModelList",blogTools.commentModelList(article_id));
|
|
return map;
|
|
}
|
|
}
|