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__/ ...@@ -25,7 +25,7 @@ __pycache__/
.deps/ .deps/
# *.db # *.db
# *.db-shm # *.db-shm
# *.db-wal *.db-wal
# OS / editor # OS / editor
.DS_Store .DS_Store
......
...@@ -58,23 +58,16 @@ class PositionTagUpdateResponse(BaseModel): ...@@ -58,23 +58,16 @@ class PositionTagUpdateResponse(BaseModel):
updated_at: datetime updated_at: datetime
class BlockingReference(BaseModel): class ReferenceItem(BaseModel):
type: str
id: int
name: str
class DetachableReference(BaseModel):
type: str
id: int id: int
name: str name: str
class ReferenceCheckResponse(BaseModel): class ReferenceCheckResponse(BaseModel):
can_delete: bool can_delete: bool
blocking_references: List[BlockingReference] agent_references: List[ReferenceItem]
detachable_references: List[DetachableReference] qrcode_references: List[ReferenceItem]
hints: List[str] official_tab_references: List[ReferenceItem]
class AgentBrief(BaseModel): class AgentBrief(BaseModel):
......
...@@ -9,14 +9,13 @@ from app.model.agent_group import QrcodeChannel ...@@ -9,14 +9,13 @@ from app.model.agent_group import QrcodeChannel
from app.model.position_tag import OfficialTab, PositionTag from app.model.position_tag import OfficialTab, PositionTag
from app.schema.position_tag import ( from app.schema.position_tag import (
AgentBrief, AgentBrief,
BlockingReference,
DetachableReference,
PositionTagAgentStatistics, PositionTagAgentStatistics,
PositionTagCreate,
PositionTagListAgentItem, PositionTagListAgentItem,
PositionTagListQrcodeChannelItem, PositionTagListQrcodeChannelItem,
PositionTagCreate,
PositionTagUpdate, PositionTagUpdate,
ReferenceCheckResponse, ReferenceCheckResponse,
ReferenceItem,
) )
...@@ -85,45 +84,75 @@ class PositionTagService: ...@@ -85,45 +84,75 @@ class PositionTagService:
def check_references(self, tag_id: int) -> ReferenceCheckResponse: def check_references(self, tag_id: int) -> ReferenceCheckResponse:
tag = self._get_tag_or_404(tag_id) tag = self._get_tag_or_404(tag_id)
blocking = [] agent_references: list[ReferenceItem] = []
detachable = [] qrcode_references: list[ReferenceItem] = []
hints = [] official_tab_references: list[ReferenceItem] = []
qrcode_refs = self.db.query(QrcodeChannel).filter(QrcodeChannel.position_id == tag.id).all() qrcode_rows = (
for qr in qrcode_refs: self.db.query(QrcodeChannel)
blocking.append(BlockingReference(type="qrcode_channel", id=qr.id, name=tag.code or "")) .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() tab_rows = (
for tab in tab_refs: self.db.query(OfficialTab)
blocking.append(BlockingReference(type="official_tab", id=tab.id, name=tab.tab_name)) .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() agent_positions = (
for ap in agent_positions: self.db.query(AgentPosition)
agent = self.db.query(Agent).filter(Agent.id == ap.agent_id).first() .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: 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 can_delete = (
if blocking: len(agent_references) == 0
hints.append("该岗位标签存在阻塞性引用,无法删除") and len(qrcode_references) == 0
if detachable: and len(official_tab_references) == 0
hints.append("该岗位标签被 Agent 引用,删除将解除相关 Agent 的岗位关联") )
return ReferenceCheckResponse( return ReferenceCheckResponse(
can_delete=can_delete, can_delete=can_delete,
blocking_references=blocking, agent_references=agent_references,
detachable_references=detachable, qrcode_references=qrcode_references,
hints=hints, official_tab_references=official_tab_references,
) )
def delete(self, tag_id: int, confirm_detach_agents: bool = False) -> None: def delete(self, tag_id: int, confirm_detach_agents: bool = False) -> None:
self._get_tag_or_404(tag_id) self._get_tag_or_404(tag_id)
ref_check = self.check_references(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("存在阻塞性引用,无法删除该岗位标签") raise ValueError("存在阻塞性引用,无法删除该岗位标签")
if ref_check.detachable_references: if ref_check.agent_references:
if not confirm_detach_agents: if not confirm_detach_agents:
raise ValueError("该岗位标签被 Agent 引用,需确认解除关联后删除") raise ValueError("该岗位标签被 Agent 引用,需确认解除关联后删除")
self.db.query(AgentPosition).filter(AgentPosition.position_id == tag_id).delete() self.db.query(AgentPosition).filter(AgentPosition.position_id == tag_id).delete()
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"host": "0.0.0.0", "host": "0.0.0.0",
"port": 8000, "port": 8000,
"reload": true, "reload": true,
"base_path": "/backend" "base_path": ""
}, },
"im": { "im": {
"use_console": true, "use_console": true,
......
...@@ -170,6 +170,51 @@ class TestReferenceCheck: ...@@ -170,6 +170,51 @@ class TestReferenceCheck:
assert resp.status_code == 200 assert resp.status_code == 200
data = resp.json()["data"] data = resp.json()["data"]
assert data["can_delete"] is True 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: class TestListPositionTags:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!