package cn.stylefeng.team.meta.db;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.stylefeng.team.meta.db.pojo.DatabaseConfig;
import cn.stylefeng.team.meta.db.pojo.JsonDataInfo;
import cn.stylefeng.team.meta.db.pojo.query.Group;

import java.util.HashMap;
import java.util.List;

public class OracleDataCollector extends BaseDataCollector {

    private static final String PAGE_QUERY = "SELECT * FROM {tableName} OFFSET {offset} ROWS FETCH NEXT {pageSize} ROWS ONLY";

    private static final String QUERY_TOTAL = "SELECT * FROM {tableName}";

    public OracleDataCollector(DatabaseConfig databaseConfig) {
        super(databaseConfig);
    }

    @Override
    public List<Entity> queryTotal(String tableName) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("tableName", tableName);
        return this.queryDataList(StrUtil.format(QUERY_TOTAL, stringStringHashMap));
    }

    @Override
    public Long queryTotalCount(String tableName) {
        return 0L;
    }

    @Override
    public Long queryTotalCount(String tableName, Group queryCondition) {
        return 0L;
    }

    @Override
    public List<JsonDataInfo> queryTotalToJson(String tableName) {
        List<Entity> entities = this.queryTotal(tableName);
        return toJsonDataInfo(entities);
    }

    @Override
    public List<Entity> pageQuery(String tableName, Integer pageSize, Integer pageNo) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("tableName", tableName);
        stringStringHashMap.put("pageSize", pageSize);
        stringStringHashMap.put("offset", (pageNo - 1) * pageSize);
        return this.queryDataList(StrUtil.format(PAGE_QUERY, stringStringHashMap));
    }

    @Override
    public List<JsonDataInfo> pageQueryToJson(String tableName, Integer pageSize, Integer pageNo) {
        List<Entity> entities = this.pageQuery(tableName, pageSize, pageNo);
        return toJsonDataInfo(entities);
    }

    @Override
    public List<JsonDataInfo> pageQueryToJson(String tableName, Integer pageSize, Integer pageNo, Group queryCondition) {
        return this.pageQueryToJson(tableName, pageSize, pageNo);
    }

    public static void main(String[] args) {
        DatabaseConfig databaseConfig = new DatabaseConfig("guns", "guns123456", "jdbc:oracle:thin:@//192.168.31.33:1521/ORCLPDB1", "oracle.jdbc.OracleDriver");
        OracleDataCollector oracleDataCollector = new OracleDataCollector(databaseConfig);
        List<JsonDataInfo> data = oracleDataCollector.pageQueryToJson("PHO_PBS_PRJ1", 10, 1);
        for (JsonDataInfo datum : data) {
            System.out.println(datum);
        }
    }

}
