package com.izouma.jmrh.web;

import com.izouma.jmrh.annotations.OperLog;
import com.izouma.jmrh.domain.Article;
import com.izouma.jmrh.domain.BaseEntity;
import com.izouma.jmrh.domain.Post;
import com.izouma.jmrh.dto.MyPostAndComment;
import com.izouma.jmrh.dto.PageQuery;
import com.izouma.jmrh.dto.PostDTO;
import com.izouma.jmrh.enums.AuditStatus;
import com.izouma.jmrh.enums.PostType;
import com.izouma.jmrh.exception.BusinessException;
import com.izouma.jmrh.repo.PostRepo;
import com.izouma.jmrh.service.PostService;
import com.izouma.jmrh.utils.SecurityUtils;
import com.izouma.jmrh.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
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.Optional;

@RestController
@RequestMapping("/post")
@AllArgsConstructor
public class PostController extends BaseController {
    private PostService postService;
    private PostRepo    postRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    @OperLog(value = "互动管理-编辑", type = "编辑")
    public Post save(@RequestBody Post record) {
        return postService.save(record);
    }

    @PostMapping("/create")
    @OperLog(value = "互动管理-创建", type = "创建")
    public Post create(@RequestBody Post record) {
        return postService.create(SecurityUtils.getAuthenticatedUser().getId(), record);
    }

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

    @GetMapping("/count")
    public long count(PageQuery pageQuery) {
        return postRepo.count(toSpecification(pageQuery, Post.class));
    }

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

    @PostMapping("/del/{id}")
    @OperLog(value = "互动管理-删除", type = "删除")
    public void del(@PathVariable Long id) {
        postService.del(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<Post> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/view/{id}")
    public void view(@PathVariable Long id) {
        postService.view(id);
    }

    @GetMapping("/like")
    public void like(@RequestParam Long postId, @RequestParam boolean dislike) {
        postService.like(postId, dislike);
    }

    @GetMapping("/allDTO")
    public Page<PostDTO> allDTO(PageQuery pageQuery) {
        pageQuery.getQuery().put("del", false);
        return postService.allDTO(pageQuery);
    }

    @GetMapping("/my")
    public List<MyPostAndComment> my(@RequestParam PostType type) {
        return postService.my(SecurityUtils.getAuthenticatedUser().getId(), type);
    }

    @GetMapping("/getDTO/{id}")
    public PostDTO getDTO(@PathVariable Long id) {
        return postService.getDTO(Optional.ofNullable(SecurityUtils.getAuthenticatedUser()).map(BaseEntity::getId)
                .orElse(null), id);
    }

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

