from lib.all import *

service_name = 'rev_period_data'
log_dir = '/app/log'
logger = Logger(service_name, log_dir=log_dir)


class S:
    def __init__(self):
        self.mysql_db_vas = Mysql(
            "rm-bp11t1616a1kjvmx5.mysql.rds.aliyuncs.com", 3306, "vas", "root", "Wishpal2024"
        )

    def __del__(self):
        self.mysql_db_vas.close()

    # 获取订单
    def get_orders(self):
        sql = '''
        select * from vas_order where ct<{} and order_status=1 and product_id='membership'
        '''.format(int(time.time()) - 86400 * 7)
        logger.Info("sql: {}".format(sql))
        docs = self.mysql_db_vas.query(sql)
        return docs

    # 结算一个订单
    @staticmethod
    def deal_one_order(order_id):
        url = "https://api.tiefen.fun/api/vas/deal_one_order"
        param = {
            "order_id": order_id
        }
        res = call_service(url, param)
        return res

    # 获取订单
    def get_coin_orders(self):
        sql = "select * from vas_coin_order where ct<{} and order_status=1 and product_id='membership'".format(int(time.time()) - 86400 * 7)
        logger.Info("get_coin_orders sql: {}".format(sql))
        docs = self.mysql_db_vas.query(sql)
        if not docs:
            return
        return

    def proc(self):
        logger.Info("==== start: {} ====".format(get_time_str_by_ts(int(time.time()))))
        logger.Info("==== start_order: {} ====".format(get_time_str_by_ts(int(time.time()))))
        orders = self.get_orders()
        logger.Info("len(orders): {}".format(len(orders)))
        idx = 0
        for order in orders:
            idx += 1
            order_id = safe_get_str(order, "id")
            res = self.deal_one_order(order_id)
            logger.Info("{}/{} deal_one_order, {}, {}, {}".format(idx, len(orders), order_id, res, order))
            return

        return


s = S()
s.get_orders()