package top.imwork.assist.generator.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.imwork.assist.generator.dao.BaseMapper;
import top.imwork.assist.generator.service.IGeneratorService;
import top.imwork.commons.core.pojo.ResponseMsg;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2015-2025
 * FileName: GeneratorServiceImpl
 * Author<作者姓名>:   silos
 * CreateTime<创建时间>:   2025/11/9 19:15
 * UpdateTime<修改时间>:   2025/11/9 19:15
 * Description〈功能简述〉:
 * History<历史描述>:
 * Since<版本号>: 1.0.0
 */
@Service
public class GeneratorServiceImpl implements IGeneratorService {
    @Autowired
    private BaseMapper baseMapper;

    private DataSource getDataSource(){
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://mysql.imwork.top:63306/imwork_silos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC");
        dataSource.setUser("root");
        dataSource.setPassword("zh.3417.JXW");
        return dataSource;
    }

    private Connection getConnection() throws SQLException {
        Connection connection = getDataSource().getConnection();
        return connection;
    }
    @Override
    public ResponseMsg list(Map<String, Object> params) {
        Integer pages = Integer.parseInt(String.valueOf(params.get("page")));
        Integer rows = Integer.parseInt(String.valueOf(params.get("limit")));

        Page<?> page = PageHelper.startPage(pages, rows);
        List<Map<String, Object>> list = baseMapper.queryList(params);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", (int) page.getTotal());
        map.put("data", list);
        return ResponseMsg.ok(map);
    }

    @Override
    public Map<String, String> queryTable(String tableName) {
        return Map.of();
    }
}