This commit is contained in:
lwl0608 2024-06-17 19:01:16 +08:00
parent 7e2abe3be2
commit 1a5af43d26
3 changed files with 94 additions and 80 deletions

View File

@ -0,0 +1,67 @@
from lib.all import *
import datetime
# 主播当日收入明细
# 示例获取当前月和下一个月的第一天0点时间戳
def get_first_day_of_current_and_next_month_timestamps(date=None):
if date is None:
date = datetime.datetime.now()
# 获取当前月份的第一天
first_day_of_current_month = datetime.datetime(date.year, date.month, 1)
# 获取下个月的第一天
# 首先计算下个月的第一天
next_month = date.month % 12 + 1 if date.month == 12 else date.month + 1
next_year = date.year if date.month != 12 else date.year + 1
first_day_of_next_month = datetime.datetime(next_year, next_month, 1)
# 将两个日期转换为时间戳
timestamp_current_month = first_day_of_current_month.timestamp()
timestamp_next_month = first_day_of_next_month.timestamp()
return int(timestamp_current_month), int(timestamp_next_month)
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(
"127.0.0.1", 3306, "metabase_bi", "metabase_bi", "Wishpal_2024"
)
def __del__(self):
self.mysql_db_vas.close()
self.mysql_db_bi.close()
def update_streamer_day_income(self):
# 获取主播当天收入
st = get_today_zero_time()
et = st + 86400
sql = "SELECT mid, SUM(`change`) AS income FROM `vas_ch_income` WHERE mid>0 AND ct>={} AND ct<{} GROUP BY mid".format(st, et)
rows = self.mysql_db_vas.query(sql)
dt = datetime.datetime.fromtimestamp(st)
formatted_dt = dt.strftime('%Y-%m-%d 00:00:00')
for row in rows:
mid = safe_get_int(row, "mid")
income = safe_get_int(row, "income")
sql_q = "select * from vas_cur_streamer_income where mid={} and pdate='{}'".format(mid, formatted_dt)
rows = self.mysql_db_bi.query(sql_q)
if len(rows) > 0:
sql_u = "update vas_cur_streamer_income set income=%s where mid=%s and pdate=%s"
self.mysql_db_bi.exec(sql_u, (income, mid, formatted_dt))
else:
sql_i = "insert into vas_cur_streamer_income (mid, pdate, income) values (%s,%s,%s)"
self.mysql_db_bi.exec(sql_i, (mid, formatted_dt, income))
def proc(self):
self.update_streamer_day_income()
s = S()
s.proc()

View File

@ -1,80 +0,0 @@
from lib.all import *
import datetime
# 主播当日收入明细
class S:
def __init__(self):
self.col_streamer = MongoDB(
host="mongodb://root:Wishpal2024@dds-bp1da1ddd62bede41.mongodb.rds.aliyuncs.com:3717,dds-bp1da1ddd62bede42.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-77304659",
port=3717,
db="streamer",
collection="streamer"
)
self.col_account = MongoDB(
host="mongodb://root:Wishpal2024@dds-bp1da1ddd62bede41.mongodb.rds.aliyuncs.com:3717,dds-bp1da1ddd62bede42.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-77304659",
port=3717,
db="account",
collection="account"
)
self.mysql_db_vas = Mysql(
"rm-bp11t1616a1kjvmx5.mysql.rds.aliyuncs.com", 3306, "vas", "root", "Wishpal2024"
)
self.mysql_db_bi = Mysql(
"127.0.0.1", 3306, "metabase_bi", "metabase_bi", "Wishpal_2024"
)
def __del__(self):
self.col_streamer.close()
self.col_account.close()
self.mysql_db_vas.close()
self.mysql_db_bi.close()
# 更新主播信息
def update_steamer_info(self):
# 获取主播信息
prj = {"mid": 1}
docs = self.col_streamer.find({}, projection=prj)
streamer_mids = list()
for doc in docs:
mid = safe_get_int(doc, "mid")
streamer_mids.append(mid)
print("len(docs): {}".format(len(docs)))
# 获取账号信息并更新
acnt_docs = self.col_account.find({"_id": {"$in": streamer_mids}})
for doc in acnt_docs:
mid = safe_get_int(doc, "_id")
user_id = safe_get_int(doc, "user_id")
name = safe_get_str(doc, "name")
sql_q = "select * from streamer where mid='{}'".format(mid)
rows = self.mysql_db_bi.query(sql_q)
if len(rows) > 0:
sql_u = "update streamer set user_id=%s, name=%s where mid=%s"
self.mysql_db_bi.exec(sql_u, (user_id, name, mid))
else:
sql_i = "insert into streamer (mid, user_id, name) values (%s,%s,%s)"
self.mysql_db_bi.exec(sql_i, (mid, user_id, name))
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):
self.update_steamer_info()
s = S()
s.proc()

27
ttt/month_ts.py Normal file
View File

@ -0,0 +1,27 @@
from datetime import datetime, timedelta
def get_first_day_of_current_and_next_month_timestamps(date=None):
if date is None:
date = datetime.now()
# 获取当前月份的第一天
first_day_of_current_month = datetime(date.year, date.month, 1)
# 获取下个月的第一天
# 首先计算下个月的第一天
next_month = date.month % 12 + 1 if date.month == 12 else date.month + 1
next_year = date.year if date.month != 12 else date.year + 1
first_day_of_next_month = datetime(next_year, next_month, 1)
# 将两个日期转换为时间戳
timestamp_current_month = first_day_of_current_month.timestamp()
timestamp_next_month = first_day_of_next_month.timestamp()
return int(timestamp_current_month), int(timestamp_next_month)
# 示例获取当前月和下一个月的第一天0点时间戳
current_month_timestamp, next_month_timestamp = get_first_day_of_current_and_next_month_timestamps()
print(f"Current month first day timestamp: {current_month_timestamp}")
print(f"Next month first day timestamp: {next_month_timestamp}")