package com.trs.web2frame.myutils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.trs.web2frame.tool.tyjyh.ConnectionUtil;
import com.trs.web2frame.util.Log_Exception;

public class WriteChannel {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String pid="27";
		writeChildrenContent(pid, 0);
	}
	
	private static void writeChildrenContent(String parentid, int level) throws Exception {
//		StringBuilder sb = new StringBuilder("select DisplayName,id from Channel where siteid=27 and  parentid=" + parentid+" order by DisplaySN");
		StringBuilder sb = new StringBuilder("select c.DisplayName,c.id from NaviNodeSet n left join Channel c on n.NodeId=c.id where c.siteid=27 and c.parentid=" + parentid+" order by c.DisplaySN");
		Connection connection = ConnectionUtil.getConnection("sqlserver");
		Statement st = connection.createStatement();
		ResultSet rs = st.executeQuery(sb.toString());
		
		while (rs.next()) {
//			for (int i=0; i<level; i++) {
//				System.out.print("\t");
//				Logger_txt("\t");
//			}
			System.out.println(rs.getString("DisplayName")/*+"	"+rs.getString("id")*/);
		//	if (rs.getInt(null)i)
			Logger_txt(rs.getString("DisplayName")+"	"+rs.getString("id"));
			Logger_txt("\r\n");
			writeChildrenContent(rs.getString("id"), level+1);
		}
		
		ConnectionUtil.closeConnection(rs, st, connection);
	}
	
    public static void Logger_txt(String content){
        File file = new File("D:\\download\\zrzyt");
        if(!file.exists())file.mkdirs();
        String path = "D:\\download\\zrzyt"+"/channel2345678.txt";
        FileWriter fw = null;
        try {
            File F = new File(path);
            // 如果文件不存在,就动态创建文件
            if (!F.exists()) {
                F.createNewFile();
            }
            // 设置为:True,表示写入的时候追加数据
            fw = new FileWriter(F, true);
            // 回车并换行
            fw.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
