package com.ruoyi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.annotations.UserLoginToken;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.enums.user.UserEnums;
import com.ruoyi.frequency.auth.service.AuthService;
import com.ruoyi.frequency.highendauth.service.HighendAuthService;
import com.ruoyi.post.controller.req.PostPageReq;
import com.ruoyi.post.controller.req.PostReleaseReq;
import com.ruoyi.post.controller.resp.PostDetailResp;
import com.ruoyi.post.controller.resp.PostPageResp;
import com.ruoyi.post.frequency.post.service.PostService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.vo.BodyIdReq;
import com.ruoyi.vo.RyController;
import com.ruoyi.vo.UserInfo;
import com.ruoyi.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 帖子相关
 *
 * @author liwenlong
 */
@CrossOrigin
@RestController
@RequestMapping("/api/post")
@Api(tags = "帖子相关")
public class ApiPostController extends RyController {

    @Autowired
    private PostService postService;

    @Autowired
    private AuthService authService;

    @Autowired
    private HighendAuthService highendAuthService;

    @Autowired
    private BusinessUtil businessUtil;

    @UserLoginToken
    @PostMapping("/postRelease")
    @ApiOperation(value = "帖子发布", notes = "帖子发布")
    public ResponseData postRelease(@RequestBody PostReleaseReq req) {

        UserVo userVo = getUserVo();

        //判断表现师是否认证
        if (ObjectUtil.equal(UserEnums.store.getCode(), userVo.getUserType())) {
            UserInfo userInfo = businessUtil.getUserInfo(userVo);
            if (userInfo == null && userInfo.getIsAuth() != 2 && userInfo.getIfHighend() != 2 && userInfo.getIsKujialeAuth() != 2) {
                return ResponseData.error("清先进行平台认证或高端认证");
            }
        }

        return postService.postRelease(req, userVo);
    }

    @ApiOperation(value = "帖子列表", notes = "帖子列表")
    @PostMapping("/postPage")
    public ResponseData<PageResult<Page<PostPageResp>>> postPage(@RequestBody PostPageReq req) {
        return postService.postPage(req, getUserVo());
    }

    @ApiOperation(value = "帖子列表(自己查看)", notes = "帖子列表(自己查看)")
    @PostMapping("/postPageBySelf")
    public ResponseData<PageResult<Page<PostPageResp>>> postPageBySelf(@RequestBody PostPageReq req) {
        UserVo userVo = getUserVo();
        req.setUserId(userVo.getUserId());
        req.setUserType(userVo.getUserType());

        return postService.postPage(req, userVo);
    }

    @ApiOperation(value = "帖子列表(他人查看)", notes = "帖子列表(他人查看)")
    @PostMapping("/postPageByOtherSelf")
    public ResponseData<PageResult<Page<PostPageResp>>> postPageByOtherSelf(@RequestBody PostPageReq req) {
        return postService.postPage(req, getUserVo());
    }


    @PostMapping("/postDetail")
    @ApiOperation(value = "帖子详情", notes = "帖子详情")
    public ResponseData<PostDetailResp> postDetail(@RequestBody BodyIdReq req) {
        return postService.postDetail(req.getId(), req.getIsAddBrowseNum(), getUserVo());
    }

    @PostMapping("/postEdit")
    @ApiOperation(value = "帖子编辑", notes = "帖子编辑")
    public ResponseData postEdit(@RequestBody PostReleaseReq req) {
        return postService.postEdit(req, getUserVo());
    }

    @PostMapping("/postDelete")
    @ApiOperation(value = "帖子删除", notes = "帖子删除")
    public ResponseData postDelete(@RequestBody BodyIdReq req) {
        return postService.postDelete(req.getId(), getUserVo());
    }

}
