36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
"""今日老婆 —— 数据访问(Service 层)。"""
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
_CONFIG_DIR = Path(__file__).resolve().parents[3] / "data" / "dailywife" / "config"
|
||
|
|
|
||
|
|
|
||
|
|
def get_member_list(all_list):
|
||
|
|
return [member["user_id"] for member in all_list]
|
||
|
|
|
||
|
|
|
||
|
|
def load_group_config(group_id: str) -> dict | None:
|
||
|
|
filename = _CONFIG_DIR / f"{group_id}.json"
|
||
|
|
try:
|
||
|
|
with open(filename, encoding="utf8") as f:
|
||
|
|
return json.load(f)
|
||
|
|
except Exception:
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def write_group_config(group_id: str, link_id: str, wife_id: str, date: str, config) -> None:
|
||
|
|
_CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
||
|
|
config_file = _CONFIG_DIR / f"{group_id}.json"
|
||
|
|
if config is not None:
|
||
|
|
config[link_id] = [wife_id, date]
|
||
|
|
else:
|
||
|
|
config = {link_id: [wife_id, date]}
|
||
|
|
tmp = config_file.with_suffix(".tmp")
|
||
|
|
with open(tmp, "w", encoding="utf-8") as f:
|
||
|
|
json.dump(config, f, ensure_ascii=False)
|
||
|
|
os.replace(tmp, config_file)
|