package com.chinacreator.process.dao;

import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.SQLExecutor;
import org.springframework.stereotype.Component;

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

@Component
public class HuaweiWalletOrderDao {

    public List<HashMap> queryList() throws SQLException {
        String sql = " select id,cpid,spid,channel,userid,token,type from td_huawei_order_rec where status = '3' order by ordertime";
        List<HashMap> hashMaps = SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
        return  hashMaps;
    }

    public void updataQueryList(String id,String status) throws SQLException {
        String sql = "update td_huawei_order_rec set status = ?,updatetime = sysdate where id = ?";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql,status,id);
    }

    public String getPwd(String channel) throws SQLException {
        String sql = "select netpwd from tb_cp_account_config  where  cpid = ?";
        return SQLExecutor.queryFieldWithDBName("net3g", sql, new Object[]{channel});
    }

    public void updataQueryNotifydata(String id,String status,String result) throws SQLException {
        String sql = "update td_huawei_order_rec set status = ?,isnotify = '1',rechargetime = sysdate,result = ?,updatetime = sysdate where id = ?";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql,status,result,id);
    }

    public List<HashMap> queryNotifyList() throws SQLException {
        String sql = "select id,orderno,hwspid,to_char(ordertime,'yyyymmddhh24miss') ordertime,to_char(rechargetime,'yyyymmddhh24miss') rechargetime,status,userid,spid,productype,facevalue,notifyParam1,notifyParam2,notifyParam3,signtype,notifyurl,to_char(notifycount) notifycount,costprice,invoke,result from td_huawei_order_rec where isnotify = '1' or (isnotify <> '0' and isnotify <> '2' and notifycount < 5) order by rechargetime";
        List<HashMap> hashMaps = SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
        return  hashMaps;
    }

    public void updataQueryNotifyList(String id,String isnotify) throws SQLException {
        String sql = "update td_huawei_order_rec set isnotify = ?,updatetime = sysdate where id = ?";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql,isnotify,id);
    }

    public void updataQueryNotifyList(String id,String isnotify,int notifycount,String returnCode,String returnDesc) throws SQLException {
        String sql = "update td_huawei_order_rec set isnotify = ?,notifycount = ?,returnCode = ?,returnDesc = ?,updatetime = sysdate where id = ?";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql,isnotify,notifycount,returnCode,returnDesc,id);
    }

    public String getValue(String name) throws SQLException {
        String sql = "select value from TB_DICTIONRY_INFO where name = ?";
        return SQLExecutor.queryFieldWithDBName("net3g", sql, new Object[]{name});
    }

    /**
     * 获取执行状态为0且时间超过10分钟的数据
     * @return
     */
    public List<HashMap> getQueryListlong() throws SQLException {
        String sql = " select id,spid,userid from td_huawei_order_rec where status = '0' and  updatetime < sysdate-1/24/6";
        List<HashMap> hashMaps = SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
        return  hashMaps;
    }


    public String  queryOrder(String userid, String spid) throws SQLException {
        String sql = "select count(1) from td_order_relations  where  status = '0' and userid= ? and spid = ?";
        return SQLExecutor.queryFieldWithDBName("net3g", sql, new Object[]{userid,spid});
    }

    /**
     * 更新执行状态为2且时间超过10分钟的数据为待处理
     * @return
     */
    public int updExecTimeout(){
        int res = 0;
        String sql = "update td_huawei_order_rec set isnotify = '1' where isnotify = '2' and updatetime < sysdate-1/24/6";
        try {
            Object obj = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, null);
            res = (Integer)obj;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }
}
