package com.chinacreator.process.dao;

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

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

@Component
public class ColorSealDao {

    
    /**
     * 添加记录
     * @param
     * @throws SQLException
     */
	public Object addOrderRec(HashMap params)throws SQLException {
		String sql = " INSERT INTO TD_COLORSEAL_ORDER_REC( "+
			" ID,USERID,CPID,SPID,RESULTCODE,RESULTINFO,ORDERTYPE,INTERFACE,ISREVERSE,CHANNEL) "+
			" VALUES(?,?,?,?,?,?,?,?,?,?)";
		return SQLExecutor.insertWithDBName( DataSource.NET3G, sql,
				params.get("id"),params.get("userid"),params.get("cpid"),params.get("spid"),
				params.get("resultcode"),
				params.get("description"),params.get("ordertype"),params.get("interface"),params.get("isreverse"),params.get("channel"));
    }

    public HashMap getColorSealInfo(String id) throws SQLException {
		String sql = "SELECT ID,CPID,SPID,RETRY,TO_CHAR(RETRYTIME,'YYYYMMDDHH24MISS') RETRYTIME FROM TD_COLORSEAL_ORDER_REC WHERE ID = ? AND RESULTCODE <> '000000' ";
		return SQLExecutor.queryObjectWithDBName(HashMap.class, DataSource.NET3G, sql, id);
	}
	
	/**
	 * 获取流水号
	 * @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);
	}


	/**
	 * 获取endtime
	 * @return
	 */
	public HashMap getEndtime(String userid,String spid)throws SQLException {
		String sql = " SELECT  TO_CHAR(ORDERTIME,'yyyyMMddhh24miss') ORDERTIME,TO_CHAR(ENDTIME,'yyyyMMddhh24miss') ENDTIME, ID FROM td_order_relations where userid = ? and spid = ?";
		return SQLExecutor.queryObjectWithDBName(HashMap.class, DataSource.NET3G, sql,userid,spid);
	}

	public void updateRetry(String id) throws SQLException {
		String sql = "update TD_COLORSEAL_ORDER_REC set retry =  retry + 1, resultcode = '-2',resultinfo = '重试中',retrytime = sysdate where id = ?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,id);
	}

	/*
	 * 成功更新操作
	 *
	 * */
	public void update(HashMap map) throws SQLException {
		String sql = "update TD_COLORSEAL_ORDER_REC set resultcode =?,resultinfo = ?  where id=?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,map.get("resultcode"),map.get("resultinfo"),map.get("id"));
	}


	public Date getInserttime(String id) throws SQLException {
		String sql = "select inserttime from TD_COLORSEAL_ORDER_REC where id = ?";
		return SQLExecutor.queryObjectWithDBName(Date.class,DataSource.NET3G, sql,id);
	}

	public Date getRetryttime(String id) throws SQLException {
		String sql = "select retrytime from TD_COLORSEAL_ORDER_REC where id = ?";
		return SQLExecutor.queryObjectWithDBName(Date.class,DataSource.NET3G, sql,id);
	}

	public static void main(String[] args) throws Exception{
		ColorSealDao dao = new ColorSealDao();
		//System.out.println(dao.getBusiConf("99134247"));
//		HashMap params = new HashMap();
//		params.put("id", "20201104160121000001");
//		params.put("resultcode", "0");
//		params.put("description", "0");
//		params.put("times", "1");
//		dao.update(params,3);
		System.out.println(dao.getColorSealInfo("202011041736219937940"));
//		System.out.println(dao.addOrderRec(params));


	}
}
