package com.trs.web2frame.myutils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.trs.web2frame.WCMServiceCaller;
import com.trs.web2frame.dispatch.Dispatch;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil2;

public class SysnViewToOtherChnls {
	private static Map<String, String> chnlInfoMap ;
	static {
		try {
			chnlInfoMap = queryChnlInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
//		System.out.println(chnlInfoMap.size());
//		int count = 1;
//		for(Entry<String, String> entry : chnlInfoMap.entrySet()) {
//	    	Map<String,Object> sysnMap = new HashMap<>();
//	    	sysnMap.put("CurrUserName", "dev");
//	    	sysnMap.put("CURROBJID", "8204");
//	    	sysnMap.put("CURROBJTYPE", "101");
//	    	sysnMap.put("TARGETCHANNELIDS", entry.getValue());
//	    	Dispatch result = WCMServiceCaller.Call("gov_site", "syncMetaViewToOtherChannels", sysnMap, true);
//	    	System.out.println("同步视图："+count++);
//		}
		queryChnlInfo();
	}
	private static Map<String, String> queryChnlInfo() {
        StringBuilder sb = new StringBuilder("SELECT channelid FROM wcmchannel WHERE chnltype != 11 and channelid NOT IN (SELECT channelid FROM xwcmmetaviewemployer)\r\n" + 
        		"");
//        StringBuilder sb = new StringBuilder("select oldchanelid,newchanelid from channel_mapping ");
//        StringBuilder sb = new StringBuilder("SELECT t.channelid as newchanelid,w.channelid as oldchanelid FROM TEMP_NEW_CHNL_JWGK t\r\n" + 
//        		" left join wcmchannel w on w.chnlname=t.chnlname\r\n" + 
//        		" where w.siteid=20");
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		Map<String,String> map = new HashMap<String,String>();
        try {
    		connection = ConnectionUtil2.getConnection();
    		statement = connection.createStatement();
    		set = statement.executeQuery(sb.toString());
    		int count = 1;
    		while (set.next()) {
    			Map<String,Object> sysnMap = new HashMap<>();
    	    	sysnMap.put("CurrUserName", "dev");
    	    	sysnMap.put("CURROBJID", "8204");
    	    	sysnMap.put("CURROBJTYPE", "101");
    	    	sysnMap.put("TARGETCHANNELIDS", set.getInt("channelid"));
    	    	Dispatch result = WCMServiceCaller.Call("gov_site", "syncMetaViewToOtherChannels", sysnMap, true);
    	    	System.out.println("同步视图：channelid=" + set.getInt("channelid") + "   " +count++);
    		}
        }catch(Exception e){
        	e.printStackTrace();
        } finally {
        	ConnectionUtil.closeConnection(set, statement, connection);
        }
		return map;
	}
}
