package com.chinacreator.process.dao;

import com.chinacreator.common.dao.CacheableDao;
import com.chinacreator.common.support.cache.annotation.CacheName;
import com.chinacreator.process.bean.ActivityConfigBean;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.sql.SQLException;
import java.util.List;

@Component
@CacheName("ActivityConfigBean")
public class ActivityConfigDao extends CacheableDao{
	
	/**
	 * 初始化缓存
	 * @throws SQLException 
	 */
	@SuppressWarnings("unchecked")
 	@PostConstruct
	public void loadCache() throws SQLException {
		PreparedDBUtil pdb = new PreparedDBUtil();
		String sql = "select cpid,spid,to_char(begintime,'yyyymmddhh24miss') begintime,to_char(endtime,'yyyymmddhh24miss') endtime,to_char(add_months(endtime,3),'yyyymmddhh24miss') cancelendtime  ,activetype,province  from TB_ACTIVITY_CONFIG where status = 0 ";
		List<ActivityConfigBean> activityConfigBeans = pdb.executeSelectForList(DataSource.NET3G, sql, ActivityConfigBean.class);
		for(ActivityConfigBean activityConfigBean : activityConfigBeans) {
			addCache("ActivityConfigBean", activityConfigBean.getCpid() + "_" +activityConfigBean.getSpid()+ "_" +activityConfigBean.getProvince(), activityConfigBean);
		}
	}
	
	@Cacheable(value="ActivityConfigProvinceBean", key="#cpid + '_' + #spid+ '_' + #province")
	public ActivityConfigBean findByCallerhead(String cpid,String spid,String province) {
		PreparedDBUtil pdb = new PreparedDBUtil();
		String sql = "select cpid,spid,to_char(begintime,'yyyymmddhh24miss') begintime,to_char(endtime,'yyyymmddhh24miss') endtime,to_char(endtime,'yyyymmddhh24miss') cancelendtime ,activetype,province, activehandle from TB_ACTIVITY_CONFIG where cpid = ? and spid = ? and province in ('0',?) and status =0 ";
		try {
			pdb.preparedSelect(DataSource.NET3G, sql);
			pdb.setString(1, cpid);
			pdb.setString(2, spid);
			pdb.setString(3, province);
			return (ActivityConfigBean) pdb.executePreparedForObject(ActivityConfigBean.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ActivityConfigBean findByCallerheadtype(String cpid,String spid,String province,String activetype) {
		PreparedDBUtil pdb = new PreparedDBUtil();
		String sql = "select cpid,spid,to_char(begintime,'yyyymmddhh24miss') begintime,to_char(endtime,'yyyymmddhh24miss') endtime,to_char(endtime,'yyyymmddhh24miss') cancelendtime ,activetype,province,vip,flow,canrepeat from TB_ACTIVITY_CONFIG where cpid = ? and spid = ? and province in ('0',?) and status =0  and activetype=?  order by leveler desc, province desc";
		try {
			pdb.preparedSelect(DataSource.NET3G, sql);
			pdb.setString(1, cpid);
			pdb.setString(2, spid);
			pdb.setString(3, province);
			pdb.setString(4, activetype);
			return (ActivityConfigBean) pdb.executePreparedForObject(ActivityConfigBean.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}