package cn.net.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import cn.net.cpzslibs.prot.utils.StrUtils;

public class AnalyzeLog {
//	private static String filePath = "D:\\echo\\日志\\trace.log.1";
	private static String filePath = "D:\\echo\\日志\\trace.log.1";
//	private static String savePath = "D:\\echo\\日志\\trace.log.1";
//	private static String filePath = "D:\\echo\\JinTestData\\product_info.txt";
//	private static String filePath = "D:\\echo\\JinTestData\\log\\log1.txt";
//	static String s ="2017-11-16 18:13:04:748.319 INFO {update tbl_pro_box_301_17 set second_dealer_id = '37101004_11', dealer_delivery_relationid='1510825707633' where package_label_id in(288023140001,288023140102,288023140103,288023140107,288023140108,288023140109,288023140110)}";
	public static void main(String[] args) {
//		readAllInfo();
//		readAllInfoUpdate();
//		analyzeUpdateDateSql();
//		readAllProUpdate();
		analyzeUpdateProDateSql();
		
	}

	private static void analyzeUpdateProDateSql() {
		String logFileName="pro_allUpdatePro_trace.log";
		String updateOrginalFile = "E:\\echo\\新版手持机\\后期文档\\劲酒\\日志\\所有的update\\update pro relationID\\" + logFileName+".txt";
		String allUpdateNumFile = "E:\\echo\\新版手持机\\后期文档\\劲酒\\日志\\所有的update\\update pro relationID\\date_pro_by_labelId_"+logFileName+".txt";
		try {
			FileOperation operation = new FileOperation();
//			long startTime = System.currentTimeMillis();
			List<String> list = operation.readByIO(updateOrginalFile);
			
//			long end = System.currentTimeMillis();
//			System.err.println("时长：" +(end -startTime));
			String result = "";
			for (String orginalSql : list) {
				
				String beforeSql = "sales_time='";
				String dateS = orginalSql.substring(0, 19);
				String labelId = orginalSql.substring(orginalSql.indexOf("where"), orginalSql.indexOf(")")+1);
//				System.err.println(dateS+"\nlabelId:" + labelId);
				String updateTbl = orginalSql.substring(orginalSql.indexOf("update"), orginalSql.indexOf("relation_id"));
//				System.out.println("" + updateTbl);

				String updateProSql = updateTbl+beforeSql+dateS+"' " + labelId+";\n";
//				System.err.println("" + updateProSql);
				
				result = result + updateProSql;
			}
			

//			String destDirName = "D:\\echo\\JinTestData\\log\\" + s;
//			String fileName = "updateBoxSql1";
//			operation.saveFile(destDirName, fileName, result);
			operation.contentToTxt(allUpdateNumFile, result);;
//			System.err.println("list:" + list.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	private static void readAllProUpdate() {
		String logFileName="logs.tar2";
		String updateOrginalFile = "E:\\echo\\新版手持机\\后期文档\\劲酒\\日志\\logs\\" + logFileName;
		String allUpdateNumFile = "E:\\echo\\新版手持机\\后期文档\\劲酒\\日志\\logs\\pro_allUpdatePro_"+logFileName+".txt";
		try {
			FileOperation operation = new FileOperation();
//			long startTime = System.currentTimeMillis();
			List<String> list = operation.readByIO(updateOrginalFile);
			
//			long end = System.currentTimeMillis();
//			System.err.println("时长：" +(end -startTime));
			String result = "";
			for (String s : list) {
				if (s.contains("tbl_pro_301_18 set relation_id") || s.contains("tbl_pro_301_17 set relation_id")) {
					result = result + s+"\n";
				}
			}
			

//			String destDirName = "D:\\echo\\JinTestData\\log\\" + s;
//			String fileName = "updateBoxSql1";
//			operation.saveFile(destDirName, fileName, result);
			operation.contentToTxt(allUpdateNumFile, result);;
			System.err.println("list:" + list.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void analyzeUpdateDateSql() {
		Map<String, String> relationMaps = new HashMap<>();
//		List<String> singleUpdate = new ArrayList<>();
		String logFileName="allUpdateNum_trace.log";
		String updateOrginalFile = "E:\\echo\\新版手持机\\后期文档\\劲酒\\日志\\所有的update\\" + logFileName+".txt";
		String allUpdateNumFile = "E:\\echo\\新版手持机\\后期文档\\劲酒\\日志\\所有的update\\singleUpdate_"+logFileName+".txt";
		
		try {
			FileOperation operation = new FileOperation();
//			long startTime = System.currentTimeMillis();
			List<String> list = operation.readByIO(updateOrginalFile);
			
//			long end = System.currentTimeMillis();
//			System.err.println("时长：" +(end -startTime));
			for (String orginalSql : list) {
				
//				String orginalSql = "2017-12-29 09:44:36:080.692 INFO {update tbl_delivery_order set  deliverednum=1 where relationid = '7974161'}";
				String beforeSql = "UPDATE tbl_delivery_order SET deliveryDate='";
				String endSql = "' WHERE ";
				String dateS = orginalSql.substring(0, 19);
				String relationId = orginalSql.substring(orginalSql.indexOf("relationid = '"), orginalSql.indexOf("}"));
//				System.out.println("relationId:" +relationId);
				String updateSql = beforeSql + dateS + endSql + relationId+";\n";
//				System.err.println("updateSql:" + updateSql);
				relationMaps.put(relationId, updateSql);
			}
			
			String result = "";
			for (String value : relationMaps.values()) { 
				  System.out.println("Value = " + value); 
				  result = result + value;
				}

			operation.contentToTxt(allUpdateNumFile, result);;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void readAllInfoUpdate() {
		String logFileName="trace.log.10";
		String updateOrginalFile = "E:\\echo\\新版手持机\\后期文档\\劲酒\\日志\\logs\\" + logFileName;
		String allUpdateNumFile = "E:\\echo\\新版手持机\\后期文档\\劲酒\\日志\\logs\\allUpdateNum_"+logFileName+".txt";
		try {
			FileOperation operation = new FileOperation();
//			long startTime = System.currentTimeMillis();
			List<String> list = operation.readByIO(updateOrginalFile);
			
//			long end = System.currentTimeMillis();
//			System.err.println("时长：" +(end -startTime));
			String result = "";
			for (String s : list) {
				if (s.contains("update tbl_delivery_order set  deliverednum=")) {
					result = result + s+"\n";
				}
			}
			

//			String destDirName = "D:\\echo\\JinTestData\\log\\" + s;
//			String fileName = "updateBoxSql1";
//			operation.saveFile(destDirName, fileName, result);
			operation.contentToTxt(allUpdateNumFile, result);;
			System.err.println("list:" + list.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static void readAllInfo() {
		try {
			FileOperation operation = new FileOperation();
			List<String> list = operation.readByIO(filePath);
			
//			long end = System.currentTimeMillis();
//			System.err.println("时长：" +(end -startTime));
			String result = "";
			for (String s : list) {
				if (s.contains("dealer_delivery_relationid")&&s.contains("update tbl_pro_box_") && !s.contains("update tbl_pro_box_302")) {
					String updateSql=s.substring(s.indexOf("{")+1, s.indexOf("}"));
//			         System.out.println(updateSql);
					result = result + updateSql+";\r\n";
				}
			}
			
			Date d = new Date();
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String s = format.format(d);

			String destDirName = "D:\\echo\\JinTestData\\log\\" + s;
			String fileName = "updateBoxSql1";
			operation.saveFile(destDirName, fileName, result);
			System.err.println("list:" + list.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static List<String> readByIO(String path) throws IOException{
		List<String> l = new ArrayList<>();
		String result=null; 
		FileInputStream inputStream = null;
		Scanner sc = null;
		try {
		    inputStream = new FileInputStream(path);
		    sc = new Scanner(inputStream, "UTF-8");
		    while (sc.hasNextLine()) {
		        String line = sc.nextLine();
		        if (!StrUtils.isEmpty(line)) {
					l.add(line);
					if (line.contains("dealer_delivery_relationid")&&line.contains("update tbl_pro_box_") && !line.contains("update tbl_pro_box_302")) {
						String updateSql=line.substring(line.indexOf("{")+1, line.indexOf("}"));
				         System.out.println(updateSql);
						result = result + updateSql+";\r\n";
					}
//					result=result+line+"\r\n";  
				}
		    }
		    
		    FileOperation f = new FileOperation();
		    saveFile("updateBoxSql1", result);
		    // note that Scanner suppresses exceptions
		    if (sc.ioException() != null) {
		        throw sc.ioException();
		    }
		} finally {
		    if (inputStream != null) {
		        inputStream.close();
		    }
		    if (sc != null) {
		        sc.close();
		    }
		    return l;
		}
	}
	
	private static void saveFile(String fileName,String saveStr) {
		Date d = new Date();
//		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HHmmss"); //
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String s = format.format(d);

		FileOperation operation = new FileOperation();
//		String filePath = "D:\\echo\\JinTestData\\" + s + "\\trays.text";
		String destDirName = "D:\\echo\\JinTestData\\log\\" + s;
		String filePath = destDirName + "\\" + fileName + System.currentTimeMillis()+".txt";
		
		
		
		try { 
//			System.err.println(trays.size() + " trays:" + trays.toString());
//			System.out.println(" trayStr:" + trayStr.toString());
			operation.createDir(destDirName);
			operation.createFile(new File(filePath));
//			写入
			
			operation.contentToTxt(filePath, saveStr); 
			// List<String> l = operation.readTextFile(new File(filePath)); //

		} catch (Exception e) { // TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static List<String> readLogFile(File fileName)throws IOException{  
		  String result=null;  
		  List<String> list = new ArrayList<>();
		  FileReader fileReader=null;  
		  BufferedReader bufferedReader=null;  
		  try{  
		   fileReader=new FileReader(fileName);  
		   bufferedReader=new BufferedReader(fileReader);  
		   try{  
		    String read=null;  
		    while((read=bufferedReader.readLine())!=null){  
		    	if (read != null) {
		    		if (read.contains("{update tbl_pro_box_")) {
//						read = read.substring(beginIndex, endIndex)
		    			read = read.substring(read.indexOf("{")+1, read.indexOf("}"));
					}
		    		list.add(read);
				}
		     result=result+read+"\r\n";  
		    }  
		   }catch(Exception e){  
		    e.printStackTrace();  
		   }  
		  }catch(Exception e){  
		   e.printStackTrace();  
		  }finally{  
		   if(bufferedReader!=null){  
		    bufferedReader.close();  
		   }  
		   if(fileReader!=null){  
		    fileReader.close();  
		   }  
		  }  
//		  System.out.println("读取出来的文件内容是："+"\r\n"+result);  
		  return list;  
		 } 

}
