package com.chinacreator.process.dao;

import com.chinacreator.common.dao.CacheableDao;
import com.chinacreator.common.support.cache.annotation.CacheName;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.SQLExecutor;
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("MqSmsConf")
public class MqSmsConfDao extends CacheableDao{
	
	@Cacheable(value="MqSmsConf")
	public List<HashMap> getConfList() throws SQLException {
		System.out.println("查询数据库短信配置信息");
		String sql = "SELECT T.BUSITYPE,T.CHANNEL,T.CPID,T.EXTEND1,T.EXTEND2,T.PARAMS,T.SMSTXT,T.SPID,T.STYLE,T.SUBCHANNEL,T.PROVINCE,T.STAFFID ,T.SENDTIME,DECODE(T.SENDTIME, null,0, 1) flag FROM TB_MQ_SMS_CONF T WHERE T.STATUS = '0'   order By flag  desc";
		return SQLExecutor.queryListWithDBName(HashMap.class,DataSource.NET3G, sql,null);
	}

	@Cacheable(value="MqSmsConf",key = "#contentid")
	public HashMap getConfList(String contentid) throws SQLException {
		System.out.println("查询数据库短信配置信息");
		String sql = "SELECT T.BUSITYPE,T.CHANNEL,T.CPID,T.EXTEND1,T.EXTEND2,T.PARAMS,T.SMSTXT,T.SPID,T.STYLE FROM TB_MQ_SMS_CONF T WHERE T.STATUS = '0' AND T.ID=? ";
		return SQLExecutor.queryObjectBeanWithDBName(HashMap.class,DataSource.NET3G,sql,contentid);
	}

    public boolean getChannel(String userid,String spid) throws SQLException {
        String sql = "select orderchannel from td_order_relations where userid=?  and spid=? and status='0' ";
        return !"tbcjhd".equals(SQLExecutor.queryFieldWithDBName(DataSource.NET3G, sql,userid,spid));
    }

    public boolean isSendSms(String userid,String spid) throws SQLException {
        String sql = "select count(*)  from td_order_relations where userid=? and status='0' and  spid=? and ceil((to_date(substr(id,1,14),'yyyyMMddHH24MISS')-ordertime)* 24 * 60 * 60)<300 ";
        return  Integer.parseInt(SQLExecutor.queryFieldWithDBName(DataSource.NET3G, sql,userid,spid))>0;
    }
}
