package com.izouma.jmrh.web;

import com.izouma.jmrh.annotations.OperLog;
import com.izouma.jmrh.domain.Article;
import com.izouma.jmrh.dto.ArticleDTO;
import com.izouma.jmrh.dto.PageQuery;
import com.izouma.jmrh.enums.AuditStatus;
import com.izouma.jmrh.exception.BusinessException;
import com.izouma.jmrh.repo.ArticleRepo;
import com.izouma.jmrh.service.ArticleService;
import com.izouma.jmrh.utils.ObjUtils;
import com.izouma.jmrh.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/article")
@AllArgsConstructor
@Slf4j
public class ArticleController extends BaseController {
    private ArticleService articleService;
    private ArticleRepo    articleRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    @OperLog(value = "发布管理-编辑", type = "编辑")
    public Article save(@RequestBody Article record) {
        log.info("Article record是:{}--->",record);
        if (record.getId() != null) {
            Article orig = articleRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return articleRepo.save(orig);
        }
        record.setStatus(AuditStatus.PENDING);
        return articleRepo.save(record);
    }

    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<Article> all(PageQuery pageQuery) {
        Page<Article> all = articleRepo.findAll(toSpecification(pageQuery, Article.class), toPageRequest(pageQuery));
        return all;
    }

    @GetMapping("/count")
    public long count(PageQuery pageQuery) {
        pageQuery.getQuery().put("status", AuditStatus.PENDING);
        return articleRepo.count(toSpecification(pageQuery, Article.class));
    }

    @GetMapping("/get/{id}")
    public Article get(@PathVariable Long id) {
        return articleRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    @OperLog(value = "发布管理-删除", type = "删除")
    public void del(@PathVariable Long id) {
        articleRepo.deleteById(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    @OperLog(value = "发布管理-导出", type = "导出")
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<ArticleDTO> data = all(pageQuery).getContent().stream().map(article -> {
            ArticleDTO dto = new ArticleDTO();
            BeanUtils.copyProperties(article, dto);
            dto.setPublish(article.isPublish() ? "是" : "否");
            dto.setStatus(article.getStatus().getDescription());
            return dto;
        }).collect(Collectors.toList());
        ExcelUtils.export(response, data);
    }

    @PostMapping("/audit")
    @OperLog(value = "发布管理-审核", type = "审核")
    public void audit(@RequestParam Long id, @RequestParam boolean pass) {
        Article article = articleRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        article.setStatus(pass ? AuditStatus.PASS : AuditStatus.DENY);
        article.setAuditTime(LocalDateTime.now());
        articleRepo.save(article);
    }
}

