package com.sckw.system.controller;

import com.sckw.core.web.response.BaseResult;
import com.sckw.core.web.response.result.PageDataResult;
import com.sckw.system.model.vo.req.SysDictReqVo;
import com.sckw.system.model.vo.res.SysDictResp;
import com.sckw.system.service.SysDictFlexBusinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典控制器 (MyBatis-Flex)
 * @author system
 * @date 2024
 */
@Tag(name = "字典管理")
@RestController
@RequestMapping("/flex/sysDict")
@RequiredArgsConstructor
public class SysDictFlexController {

    private final SysDictFlexBusinessService sysDictService;

    /**
     * 新增字典
     */
    @Operation(summary = "新增字典")
    @PostMapping("/insert")
    public BaseResult<Boolean> insert(@RequestBody @Valid SysDictReqVo reqVo) {
        return BaseResult.success(sysDictService.insert(reqVo.getEntity()));
    }





    /**
     * 逻辑删除字典
     */
    @Operation(summary = "逻辑删除字典")
    @PostMapping("/logicDelete")
    public BaseResult<Integer> logicDeleteById(@RequestBody SysDictReqVo reqVo) {
        int rows = sysDictService.logicDeleteById(reqVo.getId());
        return BaseResult.success(rows);
    }

    /**
     * 更新字典
     */
    @Operation(summary = "更新字典")
    @PostMapping("/update")
    public BaseResult<Boolean> update(@RequestBody SysDictReqVo reqVo) {
        return BaseResult.success(sysDictService.update(reqVo.getEntity()));
    }
    
    /**
     * 分页查询字典
     */
    @Operation(summary = "分页查询字典")
    @PostMapping("/selectPage")
    public BaseResult<PageDataResult<SysDictResp>> selectPage(@RequestBody SysDictReqVo reqVo) {
        return BaseResult.success(sysDictService.selectPage(reqVo));
    }

    /**
     * 统计字典数量
     */
    @Operation(summary = "统计字典数量")
    @PostMapping("/count")
    public BaseResult<Long> count(@RequestBody SysDictReqVo reqVo) {
        long count = sysDictService.count(reqVo.getType());
        return BaseResult.success(count);
    }

    /**
     * 查询上级字典单选下拉（支持输入搜索）
     * 用于字典项选择父级字典
     */
    @Operation(summary = "查询上级字典下拉列表")
    @PostMapping("/searchParentForSelect")
    public BaseResult<List<SysDictResp>> searchParentForSelect(@RequestBody SysDictReqVo reqVo) {
        return BaseResult.success(sysDictService.searchParentDictForSelect( reqVo));
    }

    /**
     * 快捷更改字典状态（启用/停用）
     */
    @Operation(summary = "快捷更改字典状态")
    @PostMapping("/updateStatus")
    public BaseResult<Boolean> updateStatus(@RequestBody SysDictReqVo reqVo) {
        return BaseResult.success(sysDictService.updateStatus(reqVo.getId(), reqVo.getStatus()));
    }
}
