Commit e3853e3a by sunqi02

feat: 添加手机号查询功能,重构手机号绑定逻辑,优化代码结构

1 parent 3ea98d0e
...@@ -5,7 +5,7 @@ from sqlalchemy.orm import Session ...@@ -5,7 +5,7 @@ from sqlalchemy.orm import Session
from app.infrastructure.database import get_db from app.infrastructure.database import get_db
from app.schema.common import ApiResponse, success_response_payload, SuccessFlagData from app.schema.common import ApiResponse, success_response_payload, SuccessFlagData
from app.customer_info.schema import PhoneBindingSubmitRequest from app.customer_info.schema import CustomerGroupPhoneResponse, PhoneBindingSubmitRequest
from app.customer_info.service import CustomerInfoService from app.customer_info.service import CustomerInfoService
router = APIRouter(prefix="/api/v1/customer-info", tags=["customer-info"]) router = APIRouter(prefix="/api/v1/customer-info", tags=["customer-info"])
...@@ -22,8 +22,20 @@ def submit_phone( ...@@ -22,8 +22,20 @@ def submit_phone(
): ):
"""用户提交手机号绑定。""" """用户提交手机号绑定。"""
record = service.bind_phone( record = service.bind_phone(
customer_name=req.customer_name, group_id=req.group_id, phone=req.phone group_id=req.group_id, phone=req.phone
) )
if record is None: if record is None:
raise HTTPException(status_code=404, detail="未找到匹配的客户群组记录") raise HTTPException(status_code=404, detail="未找到匹配的客户群组记录")
return success_response_payload(200, SuccessFlagData(success=True)) return success_response_payload(200, SuccessFlagData(success=True))
@router.get("/phone", response_model=ApiResponse[CustomerGroupPhoneResponse])
def get_phone(
group_id: str,
service: CustomerInfoService = Depends(get_customer_info_service),
):
"""查询群手机号绑定信息。"""
record = service.get_phone(group_id)
if record is None:
raise HTTPException(status_code=404, detail="未找到匹配的客户群组记录")
return success_response_payload(200, record)
...@@ -3,12 +3,11 @@ from __future__ import annotations ...@@ -3,12 +3,11 @@ from __future__ import annotations
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
from pydantic import BaseModel, Field from pydantic import BaseModel, Field, field_serializer
from pydantic.config import ConfigDict from pydantic.config import ConfigDict
class PhoneBindingSubmitRequest(BaseModel): class PhoneBindingSubmitRequest(BaseModel):
customer_name: str = Field(..., max_length=255, description="客户名称")
group_id: str = Field(..., max_length=255, description="群ID") group_id: str = Field(..., max_length=255, description="群ID")
phone: str = Field(..., max_length=20, description="手机号码") phone: str = Field(..., max_length=20, description="手机号码")
...@@ -24,3 +23,9 @@ class CustomerGroupPhoneResponse(BaseModel): ...@@ -24,3 +23,9 @@ class CustomerGroupPhoneResponse(BaseModel):
last_pushed_at: Optional[datetime] = None last_pushed_at: Optional[datetime] = None
create_time: datetime create_time: datetime
update_time: datetime update_time: datetime
@field_serializer("phone")
def mask_phone(self, value: Optional[str]) -> Optional[str]:
if value and len(value) == 11:
return value[:3] + "****" + value[7:]
return value
...@@ -43,25 +43,30 @@ class CustomerInfoService: ...@@ -43,25 +43,30 @@ class CustomerInfoService:
logger.info("同步完成: affected=%s", affected) logger.info("同步完成: affected=%s", affected)
return affected return affected
def bind_phone(self, customer_name: str, group_id: str, phone: str) -> Optional[CustomerGroupPhone]: def bind_phone(self, group_id: str, phone: str) -> Optional[CustomerGroupPhone]:
"""用户填写手机号。""" """用户填写手机号。"""
record = ( record = (
self.db.query(CustomerGroupPhone) self.db.query(CustomerGroupPhone)
.filter( .filter(CustomerGroupPhone.group_id == group_id)
CustomerGroupPhone.customer_name == customer_name,
CustomerGroupPhone.group_id == group_id,
)
.first() .first()
) )
if not record: if not record:
logger.warning("未找到匹配记录: customer_name=%s group_id=%s", customer_name, group_id) logger.warning("未找到匹配记录: group_id=%s", group_id)
return None return None
record.phone = phone record.phone = phone
self.db.commit() self.db.commit()
self.db.refresh(record) self.db.refresh(record)
logger.info("手机号绑定成功: customer_name=%s group_id=%s", customer_name, group_id) logger.info("手机号绑定成功: group_id=%s", group_id)
return record return record
def get_phone(self, group_id: str) -> Optional[CustomerGroupPhone]:
"""查询群绑定的手机号。"""
return (
self.db.query(CustomerGroupPhone)
.filter(CustomerGroupPhone.group_id == group_id)
.first()
)
def get_push_candidates(self, mode: str = "sync") -> list[CustomerGroupPhone]: def get_push_candidates(self, mode: str = "sync") -> list[CustomerGroupPhone]:
"""获取需要推送的记录。 """获取需要推送的记录。
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!