package org.nl.system.controller.generator;

import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.exception.BadRequestException;
import org.nl.system.service.generator.ICodeGenConfigService;
import org.nl.system.service.generator.ICodeGeneratorService;
import org.nl.system.service.generator.dao.CodeColumnConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 列的数据信息表 前端控制器
 * </p>
 *
 * @author lyd
 * @since 2023-05-03
 */
@SaIgnore
@RestController
@RequestMapping("/api/generator")
@Api(tags = "系统：代码生成管理")
public class CodeGeneratorController {
    @Autowired
    private ICodeGeneratorService generatorService;
    @Autowired
    private ICodeGenConfigService genConfigService;

    @Value("${generator.enabled}")
    private Boolean generatorEnabled;

    @ApiOperation("查询数据库数据")
    @GetMapping(value = "/tables")
    public ResponseEntity<Object> queryTables(@RequestParam(defaultValue = "") String name, PageQuery pageable){
        return new ResponseEntity<>(TableDataInfo.build(generatorService.getTables(name, pageable)), HttpStatus.OK);
    }

    @ApiOperation("查询字段数据")
    @GetMapping(value = "/columns")
    public ResponseEntity<Object> queryColumns(@RequestParam String tableName){
        return new ResponseEntity<>(TableDataInfo.build(generatorService.getColumns(tableName)), HttpStatus.OK);
    }

    @ApiOperation("批量修改字段数据")
    @PutMapping
    public ResponseEntity<HttpStatus> save(@RequestBody List<CodeColumnConfig> columnInfos){
        generatorService.updateBatchById(columnInfos);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("同步字段数据")
    @PostMapping(value = "sync")
    public ResponseEntity<HttpStatus> sync(@RequestBody List<String> tables){
        for (String table : tables) {
            generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("生成代码")
    @PostMapping(value = "/{tableName}/{type}")
    public ResponseEntity<Object> generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
        if(!generatorEnabled && type == 0){
            throw new BadRequestException("此环境不允许生成代码，请选择预览或者下载查看！");
        }
        switch (type){
            // 生成代码
            case 0: generatorService.generator(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName));
                break;
            // 预览
            case 1: return generatorService.preview(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName));
            // 打包
            case 2: generatorService.download(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName), request, response);
                break;
            default: throw new BadRequestException("没有这个选项");
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

