package com.uas.search.console.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 java.util.HashSet;
import java.util.Set;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.uas.search.console.model.ComponentSimpleInfo;
import com.uas.search.console.model.PropertyValueSimpleInfo;

/**
 * 用于对从数据库导出的器件和属性值两个文本文件进行处理，将每个器件所拥有的属性值提取出来，之后每个器件作为一行，以json的形式写入多个文件中
 * 
 * @author sunyj
 * @since 2016年8月8日 下午9:00:31
 */
public class MergeComponentData {

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

	// 器件和属性值两个文本文件的路径
	private static final String DATA_DIR = "C:\\Users\\sunyj-pc\\Desktop\\data\\prod";

	/**
	 * 将从本地文件读取的一行字符串数据解析为器件对象
	 * 
	 * @param data
	 *            格式为"CMP_ID" "CMP_BRID" "CMP_CODE" "CMP_KIID" "CMP_RESERVE"
	 *            "CMP_SAMPLEQTY" "CMP_ORIGINALQTY" "CMP_UUID" "CMP_INASTOCKQTY"
	 * @return
	 */
	private static ComponentSimpleInfo parseComponent(String data) {
		ComponentSimpleInfo component = new ComponentSimpleInfo();
		String[] strs = data.split("\t");
		component.setId(Long.parseLong(strs[0]));
		component.setBrandid(Long.parseLong(strs[1]));
		component.setCode(strs[2].substring(1, strs[2].length() - 1));
		if (!StringUtils.isEmpty(strs[3])) {
			component.setReserve(Double.valueOf(strs[3]));
		}
		if (!StringUtils.isEmpty(strs[4])) {
			component.setSampleQty(Double.valueOf(strs[4]));
		}
		if (!StringUtils.isEmpty(strs[5])) {
			component.setOriginalQty(Double.valueOf(strs[5]));
		}
		if (!StringUtils.isEmpty(strs[6])) {
			component.setInactionStockQty(Double.valueOf(strs[6]));
		}
		component.setKindid(Long.parseLong(strs[7]));
		component.setUuid(strs[8].substring(1, strs[8].length() - 1));
		return component;
	}

	/**
	 * 将从本地文件读取的一行字符串数据解析为属性值对象
	 * 
	 * @param data
	 *            格式为"PV_COMPONENTID" "PV_PROPERTYID" "PV_VALUE"，如1 394
	 *            "Copper Alloy"
	 * @return
	 */
	private static PropertyValueSimpleInfo parsePropertyValue(String data) {
		PropertyValueSimpleInfo propertyValue = new PropertyValueSimpleInfo();
		String[] strs = data.split("\t");
		propertyValue.setComponentid(Long.parseLong(strs[0]));
		propertyValue.setPropertyid(Long.parseLong(strs[1]));
		propertyValue.setValue(strs[2].substring(1, strs[2].length() - 1));
		return propertyValue;
	}

	public static void merge() {
		BufferedReader componentReader = null;
		BufferedReader propertyValueReader = null;
		PrintWriter printWriter = null;
		// 分成多个文件存储
		int fileIndex = 1;
		try {
			File file = new File(DATA_DIR + "\\components");
			if (!file.exists()) {
				file.mkdirs();
			}
			componentReader = new BufferedReader(new FileReader(DATA_DIR + "\\Component.txt"));
			propertyValueReader = new BufferedReader(new FileReader(DATA_DIR + "\\PropertyValue.txt"));
			printWriter = new PrintWriter(DATA_DIR + "\\components\\ComponentWithProperty_" + fileIndex + ".txt");
			// 第一行内容为列名，不做解析
			componentReader.readLine();
			propertyValueReader.readLine();
			String componentLine = null;
			String propertyValueLine = null;
			PropertyValueSimpleInfo propertyValue = null;
			int dataCount = 0;
			while ((componentLine = componentReader.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 + "\\components\\ComponentWithProperty_" + fileIndex + ".txt");
				}

				ComponentSimpleInfo component = parseComponent(componentLine);
				Set<PropertyValueSimpleInfo> properties = new HashSet<>();
				if (propertyValue == null && !StringUtils.isEmpty(propertyValueLine = propertyValueReader.readLine())) {
					propertyValue = parsePropertyValue(propertyValueLine);
				}
				while (propertyValue != null && propertyValue.getComponentid() != null
						&& propertyValue.getComponentid().equals(component.getId())) {
					propertyValue.setComponentid(null);
					properties.add(propertyValue);
					// 读到文件最后一行
					if ((propertyValueLine = propertyValueReader.readLine()) == null) {
						break;
					}
					propertyValue = parsePropertyValue(propertyValueLine);
				}
				component.setProperties(properties);
				// System.out.println(component);
				printWriter.println(JSONObject.toJSONString(component));
			}
			printWriter.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				componentReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				propertyValueReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			printWriter.close();
		}
	}

	public static void readData() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(DATA_DIR + "\\components\\ComponentWithProperty_1.txt"));
			String line = br.readLine();
			System.out.println(JSONObject.parseObject(line, ComponentSimpleInfo.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) {
	// merge();
	// // readData();
	// }

}
