package com.chinacreator.process.dao;

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

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 ActiveHandleDao {

	public OrderBean existUser(String userid,String cpid,String spid,String activetype) throws SQLException{
		String sql = "select userid,cpid,spid,orderstatus ordertype,id,to_char(ordertime,'yyyymmddhh24miss') updatetime,usertype,to_char(oldcanceltime,'yyyymmddhh24miss') oldcanceltime,orderchannel  channel,ischarge, "
				+ "to_char(endtime,'yyyymm') endtime,activetype,vipstatus 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,activetype);
	}
	
	public List<OrderBean> existUserBestv(String userid,String cpid,String spid) throws SQLException{
		String sql = "select userid,cpid,spid,orderstatus ordertype,id,to_char(ordertime,'yyyymmddhh24miss') updatetime,usertype,to_char(oldcanceltime,'yyyymmddhh24miss') oldcanceltime,orderchannel  channel,ischarge, "
				+ "to_char(endtime,'yyyymmddhh24miss') endtime,activetype,vipstatus from TD_BUSSINESS_HANDLE where userid = ? and cpid = ? and spid = ? and fhcpid is null and fhspid is null";
		return  SQLExecutor.queryListWithDBName(OrderBean.class, DataSource.NET3G, sql, userid,cpid,spid);
	}
	
	public void insertBeans(OrderBean bean) throws SQLException{
		String sql = "insert into TD_BUSSINESS_HANDLE(id,userid,cpid,spid,province,area,ordertime,activitystatus,orderchannel,"
				+ "orderstatus,inserttime,endtime,activitytime,ischarge,effectivetime,flowstatus,activetype,vipstatus) values(to_char(sysdate,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval,#[userid],#[cpid],#[spid],#[province],"
				+ "#[area],to_date(#[updateTime],'yyyymmddhh24miss'),0,#[channel],0,sysdate,#if($endtime && !$endtime.equals(\"\")) to_date(#[endtime], 'yyyymmddhh24miss') #else '' #end,"
				+ "sysdate,'2',to_date(#[updateTime],'yyyymmddhh24miss'),#[flowstatus],#[activeType],#[vipstatus])";
		SQLExecutor.insertBean(DataSource.NET3G, sql, bean);
	}
	
	public void UpdateRelations(OrderBean bean) throws SQLException{
		String sql =  "update TD_BUSSINESS_HANDLE set canceltime='', ordertime=to_date(#[updateTime],'yyyymmddhh24miss'),"
				+ "endtime=to_date('20171130235959','yyyymmddhh24miss'),orderstatus=0,effectivetime=to_date(#[updateTime],'yyyymmddhh24miss'),orderchannel=#[channel] where id=#[id]";
		SQLExecutor.updateBean(DataSource.NET3G, sql, bean);
	}
	
	public void UpdateRelationsBestv(OrderBean bean) throws SQLException{
		String sql =  "update TD_BUSSINESS_HANDLE set canceltime='', ordertime=to_date(#[updateTime],'yyyymmddhh24miss'),"
				+ "endtime='',orderstatus=0,effectivetime=to_date(#[updateTime],'yyyymmddhh24miss'),orderchannel=#[channel],vipstatus=#[vipstatus] where id=#[id]";
		SQLExecutor.updateBean(DataSource.NET3G, sql, bean);
	}
	
	public void cancelOrder(OrderBean bean) throws SQLException{
		String sql = "update TD_BUSSINESS_HANDLE set canceltime = to_date(#[updateTime],'yyyymmddhh24miss'),endtime = decode(sign(to_date(#[updateTime],'yyyymmddhh24miss')-endtime),1,endtime,last_day(trunc(to_date(#[updateTime],'yyyymmddhh24miss')))+1-1/(24*60*60)),cancelchannel= #[channel],"
				+ "orderstatus = 1  where id = #[id]";
		SQLExecutor.updateBean(DataSource.NET3G, sql,bean);
	}
	
	public void cancelOrderBestv(OrderBean bean) throws SQLException{
		String sql = "update TD_BUSSINESS_HANDLE set canceltime = to_date(#[updateTime],'yyyymmddhh24miss'),endtime = LAST_DAY (TRUNC (to_date(#[updateTime],'yyyymmddhh24miss')))+1-1/(24 * 60 * 60),cancelchannel= #[channel],"
				+ "orderstatus = 1  where id = #[id]";
		SQLExecutor.updateBean(DataSource.NET3G, sql,bean);
	}
	
	
	public void updateChannel(String id,String channel) throws SQLException{
		String sql = "update TD_BUSSINESS_HANDLE set orderchannel = ? where id = ?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql, channel,id);
	}
	
	public void updateCharge(String id) throws SQLException{
		String sql = "update TD_BUSSINESS_HANDLE set ISCHARGE = 2 where id = ?";
		SQLExecutor.updateWithDBName(DataSource.NET3G, sql,id);
	}
}
