78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
import time
|
|
|
|
from lib.all import *
|
|
import datetime
|
|
|
|
|
|
# 流水同比
|
|
|
|
class S:
|
|
def __init__(self):
|
|
self.mysql_db_vas = Mysql(
|
|
"rm-bp11t1616a1kjvmx5.mysql.rds.aliyuncs.com", 3306, "vas", "root", "Wishpal2024"
|
|
)
|
|
self.mysql_db_bi = Mysql(
|
|
"172.31.37.71", 3306, "bi", "root", "Wishpal@2023"
|
|
)
|
|
|
|
def __del__(self):
|
|
self.mysql_db_vas.close()
|
|
self.mysql_db_bi.close()
|
|
|
|
def get_last_ptime(self):
|
|
sql = '''select * from vas_yoy_flow order by ptime desc limit 1'''
|
|
rows = self.mysql_db_bi.query(sql)
|
|
if len(rows) > 0:
|
|
dt = rows[0].get("ptime")
|
|
return int(dt.timestamp())
|
|
return 0
|
|
|
|
def get_flow(self, st, et):
|
|
sql = '''select sum(pay_amount) flow from vas_order where ct>={} and ct<{} and order_status in (1,2);'''.format(st, et)
|
|
rows = self.mysql_db_vas.query(sql)
|
|
if len(rows) > 0:
|
|
return safe_get_int(rows[0], "flow")
|
|
return 0
|
|
|
|
def insert_flow(self, ptime, flow):
|
|
sql = '''insert into vas_yoy_flow (ptime, flow) values (%s,%s)'''
|
|
return self.mysql_db_bi.exec(sql, (ptime, flow))
|
|
|
|
def update_day_flow(self, flow):
|
|
t = get_today_zero_time()
|
|
dt = datetime.datetime.fromtimestamp(t)
|
|
formatted_dt = dt.strftime('%Y-%m-%d 00:00:00')
|
|
|
|
sql_q = "select * from vas_day_flow where pdate='{}'".format(get_time_str_by_ts(t))
|
|
rows = self.mysql_db_bi.query(sql_q)
|
|
if len(rows) > 0:
|
|
sql_u = "update vas_day_flow set flow=%s where pdate=%s"
|
|
return self.mysql_db_bi.exec(sql_u, (flow, formatted_dt))
|
|
else:
|
|
sql_i = "insert into vas_day_flow (pdate, flow) values (%s,%s)"
|
|
return self.mysql_db_bi.exec(sql_i, (formatted_dt, flow))
|
|
|
|
def proc(self):
|
|
zero_time = get_today_zero_time()
|
|
last_time = self.get_last_ptime()
|
|
|
|
while last_time <= int(time.time()):
|
|
next_time = last_time + 300
|
|
if next_time > int(time.time()):
|
|
return
|
|
|
|
flow = self.get_flow(zero_time, next_time)
|
|
dt = datetime.datetime.fromtimestamp(next_time)
|
|
formatted_dt = dt.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
self.insert_flow(formatted_dt, flow)
|
|
# self.update_day_flow(flow)
|
|
print(get_time_str_by_ts(next_time), flow)
|
|
|
|
last_time += 300
|
|
time.sleep(0.5)
|
|
|
|
|
|
s = S()
|
|
s.proc()
|