/*     */ package com.connor.erp.ProcessImport.ProcessAddTransferToERP;
import com.connor.erp.BomAddTransferToERP.AssemblyResult;
import com.connor.erp.BomAddTransferToERP.BomAdd;
import com.connor.erp.BomAddTransferToERP.BomAddEntry;
import com.connor.erp.BomChgTransferToERP.CompareResult;
import com.connor.erp.BomChgTransferToERP.DBUtils;
import com.connor.erp.BomChgTransferToERP.UserList;
/*     */ 
/*     */ import com.connor.erp.ProcessImport.ProcessImportReadXMLStr;
/*     */ import com.connor.erp.ProcessImport.TransferRouteXMLWriter;
/*     */ import com.connor.erp.ProcessImport.ValidateFailDialog;
/*     */ import com.connor.erp.ProcessImport.ValidateRouteXMLWriter;
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.erpintegration.entity.RouteCheckResult;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
///*     */ import com.connor.tcerp.conn.PlmErpIntegration;
/*     */ import com.teamcenter.rac.aif.AbstractAIFDialog;
/*     */ import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
/*     */ import com.teamcenter.rac.common.TCTable;
import com.teamcenter.rac.kernel.TCComponent;
/*     */ import com.teamcenter.rac.kernel.TCComponentForm;
/*     */ 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 net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/*     */ import java.awt.BorderLayout;
/*     */ import java.awt.Component;
/*     */ import java.awt.Dimension;
/*     */ import java.awt.event.ActionEvent;
/*     */ import java.awt.event.ActionListener;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
import java.util.ResourceBundle;

/*     */ import javax.swing.JButton;
/*     */ import javax.swing.JPanel;
/*     */ import javax.swing.JScrollPane;
/*     */ import javax.swing.JTable;
/*     */ import javax.swing.SwingUtilities;

