package com.chinacreator.process.dao;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.chinacreator.process.bean.NetOrderBean;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.SQLExecutor;

@Component
public class FhGoodsHandleDao {
	/**
	 * 查询复合产品
	 * @param userid
	 * @param cpid
	 * @param spid
	 * @param activetype
	 * @param fhcpid
	 * @param fhspid
	 * @return
	 * @throws SQLException
	 */
	public OrderBean existUserByFh(String userid,String cpid,String spid,String activetype,String fhcpid,String fhspid) 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,cancelchannel,vipconstatus,to_char(vipcontime,'yyyymmddhh24miss')  vipcontime, vipconflag from TD_BUSSINESS_HANDLE  where userid =? and cpid =? and spid= ? and ACTIVETYPE =? and fhcpid =? and fhspid =? ";
		return  SQLExecutor.queryObjectWithDBName(OrderBean.class, DataSource.NET3G, sql, userid,cpid,spid,activetype,fhcpid,fhspid);
	}
	
	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,ischarge,flowstatus,activetype,vipstatus,canceltime,endtime,fhcpid,fhspid, vipconflag, cancelchannel)values( "
				+ "to_char(sysdate,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval, #[userid],#[cpid],#[spid],#[province],#[area], "
				+ "to_date(#[ordertime],'yyyymmddhh24miss'), sysdate, 0,sysdate, "
				+ "#[orderchannel],#[orderstatus],sysdate,#[orderstatus],#[flowstatus],#[activeType],2,to_date(#[canceltime],'yyyymmddhh24miss'),to_date(#[endtime],'yyyymmddhh24miss'),#[fhcpid],#[fhspid],#[vipconflag],#[cancelchannel])";
		SQLExecutor.insertBean(DataSource.NET3G, sql, bean);
	}
	
	  public List<NetOrderBean> findOrder(String cpid, String userid,String spid) throws SQLException
	  {
	    PreparedDBUtil pdb = new PreparedDBUtil();
	    String sql = "select userid, cpid, spid, province, area, orderchannel, cancelchannel, ordertime, endtime, canceltime,(case when canceltime is null then '0' when endtime >= sysdate then '1' else '2' end) status, ordertype,isexperience from TD_ORDER_RELATIONS where cpid=? and userid=? and spid=? order by ordertime desc";
	    pdb.preparedSelect(DataSource.NET3G, sql);
	    pdb.setString(1, cpid);
	    pdb.setString(2, userid);
	    pdb.setString(3, spid);
	    return  pdb.executePreparedForList(NetOrderBean.class);
	  }
	  
		public void cancelOrder(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= #[cancelchannel],"
					+ "orderstatus = 1  where id = #[id]";
			SQLExecutor.updateBean(DataSource.NET3G, sql,bean);
		}
		
		/**
		 * 是退订状态时再订购数据更新
		 * @param bean
		 * @throws SQLException
		 */
		public void UpdateOrder(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], vipconflag=#[vipconflag] where id=#[id]";
			SQLExecutor.updateBean(DataSource.NET3G, sql, bean);
		}
	  
	  public void updatestatus(String id) throws SQLException {
			String sql = "update TD_BUSSINESS_HANDLE set vipstatus=4   where id = ?";
			SQLExecutor.updateWithDBName(DataSource.NET3G, sql, id);
		}
}
