package com.chinacreator.process.dao;

import java.sql.SQLException;
import org.springframework.stereotype.Component;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.SQLExecutor;

@Component
public class VacActiveDao {

	public OrderBean existUser(String userid, String cpid, String spid) throws SQLException {
		String sql = "select vipstatus,to_char(viptime,'yyyymmddhh24miss') viptime,userid,cpid,spid,orderstatus ,id,to_char(ordertime,'yyyymmddhh24miss') ordertime,to_char(canceltime,'yyyymmddhh24miss') canceltime,to_char(endtime,'yyyymmddhh24miss') endtime,orderchannel channel  from TD_BUSSINESS_HANDLE  where userid =? and cpid =? and spid=? and ACTIVETYPE=6 and fhcpid is null and fhspid is null";
		return SQLExecutor.queryObjectWithDBName(OrderBean.class, DataSource.NET3G, sql, userid, cpid, spid);
	}
	
	public OrderBean existUser(String userid, String cpid, String spid,String activitytype) throws SQLException {
		String sql = "select vipstatus,to_char(viptime,'yyyymmddhh24miss') viptime,userid,cpid,spid,orderstatus ,id,to_char(ordertime,'yyyymmddhh24miss') ordertime,to_char(canceltime,'yyyymmddhh24miss') canceltime,to_char(endtime,'yyyymmddhh24miss') endtime,orderchannel channel  from TD_BUSSINESS_HANDLE  where userid =? and cpid =? and spid=? and ACTIVETYPE=? and fhcpid is null and fhspid is null";
		return SQLExecutor.queryObjectWithDBName(OrderBean.class, DataSource.NET3G, sql, userid, cpid, spid,activitytype);
	}

	public void insertBeans(OrderBean bean) throws SQLException {
		String sql =  "insert into  TD_BUSSINESS_HANDLE(id,userid,cpid,spid,province,area,ordertime,effectivetime,activitystatus,activitytime,orderchannel,orderstatus,inserttime,endtime,ischarge,flowstatus,activetype,vipstatus)values( "
				+ "to_char(sysdate,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval, "
				+ "#[userid],#[cpid],#[spid],#[province],#[area], "
				+ "to_date(#[ordertime],'yyyymmddhh24miss'), "
				+ "sysdate,  "
				+ "0,sysdate, "
				+ "#[channel],0,sysdate,'',2,8,#[activeType],2)";
		SQLExecutor.insertBean(DataSource.NET3G, sql, bean);
	}

	public void update(OrderBean bean) throws SQLException {
		String sql = "update TD_BUSSINESS_HANDLE set ordertime = to_date(#[ordertime],'yyyymmddhh24miss'),canceltime ='',endtime='',orderchannel= #[channel],"
				+ "orderstatus = 0 ,#if(!$viptime.substring(0,6).equals($ordertime.substring(0,6)))  vipstatus=2,  viptime='' #else vipstatus=#[vipstatus],viptime=to_date(#[viptime],'yyyymmddhh24miss')  #end where id = #[id]";
		SQLExecutor.updateBean(DataSource.NET3G, sql, bean);
	}
	
	public void cancelUpdate(OrderBean bean) throws SQLException {
		String sql = "update TD_BUSSINESS_HANDLE set  canceltime =to_date(#[canceltime],'yyyymmddhh24miss'),endtime=last_day(trunc(to_date(#[canceltime],'yyyymmddhh24miss')))+1-1/(24*60*60),cancelchannel= #[channel],"
				+ "orderstatus =1  where id = #[id]";
		SQLExecutor.updateBean(DataSource.NET3G, sql, bean);
	}
}
