package com.chinacreator.process.dao;

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

import java.sql.SQLException;

import org.springframework.stereotype.Component;

@Component
public class NetOrderDao
{

  public void order(NetOrderBean order)
    throws Exception
  {
    String sql = "merge into td_order_relations a using (select #[cpid] cpid, #[spid] spid, #[userid] userid from dual) b "
    		+ "on(a.cpid = b.cpid and a.spid = b.spid and a.userid = b.userid) "
    		+ "when matched then update set ordertime=#if($ordertime && !$ordertime.equals(\"\")) to_date(#[ordertimestr], 'yyyymmddhh24miss') #else sysdate #end,"
    		+ "effecttime=#if($effecttime && !$effecttime.equals(\"\")) to_date(#[effecttimestr], 'yyyymmddhh24miss') #elseif($ordertime) to_date(#[ordertimestr], 'yyyymmddhh24miss')  #else sysdate #end, "
    		+ "canceltime='',#if($endtime && !$endtime.equals(\"\"))endtime=to_date(#[endtimestr], 'yyyymmddhh24miss') #else endtime='' #end,"
    		+ "status=#[status],synccount=0,syncsucceed='1',lastsynctime='',issms='1',"
    		+ "orderchannel=#[orderchannel],fromurl=#[fromurl],type=#[type],orderstatus=#[orderstatus],"
    		+ "ordertype=#[ordertype],apptype=#[apptype],isexperience=#[isexperience],ordervaccode=#[ordervaccode],"
    		+ "videoid=#[videoid],videourl=#[videourl],videoname=#[videoname],videotag=#[videotag],videosize=#[videosize],"
    		+ "cachesucceed=1,showstatus=#[showstatus],batchid=#[batchid],orderchannel2=#[channel2],orderstaffid=#[staffid],orderdepartid=#[departid] when not matched then insert (id,cpid,spid,userid,ordertime,"
    		+ "effecttime,endtime,status,province,area,orderchannel,synccount,syncsucceed,fromurl,type,orderstatus,ordertype,apptype,"
    		+ "isexperience,videoid,videoname,videourl, videotag, videosize,ordervaccode,cachesucceed, showstatus,batchid,orderchannel2,orderstaffid,orderdepartid)"
    		+ "values( to_char(sysdate,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval,#[cpid],#[spid],#[userid],"
    		+ "#if($ordertime && !$ordertime.equals(\"\")) to_date(#[ordertimestr], 'yyyymmddhh24miss') #else sysdate #end,"
    		+ "#if($effecttime && !$effecttime.equals(\"\")) to_date(#[effecttimestr], 'yyyymmddhh24miss') #elseif($ordertime) to_date(#[ordertimestr], 'yyyymmddhh24miss') #else sysdate #end, "
    		+ "#if($endtime && !$endtime.equals(\"\"))to_date(#[endtimestr], 'yyyymmddhh24miss') #else '' #end,#[status],#[province],#[area],"
    		+ "#[orderchannel],0,1,#[fromurl],#[type],#[orderstatus],#[ordertype],#[apptype],#[isexperience],"
    		+ "#[videoid],#[videoname],#[videourl],#[videotag],#[videosize],#[ordervaccode],1, #[showstatus],#[batchid],#[channel2],#[staffid],#[departid])";

    SQLExecutor.insertBean(DataSource.NET3G, sql, order);
  }

  public void orderUpdate(NetOrderBean order) throws SQLException {
	  String sql = "update td_order_relations "
			  + "set orderchannel=#[orderchannel],smstime = sysdate "
			  + "where userid=#[userid] and cpid=#[cpid] and spid=#[spid] ";
	  SQLExecutor.updateBean(DataSource.NET3G, sql, order);
	}
  public void cancelUpdate(NetOrderBean order) throws SQLException {
	  String sql = "update td_order_relations "
			  + "set cancelchannel=#[cancelchannel] ,smstime = sysdate "
			  + "where userid=#[userid] and cpid=#[cpid] and spid=#[spid] ";
	  SQLExecutor.updateBean(DataSource.NET3G, sql, order);
  }

