package com.chinacreator.process.dao;

import com.chinacreator.process.bean.OrderLog;
import com.chinacreator.process.bean.PointShopOrderBean;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.SQLExecutor;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

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

@Component
public class TaobDao {

    private Logger log = Logger.getLogger("taob");

    /**
     * 更新callBack状态
     *
     * @param id
     * @return
     * @throws SQLException
     */

    public void updParamsByCb(String rspParams, String id) throws SQLException {
        String sql = "UPDATE TD_TAOBAO_ACTIVITY_DATA SET CALLBACKRES = ?, CALLBACKTIMES = SYSDATE WHERE id = ?";
        Object obj = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, rspParams, id);
    }

    /**
     * 根据orderId和orderNo查询订购信息
     *
     * @param id
     * @return
     * @throws SQLException
     */
    public PointShopOrderBean getOrderRec(String userid,String cpid,String spid) throws SQLException {
        String sql = "SELECT * FROM TD_ORDER_RELATIONS WHERE USERID = ? AND CPID=? AND SPID=?";
        return SQLExecutor.queryObjectWithDBName(PointShopOrderBean.class, DataSource.NET3G, sql, userid,cpid,spid);
    }


    /**
     * 获取状态
     *
     * @return
     * @throws SQLException
     */
    public String getBackVipstatus(String id) throws SQLException {
        String sql = "SELECT VIPSTATUS FROM TD_BACKBUSI_ORDER_REC WHERE ID = ? ";
        return SQLExecutor.queryObjectWithDBName(String.class, DataSource.NET3G, sql, id);
    }

    /**
     * 获取处理结果
     *
     * @param orderId
     * @return
     * @throws SQLException
     */
    public String getResultCode(String orderId) throws SQLException {
        String sql = "SELECT RESULTCODE FROM TD_POINTS_ORDER_REC WHERE ID = ? ";
        return SQLExecutor.queryObjectWithDBName(String.class, DataSource.NET3G, sql, orderId);
    }


    /**
     * 更新业务状态信息
     *
     * @param resultCode
     * @param resultInfo
     * @param id
     * @param tabname
     * @return
     * @throws SQLException
     */
    public boolean updBusiStatus(String resultCode, String resultInfo, String id, String tabname) throws SQLException {
        //赠送会员状态，0成功，1未赠送，2赠送中，3失败，4不赠送
        String sql = " UPDATE " + tabname + " SET RESULTCODE = ?, RESULTINFO = ? WHERE ID = ? ";
        Object obj = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultCode, resultInfo, id);
        return ((Integer) obj) > 0 ? true : false;
    }


    /**
     * 获取业务配置信息
     *
     * @param cpid
     * @param spid
     * @return
     * @throws SQLException
     */
    public List<HashMap> getBackBusiConf(String cpid, String spid) throws SQLException {
        String sql = "SELECT * FROM TB_BACKBUSI_CONF WHERE STATUS = '0' AND CPID = ? AND SPID = ? ";
        return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, cpid, spid);
    }

    public static void main(String[] args) throws SQLException {
        TaobDao dao = new TaobDao();
        //System.out.println(dao.findSpInfo("1167"));
        //System.out.println(dao.getPwdByGoodsCode("pointshop130"));
        //System.out.println(dao.getOrderRec("201906211723268662899","d4078706-2ff3-4f1b-9aad-80436a294b22"));
    }


    /*******************************20210127淘宝异步处理开始*****************************/

    /**
     * 查询要异步的数据
     *
     * @param
     * @return
     */
    public List<HashMap> getAsynData() {
        //处理状态为1的数据（待处理）
        String sql = " SELECT * FROM (SELECT ID, USERID, ORDERID,CHANNEL, CPID, SPID, TYPE,resultcode FROM " +
                "TD_TAOBAO_ACTIVITY_DATA WHERE resultcode = '1'  ORDER BY INSERTTIME) WHERE ROWNUM < 1000 ";
        PreparedDBUtil pdb = new PreparedDBUtil();
        try {
            pdb.preparedSelect(DataSource.NET3G, sql);
            return pdb.executePreparedForList(HashMap.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新业务处理状态
     *
     * @param reesultCode
     * @param resultInfo
     * @param id
     * @throws SQLException
     */
    public void updSendStatus(String reesultCode, String resultInfo, String id) throws SQLException {
        String sql = " UPDATE TD_TAOBAO_ACTIVITY_DATA SET  RESULTCODE = ?, RESULTINFO = ? WHERE ID = ? ";
        Object obj = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, reesultCode, resultInfo, id);
    }

    /**
     * 更新赠送会员状态
     *
     * @param bean
     * @param tabname
     * @return
     * @throws SQLException
     */
    public boolean upStatus(PointShopOrderBean bean) throws SQLException {
        //办理结果编码，0成功，1待处理，2处理中，其他为异常
        String sql = " UPDATE  TD_TAOBAO_ACTIVITY_DATA   SET resultcode = ? ,  RESULTINFO = ?, RETRYTIME = sysdate WHERE ID = ? ";
        Object obj = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, bean.getResultCode(), bean.getResultInfo(), bean.getId());
        return ((Integer) obj) > 0 ? true : false;
    }

    public String getPwd(String cpid, String spid) throws SQLException {
        String sql = "SELECT NETPWD FROM  TB_CP_ACCOUNT_CONFIG a, tb_sp_info b where a.cpid=b.cpid and b.cpid=? and b.spid=?";
        return SQLExecutor.queryObjectWithDBName(String.class, DataSource.NET3G, sql, cpid, spid);
    }

    /*******************************20210127淘宝异步处理结束*****************************/
}
