package com.cnd3b.service;

import com.cnd3b.common.BaseClass;
import com.cnd3b.common.data.SQLFactory;
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.common.parameter.parameter;

import java.util.ArrayList;

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

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

    /**
     * 账号付款到期
     */
    public void accountExpiration() {
        DBConnect dbConnect = new DBConnect();
        SQLFactory factory = new SQLFactory(this, "失效账号查询");
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        for (Row row : rows) {
            long userid = row.getLong("userid");
            if (parameter.userIdList.containsKey(userid)) {
                String token = parameter.userIdList.get(userid).getString("token");
                parameter.tokenlist.remove(token);
                parameter.userIdList.remove(userid);
                dbConnect.runSqlUpdate("update tenterprise_users set accesstoken=null where tenterprise_userid='" + userid + "'");
            }
        }
    }
}