  public NetOrderBean findOrder(String cpid, String spid, String userid) throws SQLException
  {
    PreparedDBUtil pdb = new PreparedDBUtil();
    String sql = "select userid, cpid, spid, province, area, orderchannel, cancelchannel, ordertime, endtime, canceltime,(case when endtime is null then '0' when endtime >= sysdate then '1' else '2' end) status, ordertype,isexperience from TD_ORDER_RELATIONS where cpid=? and spid=? and userid=? order by ordertime desc";
    pdb.preparedSelect(DataSource.NET3G, sql);
    pdb.setString(1, cpid);
    pdb.setString(2, spid);
    pdb.setString(3, userid);
    return (NetOrderBean)pdb.executePreparedForObject(NetOrderBean.class);
  }

  public void cancelOrder(NetOrderBean order)
    throws Exception
  {
    String sql = "merge into td_order_relations a using (select #[cpid] cpid, #[spid] spid, #[userid] userid from dual) b on (a.userid = b.userid and a.cpid = b.cpid and a.spid = b.spid and a.status in ('0', '1')) when matched then update set canceltime=#if($canceltime && !$canceltime.equals(\"\")) to_date(#[canceltimestr], 'yyyymmddhh24miss') #else sysdate #end, endtime=#if($endtime && !$endtime.equals(\"\")) to_date(#[endtimestr], 'yyyymmddhh24miss') #elseif($canceltime && !$canceltime.equals(\"\")) trunc(last_day(to_date(#[canceltimestr], 'yyyymmddhh24miss')))+1-1/86400 #else trunc(last_day(sysdate))+1-1/86400 #end, status='1', synccount=0,syncsucceed='1',lastsynctime='',issms='1', cancelchannel=#[cancelchannel], orderstatus=#[orderstatus], type=#[type],cachesucceed=1,batchid=#[batchid],cancelchannel2=#[channel2],cancelstaffid=#[staffid],canceldepartid=#[departid] when not matched then insert (id, cpid,spid,userid,ordertime,effecttime,canceltime,endtime,status,province,area,cancelchannel,synccount,syncsucceed,type,orderstatus,ordertype,cachesucceed,batchid,cancelchannel2,cancelstaffid,canceldepartid) values ( to_char(sysdate,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval, #[cpid], #[spid], #[userid], #if($ordertime) to_date(#[ordertimestr], 'yyyymmddhh24miss') #elseif($canceltime) to_date(#[canceltimestr], 'yyyymmddhh24miss') #else sysdate  #end,#if($effecttime) to_date(#[effecttimestr], 'yyyymmddhh24miss') #elseif($canceltime) to_date(#[canceltimestr], 'yyyymmddhh24miss') #else sysdate  #end, #if($canceltime) to_date(#[canceltimestr], 'yyyymmddhh24miss') #else sysdate #end, #if($endtime) to_date(#[endtimestr], 'yyyymmddhh24miss') #elseif($canceltime) trunc(last_day(to_date(#[canceltimestr], 'yyyymmddhh24miss')))+1-1/86400 #else trunc(last_day(sysdate))+1-1/86400 #end, '1', #[province], #[area], #[cancelchannel], 0, 1, #[type], #[orderstatus], #[ordertype],1,#[batchid],#[channel2],#[staffid],#[departid])";
    SQLExecutor.insertBean(DataSource.NET3G, sql, order);
  }

  public void addOrderLog(OrderLog orderLog) throws SQLException {
	  String sql = "insert into tl_order_log (id, userid, province, area, apptype, useragent, origin, "
			+"channel, cpid, spid, errorcode, errorinfo, status, orderstatus, ordertype, inserttime, isexperience) values ( "
			+"to_char(sysdate, 'yyyymmddhh24miss') || SEQ_COMMON6.nextval,"
			+"#[userid], #[province], #[area], #[apptype], #[useragent], #[origin], #[channel], #[cpid],"
			+"#[spid], #[errorcode], #[errorinfo], #[status], #[orderstatus], #[ordertype],"
			+"sysdate, #[isexperience])";
	  SQLExecutor.insertBean(DataSource.NET3G, sql, orderLog);
	}



}
