Commit 2b194304 by imwangxiaozhou

refactor(position-tag): 重构岗位标签引用校验逻辑与数据模型

- 替换原有的BlockingReference和DetachableReference模型为统一的ReferenceItem
- 调整ReferenceCheckResponse返回结构,将引用按agent、qrcode、official_tab分组返回
- 更新服务层的引用校验与标签删除逻辑,适配新的数据模型
- 新增测试用例验证引用分组功能的正确性
- 修改配置文件中的base_path为空白字符串
1 parent a873df96
......@@ -25,7 +25,7 @@ __pycache__/
.deps/
# *.db
# *.db-shm
# *.db-wal
*.db-wal
# OS / editor
.DS_Store
......
......@@ -58,23 +58,16 @@ class PositionTagUpdateResponse(BaseModel):
updated_at: datetime
class BlockingReference(BaseModel):
type: str
id: int
name: str
class DetachableReference(BaseModel):
type: str
class ReferenceItem(BaseModel):
id: int
name: str
class ReferenceCheckResponse(BaseModel):
can_delete: bool
blocking_references: List[BlockingReference]
detachable_references: List[DetachableReference]
hints: List[str]
agent_references: List[ReferenceItem]
qrcode_references: List[ReferenceItem]
official_tab_references: List[ReferenceItem]
class AgentBrief(BaseModel):
......
......@@ -9,14 +9,13 @@ from app.model.agent_group import QrcodeChannel
from app.model.position_tag import OfficialTab, PositionTag
from app.schema.position_tag import (
AgentBrief,
BlockingReference,
DetachableReference,
PositionTagAgentStatistics,
PositionTagCreate,
PositionTagListAgentItem,
PositionTagListQrcodeChannelItem,
PositionTagCreate,
PositionTagUpdate,
ReferenceCheckResponse,
ReferenceItem,
)
......@@ -85,45 +84,75 @@ class PositionTagService:
def check_references(self, tag_id: int) -> ReferenceCheckResponse:
tag = self._get_tag_or_404(tag_id)
blocking = []
detachable = []
hints = []
agent_references: list[ReferenceItem] = []
qrcode_references: list[ReferenceItem] = []
official_tab_references: list[ReferenceItem] = []
qrcode_refs = self.db.query(QrcodeChannel).filter(QrcodeChannel.position_id == tag.id).all()
for qr in qrcode_refs:
blocking.append(BlockingReference(type="qrcode_channel", id=qr.id, name=tag.code or ""))
qrcode_rows = (
self.db.query(QrcodeChannel)
.filter(QrcodeChannel.position_id == tag.id)
.order_by(QrcodeChannel.id.asc())
.all()
)
for row in qrcode_rows:
qrcode_references.append(
ReferenceItem(
id=row.id,
name=row.qrcode_name or tag.code or "",
)
)
tab_refs = self.db.query(OfficialTab).filter(OfficialTab.position_id == tag_id).all()
for tab in tab_refs:
blocking.append(BlockingReference(type="official_tab", id=tab.id, name=tab.tab_name))
tab_rows = (
self.db.query(OfficialTab)
.filter(OfficialTab.position_id == tag_id)
.order_by(OfficialTab.id.asc())
.all()
)
for row in tab_rows:
official_tab_references.append(
ReferenceItem(
id=row.id,
name=row.tab_name or "",
)
)
agent_positions = self.db.query(AgentPosition).filter(AgentPosition.position_id == tag_id).all()
for ap in agent_positions:
agent = self.db.query(Agent).filter(Agent.id == ap.agent_id).first()
agent_positions = (
self.db.query(AgentPosition)
.filter(AgentPosition.position_id == tag_id)
.order_by(AgentPosition.id.asc())
.all()
)
for row in agent_positions:
agent = self.db.query(Agent).filter(Agent.id == row.agent_id).first()
if agent:
detachable.append(DetachableReference(type="agent", id=agent.id, name=agent.name))
agent_references.append(
ReferenceItem(
id=agent.id,
name=agent.name,
)
)
can_delete = len(blocking) == 0
if blocking:
hints.append("该岗位标签存在阻塞性引用,无法删除")
if detachable:
hints.append("该岗位标签被 Agent 引用,删除将解除相关 Agent 的岗位关联")
can_delete = (
len(agent_references) == 0
and len(qrcode_references) == 0
and len(official_tab_references) == 0
)
return ReferenceCheckResponse(
can_delete=can_delete,
blocking_references=blocking,
detachable_references=detachable,
hints=hints,
agent_references=agent_references,
qrcode_references=qrcode_references,
official_tab_references=official_tab_references,
)
def delete(self, tag_id: int, confirm_detach_agents: bool = False) -> None:
self._get_tag_or_404(tag_id)
ref_check = self.check_references(tag_id)
if ref_check.blocking_references:
if ref_check.qrcode_references or ref_check.official_tab_references:
raise ValueError("存在阻塞性引用,无法删除该岗位标签")
if ref_check.detachable_references:
if ref_check.agent_references:
if not confirm_detach_agents:
raise ValueError("该岗位标签被 Agent 引用,需确认解除关联后删除")
self.db.query(AgentPosition).filter(AgentPosition.position_id == tag_id).delete()
......
......@@ -5,7 +5,7 @@
"host": "0.0.0.0",
"port": 8000,
"reload": true,
"base_path": "/backend"
"base_path": ""
},
"im": {
"use_console": true,
......
......@@ -170,6 +170,51 @@ class TestReferenceCheck:
assert resp.status_code == 200
data = resp.json()["data"]
assert data["can_delete"] is True
assert data["agent_references"] == []
assert data["qrcode_references"] == []
assert data["official_tab_references"] == []
def test_references_are_grouped_by_three_categories(self):
_seed_employees()
create_resp = _create_position_tag(user_ids=[1])
tag_id = create_resp.json()["data"]["id"]
db = TestingSessionLocal()
tag = db.query(PositionTag).filter(PositionTag.id == tag_id).first()
assert tag is not None
agent = Agent(id=301, name="复盘 Agent", code="agent_301", status=AgentStatus.enable)
db.add(agent)
db.flush()
db.add(AgentPosition(agent_id=agent.id, position_id=tag_id))
qrcode = db.query(QrcodeChannel).filter(QrcodeChannel.id == 1).first()
assert qrcode is not None
qrcode.position_id = tag_id
qrcode.position_name = tag.name
qrcode.qrcode_name = "驻店销售岗渠道活码"
db.add(
OfficialTab(
tab_name="驻店销售岗 Tab",
position_id=tag_id,
position_code=tag.code,
position_name=tag.name,
display_status="enable",
sort_order=1,
)
)
db.commit()
db.close()
resp = client.get(f"/api/v1/position-tags/{tag_id}/references")
assert resp.status_code == 200
data = resp.json()["data"]
assert data["can_delete"] is False
assert data["agent_references"] == [{"id": 301, "name": "复盘 Agent"}]
assert data["qrcode_references"] == [{"id": 1, "name": "驻店销售岗渠道活码"}]
assert len(data["official_tab_references"]) == 1
assert data["official_tab_references"][0]["name"] == "驻店销售岗 Tab"
class TestListPositionTags:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!