package com.cnd3b.service;

import com.cnd3b.common.BaseClass;
import com.cnd3b.common.data.Row;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.db.DBConnect;
import com.cnd3b.common.data.db.SQLiteJDBC;
import com.cnd3b.utility.wechatpay.apppay.apppay;
import com.cnd3b.utility.wechatpay.nativepay.nativepay;

/**
 * 微信订单自动查询订单状态
 */
public class WechatPayAutoQuery extends BaseClass implements Runnable {

    @Override
    public void run() {
        try {
            if (new SQLiteJDBC().checkServicesUsed("WechatPayAutoQuery",false)) {
                APP();
                NATIVE();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void APP() {
        DBConnect dbConnect = new DBConnect();
        Rows rows = dbConnect.runSqlQuery("select*from twechatpayorder where trade_type='APP' and (isnull(return_code,'')!='SUCCESS' or isnull(result_code,'')!='SUCCESS' or isnull(trade_state,'')!='SUCCESS' ) and DATEADD(MINUTE,30,createdate)>GETDATE() and DATEADD(SECOND,30,createdate)<GETDATE()");
        for (Row row : rows) {
            apppay apppay = new apppay();
            String out_trade_no=row.getString("out_trade_no");
            apppay.queryOrder(out_trade_no);
        }
    }

    public void NATIVE() throws Exception {
        DBConnect dbConnect = new DBConnect();
        Rows rows = dbConnect.runSqlQuery("select*from twechatpayorder where trade_type='NATIVE' and isnull(trade_state,'')!='SUCCESS' and DATEADD(MINUTE,30,createdate)>GETDATE() and DATEADD(SECOND,30,createdate)<GETDATE()");
        nativepay nativepay = new nativepay();
        nativepay.setup();
        for (Row row : rows) {
            String out_trade_no=row.getString("out_trade_no");
            nativepay.queryOrder(out_trade_no);
        }
        nativepay.after();
    }
}
