package com.roma.romaapi.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.roma.romaapi.service.DbApiService;
import com.roma.romaapi.utils.CustomResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@CrossOrigin
public class DbApiController {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    DbApiService dbApiService;
    @RequestMapping("/test1")
    public Object test1() throws JsonProcessingException {
//        // 获取redis中的值
//        String res = stringRedisTemplate.opsForValue().get("sysConfig");
//        System.out.print("res --->");
//        System.out.print(res);
        ObjectMapper mapper = new ObjectMapper();
        String json = "{\"name\":\"mkyong\", \"age\":\"37\"}";

        Object jsonMap = mapper.readValue(json, Map.class);
        System.out.print(jsonMap);

        return "sqlId";
    }

    @RequestMapping("/dbapi/{code}")
    public Map dbApi(@PathVariable("code") String sqlCode, HttpServletRequest request) {
        Map<String, String[]> maps = request.getParameterMap();
        // 通过sqlId执行sql语句
        Map<String, Object> ret = dbApiService.transferSql(sqlCode, maps);
        return CustomResponse.formatResponse(ret);
    }
}

