package com.chinacreator.process.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.process.bean.BackShareOrderBean;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.SQLExecutor;

/**
 * 后向异步送会员
 * @author xu.zhou
 * @date 20200818
 */
@Component
public class BackBusiVipAsynDao{
	
	private Logger log = Logger.getLogger("backbusivipsyn");
	
    /**
     * 保存订购日志
     * @param backShareOrder
     * @throws SQLException
     * @throws BusinessException
     */
    public void addShareOrderLog(BackShareOrderBean backShareOrder) throws SQLException  {
    	String sql = "	INSERT INTO TL_BACKORDER_SHARE_PARMS (ID, USERID,ERRORCODE, ERRORINFO, CPID, SPID, REMARK) VALUES ( "+
    				" TO_CHAR(SYSDATE, 'yyyymmddhh24miss')||SEQ_COMMON6.nextval, ?, ?, ?, ?, ?, ?)";
    	SQLExecutor.insertWithDBName(DataSource.NET3G, sql, 
    			backShareOrder.getUserid(),
    			backShareOrder.getErrorcode(),
    			backShareOrder.getErrorinfo(),
    			backShareOrder.getCpid(),
    			backShareOrder.getSpid(),
    			backShareOrder.getRemark());
    }
	
	/**
	 * 更新调能力平台状态
	 * @param id
	 * @param resultcode
	 * @param resultinfo
	 * @return
	 * @throws SQLException
	 */
	public void updShareStatus(String id, String resultcode, String resultinfo) throws SQLException {	
		String sql = " UPDATE TD_BACKBUSI_ORDER_REC SET RESULTCODE = ? , RESULTINFO = ? WHERE ID = ? ";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode, resultinfo, id);
	}
	
	/**
	 * 查询要异步调能力平台的数据
	 * @return
	 */
	public List<HashMap> getInvokeShareData(){
		String sql = " SELECT * FROM (SELECT ID, USERID, ORDERID,CHANNEL, CPID, SPID, BUSITYPE FROM TD_BACKBUSI_ORDER_REC "+
						" WHERE INSERTTIME > SYSDATE - 10 "+
						" AND CHANNEL <> 'CHSvideoD00' "+ 
						" AND RESULTCODE = '3'  "+
						" AND BUSITYPE IN ('2', '3')  "+
						" ORDER BY INSERTTIME) "+
						" WHERE ROWNUM < 1000 ";
		PreparedDBUtil pdb = new PreparedDBUtil();
		try {
			pdb.preparedSelect(DataSource.NET3G, sql);
			return pdb.executePreparedForList(HashMap.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 查询要异步赠送会员的数据
	 * @param trycount
	 * @return
	 */
	public List<HashMap> getVipAsynData(String trycount){
		//赠送会员状态，0成功，1待赠送，2赠送中，3失败，8已冻结
		String sql = " SELECT * FROM (SELECT ID, USERID, ORDERID,CHANNEL, CPID, SPID, VIPSTATUS, BUSITYPE, RETRYCOUNT FROM TD_BACKBUSI_ORDER_REC WHERE INSERTTIME > SYSDATE - 10 "+
				" AND CHANNEL <> 'CHSvideoD00' "+
				" AND RESULTCODE = '0' "+
				" AND (VIPSTATUS = '1' OR VIPSTATUS = '3') "+
				" AND (BUSITYPE = '1' OR BUSITYPE = '3') "+
				" AND RETRYCOUNT < ? "+
				" AND (RETRYTIME IS NULL OR TO_NUMBER((SYSDATE - RETRYTIME)*24*60) >=2) "+ 
				" ORDER BY INSERTTIME) "+
				" WHERE ROWNUM < 1000 ";
		PreparedDBUtil pdb = new PreparedDBUtil();
		try {
			pdb.preparedSelect(DataSource.NET3G, sql);
			pdb.setInt(1, Integer.parseInt(trycount));
			return pdb.executePreparedForList(HashMap.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取业务配置信息
	 * @param cpid
	 * @param spid
	 * @return
	 * @throws SQLException
	 */
	 public List<HashMap> getBackBusiConf(String cpid, String spid) throws SQLException {
		 String sql = "SELECT * FROM TB_BACKBUSI_CONF WHERE STATUS = '0' AND CPID = ? AND SPID = ? ";
		 return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, cpid, spid);
	 }
	 
	 
	/**
	 * 更新赠送会员状态为正在赠送
	 * @param vipstatus
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public void updVipSendStatus(String id) throws SQLException {	
		String sql = " UPDATE TD_BACKBUSI_ORDER_REC SET VIPSTATUS = 2 WHERE ID = ? ";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql, id);
	}
	
	/**
	 * 更新赠送会员结果
	 * @param id
	 * @param vipstatus
	 * @return
	 * @throws SQLException
	 */
	public void updVipSendStatus(String id, String vipstatus, String time) throws SQLException {	
		String sql = " UPDATE TD_BACKBUSI_ORDER_REC SET VIPSTATUS = ?, RETRYCOUNT = RETRYCOUNT+1, RETRYTIME = SYSDATE, TIMES = ? WHERE ID = ? ";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql, vipstatus, time, id);
	}
	
	/**
	 * 根据渠道查询密码
	 * @param channel
	 * @return
	 * @throws SQLException
	 */
	public HashMap getChannelPwdByChannel(String channel)throws SQLException {
		String sql = " SELECT ID, PASSWORD FROM TB_CHANNEL_ORG where status = 0 and id= ? ";
		return SQLExecutor.queryObjectWithDBName(HashMap.class, DataSource.NET3G, sql, channel);
	}
	
	/**
	 * 当调会员赠送接口为失败时反查赠送会员结果，防止调接口超时获取不到真实赠送会员的结果
	 * @param orderid
	 * @return true 赠送成功，false 赠送失败
	 * @throws SQLException
	 */
	public boolean getVipSendRes(String orderid){
		String sql = "SELECT COUNT(1) CNT FROM TL_VIP_LOG WHERE REQUESTID = ? AND RESULTCODE = '0' ";
		int cnt = 0;
		try {
			cnt = (int)SQLExecutor.queryObjectWithDBName(Integer.class, DataSource.NET3G, sql, orderid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(cnt > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public static void main(String[] args) {
		BackBusiVipAsynDao dao = new BackBusiVipAsynDao();
		//System.out.println(dao.getVipAsynData("1"));
		System.out.println(dao.getVipSendRes("201811191529237555154"));
	}
}
