 package com.connor.erp.materialquery.ViewStockOfERP;
 
 import com.connor.erp.materialquery.StockReadXMLStr;
import com.connor.erp.processresource.EquipmentAcquiredFromERP.TCPreferenceUitl;
import com.connor.erp.toolkit.HttpUtil;
 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.net.URLEncoder;
import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 public class ViewStockOfERPCommand extends AbstractAIFCommand {
   private TCSession session;
   
   private AbstractAIFUIApplication application;
   
   private List<String> listItemId;
   
   private String erpPreference = "connor_tc_to_erp_url";
   
   public ViewStockOfERPCommand(TCSession pSession, AbstractAIFUIApplication app, List<String> plistItemId) {
/*  53 */     this.session = pSession;
/*  54 */     this.application = app;
/*  55 */     this.listItemId = plistItemId;
/*  56 */     callERPDll();
   }
   
   private void callERPDll() {
/*  65 */     String user_id = "";
     try {
/*  67 */       user_id = this.session.getUser().getProperty("user_id");
/*  68 */     } catch (TCException e) {
/*  69 */       e.printStackTrace();
     } 
///*  71 */     String xmlFileName = 
///*  72 */       System.getProperty("java.io.tmpdir") + 
///*  73 */       File.separator + user_id + "_" + 
///*  74 */       UtilApi.getCurrentTime() + 
///*  75 */       "_query_item";
///*  76 */     String xmlFilePath = xmlFileName + ".xml";
//     try {
///*  78 */       MaterialQueryXMLWriter.createXML(user_id, xmlFilePath, this.listItemId, ActionType.query, FileType.item);
///*  79 */     } catch (FileNotFoundException e) {
///*  81 */       e.printStackTrace();
///*  82 */     } catch (ParserConfigurationException e) {
///*  84 */       e.printStackTrace();
///*  85 */     } catch (TransformerException e) {
///*  87 */       e.printStackTrace();
//     } 
///*  89 */     String result = UtilApi.getErrorInfoFromCode(PlmErpIntegration.getItemInfo(xmlFilePath));
///*  90 */     if (!result.equals("成功")) {
///*  91 */       MessageBox.post(this.application.getDesktop().getShell(), result, "验证出现错误", 1);
//       return;
//     } 
/*  94 */     Map<String, List<String[]>> map = new HashMap<String, List<String[]>>();
			  Map<String, Object> requestMap = new HashMap<String, Object>();
			  requestMap.put("items", listItemId);
			  Map<String, Object> responseMap = doGet(requestMap, "MTLINVOnhandQty");
/*  95 */     StockReadXMLStr.loadElements(responseMap, map);
/*  96 */     if (map.size() == 0) {
/*  97 */       MessageBox.post(this.application.getDesktop().getShell(), "未找到可以展示的库存物料！！", "警告", 4);
       return;
     } 
/* 100 */     setRunnable((Runnable)new ViewStockOfERPDialog(map));
   }
   
   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;
   }
   
 }

