package com.chinacreator.process.dao;

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

import com.chinacreator.process.bean.KuaiShouActivationBean;
import com.chinacreator.process.bean.OrderRealationBean;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.chinacreator.process.bean.ContinueBean;
import com.chinacreator.process.bean.KuaishouPushBean;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.SQLExecutor;

@Component
public class KuaishouDao{
	
	/**
	 * 更新推送结果
	 * @param pushid
	 * @param resultcode
	 * @param resultinfo
	 * @return
	 * @throws SQLException
	 */
	public boolean updatePush(String pushid, String resultcode, String resultinfo)throws SQLException {
		String sql = "UPDATE TD_KAFKA_KUAISHOU_PUSH SET RESULTCODE = ? , RESULTINFO = ? , PUSHTIME = SYSDATE WHERE ID = ? ";
		Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode,resultinfo,pushid);
		//影响行
		//System.out.println(rows);
		if(Integer.parseInt(rows.toString()) > 0){
			return true;
		}else{
			return false;
		}

	}
	
  	/**
  	 * 查询是否有未推送的数据
  	 * @param serialNumber 手机号码
  	 * @return
  	 */
  	public boolean queryPush(String pushid)throws SQLException {
		 String sql = " SELECT COUNT(1) CNT FROM TD_KAFKA_KUAISHOU_PUSH WHERE RESULTCODE = '1' AND ID = ? ";
		int cnt = SQLExecutor.queryObjectWithDBName(Integer.class, DataSource.NET3G,sql, pushid);
		if(cnt > 0){
			return true;
		}else{
			return false;
		}
  	}
  	
  	/**
  	 * 根据手机号码，推送月份查询指定月是否有过推送
  	 * @param userid
  	 * @param month
  	 * @param pushtype  	 
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> queryPush(String userid, String month, String pushtype)throws SQLException {
  		String sql = "SELECT SERIAL_NUMBER, SPIDS, RESULTCODE,PUSHTYPE,RESULTINFO,PUSHMONTH FROM TD_KAFKA_KUAISHOU_PUSH WHERE SERIAL_NUMBER = ? AND PUSHMONTH = ? AND PUSHTYPE = ? ";
  		return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, userid, month, pushtype);
  	}
  	
  	/**
  	 * 推送类型：0为流量已耗尽，1为流量未耗尽，2为月末推送
  	 * 根据手机号码，推送月份查询指定月是否有过推送流量未消耗
  	 * @param userid
  	 * @param month	 
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> queryPush(String userid, String month)throws SQLException {
  		String sql = "SELECT SERIAL_NUMBER, SPIDS, RESULTCODE,PUSHTYPE,PUSHMONTH FROM TD_KAFKA_KUAISHOU_PUSH WHERE SERIAL_NUMBER = ? AND PUSHMONTH = ? AND PUSHTYPE IN ('1','2') ";
  		return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, userid, month);
  	}
  	
  	/**
  	 * 获取ID
  	 * @return
  	 * @throws SQLException
  	 */
  	public String getNo()throws SQLException {
  		String sql = "SELECT TO_CHAR(SYSDATE,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval ID FROM DUAL ";
  		return (String)SQLExecutor.queryFieldWithDBName("net3g",sql,null);
  	}

  	/**
  	 * 通过手机号码和SPID获取订购关系
  	 * @param userid
  	 * @param spid
  	 * @return
  	 * @throws SQLException
  	 */
	public List<HashMap> findOrderRelaBySpid(String userid, String spid)throws SQLException {
		String sql = " SELECT CPID,SPID,USERID, TO_CHAR(ORDERTIME, 'yyyymmddhh24miss') ORDERTIME, "+ 
			" TO_CHAR(endtime, 'yyyymmddhh24miss') endtime, TO_CHAR(CANCELTIME, 'yyyymmddhh24miss') CANCELTIME, "+
			" (case when endtime is null then '0' when endtime >= sysdate then '1' else '2' end) STATUS "+
			" FROM TD_ORDER_RELATIONS WHERE CPID = 'kuaishou' AND USERID = ? AND SPID = ? ";
		return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, userid, spid);
    }
	

  	/**
  	 * 添加推送数据
  	 * @param bean
  	 * @throws SQLException
  	 */
  	public void insertPush(KuaishouPushBean bean) throws SQLException {
	    String sql = "INSERT INTO TD_KAFKA_KUAISHOU_PUSH(ID,SERIAL_NUMBER,PUSHTYPE,SPIDS ,PUSHMONTH ,RESULTCODE ,RESULTINFO, INSERTTIME)  " +
	    		" VALUES(#[id],#[serial_number],#[pushtype],#[spids],#[pushmonth],#[resultcode],#[resultinfo], SYSDATE )";   	
	    SQLExecutor.insertBean("net3g", sql, bean);
	 }
  	
  	/**
  	 * 添加推送数据到测试表
  	 * @param bean
  	 * @throws SQLException
  	 */
  	public void insertPushZx(KuaishouPushBean bean) throws SQLException {
	    String sql = "INSERT INTO KCZX.TD_KAFKA_KUAISHOU_PUSH_ZX(ID,SERIAL_NUMBER,PUSHTYPE,SPIDS ,PUSHMONTH ,RESULTCODE ,RESULTINFO, INSERTTIME)  " +
	    		" VALUES(#[id],#[serial_number],#[pushtype],#[spids],#[pushmonth],#[resultcode],#[resultinfo], SYSDATE )";   	
	    SQLExecutor.insertBean("net3g", sql, bean);
	 }
  	
  	/**
  	 * 获取当月订购了快手业务的数据
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> getFirstMonthReal()throws SQLException {
  		String sql = "SELECT USERID,SPID FROM TD_ORDER_RELATIONS WHERE CPID = 'kuaishou' AND (ENDTIME IS NULL OR ENDTIME >= SYSDATE) ";
  		return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, null);
  	}
  	
  	/**
  	 * 根据分区获取快手订购数据
  	 * @param partition
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> getFirstMonthRealByPart(String partition)throws SQLException {
  		String sql = " SELECT DISTINCT USERID,SPID FROM TD_ORDER_RELATIONS PARTITION("+partition+") WHERE CPID = 'kuaishou' AND (ENDTIME IS NULL OR ENDTIME >= SYSDATE) ";
  		return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, null);
  	}
  	
  	/**
  	 * 获取TD_KUAISHOU_FIRSTMONTH表RESULTCODE为待处理且CALCULATEDATE为NULL或CALCULATEDATE大于当前时间
  	 * 每次取800条数据
  	 * @param partition 分区标识
  	 * @param rows 每次取多少条数据
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> getFirstMonthByPart(String partition, int rows)throws SQLException {
  		//String sql = " SELECT ID, USERID, SPID, TO_CHAR(CALCULATEDATE,'YYYYMMDDHH24MISS') CALCULATEDATE FROM TD_KUAISHOU_FIRSTMONTH PARTITION("+partition+") WHERE RESULTCODE = '1' AND (CALCULATEDATE IS NULL OR SYSDATE >= CALCULATEDATE ) ";
  		String sql = " SELECT ID, USERID, SPID, TO_CHAR(CALCULATEDATE,'YYYYMMDDHH24MISS') CALCULATEDATE FROM TD_KUAISHOU_FIRSTMONTH PARTITION("+partition+") "+
  						" WHERE RESULTCODE = '1' AND CALCULATEDATE IS NOT NULL AND SYSDATE >= CALCULATEDATE  ";
		PreparedDBUtil pdb = new PreparedDBUtil();	  
		return pdb.executeSelectForList(DataSource.NET3G, sql,0,rows, HashMap.class);
  	}
  	
  	/**
  	 * 获取TD_KUAISHOU_FIRSTMONTH表实时订购的数据
  	 * UPDTIME为空的优先获取，按添加时间INSERTTIME排序，这部分是新订购的，
  	 * 再按UPDTIME倒序获取，处理失败的会重试，保证每条数据都按UPDTIME顺序处理
  	 * @param rows
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> getOrderPushNew(int rows)throws SQLException {
  		//String sql = " SELECT ID, USERID, SPID, TO_CHAR(CALCULATEDATE,'YYYYMMDDHH24MISS') CALCULATEDATE, UPDTIME FROM TD_KUAISHOU_FIRSTMONTH "+
  			//		 " WHERE RESULTCODE = '1' AND CALCULATEDATE IS NULL ORDER BY UPDTIME DESC ";
  		String sql = 
  				" SELECT * FROM (SELECT ID, USERID, SPID, TO_CHAR(CALCULATEDATE,'YYYYMMDDHH24MISS') CALCULATEDATE, UPDTIME FROM TD_KUAISHOU_FIRSTMONTH "+ 
  				" WHERE RESULTCODE = '1' AND CALCULATEDATE IS NULL AND UPDTIME IS NULL ORDER BY INSERTTIME ) "+
  				" UNION ALL "+
  				" SELECT * FROM (SELECT ID, USERID, SPID, TO_CHAR(CALCULATEDATE,'YYYYMMDDHH24MISS') CALCULATEDATE, UPDTIME FROM TD_KUAISHOU_FIRSTMONTH "+ 
  				" WHERE RESULTCODE = '1' AND CALCULATEDATE IS NULL AND UPDTIME IS NOT NULL ORDER BY UPDTIME )";
		PreparedDBUtil pdb = new PreparedDBUtil();	  
		return pdb.executeSelectForList(DataSource.NET3G, sql,0,rows, HashMap.class);
  	}
  	
  	/**
  	 * 根据ID更新TD_KUAISHOU_FIRSTMONTH表的处理状态
  	 * @param id
  	 * @param resultcode
  	 * @param errorinfo
  	 * @param realflag
  	 * @return
  	 * @throws SQLException
  	 */
  	public boolean updFirstMonthRc(String id, String resultcode, String errorinfo, String realflag, String calculatedate) throws SQLException {
  		Object rows = null;
  		//更新数据状态为处理中
		String sql = "UPDATE TD_KUAISHOU_FIRSTMONTH SET RESULTCODE = ? , RESULTINFO = ? , UPDTIME = SYSDATE WHERE ID = ? AND RESULTCODE = '2' ";
		if("0".equals(resultcode)){//处理成功
			sql = "UPDATE TD_KUAISHOU_FIRSTMONTH SET RESULTCODE = ? , RESULTINFO = ? , REALFLAG = ? , UPDTIME = SYSDATE, CALCULATEDATE = TO_DATE(?, 'YYYYMMDDHH24MISS') WHERE ID = ?  AND RESULTCODE = '2' ";
			resultcode = "1"; //设置为1，待处理，日期到了CALCULATEDATE可再捞取
			rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode, errorinfo, realflag, calculatedate, id);
		}else{
			rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode, errorinfo, id);
		}
		if(Integer.parseInt(rows.toString()) > 0){
			return true;
		}else{
			return false;
		}
  	}
  	
  	/**
  	 * 更新状态为执行中
  	 * @param id
  	 * @return
  	 * @throws SQLException
  	 */
  	public boolean updFirstMonthExecing(String id) throws SQLException {
  		//更新数据状态为处理中
		String sql = "UPDATE TD_KUAISHOU_FIRSTMONTH SET RESULTCODE = '2' , UPDTIME = SYSDATE WHERE ID = ?  ";

		Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, id);
		
		if(Integer.parseInt(rows.toString()) > 0){
			return true;
		}else{
			return false;
		}
  	}
  	
  	/**
  	 * 根据手机号码查询所有快手的订购关系
  	 * @param userid
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> getRealByUserid(String userid)throws SQLException {
  		String sql = " SELECT USERID, SPID, TO_CHAR(ORDERTIME,'YYYYMMDDHH24MISS') ORDERTIME,TO_CHAR(ORDERTIME,'YYYYMM') ORDERMONTH FROM TD_ORDER_RELATIONS "+
  					 " WHERE USERID = ? "+
  				     " AND CPID = 'kuaishou' "+
  					 " AND (ENDTIME IS NULL OR ENDTIME >= SYSDATE) "+
  				     " AND SPID IN (SELECT SP_ID FROM TB_SP_AOP_CONFIG) "+ //SPID必须包含在TB_SP_AOP_CONFIG表，只有这个表才能关联KAFKA推送过来的快手流量消耗数据
  					 " ORDER BY ORDERTIME DESC ";
  		return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, userid);
  	}
  	
  	/**
  	 * 获取订购了快手业务的数据，每次取1000条
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> getOrderPush()throws SQLException {
  		String sql = "SELECT ID,USERID,SPID,TO_CHAR(ORDERTIME, 'YYYYMMDDHH24MISS') ORDERTIME FROM TD_KUAISHOU_ORDER WHERE STATUS = '1' ORDER BY ORDERTIME";
		  PreparedDBUtil pdb = new PreparedDBUtil();
		  return pdb.executeSelectForList(DataSource.NET3G, sql,0,1000, HashMap.class);
  	}
  	
	/**
	 * 更新推送结果
	 * @param pushid
	 * @param resultcode
	 * @param resultinfo
	 * @return
	 * @throws SQLException
	 */
	public boolean updOrderPush(String id, String status, String procinfo)throws SQLException {
		String sql = "UPDATE TD_KUAISHOU_ORDER SET STATUS = ?  , PROCINFO = ? , PROCTIME = SYSDATE WHERE ID = ? ";
		Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, status, procinfo,id);
		if(Integer.parseInt(rows.toString()) > 0){
			return true;
		}else{
			return false;
		}
	}
  	
	/**
	 * 更新推送结果
	 * @param pushid
	 * @param resultcode
	 * @param resultinfo
	 * @return
	 * @throws SQLException
	 */
	public boolean updOrderPush(String id, String status, String procinfo, String totaltime, String pushid)throws SQLException {
		String sql = "UPDATE TD_KUAISHOU_ORDER SET STATUS = ?  , PROCINFO = ? , TOTALTIME = ? , PUSHID = ? , PROCTIME = SYSDATE WHERE ID = ? ";
		Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, status, procinfo, totaltime, pushid, id);
		if(Integer.parseInt(rows.toString()) > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 获取未处理的流量已耗尽数据，每次取800条
	 * @return
	 * @throws SQLException
	 */
	public List<HashMap> qryProcPushList()throws SQLException {
		  String sql = " SELECT ID,SERIAL_NUMBER,PUSHTYPE,PUSHMONTH,SPIDS FROM NET3G.TD_KAFKA_KUAISHOU_PUSH WHERE PUSHTYPE = '0' AND RESULTCODE = '1' ORDER BY INSERTTIME ";
		  PreparedDBUtil pdb = new PreparedDBUtil();	  
		  return pdb.executeSelectForList(DataSource.NET3G, sql,0,800, HashMap.class);

	}
	
	/**
	 * 获取未处理的流量已耗尽数据，每次取1000条
	 * @return
	 * @throws SQLException
	 */
	public List<HashMap> qryProcPushListAll()throws SQLException {
		  String sql = " SELECT ID,SERIAL_NUMBER,PUSHTYPE,PUSHMONTH,SPIDS FROM NET3G.TD_KAFKA_KUAISHOU_PUSH WHERE RESULTCODE = '1' ";
		  PreparedDBUtil pdb = new PreparedDBUtil();	  
		  return pdb.executeSelectForList(DataSource.NET3G, sql,0,800, HashMap.class);

	}
	
	/**
	 * 获取FAKEID
	 * @param userid
	 * @param cpid
	 * @return
	 * @throws SQLException
	 */
	public String getFakeid(String userid, String cpid) throws SQLException {
		String fakeid = null;
		//String sql = "SELECT FAKEID FROM TD_CP_USERID_FAKEID WHERE USERID = ? AND CPID = ? ";
		String sql = "SELECT FAKEIDS FROM TD_ORDER_RELATIONS WHERE USERID = ? AND CPID = ? AND FAKEIDS IS NOT NULL";
		fakeid = (String)SQLExecutor.queryFieldWithDBName("net3g",sql,userid,cpid);
		if(fakeid != null && !"".equals(fakeid)){
			JSONObject fakeJson = JSONObject.parseObject(fakeid.trim());
			fakeid = fakeJson.getString(cpid);
		}
		return fakeid;
	}
	
	/**
	 * 判断数据是否已同步完成，只有同步完成后才能调推送接口，否则快手方没有订购数据，接口返回失败
	 * @param userid
	 * @param spid
	 * @return
	 * @throws SQLException
	 */
	public boolean hasSync(String userid, String spid) throws SQLException {
		String res = null;
		String sql = "SELECT USERID FROM TD_ORDER_RELATIONS WHERE USERID = ? AND SPID = ? AND SYNCSUCCEED = '0' ";
		res = (String)SQLExecutor.queryFieldWithDBName("net3g",sql,userid,spid);
		if(res != null && userid.equals(res)){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断数据是否已同步完成，只有同步完成后才能调推送接口，否则快手方没有订购数据，接口返回失败
	 * @param id 订购关系表ID
	 * @return
	 * @throws SQLException
	 */
	public boolean hasSync(String id) throws SQLException {
		String res = null;
		String sql = "SELECT ID FROM TD_ORDER_RELATIONS WHERE ID = ? AND SYNCSUCCEED = '0' ";
		res = (String)SQLExecutor.queryFieldWithDBName("net3g",sql,id);
		if(res != null && id.equals(res)){
			return true;
		}
		return false;
	}
	
	/**
	 * 根据ID获取SPID
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public String getSpidById(String id) throws SQLException {
		String res = null ;
		String sql = "SELECT SPID FROM TD_ORDER_RELATIONS WHERE ID = ? ";
		res = (String)SQLExecutor.queryFieldWithDBName("net3g",sql,id);
		return res;
	}
	
	/**
	 * 根据ID查询订购关系
	 * @param id
	 * @return HASHMAP
	 * @throws SQLException
	 */
	public HashMap getOrderRealById(String id) throws SQLException {
		String sql = "SELECT SPID,USERID, TO_CHAR(ORDERTIME,'YYYYMMDDHH24MISS') ORDERTIME, "+	        
				" (CASE WHEN ENDTIME IS NULL THEN 'NULL'  ELSE TO_CHAR(ENDTIME,'YYYYMMDDHH24MISS') END) ENDTIME, "+
				" (CASE WHEN CANCELTIME IS NULL THEN 'NULL'  ELSE TO_CHAR(CANCELTIME,'YYYYMMDDHH24MISS') END) CANCELTIME, "+
				" (CASE WHEN ENDTIME IS NULL THEN '0' WHEN ENDTIME >= SYSDATE THEN '1' ELSE '2' END) STATUS "+ 
				" FROM TD_ORDER_RELATIONS WHERE ID = ? ";
		return SQLExecutor.queryObjectWithDBName(HashMap.class, "net3g", sql, id);
	}
	
	/**
	 * 查询SPID是否在TB_SP_AOP_CONFIG表，这个表的数据才会与KAFKA数据关联
	 * @param spid
	 * @return  true是，false否
	 * @throws SQLException
	 */
	public boolean hasAopConf(String spid) throws SQLException {
		String sql = " SELECT COUNT(1) CNT FROM TB_SP_AOP_CONFIG WHERE SP_ID = ? ";
		int cnt = SQLExecutor.queryObjectWithDBName(Integer.class, DataSource.NET3G,sql, spid);
		if(cnt > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 判断是否后向产品，后向产品可以在一个月内多次订购
	 * @param spid
	 * @return true是，false否
	 * @throws SQLException
	 */
	public boolean hasBackBusi(String spid) throws SQLException {
		String sql = "SELECT COUNT(1) CNT FROM TB_BACKBUSI_CONF WHERE STATUS = '0' AND SPID = ? ";
		int cnt = SQLExecutor.queryObjectWithDBName(Integer.class, DataSource.NET3G,sql, spid);
		if(cnt > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public static void main(String[] args) throws SQLException {
		KuaishouDao dao = new KuaishouDao();
		//System.out.println(dao.updatePush("202005091510455306373", "0", "ok"));
		//System.out.println(dao.findOrderRelaBySpid("18673197465", "1022"));
		//System.out.println(dao.getNo());
		//System.out.println(dao.queryPush("202005091510455306373"));
		//System.out.println(dao.getOrderPush());
		//System.out.println(dao.hasSync("18673197465", "190"));
		//System.out.println(dao.getSpidById("20201022095726288224"));
		System.out.println(dao.updFirstMonthExecing("20201022095726288224A"));
	}

	/**
	 * 根据orderid查TD_KUAISHOU_ACTIVATE
	 *
	 * @param orderid 订购关系表的id
	 * @return TD_KUAISHOU_ACTIVATE
	 * @throws SQLException
	 */
	public KuaiShouActivationBean getKSActivate(String orderid) throws SQLException {
		String sql = "SELECT ID, USERID, SPNAME, SPID, ORDERTIME, ORDERCHANNEL, CANCELTIME from TD_KUAISHOU_VALID_STATUS where orderid = ? ";
		return SQLExecutor.queryObjectWithDBName(KuaiShouActivationBean.class, "net3g", sql, orderid);

	}

	public void updKSActivate(KuaiShouActivationBean activationBean) throws SQLException {
		String sql = "update TD_KUAISHOU_VALID_STATUS set ORDERTIME = to_date(#[ordertime], 'yyyymmddhh24miss'),ORDERCHANNEL = #[orderchannel],ORDERCHANNEL2 = #[orderchannel2],CANCELTIME =to_date(#[canceltime], 'yyyymmddhh24miss') ,RESULTCODE = #[resultcode],ERRORINFO = #[errorinfo], DATA =#[data], STATUS =#[status],ACTYPE =#[actype] ,UPDATETIME =to_date(#[updatetime],'yyyymmddhh24miss'),UPDTIME = SYSDATE where orderid = #[orderid]";
		SQLExecutor.updateBean("net3g", sql, activationBean);
	}

	public void inserttimeKSActivate(KuaiShouActivationBean activationBean) throws SQLException {
		String sql = "insert into TD_KUAISHOU_VALID_STATUS (ID, USERID,ORDERID, SPNAME, SPID, ORDERTIME, ORDERCHANNEL,ORDERCHANNEL2, CANCELTIME, RESULTCODE, ERRORINFO, DATA, STATUS, ACTYPE, UPDATETIME)\n" +
				"values (#[id], #[userid], #[orderid], #[spname], #[spid],  to_date(#[ordertime], 'yyyymmddhh24miss'), #[orderchannel],#[orderchannel2], to_date(#[canceltime], 'yyyymmddhh24miss'), #[resultcode], #[errorinfo], #[data], #[status], #[actype], to_date(#[updatetime], 'yyyymmddhh24miss'))";
		SQLExecutor.insertBean("net3g", sql, activationBean);
	}


	/**
	 * 按分区标识获取全量快手有效用户数据 TD_KUAISHOU_VALID_CLIENT_REC
	 * 每次取800条数据
	 *
	 * @param partition 分区标识
	 * @param rows      每次取多少条数据
	 * @return 获取到的数据
	 */
	public List<KuaiShouActivationBean> getOrderByPart(String partition, int rows) throws SQLException {
		String sql = "select ORDERID,USERID,SPID,SPNAME,to_char(ORDERTIME ,'yyyymmddhh24miss') ORDERTIME,ORDERCHANNEL,ORDERCHANNEL2,to_char(CANCELTIME ,'yyyymmddhh24miss') CANCELTIME,RESULTCODE,ACTIVEMONTH from\n" +
				"                net3g.TD_KUAISHOU_VALID_CLIENT_REC PARTITION(" + partition + ") \n" +
				"                       where resultcode in ('1','3')\n" +
				"                        order by ordertime desc";
		PreparedDBUtil pdb = new PreparedDBUtil();
		return pdb.executeSelectForList(DataSource.NET3G, sql, 0, rows, KuaiShouActivationBean.class);
	}



	public void updOrdertMonth(String orderid) throws SQLException {
		//更新数据状态为处理中
		String sql = "UPDATE TD_KUAISHOU_VALID_CLIENT_REC SET RESULTCODE = '2' , UPDTIME = SYSDATE WHERE ORDERID = ? and RESULTCODE IN ('1','3') ";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql, orderid);


	}

	public Boolean updOrderValidData(String id, String resultcode) throws SQLException {
		//更新数据状态为已处理或失败
		String sql = "UPDATE TD_KUAISHOU_VALID_CLIENT_REC SET RESULTCODE = ? , UPDTIME = SYSDATE WHERE ORDERID = ? and RESULTCODE = '2' ";
		Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode, id);
		return Integer.parseInt(rows.toString()) > 0;

	}

	//    每周查询激活状态


	/* *
	 * 获取快手新增加的订购数据 TD_KUAISHOU_FIRSTMONTH
	 *
	 *
	 */
	public List<KuaiShouActivationBean> getOrderKuaiShou(int rows) throws SQLException {
		String sql = "SELECT ID, USERID,ACTICATIONSTATUS FROM TD_KUAISHOU_FIRSTMONTH A " +
				" LEFT JOIN net3g.TB_SP_INFO b on a.spid = b.spid " +
				" where a.SPID IN (SELECT SPID FROM net3g.TB_SP_INFO WHERE CPTYPE IN ('0', '1', '3') and SPNAME like '%快手%')" +
				" and decode(updtime,null,inserttime,updtime) <= SYSDATE " +
				" and ACTICATIONSTATUS in (1,3) " +
				" order by decode(updtime,null,inserttime,updtime) desc ";
		PreparedDBUtil pdb = new PreparedDBUtil();
		return pdb.executeSelectForList(DataSource.NET3G, sql, 0, rows, KuaiShouActivationBean.class);
	}

	public List<KuaiShouActivationBean> getUnActivationStatus(int rows) throws SQLException {
		String sql = "select ORDERID ID,USERID from TD_KUAISHOU_VALID_STATUS where status = 1  " +
				"and decode(updtime,null,inserttime,updtime) < (sysdate - 7) " +
				"order by decode(updtime,null,inserttime,updtime)";
		PreparedDBUtil pdb = new PreparedDBUtil();
		return pdb.executeSelectForList(DataSource.NET3G, sql, 0, rows, KuaiShouActivationBean.class);
	}

	/*  *
	 * 将状态更新为处理中 2
	 *
	 * @param id               订购关系表的id
	 * @param acticationstatus 是否已查询激活状态的标记
	 * @return 是否更新成功
	 */
	public void updOrderKuaiShouDataToTwo(String id, String acticationstatus) throws SQLException {
		String sql = "UPDATE TD_KUAISHOU_FIRSTMONTH SET acticationstatus = ?  WHERE ID = ?  AND acticationstatus in ('1','3')";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql, acticationstatus, id);
	}

	/* *
	 * 将状态改为处理结束
	 *
	 * @param id               订购关系表的id
	 * @param acticationstatus 是否已查询激活状态的标记
	 * @return 是否更新成功
	 */
	public boolean updOrderKuaiShouData(String id, String acticationstatus) throws SQLException {
		String sql = "UPDATE TD_KUAISHOU_FIRSTMONTH SET acticationstatus = ?  WHERE ID = ?  AND acticationstatus = '2' ";
		Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, acticationstatus, id);
		return Integer.parseInt(rows.toString()) > 0;
	}

	/* *
	 * 根据id查订购关系
	 *
	 * @param id 订购关系表的id
	 * @return 订购关系
	 */
	public OrderRealationBean getOrderRealationById(String id) throws SQLException {
		String sql = "SELECT a.id,a.userid,a.spid,b.spname,to_char(a.ORDERTIME ,'yyyymmddhh24miss') ORDERTIME,\n" +
				"                a.ORDERCHANNEL,a.ORDERCHANNEL2,to_char(a.canceltime ,'yyyymmddhh24miss   ') canceltime\n" +
				"        FROM td_order_relations a LEFT JOIN TB_SP_INFO b\n" +
				"        ON a.spid = b.spid\n" +
				"        WHERE  a.id = ?";
		return SQLExecutor.queryObjectWithDBName(OrderRealationBean.class, "net3g", sql, id);
	}

}
