package com.chinacreator.process.dao;

import java.sql.SQLException;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import com.chinacreator.common.support.cache.annotation.CacheName;
import com.chinacreator.process.bean.CPInfo;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.SQLExecutor;

@Component
@CacheName("CPInfo")
public class CPDao {
	
	@Cacheable(value="CPInfo", key="#cpid")
	public CPInfo findById(String cpid) throws SQLException {
		String sql = "select a.cpid, a.cpname, a.status, b.syncaddr, b.netpwd, b.mobtype, b.sdspid from tb_cp_info a, TB_CP_ACCOUNT_CONFIG b where a.cpid = b.cpid and a.cptype=0 and a.cpid=?";
		return SQLExecutor.queryObjectWithDBName(CPInfo.class, DataSource.NET3G, sql, cpid);
	} 
	
	/**
	 * 查询CP指定省份是否开发
	 * @param cpid
	 * @param province
	 * @return
	 * @throws SQLException
	 */
	
	public boolean isOpen(String cpid, String province) throws SQLException {
		String sql = "select count(cpid) from TB_CP_PROVINCE where cpid=? and province=? and state=1";
		return NumberUtils.toInt(SQLExecutor.queryFieldWithDBName(DataSource.NET3G, sql, cpid, province), 0) == 0;
	}
	
	public static void main(String[] args) throws SQLException {
		System.out.println(new CPDao().isOpen("sohu", "湖南"));
	}
}