import org.apache.logging.log4j.core.appender.rolling.action.IfAccumulatedFileCount;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ProcessAddTransferToERPDialog
/*     */   extends AbstractAIFDialog
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*     */   private TCSession session;
/*     */   private AbstractAIFUIApplication application;
/*     */   private TCTable table;
/*     */   private List<String[]> listRowDatas;
/*     */   private Map<String, List<String>> map;
/*     */   private JButton btnImport;
/*     */   private JButton btnValidate;
/*     */   private JButton btnCancel;
/*     */   private JScrollPane scrollPane;
/*     */   private List<TCComponentForm> listProcessRevMasters;
private static String erpPreference = "connor_tc_to_erp_url";
/*     */   private boolean isDepartmentNull = false;
/*     */   private String msgString;
/*     */   public ProcessAddTransferToERPDialog(TCSession pSession, AbstractAIFUIApplication app, List<String[]> plistRowDatas, Map<String, List<String>> pmap, List<TCComponentForm> plistProcessRevMasters, boolean isDepartmentNull) {
/*  81 */     super(false);
/*  82 */     this.session = pSession;
/*  83 */     this.application = app;
/*  84 */     this.listRowDatas = plistRowDatas;
/*  85 */     this.map = pmap;
/*  86 */     this.listProcessRevMasters = plistProcessRevMasters;
/*  87 */     this.isDepartmentNull = isDepartmentNull;
	
			System.out.println("listRowDatas========"+listRowDatas);
			System.out.println("map========"+map);
			System.out.println("listProcessRevMasters========"+listProcessRevMasters);
/*  88 */     initUI();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void initUI() {
/*  95 */     setTitle("新增工艺路线");
this.setLayout(new PropertyLayout());
/*  96 */     String[] colName = { "装配件号", "工序号", "工序说明", "部门", "标准工时", "辅助工时" };
/*  97 */     this.table = new TCTable(this.session, colName)
/*     */       {
/*     */         private static final long serialVersionUID = 1L;
/*     */ 
/*     */ 
/*     */ 
/*     */         
/*     */         public boolean isCellEditable(int i, int j) {
/* 105 */           return false;
/*     */         }
/*     */       };
/*     */     
/* 109 */     fillData();
/* 110 */     int totalWidth = UtilApi.FitTableColumns((JTable)this.table);
/* 111 */     this.btnValidate = new JButton("工艺路线导入验证");
/* 112 */     this.btnValidate.addActionListener(new ActionListener()
/*     */         {
/*     */           public void actionPerformed(ActionEvent e)
/*     */           {
/* 116 */             ProcessAddTransferToERPDialog.this.validateImport();
/*     */           }
/*     */         });
/*     */ 
/*     */     
/* 121 */     this.btnImport = new JButton("工艺路线导入ERP系统");
/* 122 */     this.btnImport.addActionListener(new ActionListener()
/*     */         {
/*     */           public void actionPerformed(ActionEvent e)
/*     */           {
/* 126 */             ProcessAddTransferToERPDialog.this.dispose();
/* 127 */             try {
	ProcessAddTransferToERPDialog.this.doImport();
} catch (TCException e1) {
	// TODO Auto-generated catch block
	e1.printStackTrace();
}
/*     */           }
/*     */         });
/*     */     
/* 131 */     this.btnImport.setEnabled(false);
/*     */     
/* 133 */     this.btnCancel = new JButton("取消");
/* 134 */     this.btnCancel.addActionListener(new ActionListener()
/*     */         {
/*     */           public void actionPerformed(ActionEvent e)
/*     */           {
/* 138 */             ProcessAddTransferToERPDialog.this.dispose();
/*     */           }
/*     */         });
/*     */ 
/*     */     
/* 143 */     JPanel paneButton = new JPanel();
/* 144 */     paneButton.add(this.btnValidate);
/* 145 */     paneButton.add(this.btnImport);
/* 146 */     paneButton.add(this.btnCancel);
/*     */     
/* 148 */     this.scrollPane = new JScrollPane((Component)this.table);
/*     */      this.setPreferredSize(new Dimension(500, 500));
/* 150 */     JPanel paneMain = new JPanel(new BorderLayout());
/* 151 */     paneMain.add("Center", this.scrollPane);
/* 152 */     paneMain.add("South", paneButton);
/* 153 */     this.add("1.1.left.top",paneMain);
/* 154 */    
			this.setResizable(true);
			showDialog();
			this.setVisible(true);
/*     */   }
/*     */   
/*     */   public void fillData() {
/* 158 */     int vecSize = this.listRowDatas.size();
/* 159 */     for (int i = 0; i < vecSize; i++) {
/* 160 */       this.table.addRow(this.listRowDatas.get(i));
/*     */     }
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 			private ResourceBundle resource = ResourceBundle.getBundle("tcUser_erpUser");

/*     */   
/*     */   private void doImport() throws TCException {
/* 169 */     String user_id = "";
/*     */     try {
/* 171 */       user_id = this.session.getUser().getProperty("user_id");
/* 172 */     } catch (TCException ex) {
/* 173 */       ex.printStackTrace();
/*     */     } 
///* 177 */     int rowCount = this.table.getRowCount();
	
				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);
				
/* 178 */  	  List rootList=new ArrayList();
				List<RootAddEnty> entryMap=new ArrayList<RootAddEnty>();
				List<Map<String, String>> list=new ArrayList<Map<String,String>>();
				Map bomMap=new HashMap();
//				if(resource.containsKey(user_id)) {
					bomMap.put("creater", userList.getERP_USER_CODE());
//	             }else {
//	            	 bomMap.put("creater", "ZX_COMM");
//				}
				RootAddEnty rootEntry=new RootAddEnty();
				String assembly = table.getValueAt(0, 0).toString();
				int rowCount = this.table.getRowCount();
				int t=0;
				 for (int i = 0; i < rowCount; i++) {
					 rootEntry.setAssembly_item(assembly);
					 TCComponentForm tcComponentForm = listProcessRevMasters.get(t);
					 AIFComponentContext[] whereReferenced = tcComponentForm.whereReferenced();
					 TCComponent component = (TCComponent) whereReferenced[0].getComponent();
					 rootEntry.setVersion(component.getStringProperty("item_revision_id"));
					if(assembly!=table.getValueAt(i, 0).toString()) {
						t++;
						entryMap.add(rootEntry);
						rootEntry.setRouteLine(list);
						rootEntry=new RootAddEnty();
						assembly=table.getValueAt(i, 0).toString();
						list=new ArrayList<Map<String,String>>();
					}
					if(i==rowCount-1) {
						entryMap.add(rootEntry);
						rootEntry.setRouteLine(list);
						rootEntry=new RootAddEnty();
					}
					Map<String, String> jsonMap=new HashMap<String, String>();
					jsonMap.put("operation_seq_num", table.getValueAt(i, 1).toString());
					jsonMap.put("operation_description", table.getValueAt(i, 2).toString());
					jsonMap.put("department_code", table.getValueAt(i, 3).toString());
					
					String amount = table.getValueAt(i, 4).toString();
					if(amount.equals(" ")|| amount.equals("0")) {
						amount=null;
					}
					
					String amount_auxiliary= table.getValueAt(i, 5).toString();
					if(amount_auxiliary.equals(" ")|| amount_auxiliary.equals("0")) {
						amount_auxiliary=null;
					}
					jsonMap.put("usage_rate_or_amount", amount);
					jsonMap.put("usage_rate_or_amount_auxiliary", amount_auxiliary);
					list.add(jsonMap);
/*     */   } 
				 ProcessAddTransferToERPOperation operation=new ProcessAddTransferToERPOperation(application,session,entryMap,bomMap,ProcessAddTransferToERPDialog.this);
				 session.queueOperation(operation);
				 
//				 System.out.println("关闭页面");
///* 223 */     this.dispose();
/* 224 */     
/*     */   }

/*     */ 
/*     */   
/*     */   public void validateImport() {
/* 229 */     if (this.isDepartmentNull) {
/* 230 */       MessageBox.post(this.application.getDesktop().getShell(), "部门(设备)不能为空！", "警告", 4);
/*     */       return;
/*     */     } 
/* 233 */     String user_id = "";
/*     */     try {
/* 235 */       user_id = this.session.getUser().getProperty("user_id");
/* 236 */     } catch (TCException ex) {
/* 237 */       ex.printStackTrace();
/*     */     } 


			List<RootEntry> entryMap=new ArrayList<RootEntry>();
			List<Map<String, String>> list=new ArrayList<Map<String,String>>();
			Map bomMap=new HashMap();
			
			
			
			RootEntry rootEntry=new RootEntry();
			String assembly = table.getValueAt(0, 0).toString();
			int rowCount = this.table.getRowCount();
			 for (int i = 0; i < rowCount; i++) {
				 rootEntry.setAssembly_item(assembly);
				
				if(assembly!=table.getValueAt(i, 0).toString()) {
					entryMap.add(rootEntry);
					rootEntry.setRouteLine(list);
					rootEntry=new RootEntry();
					assembly=table.getValueAt(i, 0).toString();
					list=new ArrayList<Map<String,String>>();
				}
				if(i==rowCount-1) {
					entryMap.add(rootEntry);
					rootEntry.setRouteLine(list);
//					rootEntry=new RootEntry();
				}
				Map<String, String> jsonMap=new HashMap<String, String>();
				jsonMap.put("department_code", table.getValueAt(i, 3).toString());
				list.add(jsonMap);
/*     */   } 

			 bomMap.put("ROUTE", entryMap);
			 List<RootCheckResult> doPost = doPost(bomMap,"RouteAddCheck");

///* 250 */     String result = UtilApi.getErrorInfoFromCode(8);
/* 251 */     if (!msgString.equals("succ")) {
/* 252 */       MessageBox.post(this.application.getDesktop().getShell(), msgString, "验证出现错误", 1);
/*     */       return;
/*     */     } 
///* 255 */     StringBuffer errorInfo = new StringBuffer("");
//String returnInfo ="";

/* 256 */    String errorInfo = ProcessImportReadXMLStr.loadAddElements(doPost);
			System.out.println(errorInfo);
/* 257 */     if (errorInfo.toString().equals("") ) {//|| returnInfo.equals("成功")
/* 258 */       MessageBox.post(this.application.getDesktop().getShell(), "导入验证成功，可以执行导入操作！", "成功", 2);
/* 259 */       this.btnImport.setEnabled(true);
/*     */     }
/* 261 */     else if (errorInfo.equals("所有的工艺路线都已经存在")) {
/* 262 */       dispose();
/* 263 */       Object[] obj = new Object[1];
/* 264 */       obj[0] = Integer.valueOf(11);
/* 265 */       TCUserService service = this.session.getUserService();
/* 266 */       int formNum = this.listProcessRevMasters.size();
/* 267 */       for (int i = 0; i < formNum; i++) {
/*     */         try {
/* 269 */           service.call("openByPass", obj);
/* 270 */           ((TCComponentForm)this.listProcessRevMasters.get(i)).setLogicalProperty("nhl8_ERPimpSucc", true);
/* 271 */           service.call("closeByPass", obj);
/* 272 */         } catch (TCException e1) {
/* 273 */           e1.printStackTrace();
/*     */         } 
/*     */       } 
/* 276 */       MessageBox.post(this.application.getDesktop().getShell(), "工艺路线已经存在，不可重复导入；如需变更，请执行变更导入！", "警告", 2);
/*     */     } else {
/* 278 */       SwingUtilities.invokeLater((Runnable)new ValidateFailDialog("验证失败，请查看详细信息：", "警告", errorInfo.toString()));
/*     */     } 
/*     */   }
private List<RootCheckResult> 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");
	String jsonString=new Gson().toJson(itemCheckMap);
	JSONObject itemCheckJson = JSONObject.fromObject(jsonString);
	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);
		if(svr.equals("RouteAddCheck")) {
			msgString = jsonObject.getString("msg");
			if(jsonObject.has("cont")) {
				JSONObject jsonObject2 = jsonObject.getJSONObject("cont");
				if(jsonObject2.has("checkResult")) {
					JSONArray jsonObject3 = jsonObject2.getJSONArray("checkResult");
					Gson gson = new Gson();  
					List<RootCheckResult> list= gson.fromJson(jsonObject3.toString(), new TypeToken<List<RootCheckResult>>() {}.getType());
					return list;
				}
			}
		}
//		else if (svr.equals("ROUTEAdd")) {
//			msgString = jsonObject.getString("msg");
//			if(jsonObject.has("cont")) {
//				JSONObject jsonObject2 = jsonObject.getJSONObject("cont");
//				
//				if(jsonObject2.has("compareResult")) {
//					
//					JSONArray jsonObject3 = jsonObject2.getJSONArray("compareResult");
//					List<CompareResult> list = JSONArray.toList(jsonObject3,CompareResult.class);
////					System.out.println(jsonObject3+"list"+list.size());
//					
//					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\ProcessImport\ProcessAddTransferToERP\ProcessAddTransferToERPDialog.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       1.1.3
 */