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.CmsCategory;
import com.kexue.skills.entity.base.IdDto;
import com.kexue.skills.entity.dto.CmsCategoryDto;
import com.kexue.skills.service.CmsCategoryService;
import com.kexue.skills.service.CmsTagService;
import com.kexue.skills.service.CmsCategoryTagService;
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;

/**
 * (CmsCategory)表控制层
 *
 * @author 王志维
 * @since 2025-02-21 23:01:48
 */
@RestController
@RequestMapping("api/cmsCategory")
@Tag(name = "内容类目（skills-分类） Api")
@CrossOrigin(origins = "*")
public class CmsCategoryController {
    /**
     * 服务对象
     */
    @Resource
    private CmsCategoryService cmsCategoryService;
    
    /**
     * 标签服务对象
     */
    @Resource
    private CmsTagService cmsTagService;
    
    /**
     * 分类标签关联服务对象
     */
    @Resource
    private CmsCategoryTagService cmsCategoryTagService;
    /**
     * 分页查询
     *
     * @param queryDto 筛选条件
     * @return 查询结果
     */
    @PostMapping("/getPageList")
    @Operation(summary = "查询分页列表", description = "查询分页列表")
    public CommonResult<PageInfo<CmsCategory>> getPageList(@RequestBody CmsCategoryDto queryDto) {
        return CommonResult.success(cmsCategoryService.getPageList(queryDto));
    }

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

    /**
     * 新增数据
     *
     * @param cmsCategory 实体
     * @return 新增结果
     */
    @PostMapping("/insert")
    @Operation(summary = "新增分类", description = "新增分类")
    @RequireAuth
    public CommonResult<CmsCategory> insert(@RequestBody CmsCategory cmsCategory) {
        return CommonResult.success(cmsCategoryService.insert(cmsCategory));
    }

    /**
     * 编辑数据
     *
     * @param cmsCategory 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    @Operation(summary = "更新分类", description = "更新分类")
    @RequireAuth
    public CommonResult<CmsCategory> update(@RequestBody CmsCategory cmsCategory) {
        return CommonResult.success(cmsCategoryService.update(cmsCategory));
    }

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

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

    /**
     * 获取分类树列表
     *
     * @return 分类树列表
     */
    @GetMapping("/getCategoryTreeList")
    @Operation(summary = "获取分类树列表", description = "获取分类树列表，包含分类名称、分类ID和子分类列表")
    public CommonResult<java.util.List<CmsCategory>> getCategoryTreeList() {
        return CommonResult.success(cmsCategoryService.getCategoryTreeList());
    }

    /**
     * 获取分类字典
     *
     * @return 分类字典，key为分类ID，value为分类名称
     */
    @GetMapping("/getCategoryDict")
    @Operation(summary = "获取分类字典", description = "获取分类字典，返回Map<Integer,String>，分类ID和分类名称的映射")
    public CommonResult<java.util.Map<Long, String>> getCategoryDict() {
        return CommonResult.success(cmsCategoryService.getCategoryDict());
    }
    
    /**
     * 获取标签列表
     *
     * @param categoryId 分类ID（可选），不传则返回所有标签
     * @return 标签列表
     */
    @GetMapping("/tagList")
    @Operation(summary = "获取标签列表", description = "获取标签列表，若传入分类ID则返回该分类下的标签，否则返回所有标签")
    public CommonResult<java.util.List<com.kexue.skills.entity.CmsTag>> getTagList(@RequestParam(value = "categoryId", required = false) Long categoryId) {
        if (categoryId != null) {
            // 根据分类ID查询标签列表
            return CommonResult.success(cmsCategoryTagService.getTagsByCategoryId(categoryId));
        } else {
            // 查询所有标签
            com.kexue.skills.entity.dto.CmsTagDto queryDto = new com.kexue.skills.entity.dto.CmsTagDto();
            queryDto.setDeleteFlag(0);
            queryDto.setStatus(1);
            return CommonResult.success(cmsTagService.getList(queryDto));
        }
    }
}