package com.ekexiu.portal.job;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.sql.DataSource;

import org.jfw.util.ListUtil;
import org.jfw.util.StringUtil;
import org.jfw.util.bean.AfterBeanFactory;
import org.jfw.util.bean.BeanFactory;
import org.jfw.util.context.JfwAppContext;

public class SchJob implements AfterBeanFactory {
	private DataSource dataSource;
	private ScheduledExecutorService service;

	@Override
	public void handle(BeanFactory bf) throws Throwable {
		this.service = JfwAppContext.getScheduledExecutorService();
		this.dataSource = (DataSource) bf.getBean("dataSource");

		this.service.scheduleWithFixedDelay(new Runnable() {
			@Override
			public void run() {
				try {
					Connection con = SchJob.this.dataSource.getConnection();
					try {
						Set<String> all = fillSetValue(con, "SELECT SUBJECT FROM PROFESSOR");
						Set<String> exists = fillSetValue(con, "SELECT CAPTION FROM DATA_DICT WHERE DICT_CODE='SUBJECT'");
						for (String s : exists)
							all.remove(s);
						batchAddCode(con, "SUBJECT", all);
						con.commit();
					} catch (SQLException e) {
						try {
							con.rollback();
						} catch (SQLException ee) {
						}
						throw e;
					} finally {
						try {
							con.close();
						} catch (Exception e) {
						}
					}
				} catch (Throwable th) {
				}
			}
		}, 1, 5, TimeUnit.MINUTES);
		this.service.scheduleWithFixedDelay(new Runnable() {
			@Override
			public void run() {
				try {
					Connection con = SchJob.this.dataSource.getConnection();
					try {
						Set<String> all = fillSetValue(con, "SELECT INDUSTRY FROM PROFESSOR");
						Set<String> exists = fillSetValue(con, "SELECT CAPTION FROM DATA_DICT WHERE DICT_CODE='INDUSTRY'");
						for (String s : exists)
							all.remove(s);
						batchAddCode(con, "INDUSTRY", all);
						con.commit();
					} catch (SQLException e) {
						try {
							con.rollback();
						} catch (SQLException ee) {
						}
						throw e;
					} finally {
						try {
							con.close();
						} catch (Exception e) {
						}
					}
				} catch (Throwable th) {
				}
			}
		}, 1, 5, TimeUnit.MINUTES);
		this.service.scheduleWithFixedDelay(new Runnable() {
			@Override
			public void run() {
				try {
					Connection con = SchJob.this.dataSource.getConnection();
					try {
						Set<String> all = fillSetValue(con, "SELECT CAPTION FROM RESEARCH_AREA");
						Set<String> exists = fillSetValue(con, "SELECT CAPTION FROM DATA_DICT WHERE DICT_CODE='RESEARCH_AREA'");
						for (String s : exists)
							all.remove(s);
						batchAddCode(con, "RESEARCH_AREA", all);
						con.commit();
					} catch (SQLException e) {
						try {
							con.rollback();
						} catch (SQLException ee) {
						}
						throw e;
					} finally {
						try {
							con.close();
						} catch (Exception e) {
						}
					}
				} catch (Throwable th) {
				}
			}
		}, 1, 5, TimeUnit.MINUTES);
		this.service.scheduleWithFixedDelay(new Runnable() {
			@Override
			public void run() {
				try {
					Connection con = SchJob.this.dataSource.getConnection();
					try {
						Set<String> all = fillSetValue(con, "SELECT ADDRESS FROM PROFESSOR");
						Set<String> exists = fillSetValue(con, "SELECT CAPTION FROM DATA_DICT WHERE DICT_CODE='ADDRESS'");
						for (String s : exists)
							all.remove(s);
						batchAddCode(con, "ADDRESS", all);
						con.commit();
					} catch (SQLException e) {
						try {
							con.rollback();
						} catch (SQLException ee) {
						}
						throw e;
					} finally {
						try {
							con.close();
						} catch (Exception e) {
						}
					}
				} catch (Throwable th) {
				}
			}
		}, 1, 5, TimeUnit.MINUTES);

	}

	private void mergSet(String valList, Set<String> vals) {
		if (valList == null)
			return;
		List<String> ss = ListUtil.splitTrimExcludeEmpty(valList, ',');
		vals.addAll(ss);
	}

	private Set<String> fillSetValue(Connection con, String sql) throws SQLException {
		Set<String> result = new HashSet<String>();
		Statement st = con.createStatement();
		try {
			ResultSet rs = st.executeQuery(sql);
			try {
				while (rs.next()) {
					mergSet(rs.getString(1), result);
				}
			} finally {
				try {
					rs.close();
				} catch (Exception e) {
				}
			}
		} finally {
			try {
				st.close();
			} catch (Exception e) {
			}
		}
		return result;
	}

	private void batchAddCode(Connection con, String dictCode, Set<String> vals) throws SQLException {
		if (vals.isEmpty())
			return;
		PreparedStatement ps = con.prepareStatement("INSERT INTO DATA_DICT(DICT_CODE,CODE,CAPTION,ACTIVED) VALUES(?,?,?,'1')");
		try {
			for (String s : vals) {
				ps.setString(1, dictCode);
				ps.setString(2, StringUtil.buildUUID());
				ps.setString(3, s);
				ps.addBatch();
			}
			ps.executeBatch();
		} finally {
			try {
				ps.close();
			} catch (Exception e) {
			}
		}
	}
}
