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 20210115
 */
@Component
public class ChannelOrderAsynDao{
	
	private Logger log = Logger.getLogger("channelordersyn");
	
	/**
	 * 更新数据为正在处理状态
	 * @param id
	 * @param resultcode
	 * @param resultinfo
	 * @return
	 * @throws SQLException
	 */
	public void updSyncStatus(String id) throws SQLException {	
		String sql = " UPDATE TD_CHANNEL_SYNC_REC SET RESULTCODE = 2 , RESULTINFO = '处理中', UPDTIME = SYSDATE WHERE RESULTCODE NOT IN ('0','2') AND ID = ? ";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql, id);
	}
	
	/**
	 * 批量更新
	 * @param ids
	 * @return
	 * @throws SQLException
	 */
	public int batchUpdSyncStatus(String ids) throws SQLException {	
		String sql = " UPDATE TD_CHANNEL_SYNC_REC SET RESULTCODE = 2 , RESULTINFO = '处理中', UPDTIME = SYSDATE WHERE RESULTCODE NOT IN ('0','2') AND ID IN ( "+ids+" ) ";
		Object res = SQLExecutor.updateWithDBName(DataSource.NET3G, sql);
		return (Integer)res;
	}
	
	/**
	 * 获取要同步的数据
	 * 查询口径：查询SPID包含在配置表、处理结果不为0和2、处理时间为空或者当前大于处理时间超过2分钟、重试次数小于3，每次取1000条
	 * @return
	 * @throws SQLException 
	 */
	public List<HashMap> getAsynData() throws SQLException{
		String sql = " SELECT * FROM (SELECT ID, USERID, ORDERID,CHANNEL, CPID, SPID, TYPE, RETRYCOUNT, TO_CHAR(ORDERTIME,'YYYYMMDDHH24MISS') ORDERTIME, TO_CHAR(CANCELTIME,'YYYYMMDDHH24MISS') CANCELTIME, TO_CHAR(ENDTIME, 'YYYYMMDDHH24MISS') ENDTIME FROM TD_CHANNEL_SYNC_REC "+
				" WHERE INSERTTIME > SYSDATE - 10 "+
				" AND RESULTCODE NOT IN ('0','2') "+ //2为处理中，0为处理成功
				" AND RETRYCOUNT < 3 "+
				" AND (UPDTIME IS NULL OR TO_NUMBER((SYSDATE - UPDTIME)*24*60) >= 2) "+ 
				" AND SPID IN (SELECT SPID FROM TB_CHANNEL_SYNC_CONF WHERE STATUS = '0') "+
				" ORDER BY INSERTTIME) "+
				" WHERE ROWNUM < 1000 ";
		PreparedDBUtil pdb = new PreparedDBUtil();
		pdb.preparedSelect(DataSource.NET3G, sql);
		return pdb.executePreparedForList(HashMap.class);

	}
	
	/**
	 * 获取同步配置信息
	 * @return
	 * @throws SQLException
	 */
	 public List<HashMap> getAsynConf() throws SQLException {
		 String sql = "SELECT * FROM TB_CHANNEL_SYNC_CONF WHERE STATUS = '0' ";
		 return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
	 }
	 	
	/**
	 * 更新处理结果
	 * @param id
	 * @param resultcode
	 * @param resultinfo
	 * @param time
	 * @throws SQLException
	 */
	public void updSyncRes(String id, String resultcode, String resultinfo, String time) throws SQLException {	
		String sql = " UPDATE TD_CHANNEL_SYNC_REC SET RESULTCODE = ?, RESULTINFO = ?, RETRYCOUNT = RETRYCOUNT+1, UPDTIME = SYSDATE, TIMES = ? WHERE ID = ? AND RESULTCODE = '2' ";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode, resultinfo, time, id);
	}
	
	public static void main(String[] args) throws SQLException {
		ChannelOrderAsynDao dao = new ChannelOrderAsynDao();
		//System.out.println(dao.batchUpdSyncStatus("'20210115100825000001','20210115100825000002'"));
		System.out.println(",'a','b'".substring(1));
	}
}
