package easydo.technology.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.dynamic.datasource.annotation.DS;

import easydo.technology.annotation.WebLog;
import easydo.technology.exception.BizException;
import easydo.technology.model.CftzBankInfo;
import easydo.technology.util.OracleClient;
import lombok.extern.java.Log;

@Log
@RestController
@RequestMapping("/finance")
@DS("oa")
public class FinanceController {
    @Resource
    DataSource dataSource;
    @Resource
    OracleClient oracleClient;

    @WebLog
    @RequestMapping(value = "/getPage")
    public Object getPage(@RequestBody Map<String, Object> map) throws Exception{
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            Map<String, Object> page = oracleClient.getJdbcPage(map, CftzBankInfo.class, connection);
            return page;
        } catch (Exception e) {
            throw e;
        } finally {
            oracleClient.finallyExecute(connection);
        }
    }

    public static void main(String[] args) {
        // Oracle数据库的JDBC URL格式：jdbc:oracle:thin:@<hostname>:<port>:<dbname>
        String jdbcUrl = "jdbc:oracle:thin:@172.16.1.20:1521:orcl";
        String username = "CFTZOA";
        String password = "CFTZOAVIEW";
        OracleClient oracleClient = new OracleClient();
        Connection connection = null;
        try {
            // 加载Oracle JDBC驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 建立连接
            connection = DriverManager.getConnection(jdbcUrl, username, password);

            Map<String, Object> map = new HashMap<>();
            map.put("current", 1);
            map.put("size", 10);
            Map<String, Object> page = oracleClient.getJdbcPage(map, CftzBankInfo.class, connection);
        } catch (Exception e) {
            oracleClient.finallyExecute(connection);
        }
    }
}
