package com.api.customization.qc2738217.util;

import weaver.conn.RecordSet;
import weaver.general.Util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * ClassName:com.api.customization.qc2738217.util.CubOscUtil
 *
 * @date:2023/12/26 10:37
 * @author:Oscar
 */
public class CubOscUtil {
    /**
     * 通过Sql语句获取 LIST ->Map 对象
     * @param sql
     * @return
     */
    public static List<Map> getSqlToList(String sql){
        List<Map> list=new ArrayList<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        while (rs.next()) {
            Map entity = new HashMap();
            String[] columnName = rs.getColumnName();
            for (String colume : columnName) {
                String columeParam = Util.null2String(rs.getString(colume));
                entity.put(colume, columeParam);
            }
            list.add(entity);
        }
        return list;
    }
    /**
     * 通过Sql语句获取Map对象
     * @param sql
     * @return
     */
    public static Map getSqlToMap(String sql){
        List<Map> list=getSqlToList(sql);
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }
    public static void outlog( String meg,String name){
        SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format4 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String str = format3.format(date);
        String str2 = format4.format(date);
        meg= str+"___"+meg;
        CubOscUtil cubOscUtil=new CubOscUtil();
        File f2 = new File(cubOscUtil.getClass().getResource("").getPath());
        System.out.println(meg);
        File fileName = new File (f2+"/log_"+str2+"_"+name+".txt");
        if(!fileName.exists()){
            try {
                fileName.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileWriter writer = null;
        try {
            writer = new FileWriter(fileName, true);
            writer.write(meg+"\r\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
