package com.kexue.skills.controller;

import com.github.pagehelper.PageInfo;
import com.kexue.skills.annotation.RequireAuth;
import com.kexue.skills.common.CommonResult;
import com.kexue.skills.entity.CmsContent;
import com.kexue.skills.entity.base.IdDto;
import com.kexue.skills.entity.dto.CmsContentDto;
import com.kexue.skills.service.CmsContentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (CmsContent)表控制层
 *
 * @author 王志维
 * @since 2025-02-21 23:01:48
 */
@RestController
@RequestMapping("api/cmsContent")
@Tag(name = "内容（skills）管理 Api")
@CrossOrigin(origins = "*")
public class CmsContentController {
    /**
     * 服务对象
     */
    @Resource
    private CmsContentService cmsContentService;
    /**
     * 分页查询
     *
     * @param queryDto 筛选条件
     * @return 查询结果
     */
    @PostMapping("/getPageList")
    @Operation(summary = "查询分页列表", description = "查询分页列表")
    public CommonResult<PageInfo<CmsContent>> getPageList(@RequestBody CmsContentDto queryDto) {
        return CommonResult.success(cmsContentService.getPageList(queryDto));
    }

    /**
     * 查询列表
     *
     * @param queryDto 筛选条件
     * @return 查询结果
     */
    @PostMapping("/getList")
    @Operation(summary = "查询列表", description = "查询列表")
    public CommonResult<PageInfo<CmsContent>> getList(@RequestBody CmsContentDto queryDto) {
        return CommonResult.success(new PageInfo<>(cmsContentService.getList(queryDto)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param contentId 主键
     * @return 单条数据
     */
    @PostMapping("queryById/{contentId}")
    @Operation(summary = "通过ID查询内容", description = "通过ID查询内容")
    public CommonResult<CmsContent> queryById(@PathVariable("contentId") Long contentId) {
        // 增加阅读量
        cmsContentService.increaseViewCount(contentId);
        return CommonResult.success(cmsContentService.queryById(contentId));
    }

    /**
     * 新增数据
     *
     * @param cmsContent 实体
     * @return 新增结果
     */
    @PostMapping("/insert")
    @Operation(summary = "新增内容", description = "新增内容")
    @RequireAuth
    public CommonResult<CmsContent> insert(@RequestBody CmsContent cmsContent) {
        return CommonResult.success(cmsContentService.insert(cmsContent));
    }

    /**
     * 编辑数据
     *
     * @param cmsContent 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    @Operation(summary = "更新内容", description = "更新内容")
    @RequireAuth
    public CommonResult<CmsContent> update(@RequestBody CmsContent cmsContent) {
        return CommonResult.success(cmsContentService.update(cmsContent));
    }

    /**
     * 更新审核状态
     *
     * @param contentId 内容ID
     * @param auditStatus 审核状态
     * @param reviewerId 审核人ID
     * @param reviewerName 审核人名称
     * @param auditComment 审核意见
     * @return 审核结果
     */
    @PostMapping("/updateAuditStatus")
    @Operation(summary = "更新审核状态", description = "更新审核状态")
    @RequireAuth
    public CommonResult<Boolean> updateAuditStatus(@RequestParam("contentId") Long contentId, 
                                                  @RequestParam("auditStatus") Integer auditStatus, 
                                                  @RequestParam("reviewerId") Long reviewerId, 
                                                  @RequestParam("reviewerName") String reviewerName, 
                                                  @RequestParam(value = "auditComment", required = false) String auditComment) {
        return CommonResult.success(cmsContentService.updateAuditStatus(contentId, auditStatus, reviewerId, reviewerName, auditComment, reviewerName) > 0);
    }

    /**
     * 更新发布状态
     *
     * @param contentId 内容ID
     * @param publishStatus 发布状态
     * @param publishTime 发布时间
     * @param updateBy 更新人
     * @return 发布结果
     */
    @PostMapping("/updatePublishStatus")
    @Operation(summary = "更新发布状态", description = "更新发布状态")
    @RequireAuth
    public CommonResult<Boolean> updatePublishStatus(@RequestParam("contentId") Long contentId, 
                                                   @RequestParam("publishStatus") Integer publishStatus, 
                                                   @RequestParam(value = "publishTime", required = false) String publishTime, 
                                                   @RequestParam("updateBy") String updateBy) {
        return CommonResult.success(cmsContentService.updatePublishStatus(contentId, publishStatus, publishTime, updateBy) > 0);
    }

    /**
     * 增加阅读量
     *
     * @param contentId 内容ID
     * @return 增加结果
     */
    @PostMapping("/increaseViewCount/{contentId}")
    @Operation(summary = "增加阅读量", description = "增加阅读量")
    public CommonResult<Boolean> increaseViewCount(@PathVariable("contentId") Long contentId) {
        return CommonResult.success(cmsContentService.increaseViewCount(contentId) > 0);
    }

    /**
     * 通过主键逻辑删除
     *
     * @param idDto 主键
     * @return 删除数据
     */
    @PostMapping("/logicDeleteById")
    @Operation(summary = "逻辑删除内容", description = "逻辑删除内容")
    @RequireAuth
    public CommonResult<Boolean> logicDeleteById(@RequestBody IdDto idDto) {
        return CommonResult.success(cmsContentService.logicDeleteById(idDto.getId(), "admin") > 0);
    }

    /**
     * 删除数据
     *
     * @param contentId 主键
     * @return 删除数据
     */
    @PostMapping("deleteById/{contentId}")
    @Operation(summary = "物理删除内容", description = "物理删除内容")
    @RequireAuth
    public CommonResult<Boolean> deleteById(@PathVariable("contentId") Long contentId) {
        return CommonResult.success(cmsContentService.deleteById(contentId) > 0);
    }
}