package com.cyksj.web.controller.manage.article;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.mini.ArticleRecommendMapper;
import com.cyksj.model.entity.Article;
import com.cyksj.model.entity.ArticleRecommend;
import com.cyksj.model.views.ArticleView;
import com.cyksj.service.mange.article.ArticleService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.stream.Collectors;

/*
 *项目名: netflix
 *文件名: ArticleController
 *创建者: JavaZou
 *创建时间:2023/6/27 15:02
 */
@RestController
@RequestMapping("/manage/article")
@RequiredArgsConstructor
public class ArticleController {
	private final ArticleService articleService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final ArticleRecommendMapper articleRecommendMapper;

	/**
	 * 文章列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<ArticleView>> get() {
		SearchResult<ArticleView> search = beanSearcher.search(ArticleView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		search.getDataList().forEach(data->{
			data.setOtherArticleIds(articleRecommendMapper.selectList(Wrappers.lambdaQuery(ArticleRecommend.class)
					.eq(ArticleRecommend::getRecommendId, data.getId())).stream().map(ArticleRecommend::getId).collect(Collectors.toList()));
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 新建文章
	 */
	@PostMapping("/post")
	public Result<String> post(@RequestBody Article dto) {
		articleService.saveOrUpdateArticle(dto);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 修改文章
	 */
	@PutMapping("/put")
	public Result<String> update(@RequestBody Article dto) {
		articleService.saveOrUpdateArticle(dto);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 文章详情
	 */
	@GetMapping("/get/detail/{id}")
	public Result<Article> getArticleDetail(@PathVariable Long id) {
		Article article = articleService.getArticleDetail(id);
		Assert.notNull(article, "文章不存在");
		return GatewayResponse.SUCCESS.newBuilder().toResult(article);
	}

	/**
	 * 删除文章
	 */
	@DeleteMapping("/delete/{id}")
	public Result<String> deleteById(@PathVariable Long id) {
		articleService.update(null, Wrappers.lambdaUpdate(Article.class)
				.set(Article::getDeleted, false)
				.eq(Article::getId, id));
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 上下架文章
	 */
	@PutMapping("/put/status/{id}")
	public Result<String> updateStatus(@PathVariable Long id) {
		Article article = articleService.getById(id);
		Assert.notNull(article, "文章不存在");
		article.setStatus(!article.getStatus());
		articleService.updateById(article);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
