package com.connor.erp.BomChgTransferToERP;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.connor.erp.BomAddTransferToERP.AssemblyResult;
import com.connor.erp.BomAddTransferToERP.ItemResult;
import com.connor.erpintegration.entity.BOMLineEntity;
import com.connor.erpintegration.entity.BomCheckResult;

import net.sf.json.JSONObject;

public class BomChgImportXMLReader {
	public static final String SEPARATOR = "|";

	public static String loadXMLDatas(File file) {
		/* 22 */ StringBuffer errorInfo = new StringBuffer("");
		/* 24 */ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		/* 26 */ Document doc = null;
		try {
			/* 29 */ DocumentBuilder builder = factory.newDocumentBuilder();
			/* 30 */ doc = builder.parse(new FileInputStream(file));
			/* 31 */ } catch (ParserConfigurationException e) {
			/* 32 */ e.printStackTrace();
			/* 33 */ } catch (SAXException e) {
			/* 34 */ e.printStackTrace();
			/* 35 */ } catch (IOException e) {
			/* 36 */ e.printStackTrace();
		}
		/* 39 */ Element element = doc.getDocumentElement();
		/* 40 */ NodeList list = element.getChildNodes();
///*  41 */     loadElements(list, errorInfo);
		/* 42 */ return errorInfo.toString();
	}
	
	public static String loadElements(List<BomCheckResult> list) {
		/* 46 */ if (list.size() == 0)
			return "";
		StringBuffer errorInfo = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			BomCheckResult bomCheckResult = list.get(i);
			Map<String, Class> classMap = new HashMap<String, Class>();
			classMap.put("BOMLine", BOMLineEntity.class);
			BomCheckResult r = (BomCheckResult) JSONObject.toBean(JSONObject.fromObject(bomCheckResult),
					BomCheckResult.class, classMap);
			if (r.getAssembly_item_exist().equals("true"))
				errorInfo.append(String.valueOf(r.getAssembly_item()) + " 在ERP生产组织中已经存在<br>");
			if (r.getBom_exist().equals("true")) 
				errorInfo.append(String.valueOf(r.getAssembly_item()) + " BOM在ERP生产组织中已经存在<br>");
			if (r.getRoute_exist().equals("true"))
				errorInfo.append(String.valueOf(r.getAssembly_item()) + " 在ERP生产组织中已经存在<br>");
			if (!"true".equals(r.getLocator_integrity()))
				errorInfo.append(String.valueOf(r.getAssembly_item()) + " 在ERP生产组织中已经存在<br>");
			List<BOMLineEntity> bomline = r.getBOMLine();
			for (BOMLineEntity b : bomline) {
				if (b.getItem_exist().equals("false"))
					errorInfo.append(String.valueOf(b.getItem()) + " 对象在生产组织中不存在<br>");
				else if (b.getItem_valid().equals("false"))
					errorInfo.append(String.valueOf(b.getItem()) + " 对象在生产组织中失效<br>");
				if (!"true".equals(b.getLocator_integrity()))
					errorInfo.append(String.valueOf(b.getItem()) + " 装配子件定义了子库但没定义货位，请先登录ERP手动填写货位信息<br>");
				if (b.getOperation_seq_num_exist_in_route().equals("false"))
					errorInfo.append(
							String.valueOf(b.getItem()) + " " + b.getOperation_seq_num() + "工序    在装配件对应工艺路线中不存在！<br>");
			}
		}
		return errorInfo.toString();
	}

	public static String loadElements(List<AssemblyResult> list,String string) {
		/* 46 */ if (list.size() == 0)
			return "";
		StringBuffer errorInfo = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			AssemblyResult bomCheckResult = list.get(i);
			Map<String, Class> classMap = new HashMap<String, Class>();
			classMap.put("BOMLine", ItemResult.class);
			AssemblyResult r = (AssemblyResult) JSONObject.toBean(JSONObject.fromObject(bomCheckResult),
					AssemblyResult.class, classMap);
			String canBOMImport = r.getCanBOMImport();
			if(!canBOMImport.equals("false")) {
				continue;
			}
			if (r.getAssembly_item_exist().equals("false")) {
				errorInfo.append(String.valueOf(r.getAssembly_item()) + " 在ERP生产组织中已经存在<br>");
				System.out.println(r.getAssembly_item_exist()+"assembly_item_exist");
			}
				
			if (r.getBom_exist().equals("false")) 
				errorInfo.append(String.valueOf(r.getAssembly_item()) + " BOM在ERP生产组织中已经存在<br>");
			if (r.getRoute_exist().equals("false"))
				errorInfo.append(String.valueOf(r.getAssembly_item()) + " 装配件没有ROUTE<br>");
			if (!"true".equals(r.getLocator_integrity()))
				errorInfo.append(String.valueOf(r.getAssembly_item()) + " 在ERP生产组织中已经存在<br>");
			List<ItemResult> bomline = r.getBOMLine();
			for (ItemResult b : bomline) {
				if (b.getItem_exist().equals("false"))
					errorInfo.append(String.valueOf(b.getItem()) + " 对象在生产组织中不存在<br>");
				else if (b.getItem_valid().equals("false"))
					errorInfo.append(String.valueOf(b.getItem()) + " 对象在生产组织中失效<br>");
				if (!"true".equals(b.getLocator_integrity()))
					errorInfo.append(String.valueOf(b.getItem()) + " 装配子件定义了子库但没定义货位，请先登录ERP手动填写货位信息<br>");
				if (b.getOperation_seq_num_exist_in_route().equals("false"))
					errorInfo.append(
							String.valueOf(b.getItem()) + " " + b.getOperation_seq_num() + "工序    在装配件对应工艺路线中不存在！<br>");
			}
		}
		return errorInfo.toString();
	}
