package com.chinacreator.process.dao;

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

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

@Component
@CacheName("SPDao")
public class SPDao {
	
	@Cacheable(value="SPDao", key="#spid")
	public int getValue(String spid) throws SQLException {
		String sql = "select ERRORHANDLE from TB_SP_INFO where spid = ?";
		return NumberUtils.toInt(SQLExecutor.queryFieldWithDBName(DataSource.NET3G, sql, spid));
	}

    public SpBean querySpByProductId(String productId) {
        PreparedDBUtil pdb = new PreparedDBUtil();
        SpBean bean=null;
        String sql = "select * from TB_SP_INFO where vacproductid = ?";
        try {
            pdb.preparedSelect(DataSource.NET3G,sql);
            pdb.setString(1, productId);
            bean=(SpBean)pdb.executePreparedForObject(SpBean.class);
        } catch (Exception e) {
            e.getMessage();
        }
        return bean;
    }
	
	/**
	 * 根据SPID查询复合业务的子业务
	 * @param spid
	 * @return
	 * @throws SQLException
	 */
	public List<HashMap> getSubSpInfo( String spid ) throws SQLException{
		String sql = "SELECT A.CPID,A.SPID FROM TB_SP_INFO A ,TB_SP_CP B WHERE A.SPID = B.PRODUCTID AND B.SPID = ? AND B.HASVIP = '0'";
		return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, spid);
	}
	
	/**
	 * 根据SPID查询是否复合业务
	 * @param spid
	 * @return
	 * @throws SQLException
	 */
	public List<HashMap> getFhGoods( String spid ) throws SQLException{
		String sql = "SELECT CPID,SPID,TYPE FROM net3g.TB_SP_INFO WHERE SPID = ? AND TYPE = '4' ";
		return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, spid);
	}
	
	public static void main(String[] args) {
		try {
			//System.out.println(new SPDao().getSubSpInfo("1040"));
			System.out.println(new SPDao().getFhGoods("20190313").size());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
	
