package com.storlead.knowledge.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.storlead.framework.common.result.Result;
import com.storlead.knowledge.entity.ConfigVersionEntity;
import com.storlead.knowledge.pojo.dto.ConfigVersionDTO;
import com.storlead.knowledge.pojo.vo.ConfigVersionVO;
import com.storlead.knowledge.service.ConfigVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/knowledge/config-version/")
@Api(tags = "知识库: 配置版本管理")
public class ConfigVersionController {

    @Resource
    private ConfigVersionService configVersionService;

    @GetMapping("list")
    @ApiOperation("获取配置版本列表")
    public Result<List<ConfigVersionEntity>> list() {
        List<ConfigVersionEntity> list = configVersionService.list();
        return Result.ok(list);
    }

    @GetMapping("page")
    @ApiOperation("分页获取配置版本列表")
    public Result<IPage<ConfigVersionVO>> page(@RequestParam(defaultValue = "1") Integer pageIndex,
                                                @RequestParam(defaultValue = "10") Integer pageSize,
                                                @RequestParam(required = false) String appCode,
                                                @RequestParam(required = false) String keyword) {
        IPage<ConfigVersionVO> result = configVersionService.pageVO(pageIndex, pageSize, appCode, keyword);
        return Result.ok(result);
    }

    @GetMapping("{id}")
    @ApiOperation("根据ID获取配置版本")
    public Result<ConfigVersionEntity> getById(@PathVariable Long id) {
        ConfigVersionEntity entity = configVersionService.getById(id);
        return Result.ok(entity);
    }

    @GetMapping("by-app-key")
    @ApiOperation("根据应用编码和键值获取配置版本")
    public Result<ConfigVersionEntity> getByAppCodeAndKey(@RequestParam String appCode, @RequestParam String key) {
        ConfigVersionEntity entity = configVersionService.getByAppCodeAndKey(appCode, key);
        return Result.ok(entity);
    }

    @PostMapping("save")
    @ApiOperation("保存配置版本")
    public Result<Boolean> save(@Valid @RequestBody ConfigVersionDTO dto) {
        boolean result = configVersionService.saveByDTO(dto);
        return Result.ok(result);
    }

    @PutMapping("update")
    @ApiOperation("更新配置版本")
    public Result<Boolean> update(@Valid @RequestBody ConfigVersionDTO dto) {
        boolean result = configVersionService.updateByDTO(dto);
        return Result.ok(result);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除配置版本")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = configVersionService.removeById(id);
        return Result.ok(result);
    }
}