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.CmsTag;
import com.kexue.skills.entity.base.IdDto;
import com.kexue.skills.entity.dto.CmsTagDto;
import com.kexue.skills.service.CmsTagService;
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;

/**
 * (CmsTag)表控制层
 *
 * @author 王志维
 * @since 2025-02-21 23:01:48
 */
@RestController
@RequestMapping("api/cmsTag")
@Tag(name = "标签管理 Api")
@CrossOrigin(origins = "*")
public class CmsTagController {
    /**
     * 服务对象
     */
    @Resource
    private CmsTagService cmsTagService;
    /**
     * 分页查询
     *
     * @param queryDto 筛选条件
     * @return 查询结果
     */
    @PostMapping("/getPageList")
    @Operation(summary = "查询分页列表", description = "查询分页列表")
    public CommonResult<PageInfo<CmsTag>> getPageList(@RequestBody CmsTagDto queryDto) {
        return CommonResult.success(cmsTagService.getPageList(queryDto));
    }

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

    /**
     * 通过主键查询单条数据
     *
     * @param tagId 主键
     * @return 单条数据
     */
    @PostMapping("queryById/{tagId}")
    @Operation(summary = "通过ID查询标签", description = "通过ID查询标签")
    public CommonResult<CmsTag> queryById(@PathVariable("tagId") Long tagId) {
        return CommonResult.success(cmsTagService.queryById(tagId));
    }

    /**
     * 新增数据
     *
     * @param cmsTag 实体
     * @return 新增结果
     */
    @PostMapping("/insert")
    @Operation(summary = "新增标签", description = "新增标签")
    @RequireAuth
    public CommonResult<CmsTag> insert(@RequestBody CmsTag cmsTag) {
        return CommonResult.success(cmsTagService.insert(cmsTag));
    }

    /**
     * 编辑数据
     *
     * @param cmsTag 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    @Operation(summary = "更新标签", description = "更新标签")
    @RequireAuth
    public CommonResult<CmsTag> update(@RequestBody CmsTag cmsTag) {
        return CommonResult.success(cmsTagService.update(cmsTag));
    }

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

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


}