Skip to content

字段映射策略 ​

开放接口对外暴露的字段与内部数据模型需要进行映射转换。内部模型多使用整数编码(如 gender: 1/2),开放接口对外使用字符串枚举(如 gender: "male"/"female"),提升可读性和跨系统兼容性。


嘉宾字段映射 ​

开放接口字段内部模型字段转换规则
gendergender1 → "male", 2 → "female"
educationeducation_level1 → "高中及以下", 2 → "大专", 3 → "本科", 4 → "硕士", 5 → "博士"
income_levelincome_level1 → "10万以下", 2 → "10-20万", 3 → "20-30万", 4 → "30-50万", 5 → "50-100万", 6 → "100万以上"(由 income_amount 反推,保证一致)
income_amountincome_amount年收入金额(元),BigInteger,权威字段,直接映射
family_income_amountfamily_income_amount家庭年收入金额(元),为空时等于 income_amount,直接映射
family_income_levelfamily_income_level家庭年收入等级,与 income_level 相同的 6 档枚举,为空时等于 income_level
req_income_amountreq_income_amount择偶收入要求下界(元),直接映射
req_income_textreq_income_text择偶收入要求原文,直接映射
marital_statusmarriage_status1 → "未婚", 2 → "离异", 3 → "丧偶"
has_childrenhas_children0 → "no", 1 → "yes"
has_househas_house0 → "no", 1 → "yes"
has_carhas_car0 → "no", 1 → "yes"
workplaceworkplace直接映射
photosphotosJSON 数组,需反序列化
self_introself_intro直接映射
requirementrequirement直接映射
statusstatus1 → "incomplete", 2 → "available", 3 → "matching", 4 → "matched", 5 → "archived"

匹配字段映射 ​

开放接口字段内部模型字段转换规则
guest_a_idguest_a_id直接映射
guest_b_idguest_b_id直接映射
guest_a_matchmaker_idguest_a_matchmaker_id直接映射
guest_b_matchmaker_idguest_b_matchmaker_id直接映射
statusstatus字符串直接映射
match_scorematch_score0-100 整数,直接映射

会员字段映射 ​

开放接口字段内部模型字段转换规则
user_iduser_id直接映射
guest_idguest_id直接映射
plan_idplan_id直接映射
plan_nameplan_name直接映射
expire_atexpire_at直接映射
statusstatus字符串直接映射

活动字段映射 ​

开放接口字段内部模型字段转换规则
titletitle直接映射
cover_urlcover_url直接映射
addressaddress直接映射
male_quotamale_quota整数
female_quotafemale_quota整数
male_feemale_fee直接映射
female_feefemale_fee直接映射
statusstatus字符串直接映射
audit_statusaudit_status字符串直接映射

公司字段映射 ​

开放接口字段内部模型字段转换规则
namename直接映射
contact_namecontact_name直接映射
contact_phonecontact_phone直接映射
statusstatus0 → "frozen", 1 → "active", 2 → "pending", 3 → "rejected"

工作微信号字段映射 ​

开放接口字段内部模型字段转换规则
statusstatus0 → "disabled", 1 → "active"
admin_idadmin_id直接映射
wechat_numberwechat_number直接映射
phonephone直接映射
wechat_nicknamewechat_nickname直接映射
avataravatar直接映射
qr_codeqr_code直接映射
remarkremark直接映射
association_count计算字段关联表记录数
user_count计算字段用户表关联数

注意:工作微信号的 status 字段是唯一需要转换的字段,其余字段均为直接映射。关联数量(association_count、user_count)为运行时计算字段,不对应单一数据库列。

微信群字段映射 ​

开放接口字段内部模型字段转换规则
statusstatus0 → "disabled", 1 → "active"
namename直接映射
group_nogroup_no直接映射
qr_codeqr_code直接映射
remarkremark直接映射
matchmaker_count计算字段关联红娘数(work_group_matchmakers 表)
work_wechat_count计算字段关联工作微信号数(work_group_work_wechats 表)

注意:微信群的 status 字段是唯一需要转换的字段,其余字段均为直接映射。成员数量(matchmaker_count、work_wechat_count)为运行时计算字段,不对应单一数据库列。

实现方式 ​

在 app/schemas/open_api.py 中使用 Pydantic v2 的 field_validator 进行字段转换:

python
from pydantic import BaseModel, field_validator
from typing import Optional, List

class GuestOut(BaseModel):
    id: int
    name: str
    gender: str
    education: Optional[str] = None
    marital_status: Optional[str] = None
    status: str
    photos: List[str] = []

    @field_validator("gender", mode="before")
    @classmethod
    def convert_gender(cls, v):
        if isinstance(v, int):
            return {1: "male", 2: "female"}.get(v, "unknown")
        return v

    @field_validator("education", mode="before")
    @classmethod
    def convert_education(cls, v):
        if isinstance(v, int):
            return {1: "高中及以下", 2: "大专", 3: "本科", 4: "硕士", 5: "博士"}.get(v)
        return v