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

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.landray.kmss.km.review.webservice.Exception;
import com.landray.kmss.km.review.webservice.IKmReviewThirdWebserviceServiceProxy;

import cn.com.countrygarden.bi.main.service.dup.ContractStatisticalService;



/**
 * 
 * 
 * <pre>
 * 合同录入统计web
 * </pre>
 * 
 * @author developer developer@meicloud.com
 * @version 1.00.00
 * 
 *          <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 */

@Controller
public class ContractStatisticalController2  implements ServletContextListener{
	

	@Autowired
    private ContractStatisticalService contractStatisticalService;
	//BIP提供一个接口:只需传入流程结束的开始时间的参数;==>获取BIP传过来的数据
	
	
	private java.util.Timer timer = null ;  
    public void contextDestroyed(ServletContextEvent event) {  
        // TODO Auto-generated method stub  
  
    }  
  
    public void contextInitialized(ServletContextEvent event) {  
    	List<String> lists=contractStatisticalService.findAllBipNumber();
    	//定义一个Map
    	Map<String,String> BipMap=new HashMap<>();
    	for (String str : lists) {
    		BipMap.put(str, str);
		}
    	
        timer = new java.util.Timer(true) ;  
        event.getServletContext().log("定时器已启动。"); 
        TimerTask timerTask = new TimerTask() {
			@Override
			public void run() {
			try {
				Object a=findBip();
				JSONObject obj = JSONObject.parseObject(a.toString());
				//JSONObject jsonObject=JSONObject.fromObject(a);
				JSONArray  b=(JSONArray) obj.get("data");
				  Map<String, Object> map = new HashMap<String, Object>();
				 List<String> BIPLists=new ArrayList<>();
				for (Object c : b) {
					 //Gson g=new Gson();
					 //使用gson.将其转换为对象
					// BipProcessNumber bip= g.fromJson((String) c, BipProcessNumber.class);
					// bip.getDocSubject();
//					 //不存在则添加到对应Dup数据库--未录入
//					 if(!map.containsKey(bip.getFdNumber())) {
//						 List<ContractStatistical> contractList = new ArrayList<>(); 
//						 ContractStatistical dto=new ContractStatistical();
//						 dto.setBipNum(bip.getFdNumber());
//						 dto.setCommName(bip.getDocSubject());
//						 dto.setOrganCode(bip.getFdDepartment());
//						 
//						 contractStatisticalService.inserContranctStatical(contractList);
//					 }
					 //对比所有的流程编号,如果不存在.则添加到Dup数据库上.存为未录取数据
	
					System.out.println("明");
					}
				System.out.println("sb李明涛");
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
				//查询ERP数据库的所有合同数据
			//List<ContractStatistical> list = contractStatisticalService.findcontractStatisticalService();
			//对比BIP流程id==>ERP不存在则表示没有录入=未录入
			//if() {'
				//contractStatisticalService.插入数据
			//}
			//在ERP数据库中查询审核状态为“未审核”的合同。
			//if(){
			//	}
			//统计ERP中已审核的合同
 
				
			//查询ERP数据	
			//System.out.println("启动执行!!1231234");
			}
		};
        timer.schedule(timerTask, 0, 5000); 
        event.getServletContext().log("已经添加任务调度表。" ) ;  
          
    }  
    
    public String  findBip() throws Exception, RemoteException {
    	System.out.println("sbtao");
    	IKmReviewThirdWebserviceServiceProxy lin=new IKmReviewThirdWebserviceServiceProxy();	
		String a=lin.getCommunityReview("2017-10-10 00:10:00", "2018-10-10 00:10:00");
			System.out.println(a.toString());
		return a.toString();
    	
    }
    
    }
