From b2f92c12f20dba3381a95e4d544f04035f9b5eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E8=89=B3?= Date: Mon, 17 Aug 2026 13:14:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8=20sell/base=5Fprice.py=20?= =?UTF-8?q?=E5=86=99=E5=AD=97=E6=A5=BC=E5=87=BA=E5=94=AE=E9=87=8D=E7=AE=97?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E4=B8=AD=E8=BF=BD=E5=8A=A0=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=20mom/yoy=20=E7=8E=AF=E6=AF=94=E5=90=8C?= =?UTF-8?q?=E6=AF=94=E6=8C=87=E6=A0=87=EF=BC=8C=E9=81=BF=E5=85=8D=E9=87=8D?= =?UTF-8?q?=E7=AE=97=E5=90=8E=E6=8C=87=E6=A0=87=E4=B8=A2=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dags/office/sell/base_price.py | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/dags/office/sell/base_price.py b/dags/office/sell/base_price.py index cb9ee11..3638514 100644 --- a/dags/office/sell/base_price.py +++ b/dags/office/sell/base_price.py @@ -128,6 +128,64 @@ def calcByOffice(city, district, office_name): upsert=True) office_sell_stat_conn.bulk_write([action]) + # 🏁 触发 MoM 和 YoY(环比与同比)的自动计算与更新(出售) + calculate_mom_yoy_for_office(city, district, office_name) + + +def calculate_mom_yoy_for_office(city, district, office_name): + """ + 计算特定写字楼出售历史所有月份的 mom (环比) 和 yoy (同比) 并更新 + """ + query = { + 'cat': 'office', + 'city': city, + 'district': district, + 'name': office_name + } + docs = list(office_sell_stat_conn.find(query).sort('date', 1)) + if not docs: + return + + doc_dict = {doc['date']: doc for doc in docs} + actions = [] + for doc in docs: + curr_date = doc['date'] + curr_price = doc.get('avg_price') + if not curr_price: + continue + + # 计算前一个月的 YYYYMM + year = curr_date // 100 + month = curr_date % 100 + if month == 1: + prev_date = (year - 1) * 100 + 12 + else: + prev_date = curr_date - 1 + + # 计算上年同月的 YYYYMM + ly_date = curr_date - 100 + + # 计算 mom (环比) + mom = None + if prev_date in doc_dict and doc_dict[prev_date].get('avg_price'): + prev_price = doc_dict[prev_date]['avg_price'] + mom = round((curr_price - prev_price) / prev_price * 100, 2) + + # 计算 yoy (同比) + yoy = None + if ly_date in doc_dict and doc_dict[ly_date].get('avg_price'): + ly_price = doc_dict[ly_date]['avg_price'] + yoy = round((curr_price - ly_price) / ly_price * 100, 2) + + action = UpdateOne( + {'_id': doc['_id']}, + {'$set': {'mom': mom, 'yoy': yoy}} + ) + actions.append(action) + + if actions: + office_sell_stat_conn.bulk_write(actions) + def calcByCity(city, year_month): """