/*     */ package com.connor.erp.BomAddTransferToERP;
import com.connor.erp.BomChgTransferToERP.DBUtils;
import com.connor.erp.BomChgTransferToERP.UserList;
/*     */ 
/*     */ import com.connor.erp.BomImport.BomTransferDialog;
/*     */ import com.connor.erp.BomImport.TransferBomXMLWriter;
/*     */ import com.connor.erp.BomImport.ValidateBomXMLWriter;
import com.connor.erp.ItemAddTransferSendToERP.SendToERPDialog;
/*     */ import com.connor.erp.ProcessImport.ValidateFailDialog;
/*     */ import com.connor.erp.materialquery.ViewStockOfERP.CMap;
/*     */ import com.connor.erp.materialquery.ViewStockOfERP.CMap2;
/*     */ import com.connor.erp.materialquery.ViewStockOfERP.CTable;
import com.connor.erp.processresource.EquipmentAcquiredFromERP.TCPreferenceUitl;
/*     */ import com.connor.erp.toolkit.ActionType;
/*     */ import com.connor.erp.toolkit.AimType;
/*     */ import com.connor.erp.toolkit.FileType;
import com.connor.erp.toolkit.HttpUtil;
/*     */ import com.connor.erp.toolkit.UtilApi;
///*     */ import com.connor.tcerp.conn.PlmErpIntegration;
/*     */ import com.teamcenter.rac.aif.AbstractAIFUIApplication;
/*     */ import com.teamcenter.rac.kernel.TCComponentForm;
/*     */ import com.teamcenter.rac.kernel.TCComponentItemRevision;
/*     */ import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
/*     */ import com.teamcenter.rac.kernel.TCSession;
/*     */ import com.teamcenter.rac.kernel.TCUserService;
/*     */ import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PropertyLayout;
import com.teamcenter.rac.util.tabbedpane.iTabbedPane;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/*     */ import java.awt.BorderLayout;
/*     */ import java.awt.Dimension;
/*     */ import java.awt.event.ActionEvent;
/*     */ import java.awt.event.ActionListener;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
import java.util.ResourceBundle;
import java.util.Map.Entry;
import java.util.Set;
/*     */ import java.util.Vector;
/*     */ import javax.swing.JButton;
/*     */ import javax.swing.JPanel;
/*     */ import javax.swing.JScrollPane;
/*     */ import javax.swing.JTable;
/*     */ import javax.swing.SwingUtilities;
/*     */ import javax.swing.table.DefaultTableModel;
/*     */ import javax.swing.table.TableModel;
/*     */ import javax.xml.parsers.ParserConfigurationException;
/*     */ import javax.xml.transform.TransformerException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */
public class BomAddTransferDialog
/*     */   extends BomTransferDialog
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*     */   private String xmlFilePath;
/*  63 */   private String user_id = "";
/*     */   private Vector<Object> title;
/*     */   private JButton btnBomCheck;
/*     */   private JButton btnBomImportERP;
/*     */   private JButton btnCancel;
/*     */   private JTable table;
			private String msgString;
			private ResourceBundle resource = ResourceBundle.getBundle("tcUser_erpUser");
/*     */   private static String erpPreference = "connor_tc_to_erp_url";

private String canAllImport;

