package com.chinacreator.process.dao;

import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.SQLExecutor;
import oracle.sql.CLOB;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

@Component
public class NewYearVideoDao {
	/**
	 * 获取配置数据
	 * @return
	 * @throws SQLException
	 */
    public List<HashMap> getpushData()throws SQLException {
		String sql = " SELECT id,userid,base64code,videomodules FROM TD_WXVIDEO_QUEUE A WHERE A.STATUS = '-1' order by seqnumber";
		return  SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
    }

	public List<HashMap> getData()throws SQLException {
		String sql = " SELECT id,userid,returnvideourl,modulesname,modulestime FROM TD_WXVIDEO_QUEUE A WHERE A.STATUS = '0' order by seqnumber";
		return  SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
	}

	public List<HashMap> getFailData()throws SQLException {
		String sql = " SELECT id,userid,TO_CHAR(finishtime,'yyyy-MM-dd HH:mm:ss') finishtime FROM TD_WXVIDEO_QUEUE A WHERE A.STATUS = '6' order by seqnumber";
		return  SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
	}

	
	/**
	 * 获取流水号
	 * @return
	 */
	public String getNo()throws SQLException {
		String sql = " SELECT TO_CHAR(SYSDATE,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval SENO FROM DUAL";
		return SQLExecutor.queryObjectWithDBName(String.class, DataSource.NET3G, sql);
	}

	/**
	 * 更新状态为下载视频并调cdn接口等
	 * @return
	 */
	public void updateHasDowload(String id)throws SQLException {
		String sql = " UPDATE TD_WXVIDEO_QUEUE SET STATUS = '3' WHERE id=?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,id);
	}


	/**
	 * 更新状态为全部完成
	 * @return
	 */
	public void updateHasFinish(String id,String resultcode,String resultinfo,String videourl)throws SQLException {
		String sql = " UPDATE TD_WXVIDEO_QUEUE SET STATUS = '4',HASCACHE = '0',VIDEOURL=?,RESULTCODE=?,RESULTINFO=? WHERE id=?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,videourl,resultcode,resultinfo,id);
	}

	/**
	 * 更新状态回滚重试
	 * @return
	 */
	public void updateRowback(String id)throws SQLException {
		String sql = " UPDATE TD_WXVIDEO_QUEUE SET STATUS = '0' ,HASCACHE = '-1' WHERE id=?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,id);
	}

	/**
	 * 更新状态回滚重试
	 * @return
	 */
	public void updateRowbackPhotos(String id)throws SQLException {
		String sql = " UPDATE TD_WXVIDEO_QUEUE SET STATUS = '-1'  WHERE id=?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,id);
	}


	/**
	 * 更新状态为处理中
	 * @return
	 */
	public void updateProcessing(String id)throws SQLException {
		String sql = " UPDATE TD_WXVIDEO_QUEUE SET STATUS = '1' WHERE id=?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,id);
	}

	/**
	 * 更新状态为已提交工单
	 * @return
	 */
	public void upHasSubmit(String id,String resultcode,String resultinfo,String photosurl)throws SQLException {
		String sql = "UPDATE TD_WXVIDEO_QUEUE SET STATUS = '2',PHOTOSURL=?,RESULTCODE=?,RESULTINFO=? WHERE id=?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,photosurl,resultcode,resultinfo,id);
	}


	/**
	 * 更新状态为已通知
	 * @return
	 */
	public void updateHasNotify(String id,String resultcode,String resultinfo)throws SQLException {
		String sql = "UPDATE TD_WXVIDEO_QUEUE SET STATUS = '7',RESULTCODE=?,RESULTINFO=? WHERE id=?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,resultcode,resultinfo,id);
	}
	
	public static void main(String[] args) throws Exception{
		NewYearVideoDao dao = new NewYearVideoDao();
		dao.updateProcessing("20210201144255000001");
//		List<HashMap> maps = dao.getpushData();
//		CLOB clob = (CLOB) maps.get(0).get("BASE64CODE");
//		String[] base64 = ClobToString(clob).split(",");
//		int flag = 0;
//		for(String s:base64) {
//			BASE64Decoder decoder = new BASE64Decoder();
//			//base64解码
//			byte[] bytes = decoder.decodeBuffer(s);
//			flag++;
//			FileOutputStream fout = new FileOutputStream("F:\\"+flag+".jpg");
//			//将字节写入文件
//			fout.write(bytes);
//			fout.close();
//		}
	}
	// Clob类型 转String
	public static String ClobToString(CLOB clob) throws SQLException, IOException {
		String reString = "";
		Reader is = clob.getCharacterStream();
		BufferedReader br = new BufferedReader(is);
		String s = br.readLine();
		StringBuffer sb = new StringBuffer();
		while (s != null) {
			sb.append(s);
			s = br.readLine();
		}
		reString = sb.toString();
		if(br!=null){
			br.close();
		}
		if(is!=null){
			is.close();
		}
		return reString;
	}
}
