package com.codvision.base.controller.backend;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.base.entity.GeneratorConfig;
import com.codvision.base.entity.vo.GeneratorConfigVO;
import com.codvision.base.service.IGeneratorConfigService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.webcore.annotation.ApiSecret;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 生成代码配置 管理模块
 *
 * @author lingee
 * @date 2023-11-03
 */
@ApiSecret(ignore = true)
@RestController
@RequestMapping("/backend/api/generator")
@Api(tags = "框架后台管理-生成代码模块")
public class GeneratorConfigController {

    @Autowired
    private IGeneratorConfigService generatorConfigService;

    @ApiOperation(value = "获取生成代码配置详情", notes = "获取生成代码配置详情接口")
    @PostMapping("/getDetail")
    public ResponseEntity<GeneratorConfigVO> getById(@ApiParam(value = "生成代码配置ID", required = true) @RequestParam Long id) {
        GeneratorConfigVO vo = generatorConfigService.getDetail(id);
        return new ResponseEntity<GeneratorConfigVO>().ok().data(vo);
    }

    @ApiOperation(value = "获取生成代码配置分页列表", notes = "获取生成代码配置分页列表接口")
    @PostMapping("/getPageList")
    public ResponseEntity<IPage<GeneratorConfigVO>> getPageList(@RequestBody QueryPage<GeneratorConfig> queryPage) {
        IPage<GeneratorConfigVO> pageResult = generatorConfigService.getPageResult(queryPage);
        return new ResponseEntity<IPage<GeneratorConfigVO>>().ok().data(pageResult);
    }

    @ApiOperation(value = "新增生成代码配置", notes = "新增生成代码配置接口")
    @PostMapping("/save")
    public ResponseEntity<String> saveGeneratorConfig(@Valid @RequestBody GeneratorConfig generatorConfig) {
        generatorConfigService.save(generatorConfig);
        return new ResponseEntity<String>().ok().message("添加成功");
    }

    @ApiOperation(value = "删除生成代码配置", notes = "删除生成代码配置接口")
    @PostMapping("/delete")
    public ResponseEntity<String> deleteById(@ApiParam(value = "生成代码配置ID", required = true) @RequestParam Long id) {
        generatorConfigService.removeById(id);
        return new ResponseEntity<String>().ok().message("删除成功");
    }

    @ApiOperation(value = "批量删除生成代码配置", notes = "批量删除生成代码配置接口")
    @PostMapping("/deleteBatch")
    public ResponseEntity<String> deleteBatch(@ApiParam(value = "生成代码配置ID", required = true) @RequestBody List<Long> ids) {
        generatorConfigService.removeBatchByIds(ids);
        return new ResponseEntity<String>().ok().message("删除成功");
    }

    @ApiOperation(value = "编辑生成代码配置", notes = "编辑接生成代码配置接口")
    @PostMapping("/update")
    public ResponseEntity<String> updateGeneratorConfig(@RequestBody GeneratorConfig generatorConfig) {
        generatorConfigService.updateById(generatorConfig);
        return new ResponseEntity<String>().ok().message("编辑成功");
    }

    @ApiOperation(value = "获取生成代码配置所有列表", notes = "获取生成代码配置所有列表接口")
    @PostMapping("/listAll")
    public ResponseEntity<List<GeneratorConfigVO>> listAll(@RequestBody GeneratorConfig queryBean) {
        List<GeneratorConfigVO> list = generatorConfigService.listAll(queryBean);
        return new ResponseEntity<List<GeneratorConfigVO>>().ok().data(list);
    }

    @ApiOperation(value = "生成代码", notes = "生成代码接口")
    @PostMapping("/generate")
    public ResponseEntity<String> generate(
            @ApiParam(value = "生成代码配置ID", required = true) @RequestParam Long id) {
        generatorConfigService.generateCode(id);
        return new ResponseEntity<String>().ok().message("代码生成成功");
    }
}
