package com.chinacreator.process.dao;

import com.chinacreator.process.bean.SDKOrderInfo;
import com.chinacreator.process.bean.SdkOrderBean;
import com.chinacreator.process.exception.ServiceException;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.SQLExecutor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

@Component
public class SdkOrderDao 
{

  public SdkOrderBean queryOrderList(String userid,String cpid,String productid)
  {
    String sql = "select id,productid,status,userid,to_char(ordertime,'yyyymmddhh24miss') ordertime,to_char(canceltime,'yyyymmddhh24miss') canceltime,orderchannel,cancelchannel from td_order_relations where cpid='youtu' and userid=? and cpid = ? and productid = ? order by id desc";
    PreparedDBUtil pdb = new PreparedDBUtil();
    try {
      pdb.preparedSelect(DataSource.SDK, sql);
      pdb.setString(1, userid);
      pdb.setString(2, cpid);
      pdb.setString(3, productid);
      return (SdkOrderBean)pdb.executePreparedForObject(SdkOrderBean.class);
    } catch (SQLException e) {
      throw new ServiceException("查询订购关系失败", e);
    }
  }

  public void order(SDKOrderInfo orderInfo, boolean isCancle)
  {
    String sql = "";
    if (!isCancle) {
      sql = "insert into td_order_relations(id,cpid,productId,appid,userid,ordertime,effectivetime,endtime,status,orderChannel,synccount,syncsucceed,type,sectionid,videoid,videoname,videourl,videotag,videoimage,appType,orderStatus,province,area,cachesucceed)values(?,?,?,?,?,to_date(?,'yyyymmddhh24miss'),sysdate,'','0',?,0,1,?,?,?,?,?,?,?,?,?,?,?,'1')";

      PreparedDBUtil pdb = new PreparedDBUtil();
      try {
        pdb.preparedInsert(DataSource.SDK, sql);
        pdb.setString(1, orderInfo.getId());
        pdb.setString(2, orderInfo.getCpid());
        pdb.setString(3, orderInfo.getProductId());
        pdb.setString(4, orderInfo.getAppid());
        pdb.setString(5, orderInfo.getUserid());
        pdb.setString(6, orderInfo.getOrdertimestr());
        pdb.setString(7, orderInfo.getOrderChannel());
        pdb.setInt(8, orderInfo.getType());
        pdb.setString(9, orderInfo.getSectionId());
        pdb.setString(10, orderInfo.getVideoId());
        pdb.setString(11, orderInfo.getVideoName());
        pdb.setString(12, orderInfo.getVideoUrl());
        pdb.setString(13, orderInfo.getVideoTag());
        pdb.setString(14, orderInfo.getVideoImage());
        pdb.setString(15, orderInfo.getAppType());
        pdb.setInt(16, orderInfo.getOrderStatus());
        pdb.setString(17, orderInfo.getProvince());
        pdb.setString(18, orderInfo.getArea());
        pdb.executePrepared();
      } catch (SQLException e) {
        throw new ServiceException("订购失败", e);
      }
    } else {
      sql = "update td_order_relations set canceltime='',endtime='',synccount=0,syncsucceed='1',lastsynctime='',orderChannel=#[orderChannel],status=#[status],appType=#[appType],ordertime=to_date(#[ordertimestr],'yyyymmddhh24miss'),effectivetime=sysdate,cancelChannel='',orderStatus=#[orderStatus],cachesucceed='1' where id=#[id]";
      try
      {
        SQLExecutor.updateBean(DataSource.SDK, sql, orderInfo);
      } catch (SQLException e) {
        throw new ServiceException("订购失败", e);
      }
    }
  }


  public void insertOrderLog(SDKOrderInfo orderInfo, String errorcode, String errorinfo, String ip) {
    String sql = "insert into tl_order_log (id,userid, cpid, productid,appid, type,channel, errorcode, errorinfo, inserttime, origin, ip,status,province,area,apptype) values (to_char(sysdate,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval, ?,?,?,?,?,?,?,?,sysdate,?,?,?,?,?,?)";

    PreparedDBUtil pdb = new PreparedDBUtil();
    String channel = null;
    if ("0".equals(orderInfo.getStatus()))
      channel = orderInfo.getOrderChannel();
    else
      channel = orderInfo.getCancelChannel();
    try
    {
      pdb.preparedInsert(DataSource.SDK, sql);
      pdb.setString(1, orderInfo.getUserid());
      pdb.setString(2, orderInfo.getCpid());
      pdb.setString(3, orderInfo.getProductId());
      pdb.setString(4, orderInfo.getAppid());
      pdb.setInt(5, orderInfo.getType());
      pdb.setString(6, channel);
      pdb.setString(7, errorcode);
      pdb.setString(8, errorinfo);
      pdb.setString(9, StringUtils.isEmpty(channel) ? orderInfo.getCpid() : channel);
      pdb.setString(10, ip);
      pdb.setString(11, orderInfo.getStatus());
      pdb.setString(12, orderInfo.getProvince());
      pdb.setString(13, orderInfo.getArea());
      pdb.setString(14, orderInfo.getAppType());
      pdb.executePrepared();
    } catch (SQLException e) {
      throw new ServiceException("保存订购日志失败", e);
    }
  }

  public void cancelOrder(SDKOrderInfo orderInfo) {
    String sql = "update td_order_relations set canceltime=to_date(#[canceltimestr],'yyyymmddhh24miss'),#if($endtime && !$endtime.equals(\"\")) endtime=to_date(#[endtimestr],'yyyymmddhh24miss') #else endtime=trunc(last_day(to_date(#[canceltimestr],'yyyymmddhh24miss')))+1-1/86400 #end,synccount=0,syncsucceed='1',orderStatus=#[orderStatus],cancelChannel=#[cancelChannel],status=#[status],appType=#[appType],cachesucceed='1' where cpid=#[cpid] #if($appid && !$appid.equals(\"\")) and appid=#[appid] #end and productId=#[productId] and userid=#[userid] and id=#[id]";
    try
    {
      SQLExecutor.updateBean(DataSource.SDK, sql, orderInfo);
    } catch (SQLException e) {
      throw new ServiceException("退订失败", e);
    }
  }
    public String getHmdInfo(String callerhead){
        PreparedDBUtil pdb = new PreparedDBUtil();
        String sql="select PROVINCE from TB_CALLER_AREACODE where callerhead = ?";
        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            pdb.setString(1, callerhead);
            return (String) pdb.executePreparedForObject(String.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public String getProvinceOrder(String spid){
        PreparedDBUtil pdb = new PreparedDBUtil();
        String sql="select  PROVINCE from  TB_PROVINCE_ORDER_CONF where spid=? and  STATUS='0'";
        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            pdb.setString(1, spid);
            return (String) pdb.executePreparedForObject(String.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }



}