Name Last Update
app Loading commit data...
bin Loading commit data...
docker Loading commit data...
logs Loading commit data...
scripts Loading commit data...
tests Loading commit data...
wxz-align-spec Loading commit data...
.gitignore Loading commit data...
README.md Loading commit data...
config.ini Loading commit data...
pyproject.toml Loading commit data...
requirements.txt Loading commit data...

门店数据同步与搜索服务

定时从外部产值 API 拉取门店数据、从 MySQL 拉取品牌数据,写入 SQLite FTS5 全文索引,提供基于 FastAPI 的 RESTful 模糊搜索接口。


目录结构

├── app/                  # 应用代码
│   ├── core/             # 公共核心库(配置、日志、DB)
│   ├── sync/             # 门店数据同步调度器
│   ├── brand_sync/       # 品牌数据同步调度器
│   └── search/           # 搜索 API(门店搜索 + 品牌搜索)
├── bin/                  # 入口文件
├── scripts/              # 管理脚本(启动/停止/状态)
├── docker/               # Docker 部署配置
├── config.ini            # 业务配置
└── .env                  # 敏感配置(不提交)

本地部署

环境要求

  • Python >= 3.10
  • pip

安装依赖

pip3 install -r requirements.txt

配置

编辑 config.ini 设置业务参数。详见文件内注释。

.env 文件用于配置 MySQL 等敏感信息,该文件不会提交到版本控制:

# MySQL 配置 - 品牌数据同步
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password
MYSQL_DATABASE=cyb_os
MYSQL_CHARSET=utf8mb4

服务管理(手动方式)

门店数据同步

从外部产值 API 同步门店数据到 SQLite:

python3 bin/run_sync.py
  • 启动时立即执行一次,之后每 12 小时执行一次(间隔可在 config.ini 中调整)
  • 日志文件:logs/sync_data.log

品牌数据同步

从 MySQL cyb_os.platform_carbrand 表同步品牌分组数据到 SQLite:

python3 bin/run_brand_sync.py
  • 启动时立即执行一次,之后每周一凌晨 3:00 执行一次(调度时间和周期可在 config.ini 中调整)
  • 日志文件:logs/sync_brand.log

启动搜索 API 服务

启动 FastAPI HTTP 搜索接口(提供门店搜索和品牌搜索两个接口):

python3 bin/run_api.py --server --host 0.0.0.0 --port 8000

CLI 模式搜索测试

门店搜索:

python3 bin/run_api.py --query "格林赛克店" --limit 10

API 接口

门店搜索

curl -X POST http://localhost:8000/ask_data/api/findMerchantIdByFt5 \
  -H "Content-Type: application/json" \
  -d '{"merchantName": "格林赛克店", "limit": 10}'

响应示例:

{
    "query": "格林赛克",
    "count": 2,
    "data": [
        {"merchant_name": "格林赛克深圳旗舰店", "merchant_id": "10001", "score": 0.5},
        {"merchant_name": "格林赛克广州分店", "merchant_id": "10002", "score": 1.2}
    ]
}

品牌搜索

curl -X POST http://localhost:8000/ask_data/api/findBrandIdByFt5 \
  -H "Content-Type: application/json" \
  -d '{"brandName": "大众", "limit": 10}'

响应示例:

{
    "query": "大众",
    "count": 1,
    "data": [
        {"brand_name": "大众集团", "brand_id": "1", "score": 0.0}
    ]
}

Docker 部署

构建并启动

cd docker
docker-compose up -d

查看日志

docker-compose logs -f

停止容器

docker-compose down

架构说明

  • sync 容器:定时同步门店数据到 SQLite
  • brand_sync 容器:定时从 MySQL 同步品牌数据到 SQLite
  • api 容器:提供 HTTP 搜索接口(门店搜索 + 品牌搜索),与 sync 共享数据卷
  • 所有容器通过 Docker named volume 共享 merchants_fts.db 文件

配置说明

config.ini

配置节 字段 说明
[api] host 搜索 API 服务监听地址
[api] port 搜索 API 服务监听端口
[api] data_url 外部产值 API 地址
[api] item_codes 接口请求的项目编码
[api] date_type 产值接口 dateType 参数值(4=按天统计)
[api] request_timeout 外部接口请求超时时间(秒)
[sync] interval_hours 门店同步间隔(小时)
[sync] data_range_days 查询数据的天数范围
[sync] check_interval 调度循环检查间隔(秒)
[brand_sync] schedule_day 品牌同步调度日期(monday/tuesday/...)
[brand_sync] schedule_time 品牌同步调度时间(24小时制 HH:MM)
[brand_sync] check_interval 调度循环检查间隔(秒)
[mysql] connect_timeout MySQL 连接超时时间(秒)
[search] default_limit 搜索结果默认返回条数上限
[search] suffix_merchant 门店名称后缀去除规则(\
[search] suffix_brand 品牌名称后缀去除规则(\
[db] db_path SQLite 数据库文件路径

.env

环境变量 说明
MYSQL_HOST MySQL 主机地址
MYSQL_PORT MySQL 端口
MYSQL_USER MySQL 用户名
MYSQL_PASSWORD MySQL 密码
MYSQL_DATABASE MySQL 数据库名
MYSQL_CHARSET MySQL 字符集

调用流程

外部产值 API ──HTTP POST──> sync 调度器 ──INSERT──> SQLite merchants_fts.db
                                                            │
外部 MySQL   ──pymysql──> brand_sync 调度器 ──INSERT──>    │
                                                        │
AI 问答系统  ──HTTP POST──> search API (门店/品牌搜索) ◀──┘