//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.luozhihui.project.controller;

import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.entity.Category;
import com.luozhihui.project.entity.Post;
import com.luozhihui.project.entity.Reply;
import com.luozhihui.project.mapper.PostMapper;
import com.luozhihui.project.mapper.ReplyMapper;
import com.luozhihui.project.mapper.CategoryMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api/forum"})
@CrossOrigin
public class ForumController {
    @Resource
    PostMapper postMapper;
    @Resource
    CategoryMapper categoryMapper;
    @Resource
    ReplyMapper replyMapper;

    public ForumController() {
    }

    @GetMapping({"/categories"})
    public ApiResponse listCategories() {
        try {
            List<Category> categories = this.categoryMapper.findAllCategories();
            return new ApiResponse(categories, 200, "获取分类成功");
        } catch (Exception var2) {
            return new ApiResponse((Object)null, 500, "服务器错误");
        }
    }

    @CrossOrigin
    @GetMapping({"/posts"})
    public ApiResponse listPosts(@RequestParam(required = false) Long categoryId, @RequestParam(required = false) String searchText, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        try {
            int offset = (page - 1) * size;
            List<Post> posts = this.postMapper.selectPostList(categoryId, searchText, offset, size);
            int total = this.postMapper.countPosts(categoryId, searchText);
            System.out.println(posts);
            System.out.println(total);
            Map<String, Object> result = new HashMap();
            result.put("records", posts);
            result.put("total", total);
            result.put("size", size);
            result.put("current", page);
            return new ApiResponse(result, 200, "获取帖子列表成功");
        } catch (Exception var9) {
            return new ApiResponse((Object)null, 500, "服务器错误");
        }
    }

    @GetMapping({"/posts/{id}"})
    public ApiResponse getPost(@PathVariable Long id) {
        try {
            Post post = this.postMapper.selectById(id);
            if (post == null) {
                return new ApiResponse((Object)null, 404, "帖子不存在");
            } else {
                post.setViewCount(post.getViewCount() + 1);
                this.postMapper.updateById(post);
                return new ApiResponse(post, 200, "获取帖子成功");
            }
        } catch (Exception var3) {
            return new ApiResponse((Object)null, 500, "服务器错误");
        }
    }

    @GetMapping({"/post"})
    public ApiResponse createPost(@RequestParam String title, @RequestParam String content, @RequestParam Integer categoryId, @RequestParam Integer userId) {
        try {
            Post post = new Post();
            post.setTitle(title);
            post.setContent(content);
            post.setCategoryId(String.valueOf((long)categoryId));
            post.setUserId(String.valueOf((long)userId));
            post.setViewCount(Integer.valueOf(String.valueOf(0)));
            post.setLikeCount(Integer.valueOf(String.valueOf(0)));
            post.setReplyCount(Integer.valueOf(String.valueOf(0)));
            post.setStatus(String.valueOf(1));
            int result = this.postMapper.insert(post);
            return result > 0 ? new ApiResponse(post.getId(), 200, "创建帖子成功") : new ApiResponse((Object)null, 500, "创建帖子失败");
        } catch (Exception var7) {
            return new ApiResponse((Object)null, 500, "服务器错误");
        }
    }

    @CrossOrigin
    @DeleteMapping({"/delePost/{id}"})
    public ApiResponse deletePost(@PathVariable Long id) {
        try {
            int result = this.postMapper.deletePost(id);
            return result > 0 ? new ApiResponse((Object)null, 200, "删除成功") : new ApiResponse((Object)null, 500, "删除失败");
        } catch (Exception var3) {
            return new ApiResponse((Object)null, 500, "服务器错误");
        }
    }

    @CrossOrigin
    @GetMapping({"/posts/{postId}/replies"})
    public ApiResponse listReplies(@PathVariable Long postId) {
        System.out.println(postId);

        try {
            List<Reply> replies = this.replyMapper.selectRepliesByPostId(postId);
            return new ApiResponse(replies, 200, "获取回复成功");
        } catch (Exception var3) {
            Exception e = var3;
            return new ApiResponse(e, 500, "服务器错误");
        }
    }

    @PostMapping({"/posts/{postId}/repliesed"})
    public ApiResponse createReply(@PathVariable Long postId, @RequestParam("userId") Long userId, @RequestParam("content") String content) {
        try {
            Reply reply = new Reply();
            reply.setPostId(postId);
            reply.setContent(content);
            reply.setUserId(userId);
            reply.setStatus(String.valueOf(1));
            System.out.println(postId);
            System.out.println(content);
            System.out.println(userId);
            int result = this.replyMapper.insertReply(reply);
            return result > 0 ? new ApiResponse(reply, 200, "回复成功") : new ApiResponse((Object)null, 500, "回复失败");
        } catch (Exception var6) {
            Exception e = var6;
            return new ApiResponse(e, 500, "服务器错误");
        }
    }

    @PutMapping({"/replies/{id}"})
    public ApiResponse updateReply(@PathVariable Long id, @RequestBody Reply reply) {
        try {
            reply.setId(id);
            int result = this.replyMapper.updateReply(reply);
            return result > 0 ? new ApiResponse(reply, 200, "修改成功") : new ApiResponse((Object)null, 500, "修改失败");
        } catch (Exception var4) {
            return new ApiResponse((Object)null, 500, "服务器错误");
        }
    }

    @CrossOrigin
    @DeleteMapping({"/replies/{id}"})
    public ApiResponse deleteReply(@PathVariable Long id) {
        try {
            int result = this.replyMapper.deleteReply(id);
            return result > 0 ? new ApiResponse((Object)null, 200, "删除成功") : new ApiResponse((Object)null, 500, "删除失败");
        } catch (Exception var3) {
            return new ApiResponse((Object)null, 500, "服务器错误");
        }
    }
}
