package com.chinacreator.process.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.cache.annotation.Cacheable;
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 VipSmsSpidSendDao{

  	/**
  	 * 添加推送数据
  	 * @param bean
  	 * @throws SQLException
  	 */
  	public void addSendLog(HashMap<String, String> params) throws SQLException {
	    String sql = " INSERT INTO TL_VIP_SMSSPID_SEND_LOG ( ID, USERID, CONFID, SENDMONTH, COLLECTID, INSERTTIME )  " +
	    		     " VALUES ( TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS')||SEQ_CHANNEL_ORG.NEXTVAL, " + 
	    		     " #[USERID], #[CONFID], #[SENDMONTH], #[COLLECTID], SYSDATE )";   	
	    SQLExecutor.insertBean("net3g", sql, params);
	 }
  	
  	/**
  	 * 获取TD_VIP_SMSSPID_REC表RESULTCODE为待处理和之前有处理过但发送时间不合法的数据数据
  	 * 每次取800条数据
  	 * @param partition 分区标识
  	 * @param rows 每次取多少条数据
  	 * @param currmonth 当前月份  	 
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> getDataByPart(String partition, int rows, String currmonth)throws SQLException {
  		//SELECT * FROM (SELECT * FROM TD_VIP_SMSSPID_REC PARTITION(T_HASH_P50) WHERE RESULTCODE = '1') WHERE ROWNUM < 800
  		//resultcocde: 1，待处理，9055，未到短信发送开始时间，9054，已过短信发送结束时间
  		String sql = " SELECT * FROM ( SELECT * FROM TD_VIP_SMSSPID_REC PARTITION("+partition+") WHERE COLLECTMONTH = ? AND RESULTCODE IN ( '1','9055','9054' ) ) WHERE ROWNUM < ?  ";
		PreparedDBUtil pdb = new PreparedDBUtil();	 
		pdb.preparedSelect(DataSource.NET3G, sql);
		pdb.setString(1, currmonth);
		pdb.setInt(2, rows);
		return pdb.executePreparedForList(HashMap.class);
  	}
  	
  	/**
  	 * 根据ID更新TD_VIP_SMSSPID_REC表的处理状态
  	 * @param id
  	 * @param resultcode
  	 * @param errorinfo
  	 * @param realflag
  	 * @return
  	 * @throws SQLException
  	 */
  	public boolean updExecRes(String id, String resultcode, String errorinfo) throws SQLException {
  		boolean res = false;
  		//更新数据状态为处理中
		String sql = "UPDATE TD_VIP_SMSSPID_REC SET RESULTCODE = ? , RESULTINFO = ? , UPDTIME = SYSDATE WHERE ID = ? AND RESULTCODE = '2' ";
		int rows = (Integer)SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode, errorinfo, id);
		if(rows > 0){
			return true;
		}
		
		return res;	
  	}
  	
  	/**
  	 * 更新状态为执行中
  	 * @param id
  	 * @return
  	 * @throws SQLException
  	 */
  	public boolean updExecing(String id) throws SQLException {
  		//更新数据状态为处理中
		String sql = "UPDATE TD_VIP_SMSSPID_REC SET RESULTCODE = '2' , UPDTIME = SYSDATE WHERE ID = ?  ";
		int rows = (Integer)SQLExecutor.updateWithDBName(DataSource.NET3G, sql, id);
		if(rows > 0){
			return true;
		}else{
			return false;
		}
  	}
	
	/**
	 * 获取发送配置信息
	 * @return
	 * @throws SQLException
	 */
	@Cacheable(value="vipsmsspidconf",key="#confid")
	public HashMap getVipsmsspidconf(String confid) throws SQLException {
		//System.out.println("查询数据库");
		String sql = "SELECT * FROM TB_VIP_SMSSPID_CONF WHERE STATUS = '0' AND ID = ? ";
		PreparedDBUtil pdb = new PreparedDBUtil();	 
		pdb.preparedSelect(DataSource.NET3G, sql);
		pdb.setString(1, confid);
		return (HashMap)pdb.executePreparedForObject(HashMap.class);
	}
	
	/**
	 * 查询用户当月是否已发送过指定短信TL_VIP_SMSSPID_SEND_LOG
	 * @param userid
	 * @param sendmonth	 
	 * @param confid	 
	 * @return true是，false否
	 * @throws SQLException
	 */
	public boolean hasAlreadySend(String userid, String sendmonth, String confid) throws SQLException {
		String sql = "SELECT COUNT(1) CNT FROM TL_VIP_SMSSPID_SEND_LOG WHERE USERID = ? AND CONFID = ? AND SENDMONTH = ? ";
		int cnt = SQLExecutor.queryObjectWithDBName(Integer.class, DataSource.NET3G,sql, userid, confid, sendmonth);
		if(cnt > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public static void main(String[] args) throws SQLException {
		VipSmsSpidSendDao dao = new VipSmsSpidSendDao();
		System.out.println(dao.getVipsmsspidconf("20220406101954000001"));
		System.out.println(dao.getVipsmsspidconf("20220406101954000001"));
		//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"));
	}
}
