package com.lemon.utils;

import com.lemon.cases.BaseCase;
import com.lemon.pojo.TestRecord;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.math.BigInteger;
import java.sql.Connection;

/**
 * @program: Lemon
 * @description:
 * @author: ray
 * @create: 2022-09-27 14:20
 **/

public class SqlUtils {


    public static Logger log = Logger.getLogger(BaseCase.class);
    /**
     * 传入sql语句执行sql查询，并且返回查询结果
     *
     * @param sql
     * @return
     */
    public static Object querySingle(String sql) {
        Object result = null;
        Connection conn = null;
        //如果sql语句为空直接返回为空
        if (StringUtils.isBlank(sql)) {
            return null;
        }
        try {
            //创建queryRunner对象
            QueryRunner runner = new QueryRunner();
            //调用查询方法，传入数据库连接、SQL语句、返回值类型
            conn = JDBCUtils.getConnection();
            // 查询操作，使用query
            result = runner.query(conn, sql, new ScalarHandler<>());
            log.info("querySingle:" + sql   + "rsult:"+ result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn);
        }
        return result;
    }


    public static void insertTestRecord(TestRecord testRecord) {
        /**
         * public class TestRecord {
         *     private Long id;
         *     private String inquiry;
         *     private String inputContent;
         *     private String outputContent;
         *     private Integer state;
         *     private String testVersion;
         *     private Date createTime;
         *     private Date updateTime;
         *     private Integer isDelete;
         *     private Long caseId;
         *     private String caseDesc;
         *     private String expectContent;
         */
        /**
         * CREATE TABLE `test_record` (
         *   `id` bigint(20) NOT NULL COMMENT '主鍵id',
         *   `inquiry` varchar(255) DEFAULT NULL COMMENT '用户输入',
         *   `input_content` varchar(2000) DEFAULT NULL COMMENT '接口入参',
         *   `output_content` varchar(2000) DEFAULT NULL COMMENT '接口出参',
         *   `state` tinyint(4) DEFAULT NULL COMMENT '用例状态 0 未通过  1通过',
         *   `test_version` varchar(255) DEFAULT NULL COMMENT '测试版本',
         *   `create_time` datetime DEFAULT NULL COMMENT '创建时间',
         *   `update_time` datetime DEFAULT NULL COMMENT '更新时间',
         *   `is_delete` tinyint(4) DEFAULT NULL COMMENT '删除状态',
         *   `case_id` bigint(20) DEFAULT NULL COMMENT '案例ID',
         *   `case_desc` varchar(255) DEFAULT NULL COMMENT '案例描述',
         *   `expect_content` varchar(2000) DEFAULT NULL COMMENT '预期结果',
         *   PRIMARY KEY (`id`)
         * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
         */
        Connection conn = null;
        try {
            //创建queryRunner对象
            QueryRunner runner = new QueryRunner();
            //调用查询方法，传入数据库连接、SQL语句、返回值类型
            conn = JDBCUtils.getConnection();
            // 插入操作，使用update
            String sql = "INSERT INTO test_record (inquiry, input_content, output_content, state, test_version, create_time, update_time, is_delete, case_id, case_desc, expect_content) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            // 插入数据并获取生成的主键
            BigInteger generatedId = (BigInteger)runner.insert(conn, sql, new ScalarHandler<BigInteger>(),
                    testRecord.getInquiry(),
                    testRecord.getInputContent(),
                    testRecord.getOutputContent(),
                    testRecord.getState(),
                    testRecord.getTestVersion(),
                    testRecord.getCreateTime(),
                    testRecord.getUpdateTime(),
                    testRecord.getIsDelete(),
                    testRecord.getCaseId(),
                    testRecord.getCaseDesc(),
                    testRecord.getExpectContent()
            );
            // 设置生成的主键到 testRecord 对象
            testRecord.setId(generatedId.longValue());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn);
        }
    }
}
