package com.kexue.skills.controller;

import com.kexue.skills.entity.SysDict;
import com.kexue.skills.entity.dto.SysDictDto;
import com.kexue.skills.service.SysDictService;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.annotation.Resource;
import com.github.pagehelper.PageInfo;
import com.kexue.skills.common.CommonResult;
import com.kexue.skills.entity.base.IdDto;

import java.util.List;
import java.util.Map;

/**
 * 系统字典表(SysDict)表控制层
 *
 * @author 王志维
 * @since 2025-02-21 23:01:48
 */
@RestController
@RequestMapping("api/sysDict")
@Tag(name = "系统字典 Api")
@CrossOrigin(origins = "*")
public class SysDictController {
    /**
     * 服务对象
     */
    @Resource
    private SysDictService sysDictService;
    /**
     * 分页查询
     *
     * @param queryDto 筛选条件
     * @return 查询结果
     */
    @PostMapping("/getPageList")
    @Operation(summary = "查询分页列表", description = "查询分页列表")
    public CommonResult<PageInfo<SysDict>> getPageList(@RequestBody SysDictDto queryDto) {
        return CommonResult.success(sysDictService.getPageList(queryDto));
    }
    
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("queryById/{id}")
    @Operation(summary = "通过ID查询字典", description = "通过ID查询字典")
    public CommonResult<SysDict> queryById(@PathVariable("id") Long id) {
        return CommonResult.success(sysDictService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param SysDict 实体
     * @return 新增结果
     */
    @PostMapping("/insert")
    @Operation(summary = "新增字典", description = "新增字典")
    public CommonResult<SysDict> insert(@RequestBody SysDict SysDict) {
        return CommonResult.success(sysDictService.insert(SysDict));
    }

    /**
     * 编辑数据
     *
     * @param SysDict 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    @Operation(summary = "更新字典", description = "更新字典")
    public CommonResult<SysDict> update(@RequestBody SysDict SysDict) {
        return CommonResult.success(sysDictService.update(SysDict));
    }
    
    /**
     * 删除数据
     *
     * @param id
     * @return 删除数据
     */
    @PostMapping("deleteById/{id}")
    @Operation(summary = "删除字典", description = "删除字典")
    public CommonResult<Boolean> deleteById(@PathVariable("id") Long id) {
        return CommonResult.success(sysDictService.deleteById(id));
    }

    /**
     * 查询所有的dictCode
     */
    @GetMapping("dictCodeList")
    @Operation(summary = "查询所有字典编码", description = "查询所有字典编码")
    public CommonResult<List<SysDict>> getAllDictCode() {
        return CommonResult.success(sysDictService.getAllDictCode());
    }

    /**
     * 查询所有字典映射
     *
     */
    @GetMapping("allDictMap")
    @Operation(summary = "查询所有字典映射", description = "查询所有字典映射")
    public CommonResult<Map<String,List<SysDict>>> getAllDictMap() {
        return CommonResult.success(sysDictService.getAllDictMap());
    }

    @GetMapping("{dictCode}")
    @Operation(summary = "通过字典编码查询字典列表", description = "通过字典编码查询字典列表")
    public CommonResult<List<SysDict>> getListByDictCode(@PathVariable("dictCode") String dictCode) {
        return CommonResult.success(sysDictService.getListByDictCode(dictCode));
    }

    @PostMapping("getDictListByDictCode")
    @Operation(summary = "通过字典编码查询字典列表", description = "通过字典编码查询字典列表")
    public CommonResult<List<SysDict>> getDictListByDictCode(@RequestBody SysDict sysDict) {
        return CommonResult.success(sysDictService.getListByDictCode(sysDict.getDictCode()));
    }
}
