package com.jy.generator.controller;

import com.jy.framework.model.base.R;
import com.jy.generator.builder.GeneratorBuilder;
import com.jy.generator.domain.GenCodeVo;
import com.jy.generator.service.IndexService;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/api")
public class IndexController {

    private static final String tableSql = "select table_name tableName, table_comment tableComment " +
            "from information_schema.tables where table_schema = ?";

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private IndexService indexService;

    @GetMapping("/tableList")
    public R tables() {
        String dbName = GeneratorBuilder.config.getUrl().split("/")[3].split("\\?")[0];
        List<Map<String, Object>> tableInfoList = jdbcTemplate.queryForList(tableSql, dbName);
        return R.success(tableInfoList);
    }

    @PostMapping("/genCode")
    public R genCode(@RequestBody GenCodeVo genCodeVo) {
        indexService.genCode(genCodeVo);
        return R.success();
    }

}
