package cn.com.countrygarden.bi.main.controller.renli;

import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import nc.itf.hi.ws.IPsnInfoQueryWs.IPsnInfoQueryWsPortTypeProxy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.com.countrygarden.bi.main.model.bd_psndoc;
import cn.com.countrygarden.bi.main.service.performance.bdpsndocService;

@Controller("BdController")
@RequestMapping("/BdController")
public class BdController {

	 @Autowired
		private bdpsndocService bdpsndocService;
	 
	    @ResponseBody
	    @RequestMapping(value = "/Bdpsndoc",method = {RequestMethod.POST,RequestMethod.GET})
	    public void bdpsndoc(@RequestParam(value = "pageno") int pageno,@RequestParam(value = "pagesize") int pagesize) throws ParseException{
	    	IPsnInfoQueryWsPortTypeProxy ip = new IPsnInfoQueryWsPortTypeProxy();   
			String dayin;	
			String circle;	
				try {
					dayin = ip.queryPsnInfo(pageno, pagesize);
					JSONObject jsonObject = JSON.parseObject(dayin);
					String total = jsonObject.getString("total");
					int inttotal = Integer.parseInt(total);
					int pagenototal = inttotal/pagesize;
					if(inttotal%pagesize==0){
						 pagenototal = inttotal/pagesize;
					}else{
						pagenototal = inttotal/pagesize+1;
					}
					for(int j=49;j<=pagenototal;j++){
						circle = ip.queryPsnInfo(j, pagesize);
						JSONObject jsonObjectcircle = JSON.parseObject(circle);
						JSONObject data = (JSONObject) jsonObjectcircle.get("data");
						/*System.out.println("1");*/
						JSONArray psndoc = data.getJSONArray("psndoc");	
						List<bd_psndoc> bd_psndoc = new ArrayList<bd_psndoc>();
						/*System.out.println(psndoc);*/
							if(psndoc!=null||psndoc.size()!=0){ 
								for(int i=0;i<psndoc.size();i++){ 
								JSONObject jo = (JSONObject)psndoc.getJSONObject(i);
								bd_psndoc bd =new bd_psndoc();
	  	       						String addr= jo.getString("addr");  
	  	       						String birthdate= jo.getString("birthdate");  
	  	       						String code= jo.getString("code");  
	  	       						String creationtime= jo.getString("creationtime");  
	  	       						String creator= jo.getString("creator");  
	  	       					//分布式
	  	       					    int dataoriginflag=0;
	  	       						if(jo.getString("dataoriginflag")!=null) {
	  	       						dataoriginflag= Integer.parseInt(jo.getString("dataoriginflag"));  
	  	       						}
	  	       						String email= jo.getString("email"); 
	  	       						//启用状态
	  	       						int enablestate=0;
	  	       						if(jo.getString("sex")!=null) {
	  	       						enablestate= Integer.parseInt(jo.getString("enablestate")); 
	  	       						}
	  	       						String homephone= jo.getString("homephone");  
	  	       						String id= jo.getString("id"); 
	  	       						String idtype= jo.getString("idtype"); 
	  	       						String isshopassist= jo.getString("isshopassist");
	  	       						String joinworkdate= jo.getString("joinworkdate"); 
	  	       						String mnecode= jo.getString("mnecode"); 
	  	       						String mobile= jo.getString("mobile"); 
	  	       						String modifiedtime= jo.getString("modifiedtime"); 
	  	       						String modifier= jo.getString("modifier"); 
	  	       						String name= jo.getString("name"); 
	  	       						String name2= jo.getString("name2"); 
	  	       						String name3= jo.getString("name3"); 
	  	       						String name4= jo.getString("name4");
	  	       						String name5= jo.getString("name5"); 
	  	       						String name6= jo.getString("name6"); 
	  	       						String officephone= jo.getString("officephone");
	  	       						String pk_group= jo.getString("pk_group");
	  	       						String pk_org= jo.getString("pk_org");
	  	       						String pk_psndoc= jo.getString("pk_psndoc");
	  	       						
	  	       						//性别
	  	       					    int sex=0;
	  	       						if(jo.getString("sex")!=null) {
	  	       						sex= Integer.parseInt(jo.getString("sex")); 
	  	       						}
	  	       						
	  	       						
	  	       						Date ts = null;
//	  	       						时间转换
	  	       					    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  	       					    if(jo.getString("ts")!=null) {
	  	       						 ts=df.parse(jo.getString("ts"));
	  	       					    }
	  	       					    
	  	       						String usedname= jo.getString("usedname");
	  	       						String bloodtype= jo.getString("bloodtype");
	  	       						String censusaddr= jo.getString("censusaddr");
	  	       						String characterrpr= jo.getString("characterrpr"); 
	  	       						String country= jo.getString("country"); 
	  	       						String die_date= jo.getString("die_date");
	  	       						String die_remark= jo.getString("die_remark"); 
	  	       						String edu= jo.getString("edu"); 
	  	       						String fax= jo.getString("fax"); 
	  	       						String fileaddress= jo.getString("fileaddress");
	  	       						String health= jo.getString("health"); 
	  	       						String ishiskeypsn= jo.getString("ishiskeypsn");
	  	       						String joinpolitydate= jo.getString("joinpolitydate");
	  	       						String marital= jo.getString("marital");  
	  	       						String marriagedate= jo.getString("marriagedate"); 
	  	       						String nationality= jo.getString("nationality"); 
	  	       						String nativeplace= jo.getString("nativeplace"); 
	  	       						String penelauth= jo.getString("penelauth");  
	  	       						String permanreside= jo.getString("permanreside");
	  	       					//照片	
	  	       					    byte[] photo=null;
	  	       					    String pho=jo.getString("photo");
	  	       					    if(pho!=null) {
	  	       						photo=pho.getBytes(); 
	  	       					    }
	  	       					    
	  	       						String pk_degree= jo.getString("pk_degree");
	  	       						String pk_hrorg= jo.getString("pk_hrorg"); 
	  	       						String polity= jo.getString("polity");  
	  	       						String postalcode= jo.getString("postalcode");
	  	       						
	  	       					    byte[] previewphoto=null;
	  	       					    String pre=jo.getString("previewphoto");
	  	       					    if(pre!=null) {
	  	       					    previewphoto= pre.getBytes();
	  	       					    }
	  	       					    String prof= jo.getString("prof");  
	  	       						String retiredate= jo.getString("retiredate");
	  	       						String secret_email= jo.getString("secret_email");
	  	       						String shortname= jo.getString("shortname");  
	  	       						String titletechpost= jo.getString("titletechpost"); 
	  	       						String firstname= jo.getString("firstname");  
	  	       						String lastname= jo.getString("lastname");  
	  	       						String iscadre= jo.getString("iscadre");  
	  	       						String ishisleader= jo.getString("ishisleader");
	  	       						String age= jo.getString("age");  
	  	       						String workage= jo.getString("workage"); 
	  	       						String glbdef21= jo.getString("glbdef21"); 
	  	       						String glbdef1= jo.getString("glbdef1"); 
	  	       						String glbdef2= jo.getString("glbdef2"); 
	  	       						String glbdef3= jo.getString("glbdef3"); 
	  	       						String glbdef4= jo.getString("glbdef4"); 
	  	       						String glbdef5= jo.getString("glbdef5"); 
	  	       						String glbdef6= jo.getString("glbdef6"); 
	  	       						String glbdef7= jo.getString("glbdef7");  
	  	       						String glbdef8= jo.getString("glbdef8"); 
	  	       						String glbdef9= jo.getString("glbdef9"); 
	  	       						String glbdef10= jo.getString("glbdef10");
	  	       						String glbdef11= jo.getString("glbdef11"); 
	  	       						String glbdef12= jo.getString("glbdef12"); 
	  	       						String glbdef13= jo.getString("glbdef13"); 
	  	       						String glbdef14= jo.getString("glbdef14"); 
	  	       						String glbdef15= jo.getString("glbdef15"); 
	  	       						String glbdef17= jo.getString("glbdef17"); 
	  	       						String glbdef18= jo.getString("glbdef18"); 
	  	       						String glbdef19= jo.getString("glbdef19"); 
	  	       						String glbdef20= jo.getString("glbdef20"); 
	  	       						String glbdef22= jo.getString("glbdef22"); 
	  	       						String glbdef23= jo.getString("glbdef23"); 
	  	       						String glbdef16= jo.getString("glbdef16"); 
	  	       						String glbdef24= jo.getString("glbdef24"); 
	  	       						String glbdef25= jo.getString("glbdef25"); 
	  	       						String glbdef26= jo.getString("glbdef26"); 
	  	       						String glbdef27= jo.getString("glbdef27"); 
	  	       						String glbdef28= jo.getString("glbdef28"); 
	  	       						String glbdef29= jo.getString("glbdef29");
	  	       						String glbdef30= jo.getString("glbdef30"); 
	  	       						String rn= jo.getString("rn"); 
	  	       						String dr= jo.getString("dr");
	                                bd.setADDR(addr);
	                                bd.setBIRTHDATE(birthdate); 
	                                bd.setCODE(code);  
	                                bd.setCREATIONTIME(creationtime); 
	                                bd.setCREATOR(creator);
	                                bd.setDATAORIGINFLAG(dataoriginflag);
	                                bd.setEMAIL(email); 
	                                bd.setENABLESTATE(enablestate);
	                                bd.setHOMEPHONE(homephone); 
	                                bd.setID(id); 
	                                bd.setIDTYPE(idtype);
	                                bd.setISSHOPASSIST(isshopassist);
	                                bd.setJOINWORKDATE(joinworkdate);
	                                bd.setMNECODE(mnecode);
	                                bd.setMOBILE(mobile); 
	                                bd.setMODIFIEDTIME(modifiedtime);
	                                bd.setMODIFIER(modifier);
	                                bd.setNAME(name);
	                                bd.setNAME2(name2);
	                                bd.setNAME3(name3);
	                                bd.setNAME4(name4);
	                                bd.setNAME5(name5);
	                                bd.setNAME6(name6);
	                                bd.setOFFICEPHONE(officephone);
	                                bd.setPK_GROUP(pk_group);
	                                bd.setPK_ORG(pk_org);
	                                bd.setPK_PSNDOC(pk_psndoc);
	                                bd.setSEX(sex);
	                                bd.setTS(ts);
	                                bd.setUSEDNAME(usedname);
	                                bd.setBLOODTYPE(bloodtype);
	                                bd.setCENSUSADDR(censusaddr);
	                                bd.setCHARACTERRPR(characterrpr);
	                                bd.setCOUNTRY(country);
	                                bd.setDIE_DATE(die_date);
	                                bd.setDIE_REMARK(die_remark);
	                                bd.setEDU(edu);
	                                bd.setFAX(fax); 
	                                bd.setFILEADDRESS(fileaddress);
	                                bd.setHEALTH(health);
	                                bd.setISHISKEYPSN(ishiskeypsn);
	                                bd.setJOINPOLITYDATE(joinpolitydate);
	                                bd.setMARITAL(marital);
	                                bd.setMARRIAGEDATE(marriagedate);
	                                bd.setNATIONALITY(nationality);
	                                bd.setNATIVEPLACE(nativeplace);
	                                bd.setPENELAUTH(penelauth);
	                                bd.setPERMANRESIDE(permanreside);
	                                bd.setPHOTO(photo);
	                                bd.setPK_DEGREE(pk_degree);
	                                bd.setPK_HRORG(pk_hrorg);
	                                bd.setPOLITY(polity); 
	                                bd.setPOSTALCODE(postalcode);
	                                bd.setPREVIEWPHOTO(previewphoto);
	                                bd.setPROF(prof); 
	                                bd.setRETIREDATE(retiredate); 
	                                bd.setSECRET_EMAIL(secret_email);
	                                bd.setSHORTNAME(shortname);
	                                bd.setTITLETECHPOST(titletechpost);
	                                bd.setFIRSTNAME(firstname); 
	                                bd.setLASTNAME(lastname);
	                                bd.setISCADRE(iscadre);
	                                bd.setISHISLEADER(ishisleader);
	                                bd.setAGE(age); 
	                                bd.setWORKAGE(workage); 
	                                bd.setGLBDEF21(glbdef21);
	                                bd.setGLBDEF1(glbdef1);
	                                bd.setGLBDEF2(glbdef2); 
	                                bd.setGLBDEF3(glbdef3); 
	                                bd.setGLBDEF4(glbdef4); 
	                                bd.setGLBDEF5(glbdef5); 
	                                bd.setGLBDEF6(glbdef6); 
	                                bd.setGLBDEF7(glbdef7); 
	                                bd.setGLBDEF8(glbdef8); 
	                                bd.setGLBDEF9(glbdef9); 
	                                bd.setGLBDEF10(glbdef10);
	                                bd.setGLBDEF11(glbdef11);
	                                bd.setGLBDEF12(glbdef12); 
	                                bd.setGLBDEF13(glbdef13); 
	                                bd.setGLBDEF14(glbdef14);
	                                bd.setGLBDEF15(glbdef15);
	                                bd.setGLBDEF17(glbdef17);
	                                bd.setGLBDEF18(glbdef18);
	                                bd.setGLBDEF19(glbdef19);
	                                bd.setGLBDEF20(glbdef20);
	                                bd.setGLBDEF22(glbdef22);
	                                bd.setGLBDEF23(glbdef23); 
	                                bd.setGLBDEF16(glbdef16);
	                                bd.setGLBDEF24(glbdef24);
	                                bd.setGLBDEF25(glbdef25);
	                                bd.setGLBDEF26(glbdef26);
	                                bd.setGLBDEF27(glbdef27);
	                                bd.setGLBDEF28(glbdef28);
	                                bd.setGLBDEF29(glbdef29);
	                                bd.setGLBDEF30(glbdef30);
	                                bd.setRN(rn); 
	                                bd.setDR(dr);
	                                		
	                                		bd_psndoc.add(bd);
						//设置区域								
								bd= null;
								System.gc();

							}
						}	
							bdpsndocService.insertOrUpdate(bd_psndoc);	
							bd_psndoc = null;
							System.gc();
					}						
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
	 }
	

}
