package com.microservices.cms.doc.controller;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.cms.doc.domain.*;
import com.microservices.cms.doc.service.ICmsDocCommentService;
import com.microservices.cms.doc.service.ICmsDocService;
import com.microservices.cms.doc.service.ICmsWechatService;
import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.domain.GenericsAjaxResult;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文章信息Controller
 *
 * @author otto
 * @date 2023-03-28
 */
@Api(tags = "栏目及文章相关公开接口")
@RestController
@RequestMapping("/doc/open")
public class OpenController extends BaseController {
    @Autowired
    private ICmsDocService cmsDocService;
    @Autowired
    private ICmsDocCommentService cmsDocCommentService;
    @Autowired
    private ICmsWechatService cmsWechatService;

    /**
     * 查询栏目列表
     */

    @GetMapping("/zone/{zoneId}/dirList")
    @ApiOperation("查询栏目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc,默认asc", paramType = "query", dataType = "String")
    })
    public GenericsTableDataInfo<CmsDir> dirList(@ApiParam(name = "zoneId", value = "专区Id") @PathVariable("zoneId") Long zoneId) {
        Long deptId = cmsDocService.getDeptIdByZoneId(zoneId);
        List<CmsDir> list = cmsDocService.selectCmsDocDirList(deptId, true);
        return getAllGenericsDataTableToPage(list);
    }

    /**
     * 查询栏目下文章列表
     */

    @GetMapping("/dir/{dirId}/docList")
    @ApiOperation("查询栏目下文章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc,默认asc", paramType = "query", dataType = "String")
    })
    public GenericsTableDataInfo<CmsDocBaseDto> docList(@ApiParam(name = "dirId", value = "栏目Id") @PathVariable("dirId") Long dirId
            , CmsDocSearchVo cmsDocSearchVo) {
        cmsDocSearchVo.setIsOpen(true);
        List<CmsDocBaseDto> list = cmsDocService.selectCmsDocList(dirId, cmsDocSearchVo);
        return getAllGenericsDataTableToPage(list);
    }

    /**
     * 查询栏目下文章列表（包含内容）
     */

    @GetMapping("/dir/{dirId}/docContentList")
    @ApiOperation("查询栏目下文章列表（包含内容）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc,默认asc", paramType = "query", dataType = "String")
    })
    public GenericsTableDataInfo<CmsDocDetailDto> docContentList(@ApiParam(name = "dirId", value = "栏目Id") @PathVariable("dirId") Long dirId) {
        List<CmsDocDetailDto> list = cmsDocService.selectCmsDocDetailList(dirId);
        return getAllGenericsDataTableToPage(list);
    }

    /**
     * 查询文章聚合列表
     */
    @GetMapping("/zone/{zoneId}/docOverviewList")
    @ApiOperation("查询文章聚合列表")
    public GenericsTableDataInfo<CmsDocOverviewDto> docOverviewList(@ApiParam(name = "zoneId", value = "专区Id") @PathVariable("zoneId") Long zoneId) {
        Long deptId = cmsDocService.getDeptIdByZoneId(zoneId);
        List<CmsDocOverviewDto> list = cmsDocService.selectCmsDocOverviewList(deptId, true);
        return getAllGenericsDataTableToPage(list);
    }

    /**
     * 查询首页文章列表
     */
    @GetMapping("/zone/{zoneId}/homePageDocList")
    @ApiOperation("查询首页文章列表")
    public GenericsTableDataInfo<CmsDocBaseDto> homePageDocList(@ApiParam(name = "zoneId", value = "专区Id") @PathVariable("zoneId") Long zoneId) {
        Long deptId = cmsDocService.getDeptIdByZoneId(zoneId);
        List<CmsDocBaseDto> list = cmsDocService.selectHomePageDocList(deptId);
        return getAllGenericsDataTableToPage(list);
    }

    /**
     * 查询栏目下热点文章
     */
    @GetMapping("/dir/{dirId}/hotDocList")
    @ApiOperation("查询栏目下热点文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer")
    })
    public GenericsTableDataInfo<HotCmsDocDto> hotDocListByDir(@ApiParam(name = "dirId", value = "栏目Id") @PathVariable("dirId") Long dirId) {
        List<HotCmsDocDto> list = cmsDocService.selectHotCmsDocList(dirId);
        return getAllGenericsDataTableToPage(list);
    }

    /**
     * 查询专区下热点文章
     */
    @GetMapping("/zone/{zoneId}/hotDocList")
    @ApiOperation("查询专区下热点文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer")
    })
    public GenericsTableDataInfo<HotCmsDocDto> hotDocListByDept(@ApiParam(name = "zoneId", value = "专区Id") @PathVariable("zoneId") Long zoneId) {
        Long deptId = cmsDocService.getDeptIdByZoneId(zoneId);
        List<HotCmsDocDto> list = cmsDocService.selectHotCmsDocListByDept(deptId);
        return getAllGenericsDataTableToPage(list);
    }

    /**
     * 获取文章信息详细信息
     */
    @ApiOperation("获取文章详细信息")
    @GetMapping(value = "/{id}")
    public GenericsAjaxResult<CmsDocDetailDto> getInfo(@ApiParam(name = "id", value = "文章Id") @PathVariable("id") Long id, HttpServletRequest request) {
        return genericsSuccess(cmsDocService.selectCmsDocById(id, true, request));
    }

    /**
     * 获取文章信息基础信息
     */
    @ApiOperation("获取文章信息基础信息")
    @GetMapping(value = "/baseInfo/{id}")
    public GenericsAjaxResult<CmsDocBaseDto> getBaseInfo(@ApiParam(name = "id", value = "文章Id") @PathVariable("id") Long id, HttpServletRequest request) {
        return genericsSuccess(cmsDocService.selectCmsDocBaseInfoById(id));
    }

    /**
     * 获取栏目详细信息
     */
    @ApiOperation("获取栏目详细信息")
    @GetMapping(value = "/dir/{dirId}")
    public GenericsAjaxResult<CmsDir> getDirInfo(@ApiParam(name = "dirId", value = "栏目Id") @PathVariable("dirId") Long dirId) {
        return genericsSuccess(cmsDocService.selectCmsDirById(dirId, true));
    }

    /**
     * 同步文章数据
     */
    @PostMapping("/dept/{deptId}")
    @ApiOperation("同步文章数据")
    @Log(title = "同步文章数据", businessType = BusinessType.UPDATE)
    public AjaxResult syncDocData(
            @ApiParam(name = "deptId", value = "组织Id", required = true) @PathVariable("deptId") Long deptId
            ,@RequestBody() String bodyStr
            , HttpServletRequest request) {
        cmsDocService.syncDocData(deptId,bodyStr);
        return AjaxResult.success("同步消息发送成功");
    }


    /**
     * 公开获取文章下评论列表
     */
    @GetMapping("/{docId}/journalList")
    @ApiOperation(value = "公开获取文章下评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页数", paramType = "query", dataType = "Integer"),
    })
    public AjaxResult getCommentList(
            @ApiParam("文章Id") @PathVariable Long docId, Integer page, Integer limit) {
        JSONObject result = cmsDocCommentService.getCommentList(docId, page, limit);
        return success(result);
    }


    /**
     * 获取CCF微信公众号签名
     */
    @ApiOperation("获取CCF微信公众号签名")
    @PostMapping(value = "/wechatSignature")
    public AjaxResult getWechatSignature(@RequestBody WechatSignatureInput wechatSignatureInput) {
        return success(cmsWechatService.getWechatSignature(wechatSignatureInput));
    }
}

