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

import java.rmi.RemoteException;
import java.util.ArrayList;
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.model.hi_psndoc_train;
import cn.com.countrygarden.bi.main.model.hi_psndoc_work;
import cn.com.countrygarden.bi.main.service.performance.bdpsndocService;
import cn.com.countrygarden.bi.main.service.performance.hipsndoctrainService;
import cn.com.countrygarden.bi.main.service.performance.hipsndocworkService;


@Controller("HdtController")
@RequestMapping("/HdtController")
public class HdtController {
	@Autowired
	private hipsndoctrainService hipsndoctrainService;
	
    @ResponseBody
    @RequestMapping(value = "/Hipsndoctrain",method = {RequestMethod.POST,RequestMethod.GET})
    public void hipsndoctrain(@RequestParam(value = "pageno") int pageno,@RequestParam(value = "pagesize") int pagesize){
    	IPsnInfoQueryWsPortTypeProxy ip = new IPsnInfoQueryWsPortTypeProxy();  
    	  String dayin;
     	  String circle;	
     	    try {      
  			dayin = ip.queryPsnTrainInfo(pageno, pagesize);
//  			System.out.println(dayin);
  			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=282;j<=pagenototal;j++){
  			circle = ip.queryPsnTrainInfo(j, pagesize);
  			/*System.out.println(circle);*/
  			JSONObject jsonObjectcircle = JSON.parseObject(circle);
  			JSONObject data = (JSONObject) jsonObject.get("data");
  			/*System.out.println(data);*/
  			JSONArray train = data.getJSONArray("train");	
  		   	List<hi_psndoc_train> hi_psndoc_train = new ArrayList<hi_psndoc_train>();
  			/*System.out.println(train);*/
  			if(train!=null||train.size()!=0){ 
  				for(int i=0;i<train.size();i++){ 
  					JSONObject jo = (JSONObject)train.getJSONObject(i);
  					hi_psndoc_train hit=new hi_psndoc_train();
  					String absence_count= jo.getString("absence_count"); 
  					String act_code= jo.getString("act_code");
  					String act_name= jo.getString("act_name");
  					String act_name2= jo.getString("act_name2");
  					String act_name3= jo.getString("act_name3");
  					String act_name4= jo.getString("act_name4");
  					String act_name5= jo.getString("act_name5");
  					String act_name6= jo.getString("act_name6");
  					String ass_option= jo.getString("ass_option");
  					String ass_result= jo.getString("ass_result");
  					String assist_unit= jo.getString("assist_unit");
  					String begindate= jo.getString("begindate");
  					String certificate_date= jo.getString("certificate_date");
  					String certificate_unit= jo.getString("certificate_unit");
  					String contcode= jo.getString("contcode");
  					String creationtime= jo.getString("creationtime");
  					String creator= jo.getString("creator");
  					String enddate= jo.getString("enddate"); 
  					String entrepreneur= jo.getString("entrepreneur");
  					String isallduty= jo.getString("isallduty");
  					String lastflag= jo.getString("lastflag"); 
  					String memo= jo.getString("memo");
  					String modifiedtime= jo.getString("modifiedtime");
  					String modifier= jo.getString("modifier"); 
  					String pk_act= jo.getString("pk_act");
  					String pk_group= jo.getString("pk_group"); 
  					String pk_org= jo.getString("pk_org"); 
  					String pk_psndoc= jo.getString("pk_psndoc"); 
  					String pk_psndoc_sub= jo.getString("pk_psndoc_sub");
  					String pk_psnjob= jo.getString("pk_psnjob");
  					String pk_psnorg= jo.getString("pk_psnorg");
  					String purveyor= jo.getString("purveyor"); 
  					String recordnum= jo.getString("recordnum");
  					String signon_dt= jo.getString("signon_dt"); 
  					
  					int source_type=0;
  					if(jo.getString("source_type")!=null) {
  						source_type=Integer.parseInt(jo.getString("source_type"));
  					}
  					
  					int taketrm_method=0;
  					if(jo.getString("taketrm_method")!=null){
  					taketrm_method=Integer.parseInt(jo.getString("taketrm_method"));
  					}
  					
  					String tra_content= jo.getString("tra_content"); 
  					String tra_cost= jo.getString("tra_cost");
  					String tra_mode= jo.getString("tra_mode"); 
  					String tra_result= jo.getString("tra_result"); 
  					String tra_time= jo.getString("tra_time"); 
  					String tra_type= jo.getString("tra_type"); 
  					String traim= jo.getString("traim"); 
  					String trm_certif_code= jo.getString("trm_certif_code");
  					String trm_certif_name= jo.getString("trm_certif_name"); 
  					String trm_location= jo.getString("trm_location"); 
  					String ts= jo.getString("ts"); 
  					
  					int glbdef1=0;
  					if(jo.getString("glbdef1")!=null) {
  						glbdef1=Integer.parseInt(jo.getString("glbdef1"));
  					}
  					
  					String glbdef2= jo.getString("glbdef2"); 
  					String glbdef3= jo.getString("glbdef3"); 
  					String glbdef4= jo.getString("glbdef4"); 
  					String rn= jo.getString("rn"); 
  					String dr= jo.getString("dr"); 
  					hit.setABSENCE_COUNT(absence_count); 
  					hit.setACT_CODE(act_code); 
  					hit.setACT_NAME(act_name); 
  					hit.setACT_NAME2(act_name2);
  					hit.setACT_NAME3(act_name3); 
  					hit.setACT_NAME4(act_name4); 
  					hit.setACT_NAME5(act_name5); 
  					hit.setACT_NAME6(act_name6); 
  					hit.setASS_OPTION(ass_option); 
  					hit.setASS_RESULT(ass_result); 
  					hit.setASSIST_UNIT(assist_unit); 
  					hit.setBEGINDATE(begindate); 
  					hit.setCERTIFICATE_DATE(certificate_date); 
  					hit.setCERTIFICATE_UNIT(certificate_unit); 
  					hit.setCONTCODE(contcode); 
  					hit.setCREATIONTIME(creationtime); 
  					hit.setCREATOR(creator); 
  					hit.setENDDATE(enddate); 
  					hit.setENTREPRENEUR(entrepreneur); 
  					hit.setISALLDUTY(isallduty); 
  					hit.setLASTFLAG(lastflag); 
  					hit.setMEMO(memo); 
  					hit.setMODIFIEDTIME(modifiedtime); 
  					hit.setMODIFIER(modifier); 
  					hit.setPK_ACT(pk_act); 
  					hit.setPK_GROUP(pk_group);
  					hit.setPK_ORG(pk_org);
  					hit.setPK_PSNDOC(pk_psndoc);
  					hit.setPK_PSNDOC_SUB(pk_psndoc_sub); 
  					hit.setPK_PSNJOB(pk_psnjob); 
  					hit.setPK_PSNORG(pk_psnorg); 
  					hit.setPURVEYOR(purveyor); 
  					hit.setRECORDNUM(recordnum);
  					hit.setSIGNON_DT(signon_dt); 
  					hit.setSOURCE_TYPE(source_type); 
  					hit.setTAKETRM_METHOD(taketrm_method); 
  					hit.setTRA_CONTENT(tra_content); 
  					hit.setTRA_COST(tra_cost); 
  					hit.setTRA_MODE(tra_mode); 
  					hit.setTRA_RESULT(tra_result); 
  					hit.setTRA_TIME(tra_time); 
  					hit.setTRA_TYPE(tra_type); 
  					hit.setTRAIM(traim); 
  					hit.setTRM_CERTIF_CODE(trm_certif_code); 
  					hit.setTRM_CERTIF_NAME(trm_certif_name); 
  					hit.setTRM_LOCATION(trm_location); 
  					hit.setTS(ts); 
  					hit.setGLBDEF1(glbdef1); 
  					hit.setGLBDEF2(glbdef2); 
  					hit.setGLBDEF3(glbdef3); 
  					hit.setGLBDEF4(glbdef4); 
  					hit.setRN(rn); 
  					hit.setDR(dr);
  					hi_psndoc_train.add(hit);
  					hit=null;
  					System.gc();
  				 }
  				}
  			     hipsndoctrainService.insertOrUpdate(hi_psndoc_train);
  			     hi_psndoc_train = null;
				 System.gc();
  			}
  		} catch (RemoteException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  	}
     
}