private Map<String, String> canSendErpMap=new HashMap<String, String>();
private int maxWidth = 0;
private String bomAddCheckSvr = "BOMAddCheck";
/*     */   public BomAddTransferDialog(TCSession session, AbstractAIFUIApplication app, List<TCComponentItemRevision> itemRevision, Map<String, Map<String, List<String>>> map, Vector<Vector<Object>> data) {
/*  71 */     super(session, app, itemRevision, map, data);
/*  72 */     System.out.println("BomAddTransferDialog::map==============" + map);
/*  73 */     System.out.println("BomAddTransferDialog::itemRevision==============" + itemRevision);
/*  74 */     System.out.println("BomAddTransferDialog::data==============" + data);

//						Object obj[] = new Object[1];
//						obj[0] = 1;
///* 203 */               TCUserService service = BomAddTransferDialog.this.session.getUserService();
///* 204 */               int revNum = BomAddTransferDialog.this.ItemRevision.size();
///* 205 */               for (int i = 0; i < revNum; i++) {
///*     */                 try {
///* 207 */                   service.call("openByPass", obj);
///* 208 */                   TCComponentForm form = (TCComponentForm)((TCComponentItemRevision)BomAddTransferDialog.this.ItemRevision.get(i)).getRelatedComponent("IMAN_master_form_rev");
//							System.out.println("itemRevision"+ItemRevision.get(i));
///* 209 */                   form.setLogicalProperty("nhl8_BOMimpSucc", true);
///* 210 */                   form.save();
///* 211 */                   service.call("closeByPass", obj);
///* 212 */                 } catch (TCException e1) {
///* 213 */                   e1.printStackTrace();
///*     */                 } 
///*     */               } 
/*  75 */     initUI();
/*     */   }
/*     */ 
/*     */   
/*     */   private void initUI() {
	System.out.println("showdialog");
/*  80 */    this.setTitle("新增BOM");
/*  81 */     this.btnBomCheck = new JButton("BOM导入验证");
/*  82 */     this.btnBomImportERP = new JButton("BOM导入ERP系统");
/*  83 */     this.btnCancel = new JButton("取消");
/*  84 */     this.btnBomImportERP.setEnabled(false);
/*     */     
/*  86 */     JPanel paneButton = new JPanel();
/*  87 */     paneButton.add(this.btnBomCheck);
/*  88 */     paneButton.add(this.btnBomImportERP);
/*  89 */     paneButton.add(this.btnCancel);
/*     */     
/*  91 */     this.title = new Vector();
/*  92 */     this.title.add("组织ID");
/*  93 */     this.title.add("装配件编码");
/*  94 */     this.title.add("子件编码");
/*  95 */     this.title.add("工序序号");
/*  96 */     this.title.add("数量");

//Vector 	test2=new Vector();
//test2.add("id2");
//test2.add("bm123452");
///*     */     test2.add("zj1234562");
//test2.add("xh123452");
//test2.add("122");
//				Vector 	test=new Vector();
//				test.add("id");
//				test.add("bm12345");
///*     */     test.add("zj123456");
//test.add("xh12345");
//test.add("1");
//data=new Vector();
//data.add(test);
//data.add(test2);
/*  98 */     CMap2 cMap2 = new CMap2();
/*     */     
/* 100 */     DefaultTableModel model = new DefaultTableModel(this.data, this.title);
/* 101 */     this.table = (JTable)new CTable((CMap)cMap2, model)
/*     */       {
/*     */         private static final long serialVersionUID = 1L;
/*     */ 
/*     */ 
/*     */         
/*     */         public boolean isCellEditable(int row, int column) {
/* 108 */           return false;
/*     */         }
/*     */       };
/* 111 */     int totalWidth = UtilApi.FitTableColumns(this.table);

			this.maxWidth = totalWidth + 100;
			setPreferredSize(new Dimension(maxWidth,500));
			this.setResizable(true);
/* 112 */     JPanel paneMain = new JPanel(new BorderLayout());
/* 113 */     paneMain.add("Center", new JScrollPane(this.table));
/* 114 */     paneMain.add("South",paneButton);

/* 115 */    getContentPane().add(paneMain);
/* 116 */    
/*     */     

/* 118 */     this.btnBomCheck.addActionListener(new ActionListener()
/*     */         {
/*     */           public void actionPerformed(ActionEvent e)
/*     */           {
					List<BomAddEntry> entryMap=new ArrayList<BomAddEntry>();
					Map<String, List<BomAddEntry>> bomMap=new HashMap<String, List<BomAddEntry>>();
					
					List<Map<String, String>> list=new ArrayList<Map<String,String>>();
					
					String assembly = BomAddTransferDialog.this.table.getValueAt(0, 1).toString();
					BomAddEntry bomAddEntry=new BomAddEntry();
/* 122 */             int rowCount = BomAddTransferDialog.this.table.getRowCount();
/* 123 */             for (int i = 0; i < rowCount; i++) {
						bomAddEntry.setAssembly_item(assembly.split("/")[0]);
						
						if(assembly!=BomAddTransferDialog.this.table.getValueAt(i, 1).toString()) {
							entryMap.add(bomAddEntry);
							bomAddEntry.setBOMLine(list);
							bomAddEntry=new BomAddEntry();
							assembly=BomAddTransferDialog.this.table.getValueAt(i, 1).toString();
							list=new ArrayList<Map<String,String>>();
						}
						if(i==rowCount-1) {
							entryMap.add(bomAddEntry);
							bomAddEntry.setBOMLine(list);
							bomAddEntry=new BomAddEntry();
						}
						
						
/* 124 */               if (BomAddTransferDialog.this.table.getValueAt(i, 3).toString().trim().equals("")) {
/* 125 */                 MessageBox.post(BomAddTransferDialog.this.application.getDesktop().getShell(), "工序序号不能为空！", "错误", 1);
/*     */                 return;
/*     */               } 
						Map<String, String> jsonMap=new HashMap<String, String>();
						jsonMap.put("item", BomAddTransferDialog.this.table.getValueAt(i, 2).toString().split("/")[0]);
						jsonMap.put("operation_seq_num",BomAddTransferDialog.this.table.getValueAt(i, 3).toString().split("_")[0]);
						list.add(jsonMap);
/*     */             } 
						bomMap.put("BOM", entryMap);




//					try {
//						connection = DBUtils.ConnectionFun(session);
//						if(connection!=null) {
//							DBUtils.InsertData("ps123", "PBOM", "taskNum", user_id, "a", connection);
//						}else {
//							System.out.println("connection==null");
//						}
//					} catch (SQLException e2) {
//						// TODO Auto-generated catch block
//						e2.printStackTrace();
//					}
//					JSONObject fromObject = JSONObject.fromObject(bomMap);
					List<AssemblyResult> results = doPost(bomMap,"BOMAddCheck");
//					System.out.println(fromObject.toString());
/*     */             try {
/* 130 */               BomAddTransferDialog.this.user_id = BomAddTransferDialog.this.session.getUser().getProperty("user_id");
/* 131 */             } catch (TCException ex) {
/* 132 */               ex.printStackTrace();
/*     */             } 

//					生成json
					
///* 134 */             String xmlFileName = System.getProperty("java.io.tmpdir") + File.separator + BomAddTransferDialog.this.user_id + "_" + UtilApi.getCurrentTime() + "_validate_bom_add";
///* 135 */             String xmlFilePath = xmlFileName + ".xml";
///*     */             
///*     */             try {
///* 138 */               ValidateBomXMLWriter.createXML(BomAddTransferDialog.this.user_id, xmlFilePath, FileType.bom, AimType.add, BomAddTransferDialog.this.map);
///* 139 */             } catch (FileNotFoundException e1) {
///* 140 */               e1.printStackTrace();
///* 141 */             } catch (ParserConfigurationException e1) {
///* 142 */               e1.printStackTrace();
///* 143 */             } catch (TransformerException e1) {
///* 144 */               e1.printStackTrace();
///*     */             } 
///*     */             PlmErpIntegration.validateData(xmlFilePath)
///* 147 */             String result = UtilApi.getErrorInfoFromCode(8);
/* 148 */             if (!msgString.equals("succ")) {
/* 149 */               MessageBox.post(BomAddTransferDialog.this.application.getDesktop().getShell(), msgString, "验证出现错误", 1);
/*     */             } else {
/* 151 */               StringBuffer errorInfo = new StringBuffer("");
//String returnInfo="";
/* 152 */               String returnInfo = BomAddImportXMLReader.loadXMLDatas(results,errorInfo,canSendErpMap);
//String	returnInfo="";
							

							
						Set<Entry<String, String>> entries = canSendErpMap.entrySet();
/* 153 */               if (errorInfo.toString().equals("") ||canAllImport.equals("true")) {
/* 154 */                 MessageBox.post(BomAddTransferDialog.this.application.getDesktop().getShell(), "导入验证成功，可以执行导入操作！", "成功", 2);
/* 155 */                 BomAddTransferDialog.this.btnBomImportERP.setEnabled(true);
/*     */               }
/* 157 */               else if (returnInfo.equals("所有的BOM都已经存在")) {
/* 158 */                 BomAddTransferDialog.this.dispose();
/* 159 */                 Object[] obj = new Object[1];
/* 160 */                 obj[0] = Integer.valueOf(11);
/* 161 */                 TCUserService service = BomAddTransferDialog.this.session.getUserService();
/* 162 */                 int revNum = BomAddTransferDialog.this.ItemRevision.size();
/* 163 */                 for (int j = 0; j < revNum; j++) {
/*     */                   try {
/* 165 */                     service.call("openByPass", obj);
/* 166 */                     ((TCComponentItemRevision)BomAddTransferDialog.this.ItemRevision.get(j)).getRelatedComponent("IMAN_master_form_rev").setLogicalProperty("nhl8_BOMimpSucc", true);
/* 167 */                     service.call("closeByPass", obj);
/* 168 */                   } catch (TCException e1) {
/* 169 */                     e1.printStackTrace();
/*     */                   } 
/*     */                 } 
/* 172 */                 MessageBox.post(BomAddTransferDialog.this.application.getDesktop().getShell(), "工艺BOM已经存在，不可重复导入；如需变更，请执行变更导入！", "警告", 2);
/*     */               }else if (entries.size()>0) {
							SwingUtilities.invokeLater((Runnable)new ValidateFailDialog("验证失败，请查看详细信息：", "警告", errorInfo.toString()));
						}
						else {
/* 174 */                 SwingUtilities.invokeLater((Runnable)new ValidateFailDialog("验证失败，请查看详细信息：", "警告", errorInfo.toString()));
/*     */               } 
/*     */             } 
/*     */           }
/*     */         });
/*     */ 
/*     */     
/* 181 */     this.btnBomImportERP.addActionListener(new ActionListener()
/*     */         {
/*     */           public void actionPerformed(ActionEvent e)
/*     */           {
					Map<String, String> userMap=new HashMap<String, String>();
					userMap.put("TCUser", user_id);
					List<UserList> doPost = DBUtils.doPost(userMap, session);
					UserList userList = doPost.get(0);
					List<BomAdd> entryMap=new ArrayList<BomAdd>();
					
					Map bomMap=new HashMap();
					List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
//					if(resource.containsKey(user_id)) {
//						bomMap.put("creater", resource.getString(user_id));
//		             }else {
					bomMap.put("creater", userList.getERP_USER_CODE());
//					}
					String assembly = BomAddTransferDialog.this.table.getValueAt(0, 1).toString();
					BomAdd bomAddEntry=new BomAdd();
					int indx=0;
					String revisionString="";
					TCComponentItemRevision rev = ItemRevision.get(indx);
					try {
						String object_string = rev.getStringProperty("object_string");
						System.out.println(object_string);
						if(object_string.equals(assembly.split(" ")[0])) {
							revisionString=rev.getStringProperty("item_revision_id");
						}
					} catch (TCException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					
					int rowCount = BomAddTransferDialog.this.table.getRowCount();
					for (int i = 0; i < rowCount; i++) {
						bomAddEntry.setAssembly_item(assembly.split("/")[0]);
						bomAddEntry.setVersion(revisionString);
						if(assembly!=BomAddTransferDialog.this.table.getValueAt(i, 1).toString()) {
							entryMap.add(bomAddEntry);
							bomAddEntry.setBOMLine(list);
							bomAddEntry=new BomAdd();
							assembly=BomAddTransferDialog.this.table.getValueAt(i, 1).toString();
							try {
								indx++;
								TCComponentItemRevision rev2 = ItemRevision.get(indx);
								String object_string = rev2.getStringProperty("object_string");
								if(object_string.equals(assembly.split(" ")[0])) {
									revisionString=rev2.getStringProperty("item_revision_id");
									bomAddEntry.setVersion(revisionString);
									
								}
							} catch (TCException e2) {
								// TODO Auto-generated catch block
								e2.printStackTrace();
							}
							list=new ArrayList<Map<String,Object>>();
						}
						if(i==rowCount-1) {
							entryMap.add(bomAddEntry);
							bomAddEntry.setBOMLine(list);
							bomAddEntry=new BomAdd();
						}
			
			
						if (BomAddTransferDialog.this.table.getValueAt(i, 3).toString().trim().equals("")) {
							MessageBox.post(BomAddTransferDialog.this.application.getDesktop().getShell(), "工序序号不能为空！", "错误", 1);
							return;
						} 
						
						Map<String, Object> jsonMap=new HashMap<String, Object>();
						jsonMap.put("item", BomAddTransferDialog.this.table.getValueAt(i, 2).toString().split("/")[0]);
						jsonMap.put("operation_seq_num",BomAddTransferDialog.this.table.getValueAt(i, 3).toString());
						jsonMap.put("quantity", BomAddTransferDialog.this.table.getValueAt(i, 4).toString());
						list.add(jsonMap);
					} 
					
					BomAddTransferOperation operation=new BomAddTransferOperation(application,session,entryMap,bomMap,BomAddTransferDialog.this);
					session.queueOperation(operation);
//					for(BomAdd bAdd:entryMap) {
//						List<BomAdd> entryMap2=new ArrayList<BomAdd>();
//						entryMap2.add(bAdd);
//						bomMap.put("BOM", entryMap2);
////						String task_num="task_num";
//						String task_num= doPost1(bomMap,"BOMAdd");
/////* 197 */             String result1 = UtilApi.getErrorInfoFromCode(8);
///* 198 */             if (!msgString.equals("succ")) {
///* 199 */               SwingUtilities.invokeLater((Runnable)new ValidateFailDialog("验证失败，请查看详细信息：", "警告", msgString));
///*     */             } else {
///* 201 */               Object obj[] = new Object[1];
//						obj[0] = 1;
///* 203 */               TCUserService service = BomAddTransferDialog.this.session.getUserService();
///* 204 */               int revNum = BomAddTransferDialog.this.ItemRevision.size();
//						DBUtils.InsertData(bAdd.getAssembly_item(), "PBOM", task_num, user_id, bAdd.getVersion(), session);
///* 205 */               for (int i = 0; i < revNum; i++) {
///*     */                 try {
///* 207 */                   service.call("openByPass", obj);
///* 208 */                   TCComponentForm form = (TCComponentForm)((TCComponentItemRevision)BomAddTransferDialog.this.ItemRevision.get(i)).getRelatedComponent("IMAN_master_form_rev");
///* 209 */                   form.setLogicalProperty("nhl8_BOMimpSucc", true);
///* 210 */                   form.save();
///* 211 */                   service.call("closeByPass", obj);
///* 212 */                 } catch (TCException e1) {
///* 213 */                   e1.printStackTrace();
///*     */                 } 
///*     */               } 
///* 216 */              
///* 217 */              	
///*     */             } 
//					}
//					MessageBox.post(BomAddTransferDialog.this.application.getDesktop().getShell(), "任务已成功加入ERP导入队列，请及时通过【导入结果查询】—【PBOM】功能查询导入结果", "成功", 2);
//					BomAddTransferDialog.this.dispose();
/*     */           }
/*     */         });
/*     */     
/* 222 */     this.btnCancel.addActionListener(new ActionListener()
/*     */         {
/*     */           public void actionPerformed(ActionEvent e)
/*     */           {
/* 226 */             BomAddTransferDialog.this.dispose();
/*     */           }
/*     */         });
this.centerToScreen();
this.showDialog(); 
this.setVisible(true);
System.out.println("end");
/*     */   }
private List<AssemblyResult> doPost(Map 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);
	String sid = null;
	try {
		System.out.println("开始发送请求");
		String args = itemCheckJson.toString();
		System.out.println(args);
		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==========="+args);
		String response = HttpUtil.doPost(url, param); //.doGet(url+"?"+param)
//		System.out.println("请求结果================"+response);
		
		JSONObject jsonObject = JSONObject.fromObject(response);
		System.out.println("json格式==============="+jsonObject);
		msgString = jsonObject.getString("msg");
		if(jsonObject.has("cont")) {
			JSONObject jsonObject2 = jsonObject.getJSONObject("cont");
			canAllImport = jsonObject2.getString("canAllImport");
			if(jsonObject2.has("checkResult")) {
				JSONArray jsonObject3 = jsonObject2.getJSONArray("checkResult");
				List<AssemblyResult> list = JSONArray.toList(jsonObject3,AssemblyResult.class);
				return list;
			}
		}
		
	} catch (Exception e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	return null;
}


/*     */ }


/* Location:              C:\Users\cyh\Documents\WeChat Files\wxid_ztg4pdvd5bjn12\FileStorage\File\2022-03\com.origin.erpintegration_1.0.0.jar!\com\origin\erp\BomAddTransferToERP\BomAddTransferDialog.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       1.1.3
 */