package com.securitycontrol.system.service;


import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.entity.system.SystemGlobal;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.util.List;
import java.util.Map;

/**
 * @author 黑子
 */
@Configuration
@Slf4j
public class ExportSqlService {

    public  static String TABLE_NAME="sys_logs";
    /**
     * 存储的路径
     */
    @Value("${sql.filePath}")
    private String filePath;

    /**
     * 数据库名
     */
    @Value("${sql.dbname}")
    private String database_name;


    @Resource
    private JdbcTemplate jdbcTemplate;



    @SneakyThrows
    public String  export(String logType ) {
        String dateTimeNow=DateTimeHelper.dateTimeNow();
        String path=filePath + TABLE_NAME + dateTimeNow;
        System.out.println(path);
        String sql="SELECT * FROM " + TABLE_NAME  +" where  log_type="+logType;
        SqlRowSet rows = jdbcTemplate.queryForRowSet(sql);
        // 创建文件并写入
        try  {
            FileWriter writer = new FileWriter(path+SystemGlobal.TEXT_FIX);
            while (rows.next()) {
                // 根据你的表结构生成你需要的SQL行
                String rowSql = "INSERT INTO sys_logs (log_id, opera_user_name,ip,user_id,model" +
                        ",oper_time,method,params,operate_detail,oper_type,oper_uri,log_type,result,times,failure_reason,grade,err_type,method_type,title) VALUES ('"
                        + isNotNull(rows.getString("log_id") )+ "', '"
                        +isNotNull(rows.getString("opera_user_name") )+ "', '"
                        +isNotNull(rows.getString("ip") )+ "', '"
                        + isNotNull(rows.getString("user_id")) + "', '"
                        + isNotNull(rows.getString("model") )+ "', '"
                        + isNotNull(rows.getString("oper_time")) + "', '"
                        +isNotNull(rows.getString("method") )+ "', '"
                        + isNotNull(rows.getString("params")) + "', '"
                        +isNotNull(rows.getString("operate_detail")) + "', '"
                        +isNotNull(rows.getString("oper_type") )+ "', '"
                        +isNotNull(rows.getString("oper_uri")) + "', '"
                        +isNotNull(rows.getString("log_type") )+ "', '"
                        + isNotNull(rows.getString("result") )+ "', '"
                        +isNotNull(rows.getString("times")) + "', '"
                        + isNotNull(rows.getString("failure_reason")) + "', '"
                        +isNotNull(rows.getString("grade")) + "', '"
                        +isNotNull(rows.getString("err_type")) + "', '"
                        + isNotNull(rows.getString("method_type")) + "', '"+
                        isNotNull(rows.getString("title") )+ "');";
                writer.write(rowSql + System.lineSeparator());
            }
        }catch (Exception e){
            log.error(e.toString(),e);
        }

        return dateTimeNow;
    }

    public String isNotNull(Object object){
            if(object==null){
                return "";
            }
            return object.toString();

    }


}

