 package com.connor.erp.BomAddTransferToERP;
 
 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 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.erpintegration.entity.BOMLineEntity;
import com.connor.erpintegration.entity.BomCheckResult;

import net.sf.json.JSONObject;
 
 public class BomAddImportXMLReader {
	 
//	 public static String loadXMLDatas(File file, StringBuffer errorInfo) {
//		 /*  23 */     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//		 /*     */     
//		 /*  25 */     Document doc = null;
//		 /*     */     
//		 /*     */     try {
//		 /*  28 */       DocumentBuilder builder = factory.newDocumentBuilder();
//		 /*  29 */       doc = builder.parse(new FileInputStream(file));
//		 /*  30 */     } catch (ParserConfigurationException e) {
//		 /*  31 */       e.printStackTrace();
//		 /*  32 */     } catch (SAXException e) {
//		 /*  33 */       e.printStackTrace();
//		 /*  34 */     } catch (IOException e) {
//		 /*  35 */       e.printStackTrace();
//		 /*     */     } 
//		 /*     */     
//		 /*  38 */     Element root = doc.getDocumentElement();
//		 /*  39 */     if (root.getAttributeNode("hasWrong").getNodeValue().equals("false")) {
//		 /*  40 */       return "成功";
//		 /*     */     }
//		 /*  42 */     NodeList list = root.getChildNodes();
//		 /*  43 */     return loadElements(list, errorInfo);
//		 /*     */   }
   public static void loadXMLDatas(File file, StringBuffer errorInfo) {
/*  23 */     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/*  25 */     Document doc = null;
     try {
/*  28 */       DocumentBuilder builder = factory.newDocumentBuilder();
/*  29 */       doc = builder.parse(new FileInputStream(file));
/*  30 */     } catch (ParserConfigurationException e) {
/*  31 */       e.printStackTrace();
/*  32 */     } catch (SAXException e) {
/*  33 */       e.printStackTrace();
/*  34 */     } catch (IOException e) {
/*  35 */       e.printStackTrace();
     } 
/*  38 */     Element root = doc.getDocumentElement();
///*  39 */     if (root.getAttributeNode("hasWrong").getNodeValue().equals("false"))
///*  40 */       return "成功"; 
///*  42 */     NodeList list = root.getChildNodes();
///*  43 */     return loadElements(list, errorInfo);
   }
   
   public static String loadElements(List<BomCheckResult> list, StringBuffer errorInfo) {
/*  47 */     int listLen = list.size();
/*  48 */     if (listLen == 0)
/*  49 */       return ""; 
/*  51 */     int existBomNum = 0;
/*  52 */     for (int i = 0; i < listLen; i++) {
/*  53 */       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")) {
					existBomNum++;
					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()) + " 装配件货位没定义<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>");
				}
     } 
/* 122 */     System.out.println("existBomNum=" + existBomNum);
/* 123 */     if (existBomNum == listLen)
/* 124 */       return "所有的BOM都已经存在"; 
/* 126 */     return "";
   }

public static String loadXMLDatas(List<AssemblyResult> results,StringBuffer errorInfo, Map<String, String> canSendErpMap) {
	// TODO Auto-generated method stub
			int listLen = results.size();
/*  48 */     if (listLen == 0)
/*  49 */       return ""; 
/*  51 */     int existBomNum = 0;
/*  52 */     for (int i = 0; i < listLen; i++) {
/*  53 */       AssemblyResult bomCheckResult = results.get(i);
				if(!bomCheckResult.getCanBOMImport().equals("false")) {
					canSendErpMap.put(bomCheckResult.getAssembly_item(), "true");
					continue;
				}
				Map<String, Class> classMap = new HashMap<String, Class>();
				classMap.put("BOMLine", ItemResult.class);
				AssemblyResult r =(AssemblyResult)JSONObject.toBean(JSONObject.fromObject(bomCheckResult),AssemblyResult.class,classMap);
				if(r.getAssembly_item_exist().equals("false"))
					errorInfo.append(String.valueOf(r.getAssembly_item()) + " 在ERP生产组织中已经存在<br>"); 
				if(r.getBom_exist().equals("true")) {
					existBomNum++;
					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("false".equals(r.getLocator_integrity()))
					errorInfo.append(String.valueOf(r.getAssembly_item()) + " 装配件货位没定义<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>");
				}
     } 
/* 122 */     System.out.println("existBomNum=" + existBomNum);
/* 123 */     if (existBomNum == listLen)
/* 124 */       return "所有的BOM都已经存在"; 
/* 126 */     return "";
		}
 }
