package com.chinacreator.process.dao;

import com.chinacreator.process.bean.EcbProductInfo;
import com.chinacreator.process.bean.OrderConfigBean;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.SQLExecutor;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
public class KcOrderDao {

    public List<HashMap> queryList() throws SQLException {
        String sql = " select id,type,usermob,productid,to_char(ordertime,'yyyymmddhh24miss') ordertime, to_char(canceltime,'yyyymmddhh24miss') canceltime, to_char(endtime,'yyyymmddhh24miss') endtime,channel from TD_KC_ORDER_WAIT where resultcode = '1'  and rownum <=500 order by id";
        List<HashMap> hashMaps = SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
        return  hashMaps;
    }

    public void updataQueryList(String id,String resultcode) throws SQLException {
        String sql = "update TD_KC_ORDER_WAIT set resultcode = ?,updatetime = sysdate where id = ?";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql,resultcode,id);
    }
    public void updataQueryList(String id,String resultcode,String errorinfo) throws SQLException {
        String sql = "update TD_KC_ORDER_WAIT set resultcode = ?,errorinfo = ?,updatetime = sysdate where id = ?";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql,resultcode,errorinfo,id);
    }
    public int updatalongdata() throws SQLException {
        String sql = "update TD_KC_ORDER_WAIT set resultcode = '1',errorinfo = '超时',updatetime = sysdate where resultcode = '2' and updatetime < sysdate-1/24/30";
        Object res = SQLExecutor.updateWithDBName(DataSource.NET3G, sql);
        return (Integer)res;
    }

    public HashMap getCpSp(String productid) throws SQLException {
        String sql = " select cpid,spid,authstatus from tb_intensive_productid_conf where productid = ?";
        HashMap hashMaps = SQLExecutor.queryObjectWithDBName(HashMap.class, DataSource.NET3G, sql,productid);
        return  hashMaps;
    }
    public HashMap getCpSp2(String productid) throws SQLException {
        String sql = " select cpid,spid from tb_sp_info where spid = ?";
        HashMap hashMaps = SQLExecutor.queryObjectWithDBName(HashMap.class, DataSource.NET3G, sql,productid);
        return  hashMaps;
    }

    public int isOrder(String cpid, String spid, String userid) {
        int isOrder = 1;
        PreparedDBUtil pdb = new PreparedDBUtil();
        String sql = "select count(*) from TD_ORDER_RELATIONS where cpid=? and spid=? and userid=? and status='0'";
        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            pdb.setString(1, cpid);
            pdb.setString(2, spid);
            pdb.setString(3, userid);
            pdb.executePrepared();
            if (pdb.getInt(0, 0) > 0)
                isOrder = 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isOrder;
    }

    /**
     * 特殊复合产品结束时间
     * @return
     */

    public Date getLastYearDay(Date time){
        PreparedDBUtil pdb = new PreparedDBUtil();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql = "select TO_DATE(TO_CHAR(trunc(add_months(?,13),'MM') - 1/24/60/60,'yyyyMMddhh24miss'), 'yyyyMMddhh24miss') from dual";

        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            pdb.setDate(1,time);
            String endtime = pdb.executePreparedForList(String.class).get(0);
            Date d = sdf.parse(endtime);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 特殊复合产品结束时间
     * @return
     */

    public Date getLastYearDayFirst(Date time){
        PreparedDBUtil pdb = new PreparedDBUtil();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql = "select TO_DATE(TO_CHAR(trunc(add_months(?,12),'MM') - 1/24/60/60,'yyyyMMddhh24miss'), 'yyyyMMddhh24miss') from dual";

        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            pdb.setDate(1,time);
            String endtime = pdb.executePreparedForList(String.class).get(0);
            Date d = sdf.parse(endtime);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //获得券码活动配置
    public  HashMap getCouponConfig(String spid ){
        PreparedDBUtil pdb = new PreparedDBUtil();
        String sql = "SELECT ID, CPID,SPID,TO_CHAR(STARTTIME,'YYYYMMDDHH24MISS') STARTTIME, " +
                "TO_CHAR(ENDTIME,'YYYYMMDDHH24MISS') ENDTIME,ACTIVITYCODE,COUPONTYPE,REMARK,CHANNEL,EXTEND1 " +
                " from TB_ACPRODUCT_CONFIG where sysdate between STARTTIME and  ENDTIME and status = '0' and activitycode = 'telecomActivity' and spid = ? ";
        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            pdb.setString(1,spid);
//            return pdb.executePreparedForList(HashMap.class)==null?null:pdb.executePreparedForList(HashMap.class).get(0);
            List<HashMap> list = pdb.executePreparedForList(HashMap.class);
            if (list != null && list.size() > 0) {
                HashMap map = list.get(0);
                return map;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Date getFhcpEndtime(String userid,String cpid,String spid){
        PreparedDBUtil pdb = new PreparedDBUtil();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql = "select endtime from TD_ORDER_RELATIONS where userid = ? and cpid = ? and spid = ?";
        try {
            String endtime = SQLExecutor.queryFieldWithDBName(DataSource.NET3G, sql, new Object[] { userid, cpid, spid });
            Date d = sdf.parse(endtime);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*采集需要订购后再下发其他短信的产品*/
    public  List<String> querySendSmsProduct(){
        PreparedDBUtil pdb = new PreparedDBUtil();
        String sql = "select spid from tb_sendsms_product where status = '0' ";
        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            return pdb.executePreparedForList(String.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public OrderConfigBean findBySpid(String spid){
        PreparedDBUtil pdb = new PreparedDBUtil();
        String sql = "select cpid,spid,productid vacproductid,authstatus type from tb_intensive_productid_conf where spid = ?";
        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            pdb.setString(1, spid);
            return (OrderConfigBean) pdb.executePreparedForObject(OrderConfigBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean getBack(String spid){
        PreparedDBUtil pdb = new PreparedDBUtil();
        String sql = "select count(1) from tb_backbusi_conf where spid = ? and status = '0'";
        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            pdb.setString(1, spid);
            return ((Integer)pdb.executePreparedForObject(Integer.class)>0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public EcbProductInfo queryEcb(String userid,String productId) throws SQLException{
        String sql = "select productId,productName,DISCNTPACKAGEID packageCode,DISCNTPACKAGENAME packageName,discntCode,discntName, to_char(discntStartDate,'yyyymmddhh24miss') discntStartDate, to_char(discntEndDate,'yyyymmddhh24miss') discntEndDate,discntOrderState,DICNTVALUE discntValue from wosdk.TD_PRODUCT_ORDERSYN  where serialnumber=? and productId = ?";
        return SQLExecutor.queryObjectWithDBName(EcbProductInfo.class, DataSource.NET3G, sql,userid,productId);
    }
}
