 package com.connor.erp.materialquery.ViewWorkInProcessOfERP;
 
 import com.connor.erp.materialquery.InProcessReadXMLStr;
import com.connor.erp.processresource.EquipmentAcquiredFromERP.TCPreferenceUitl;
// import com.connor.erp.materialquery.MaterialQueryXMLWriter;
 import com.connor.erp.toolkit.ActionType;
 import com.connor.erp.toolkit.FileType;
import com.connor.erp.toolkit.HttpUtil;
import com.connor.erp.toolkit.UtilApi;
// import com.origin.tcerp.conn.PlmErpIntegration;
 import com.teamcenter.rac.aif.AbstractAIFCommand;
 import com.teamcenter.rac.aif.AbstractAIFUIApplication;
 import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
 import com.teamcenter.rac.util.MessageBox;

import net.sf.json.JSONObject;

import java.io.File;
 import java.io.FileNotFoundException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerException;
 
 public class ViewWorkInProcessOfERPCommand extends AbstractAIFCommand {
   private TCSession session;
   
   private AbstractAIFUIApplication application;
   
   private List<String> listItemId;
   
   private String erpPreference = "connor_tc_to_erp_url";
   
   public ViewWorkInProcessOfERPCommand(TCSession pSession, AbstractAIFUIApplication app, List<String> plistItemId) {
/*  52 */     this.session = pSession;
/*  53 */     this.application = app;
/*  54 */     this.listItemId = plistItemId;
/*  55 */     callERPDll();
   }
   
   private void callERPDll() {
/*  64 */     String user_id = "";
     try {
/*  66 */       user_id = this.session.getUser().getProperty("user_id");
/*  67 */     } catch (TCException e) {
/*  68 */       e.printStackTrace();
     } 
///*  70 */     String xmlFileName = 
///*  71 */       System.getProperty("java.io.tmpdir") + 
///*  72 */       File.separator + user_id + "_" + 
///*  73 */       UtilApi.getCurrentTime() + 
///*  74 */       "_query_inprocess";
///*  75 */     String xmlFilePath = xmlFileName + ".xml";
//     try {
///*  77 */       MaterialQueryXMLWriter.createXML(user_id, xmlFilePath, this.listItemId, ActionType.query, FileType.inprocess);
///*  78 */     } catch (FileNotFoundException e) {
///*  80 */       e.printStackTrace();
///*  81 */     } catch (ParserConfigurationException e) {
///*  83 */       e.printStackTrace();
///*  84 */     } catch (TransformerException e) {
///*  86 */       e.printStackTrace();
//     } 
///*  88 */     String result = UtilApi.getErrorInfoFromCode(PlmErpIntegration.getInprocessInfo(xmlFilePath));
///*  89 */     if (!result.equals("成功")) {
///*  90 */       MessageBox.post(this.application.getDesktop().getShell(), result, "验证出现错误", 1);
//       return;
//     } 
/*  93 */     List<String[]> tableDataList = (List)new ArrayList<String>();
			  Map<String, Object> requstMap = new HashMap<String, Object>();
			  requstMap.put("items", listItemId);
			  Map<String, Object> responseMap = doGet(requstMap, "MTLWIPInfo");
/*  94 */     InProcessReadXMLStr.loadElements(responseMap, tableDataList);
/*  96 */     if (tableDataList.size() == 0) {
/*  97 */       MessageBox.post(this.application.getDesktop().getShell(), "未找到可以展示的物料在制品！！", "警告", 4);
       return;
     } 
/* 100 */     setRunnable((Runnable)new ViewWorkInProcessOfERPDialog(this.session, tableDataList));
   }
   
   private Map<String,Object> doGet(Map<String, Object> itemCheckMap, String svr) {
	   HashMap<String, String> preferenceHashMap = TCPreferenceUitl.getPreferenceHashMap(session, TCPreferenceService.TC_preference_site, erpPreference);
	   String url = preferenceHashMap.get("url");
	   String token = preferenceHashMap.get("token");
	   JSONObject itemCheckJson = JSONObject.fromObject(itemCheckMap);
	   Map<String,Object> resultMap = new HashMap<String, Object>(); 
//	   List<CheckResult> checkResults =null;
	   try {
		   System.out.println("开始发送请求");
		   String args = itemCheckJson.toString();
			token = URLEncoder.encode(token,"UTF-8");
			svr = URLEncoder.encode(svr,"UTF-8");
			args = URLEncoder.encode(args,"UTF-8");
			String param = "usr="+token+"&svr="+svr+"&args="+args;
		   System.out.println("url=============="+url);
		   System.out.println("param==========="+param);
		   String response = HttpUtil.doGet(url+"?"+param);
		   System.out.println("请求结果================"+response);
		   
		   JSONObject jsonObject = JSONObject.fromObject(response);
		   System.out.println("json格式==============="+jsonObject);
		   JSONObject jsonObject2 = jsonObject.getJSONObject("cont");
		   resultMap = (Map<String,Object>)JSONObject.toBean(jsonObject2,Map.class);
//		   System.out.println("cont==============="+jsonObject2);
//		   JSONArray checkResult = jsonObject2.getJSONArray("checkResult");
//		   canAllImport = jsonObject2.getBoolean("canAllImport");
//		   checkResults = JSONArray.toList(checkResult,CheckResult.class);
		   
	   } catch (Exception e1) {
		   // TODO Auto-generated catch block
		   e1.printStackTrace();
	   }
	   return resultMap;
   }
   
 }

