From 1a5af43d267c0aba6ab28ecd41447696d8623a14 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Mon, 17 Jun 2024 19:01:16 +0800 Subject: [PATCH] fix --- bi/vas_cur_steamer_income.py | 67 ++++++++++++++++++++++++++++++ bi/vas_day_income.py | 80 ------------------------------------ ttt/month_ts.py | 27 ++++++++++++ 3 files changed, 94 insertions(+), 80 deletions(-) create mode 100644 bi/vas_cur_steamer_income.py delete mode 100644 bi/vas_day_income.py create mode 100644 ttt/month_ts.py diff --git a/bi/vas_cur_steamer_income.py b/bi/vas_cur_steamer_income.py new file mode 100644 index 0000000..96e3a3d --- /dev/null +++ b/bi/vas_cur_steamer_income.py @@ -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() diff --git a/bi/vas_day_income.py b/bi/vas_day_income.py deleted file mode 100644 index ab52843..0000000 --- a/bi/vas_day_income.py +++ /dev/null @@ -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() diff --git a/ttt/month_ts.py b/ttt/month_ts.py new file mode 100644 index 0000000..85dcdc6 --- /dev/null +++ b/ttt/month_ts.py @@ -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}") \ No newline at end of file