package com.chinacreator.process.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.chinacreator.common.dao.CacheableDao;
import com.chinacreator.common.support.cache.annotation.CacheName;
import com.chinacreator.process.bean.AreaInfo;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;

@Component
@CacheName("AreaInfo")
public class CallerAreacodeDao extends CacheableDao{
	
	/**
	 * 初始化缓存
	 * @throws SQLException 
	 */
	@SuppressWarnings("unchecked")
//	@PostConstruct
	public void loadCache() throws SQLException {
		PreparedDBUtil pdb = new PreparedDBUtil();
		String sql = "select * from TB_CALLER_AREACODE ";
		List<AreaInfo> AreaInfos = pdb.executeSelectForList(DataSource.SDK, sql, AreaInfo.class);
		for(AreaInfo areaInfos : AreaInfos) {
			addCache("AreaInfo", areaInfos.getCallerhead(), areaInfos);
		}
	}
	
	@Cacheable(value="AreaInfo", key="#callerhead")
	public AreaInfo findByCallerhead(String callerhead) {
		System.out.println("从数据库读取数据");
		PreparedDBUtil pdb = new PreparedDBUtil();
		String sql = "select * from TB_CALLER_AREACODE where CALLERHEAD = ?";
		try {
			pdb.preparedSelect(DataSource.SDK, sql);
			pdb.setString(1, callerhead);
			return (AreaInfo) pdb.executePreparedForObject(AreaInfo.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	

}
