package com.chinacreator.process.dao;

import org.springframework.stereotype.*;
import com.chinacreator.process.bean.*;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.*;
import java.sql.*;

@Component
public class ActiveDao
{
    public OrderBean existUser(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 from TD_BUSSINESS_HANDLE where userid = ? and cpid = ? and spid = ?";
        return SQLExecutor.queryObjectWithDBName(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) values(to_char(sysdate,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval,#[userid],#[cpid],#[spid],#[province],#[area],to_date(#[updateTime],'yyyymmddhh24miss'),#[activated],#[channel],0,sysdate,last_day(trunc(add_months(to_date(#[updateTime],'yyyymmddhh24miss'),2)))+1-1/(24*60*60),#if($activated.equals('2')) '' #else sysdate #end,#[ischarge],to_date(#[updateTime],'yyyymmddhh24miss'),#[flowstatus])";
        SQLExecutor.insertBean(DataSource.NET3G, sql, bean);
    }
    
    public void insertOldUserBeans(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,usertype,cancelchannel,oldcanceltime,flowstatus,vipstatus) values(to_char(sysdate,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval,#[userid],#[cpid],#[spid],#[province],#[area],'',2,'',3,sysdate,'','',#[ischarge],2,#[channel],to_date(#[updateTime],'yyyymmddhh24miss'),7,3)";
        SQLExecutor.insertBean(DataSource.NET3G, sql, bean);
    }
    
    public void updateOldUser(OrderBean bean) throws SQLException {
        String sql = "update TD_BUSSINESS_HANDLE set canceltime='',ordertime=to_date(#[updateTime],'yyyymmddhh24miss'),endtime=#if($flowstatus.equals('5')) last_day(trunc(add_months(to_date(#[updateTime],'yyyymmddhh24miss'),3)))+1-1/(24*60*60) #else last_day(trunc(add_months(to_date(#[updateTime],'yyyymmddhh24miss'),2)))+1-1/(24*60*60) #end,orderchannel=#[channel],orderstatus=0,activitystatus=#[activated],flowstatus=#[flowstatus] ,vipstatus=#[vipstatus],ischarge=#[ischarge],activitytime=#if($activated.equals('2')) '' #else sysdate #end,province=#[province],area=#[area], effectivetime = #if($updateTime.substring(0,6).equals($oldcanceltime.substring(0,6)))last_day(trunc(to_date($updateTime,'yyyymmddhh24miss')))+1+(1/12) #else to_date($updateTime,'yyyymmddhh24miss') #end 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 = #if($usertype.equals('2') && $updateTime.substring(0,6).equals($ordertime.substring(0,6)) && $updateTime.substring(0,6).equals($oldcanceltime.substring(0,6))) 4 #else 1 #end 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);
    }
    
    public static void main(String[] args) throws SQLException {
        final OrderBean bean = new OrderBean();
        bean.setId("20160811083744213209");
        bean.setUserid("18574414678");
        bean.setProvince("\u6e56\u5357");
        bean.setArea("\u957f\u6c99");
        bean.setActivated("2");
        bean.setChannel("khd");
        bean.setSpid("6");
        bean.setCpid("tencent");
        bean.setActivated("2");
        bean.setUsertype("2");
        bean.setUpdateTime("20161102235959");
        bean.setOrdertime("20160828235959");
        final ActiveDao dao = new ActiveDao();
        dao.cancelOrder(bean);
    }
}