/*     */   public static void loadElements(List<CompareResult> results, Vector<Vector<Object>> erpData, Vector<String> vec) {
/*  43 */     if (results.size() == 0) {
/*     */       return;
/*     */     }
/*  46 */     for (int i = 0; i <results.size(); i++) {
/*  47 */     	 CompareResult compareResult = results.get(i);
				Map<String, Class> classMap = new HashMap<String, Class>();
				classMap.put("BOMLine", CompareLine.class);
				CompareResult r =(CompareResult)JSONObject.toBean(JSONObject.fromObject(compareResult),CompareResult.class,classMap);
				List<CompareLine> bomLine = r.getBOMLine();
				for(int t=0;t<bomLine.size();t++) {
					CompareLine compare = bomLine.get(t);
					Vector<Object> rowData = new Vector();
					rowData.add("2");
					rowData.add(compareResult.getAssembly_item());
//					rowData.add(assembly_item);
					rowData.add(compare.getItem());
//					rowData.add("item_name");
					rowData.add(compare.getOperation_seq_num());
					rowData.add(compare.getQuantity());
//					rowData.add(compare.getLineState());
	               	erpData.add(rowData);
	               	int status = Integer.parseInt(compare.getLineState());
	               	String compare_status="";
	               	switch (status) {
	                    case 0:
	                      compare_status = "比较失败";
	                       break;
	                    case 1:
	                       compare_status = "新增";
	                       break;
	                     case 2:
	                       compare_status = "修改";
	                       break;
	                     case 3:
	                       compare_status = "删除";
	                       break;
	                     case 4:
	                    	 compare_status = "未改";
	                    	 vec.add(compare_status);
                    	 break;
                   } 
	               	rowData.add(compare_status);
				}
				
///*  48 */       NodeList childlist = node.getChildNodes();
///*  49 */       int listSize = childlist.getLength();
///*  50 */       String assembly_item_number_str = "";
///*  51 */       for (int j = 0; j < listSize; j++) {
///*  52 */         Node childnode = childlist.item(j);
///*  53 */         String nodeName = childnode.getNodeName();
///*  54 */         if (nodeName.equals("assembly_item_number")) {
///*  55 */           assembly_item_number_str = childnode.getTextContent();
///*  56 */         } else if (nodeName.equals("component_item_number")) {
///*  57 */           NodeList component_item_number_child_node_list = childnode.getChildNodes();
///*  58 */           for (int k = 0; k < component_item_number_child_node_list.getLength(); k++) {
///*  59 */             Node component_item_number_child_node = component_item_number_child_node_list.item(k);
///*  60 */             String component_item_number_nodeName = component_item_number_child_node.getNodeName();
///*  61 */             if (component_item_number_nodeName.equals("item")) {
///*  62 */               Vector<Object> rowData = new Vector();
///*  63 */               NodeList item_list = component_item_number_child_node.getChildNodes();
///*  64 */               String item_name = "";
///*  65 */               String operation_seq_num = "";
///*  66 */               String item_quantity = "";
///*  67 */               String compare_status = "";
///*  68 */               for (int m = 0; m < item_list.getLength(); m++) {
///*  69 */                 Node item = item_list.item(m);
///*  70 */                 String node_name = item.getNodeName();
///*  71 */                 String node_value = item.getTextContent();
///*  72 */                 if (node_name.equals("item_name")) {
///*  73 */                   item_name = node_value;
///*  74 */                 } else if (node_name.equals("operation_seq_num")) {
///*  75 */                   operation_seq_num = node_value;
///*  76 */                 } else if (node_name.equals("item_quantity")) {
///*  77 */                   item_quantity = node_value;
///*  78 */                 } else if (node_name.equals("compare_status")) {
///*  79 */                   int status = Integer.parseInt(node_value);
///*  80 */                   switch (status) {
///*     */                     case 0:
///*  82 */                       compare_status = "比较失败";
///*     */                       break;
///*     */                     case 1:
///*  85 */                       compare_status = "新增";
///*     */                       break;
///*     */                     case 2:
///*  88 */                       compare_status = "修改";
///*     */                       break;
///*     */                     case 3:
///*  91 */                       compare_status = "删除";
///*     */                       break;
///*     */                     case 4:
///*  94 */                       compare_status = "未改";
///*  95 */                       vec.add(compare_status);
///*     */                       break;
///*     */                   } 
///*     */ 
///*     */                 
///*     */                 } 
///*     */               } 
///* 102 */               
///*     */             } 
///*     */           } 
///*     */         } 
///*     */       } 
/*     */     } 
/*     */   }
}
