package com.cclotus.cms.controller.admin

import com.cclotus.cms.domain.CmsPost
import com.cclotus.cms.domain.CmsReviewForm
import com.cclotus.cms.service.CmsPostService
import com.cclotus.cms.util.CmsConstant
import com.cclotus.cms.vo.CmsPostFormReq
import com.ruoyi.common.annotation.Log
import com.ruoyi.common.core.controller.BaseController
import com.ruoyi.common.core.domain.AjaxResult
import com.ruoyi.common.core.page.TableDataInfo
import com.ruoyi.common.enums.BusinessType
import com.ruoyi.common.reqParam.PageParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*


@Suppress("NAME_SHADOWING")
@RestController
@RequestMapping("/cms")
class CmsPostController : BaseController() {

    @Autowired
    lateinit var cmsPostService: CmsPostService

    /**
     * 查询发布内容列表
     *
     * @param contentType  内容数据类型
     * @param termAlias 分类别名
     * @param paging    是否分页，默认分页
     * @param deep      是否显示栏目下子栏目的文章
     * @param post      过滤条件
     */
    @GetMapping(path = ["/{contentType:article|page|video|picture|custom}/{termAlias}/list", "/{contentType:article|page|video|picture|custom}/list"])
    fun list(
        @PathVariable("contentType") contentType: String,
        @PathVariable(name = "termAlias", required = false) termAlias: String?,
        @RequestParam(required = false) paging: Boolean?,
        @RequestParam(required = false) deep: Boolean?,
        post: CmsPost,
        pageParam: PageParam?,
        @RequestParam(required = false) content: Boolean?,
        @RequestParam(required = false) query: Map<String, String>?,
    ): TableDataInfo {
        val pageParam = if (paging != null && !paging) null else pageParam
        post.contentType = contentType
        // 提取扩展属性查询条件
        val meta = query?.filterKeys { it.startsWith(prefix = CmsConstant.META_QUERY_PREFIX, ignoreCase = true) }
            ?.mapKeys { it.key.removePrefix(CmsConstant.META_QUERY_PREFIX) }
        return cmsPostService.list(termAlias, deep ?: false, post, pageParam, content ?: false, meta)
    }

    /**
     * 根据页面别名获取页面详情
     *
     * @param contentType 页面别名
     * @return 页面详情信息
     */
    @GetMapping("/{contentType:article|page|video|picture|custom}/{post}")
    fun detail(
        @PathVariable("contentType") contentType: String,
        @PathVariable("post") post: String,
    ): AjaxResult {
        return AjaxResult.success(cmsPostService.detail(contentType, post))
    }

    /**
     * 新增发布内容
     */
    @Log(title = "发布内容管理", businessType = BusinessType.INSERT)
    @PostMapping("/{contentType:article|page|video|picture|custom}")
    fun add(
        @PathVariable("contentType") contentType: String,
        @Validated @RequestBody cmsPostFormReq: CmsPostFormReq,
    ): AjaxResult {
        cmsPostFormReq.contentType = contentType
        cmsPostFormReq.origin = "admin"
        // 管理员发布的文章默认为审核通过状态
        cmsPostFormReq.review = "1"
        return toAjax(cmsPostService.create(cmsPostFormReq, username))
    }

    /**
     * 修改发布内容
     */
    @Log(title = "发布内容管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{contentType:article|page|video|picture|custom}")
    fun edit(
        @PathVariable("contentType") contentType: String,
        @Validated @RequestBody cmsPostFormReq: CmsPostFormReq,
    ): AjaxResult {
        return toAjax(cmsPostService.update(cmsPostFormReq, username))
    }

    /**
     * 删除发布内容
     *
     * @param contentType    内容类型
     * @param postIds 内容ID列表
     */
    @Log(title = "发布内容管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contentType:article|page|video|picture|custom}/{postIds}")
    fun remove(
        @PathVariable("contentType") contentType: String,
        @PathVariable("postIds") postIds: Array<Int>,
    ): AjaxResult {
        return toAjax(cmsPostService.deleteByIds(postIds))
    }

    /**
     * 审核内容
     *
     * @param contentType 内容类型
     * @param post 内容标识
     * @param reviewForm 审核信息
     * @return 处理结果
     */
    @Log(title = "审核内容", businessType = BusinessType.UPDATE)
    @PostMapping("/{contentType:article|page|video|picture|custom}/review/{post}")
    fun review(
        @PathVariable("contentType") contentType: String,
        @PathVariable("post") post: String,
        @RequestBody @Validated reviewForm: CmsReviewForm,
    ): AjaxResult {
        cmsPostService.review(contentType, post, reviewForm)
        return AjaxResult.success()
    }
}

