Commit bac8d242 by 章家宝

feat:红黄门店如果按周,则根据选择的周的时间来计算

1 parent bc11eed4
......@@ -1384,32 +1384,32 @@ class CommonEquityProcessor:
# ============================================================
@staticmethod
def _weekly_ranges(period_start):
def _weekly_ranges(period_start, period_end):
"""以 period_start 所在月份为基准生成累计制周窗口(每周都从 1 号起算)。
W1 = 1 号 ~ 当月第一个周日,W2 = 1 号 ~ 第二个周日,以此类推;最后一个
周日之后到月末的余段直接截到月底、单独作最后一周。
周数动态 = 当月周日个数 + (最后一个周日不在月末时为 1):通常 5 周;
当月有 5 个周日且月末落在周中时为 6 周(如 2026-03 / 08 / 11);2 月
恰为 28 天且 1 号为周一时只有 4 周(如 2027-02)。
例:2026-09(1 号为周二)→ W1 1~6、W2 1~13、W3 1~20、W4 1~27、W5 1~30。
与任务周期的 week/month 粒度无关,始终按自然月划分。
W1 = 1 号 ~ 当月第一个周日,W2 = 1 号 ~ 第二个周日,以此类推;
周数按 period_end 截断:页面按周生成时 period_end 为所选周的周日,
只生成到该周为止(如选 W4 则只有 W1~W4,不会再出 W5/W6);
period_end 不在周日时(理论上不会出现),最后一周直接截到 period_end。
例:2026-09(1 号为周二)整月 → W1 1~6、W2 1~13、W3 1~20、W4 1~27、W5 1~30。
"""
last_day = calendar.monthrange(period_start.year, period_start.month)[1]
month_start = date(period_start.year, period_start.month, 1)
month_end = date(period_start.year, period_start.month, last_day)
effective_end = min(month_end, period_end)
# 当月第一个周日:weekday() 周一=0 .. 周日=6,1 号本身就是周日时取当天
first_sunday = month_start + timedelta(days=(6 - month_start.weekday()) % 7)
ranges = []
cursor = first_sunday
while cursor <= month_end:
while cursor <= effective_end:
ranges.append((f"W{len(ranges) + 1}", month_start, cursor))
cursor += timedelta(days=7)
# 末尾不足一周的余段(如 9/28~9/30)直接到月底,单独作最后一周
if ranges[-1][2] < month_end:
ranges.append((f"W{len(ranges) + 1}", month_start, month_end))
# 末尾余段:period_end 不是周日(或最后一个周日之后仍有余量到月末)时,
# 从 1 号累计到 effective_end 作为最后一周
if not ranges or ranges[-1][2] < effective_end:
ranges.append((f"W{len(ranges) + 1}", month_start, effective_end))
return ranges
def _fetch_weekly_cumulative(self, merchant_ids, ranges):
......@@ -1491,7 +1491,7 @@ class CommonEquityProcessor:
cleaned = self._validate_red_yellow_rows(parsed, store_rows, threshold_map)
merchant_ids = list(store_index.keys())
ranges = self._weekly_ranges(task.period_start)
ranges = self._weekly_ranges(task.period_start, task.period_end)
weekly = self._fetch_weekly_cumulative(merchant_ids, ranges)
week_labels = [label for label, _s, _e in ranges]
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!