package com.chinacreator.process.dao;

import com.chinacreator.process.bean.ContractProductPushBean;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;
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;
import java.util.UUID;

@Component
public class ContractProductDao {
	
	/**
	 * 更新推送结果
	 * @param pushid
	 * @param resultcode
	 * @param resultinfo
	 * @return
	 * @throws SQLException
	 */
	public boolean updatePush(String pushid, String resultcode, String resultinfo)throws SQLException {
		String sql = "UPDATE TD_CONTRACT_PRODUCT_PUSH SET RESULTCODE = ? , RESULTINFO = ? , PUSHTIME = SYSDATE WHERE ID = ? ";
		Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode,resultinfo,pushid);
		//影响行
		//System.out.println(rows);
		if(Integer.parseInt(rows.toString()) > 0){
			return true;
		}else{
			return false;
		}
	}
  	
  	/**
  	 * 获取ID
  	 * @return
  	 * @throws SQLException
  	 */
  	public String getNo()throws SQLException {
  		String sql = "SELECT TO_CHAR(SYSDATE,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval ID FROM DUAL ";
  		return (String)SQLExecutor.queryFieldWithDBName("net3g",sql,null);
  	}


  	/**
  	 * 添加推送数据
  	 * @param bean
  	 * @throws SQLException
  	 */
  	public void insertPush(ContractProductPushBean bean) throws SQLException {
	    String sql = "INSERT INTO TD_CONTRACT_PRODUCT_PUSH(ID,USERID,CPID ,SPID,PUSHMONTH,INSERTTIME,PUSHID)  " +
	    		" VALUES(#[id],#[userid],#[cpid],#[spid],#[pushmonth],SYSDATE,#[pushid] )";
	    SQLExecutor.insertBean("net3g", sql, bean);
	 }

  	/**
  	 * 获取TD_CONTRACT_PRODUCT表RESULTCODE为待处理1且订购时间是5个月前的数据
  	 * 每次取800条数据
  	 * @param partition 分区标识
  	 * @param rows 每次取多少条数据
  	 * @return
  	 * @throws SQLException
  	 */
  	public List<HashMap> getContractProductByPart(String partition, int rows,String month)throws SQLException {
  		String sql = " SELECT ID, USERID, CPID, SPID, TO_CHAR(ORDERTIME,'YYYYMMDDHH24MISS') ORDERTIME FROM TD_CONTRACT_PRODUCT PARTITION("+partition+") "+
					 " WHERE RESULTCODE = '1' AND  TO_CHAR(ORDERTIME,'YYYYMM')="+month+"  ";
		PreparedDBUtil pdb = new PreparedDBUtil();	  
		return pdb.executeSelectForList(DataSource.NET3G, sql,0,rows, HashMap.class);
  	}
  	

	/**
	 * 根据ID更新TD_CONTRACT_PRODUCT表的处理状态
	 * @param id
	 * @param resultcode
	 * @param resultinfo
	 * @return
	 * @throws SQLException
	 */
  	public boolean updContractProduct(String id, String resultcode, String resultinfo) throws SQLException {
  		//更新数据状态为处理中
		String sql = "UPDATE TD_CONTRACT_PRODUCT SET RESULTCODE = ? , RESULTINFO = ? , UPDTIME = SYSDATE WHERE ID = ? ";
		Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode, resultinfo, id);
		if(Integer.parseInt(rows.toString()) > 0){
			return true;
		}else{
			return false;
		}
  	}
  	
  	/**
  	 * 更新状态为执行中
  	 * @param id
  	 * @return
  	 * @throws SQLException
  	 */
  	public boolean updContractProductExecing(String id) throws SQLException {
  		//更新数据状态为处理中
		String sql = "UPDATE TD_CONTRACT_PRODUCT SET RESULTCODE = '2' , UPDTIME = SYSDATE WHERE ID = ?  ";
		Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, id);
		if(Integer.parseInt(rows.toString()) > 0){
			return true;
		}else{
			return false;
		}
  	}


	/**
	 * 批量更新
	 * @param ids
	 * @return
	 * @throws SQLException
	 */
	public int batchUpdSyncStatus(String ids) throws SQLException {
		String sql = " UPDATE TD_CONTRACT_PRODUCT SET RESULTCODE = '2' , RESULTINFO = '处理中', UPDTIME = SYSDATE WHERE ID IN ( "+ids+" ) ";
		Object res = SQLExecutor.updateWithDBName(DataSource.NET3G, sql);
		return (Integer)res;
	}

	/**
	 * 根据ID查询订购关系
	 * @param id
	 * @return HASHMAP
	 * @throws SQLException
	 */
	public HashMap getOrderRealById(String id) throws SQLException {
		String sql = "SELECT SPID,USERID, TO_CHAR(ORDERTIME,'YYYYMMDDHH24MISS') ORDERTIME, "+
				" (CASE WHEN ENDTIME IS NULL THEN 'NULL'  ELSE TO_CHAR(ENDTIME,'YYYYMMDDHH24MISS') END) ENDTIME, "+
				" (CASE WHEN CANCELTIME IS NULL THEN 'NULL'  ELSE TO_CHAR(CANCELTIME,'YYYYMMDDHH24MISS') END) CANCELTIME "+
				" FROM TD_ORDER_RELATIONS WHERE ID = ? ";
		return SQLExecutor.queryObjectWithDBName(HashMap.class, "net3g", sql, id);
	}

	public List<HashMap> findSpInfo(String spid)throws SQLException {
		String sql = " select cpid, spid, spname "+
				" from tb_sp_info where spid = ? ";
		return  SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, spid);
	}


	/**
	 * 添加推送数据
	 * @param bean
	 * @throws SQLException
	 */
	public void insertPushTest(String id) throws SQLException {
		String sql = "INSERT INTO TD_CONTRACT_PRODUCT(ID,USERID,SPID,CPID,ORDERTIME,CANCELTIME,ENDTIME,STATUS,resultcode)  " +
				" VALUES(?,'18673197465','1011','aiqy',SYSDATE,SYSDATE,SYSDATE,'0','1' )";
//		SQLExecutor.insertBean("net3g", sql, bean);
		SQLExecutor.insertWithDBName(DataSource.NET3G, sql, id);
	}


	public static void main(String[] args) throws SQLException {
		ContractProductDao dao = new ContractProductDao();
		//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"));
//		System.out.println(dao.updContractProductExecing("20201022095726288224A"));


		//添加测试数据
		System.out.println("开始时间1------"+new Date());
		for (int i = 0;i<=20000;i++){
			String uuid = UUID.randomUUID().toString();
			dao.insertPushTest(uuid);
		}

		System.out.println("结束时间1------"+new Date());

	}
}
