package com.uas.search.console.b2b.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import com.uas.search.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.uas.search.console.b2b.platformmanager.model.UserSpaceDetailSimpleInfo;

/**
 * 用于对从数据库导出的企业文本文件进行处理，以json的形式写入多个文件中
 * 
 * @author sunyj
 * @since 2016年8月8日 下午9:00:31
 */
public class MergeUserSpaceDetailData {

	// 单个文件存储的最大数据数目
	public static final int SINGLE_FILE_MAX_SIZE = 100000;

	// 文本文件的路径
	private static final String DATA_DIR = "C:\\Users\\sunyj-pc\\Desktop\\temp\\data\\prod";

	/**
	 * 将从本地文件读取的一行字符串数据解析为对象
	 * 
	 * @param data
	 *            格式为"NAME" "SHORTNAME" "INDUSTRY" "ADMINNAME" "ID"
	 * @return
	 */
	private static UserSpaceDetailSimpleInfo parseUsd(String data) {
		UserSpaceDetailSimpleInfo userSpaceDetail = new UserSpaceDetailSimpleInfo();
		String[] strs = data.split("\t");
		if (!StringUtils.isEmpty(strs[0])) {
			userSpaceDetail.setName(strs[0].substring(1, strs[0].length() - 1));
		}
		if (!StringUtils.isEmpty(strs[1])) {
			userSpaceDetail.setShortname(strs[1].substring(1, strs[1].length() - 1));
		}
		if (!StringUtils.isEmpty(strs[2])) {
			userSpaceDetail.setIndustry(strs[2].substring(1, strs[2].length() - 1));
		}
		if (!StringUtils.isEmpty(strs[3])) {
			userSpaceDetail.setAdminname(strs[3].substring(1, strs[3].length() - 1));
		}
		if (!StringUtils.isEmpty(strs[4])) {
			userSpaceDetail.setId(Long.parseLong(strs[4]));
		}
		if (!StringUtils.isEmpty(strs[5])) {
			userSpaceDetail.setSearchstatus(Short.parseShort(strs[5]));
		}
		return userSpaceDetail;
	}

	public static void parse() {
		BufferedReader usdReader = null;
		PrintWriter printWriter = null;
		// 分成多个文件存储
		int fileIndex = 1;
		try {
			File file = new File(DATA_DIR + "\\usd");
			if (!file.exists()) {
				file.mkdirs();
			}
			usdReader = new BufferedReader(new FileReader(DATA_DIR + "\\AC_US_DETAIL.tsv"));
			printWriter = new PrintWriter(DATA_DIR + "\\usd\\" + fileIndex + ".txt");
			// 第一行内容为列名，不做解析
			usdReader.readLine();
			String usdLine = null;
			int dataCount = 0;
			while ((usdLine = usdReader.readLine()) != null) {
				dataCount++;
				// 一个文件存放100000条数据，一旦超过，写入新的文件
				if (dataCount > SINGLE_FILE_MAX_SIZE) {
					System.out.println("--------------------------------new file" + fileIndex);
					dataCount = 1;
					printWriter.flush();
					printWriter.close();
					fileIndex++;
					printWriter = new PrintWriter(DATA_DIR + "\\usd\\" + fileIndex + ".txt");
				}

				try {
					printWriter.println(JSONObject.toJSONString(parseUsd(usdLine)));
				} catch (Exception e) {
					System.out.println(usdLine);
				}
			}
			printWriter.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				usdReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			printWriter.close();
		}
	}

	public static void readData() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(DATA_DIR + "\\usd\\1.txt"));
			String line = br.readLine();
			System.out.println(JSONObject.parseObject(line, UserSpaceDetailSimpleInfo.class));
		} catch (FileNotFoundException e) {
			System.out.println("File not found!");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// public static void main(String[] args) {
	// parse();
	//// readData();
	// }

}
