package com.chinacreator.process.dao;

import java.sql.SQLException;

import org.springframework.stereotype.Component;

import com.chinacreator.process.bean.EcbLogBean;
import com.chinacreator.process.bean.EcbProductInfo;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.SQLExecutor;

@Component
public class EcbProductDao {

	public void saveLogDao(EcbLogBean bean) throws SQLException{
		String sql = "insert into tl_ecb_log(id,userid,cpid,spid,resultCode,errorInfo,type,handle,inserttime,times) values(to_char(sysdate,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval,"
				+ "#[userid],#[cpid],#[spid],#[resultCode],#[errorInfo],#[type],#[handle],sysdate,#[times])";
		SQLExecutor.insertBean(DataSource.NET3G, sql, bean);
	}
	
	
	public void saveProductInfo(EcbProductInfo bean) throws SQLException{
		String sql =  "insert into TD_PRODUCT_ORDERSYN(id,PROVINCECODE,SERIALNUMBER,SERVICECLASSCODE,ORDERSTATE,PRODUCTID,PRODUCTNAME,STARTDATE,ENDDATE,TRADEID,ORDERDATE,DISCNTPRODUCTID,DISCNTPRODUCTNAME,"
				+ "DISCNTPACKAGEID,DISCNTPACKAGENAME,DISCNTORDERSTATE,DISCNTCODE,DISCNTNAME,DISCNTSTARTDATE,DISCNTENDDATE,CHANNELID,ORIGIN,INSERTTIME) "
				+ "values(to_char(sysdate,'yyyyMMddHH24miss')||SEQ_COMMON6.nextval,'',#[userid],'','',#[userProductId],#[userProductName],to_date(#[discntStartDate],'yyyymmddhh24miss'),to_date(#[discntEndDate],'yyyymmddhh24miss'),'','',#[productId],#[productName],#[packageCode],#[packageName],"
				+ "#[discntOrderState],#[discntCode],#[discntName],to_date(#[discntStartDate],'yyyymmddhh24miss'),to_date(#[discntEndDate],'yyyymmddhh24miss'),'',3,sysdate) ";
		SQLExecutor.insertBean(DataSource.SDK, sql, bean);
	}
	
	public EcbProductInfo queryEcb(String id) throws SQLException{
		String sql = "select productId,productName,DISCNTPACKAGEID packageCode,DISCNTPACKAGENAME packageName,discntCode,discntName, to_char(discntStartDate,'yyyymmddhh24miss') discntStartDate, to_char(discntEndDate,'yyyymmddhh24miss') discntEndDate,discntOrderState,DICNTVALUE discntValue from TD_PRODUCT_ORDERSYN  where id=?";
		return SQLExecutor.queryObjectWithDBName(EcbProductInfo.class, DataSource.SDK, sql,id);
	}
	
	public EcbProductInfo queryEcbProductByUser(String userid) throws SQLException{
		String sql = "select DISCNTPACKAGEID  packageCode,DISCNTCODE discntCode,DISCNTPRODUCTID productId,DISCNTORDERSTATE discntOrderState,to_char(DISCNTSTARTDATE,'yyyymmddhh24miss') discntStartDate,to_char(DISCNTENDDATE,'yyyymmddhh24miss') discntEndDate from TD_PRODUCT_ORDERSYN where SERIALNUMBER = ? order by inserttime desc";
		return SQLExecutor.queryObjectWithDBName(EcbProductInfo.class, DataSource.SDK, sql, userid);
	}
	
	public static void main(String[] args) throws SQLException {
		EcbProductDao dao = new EcbProductDao();
		System.out.println(dao.queryEcb("201712251554356396931").getDiscntCode());
	}
}
