小程序后端接入 talkingq 共享库并新增消息与定位能力
This commit is contained in:
@@ -13,6 +13,55 @@ logger = logging.getLogger("app.dao.binding")
|
||||
|
||||
|
||||
class BindingDAO(BaseDAO):
|
||||
def get_device_auth(self, device_id: str) -> Optional[Mapping]:
|
||||
return (
|
||||
self.db.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT device_id, serial_number, is_active
|
||||
FROM device_auth
|
||||
WHERE device_id = :device_id
|
||||
LIMIT 1
|
||||
"""
|
||||
),
|
||||
{"device_id": device_id},
|
||||
)
|
||||
.mappings()
|
||||
.first()
|
||||
)
|
||||
|
||||
def _clear_child_from_binding(self, binding_row: Mapping[str, object]) -> None:
|
||||
row_id = int(binding_row["id"])
|
||||
status = int(binding_row["status"])
|
||||
|
||||
if status == 1:
|
||||
self.db.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE device_bindings
|
||||
SET child_id = NULL,
|
||||
status = 1,
|
||||
unbound_at = NULL,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :id
|
||||
"""
|
||||
),
|
||||
{"id": row_id},
|
||||
)
|
||||
return
|
||||
|
||||
self.db.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE device_bindings
|
||||
SET child_id = NULL,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :id
|
||||
"""
|
||||
),
|
||||
{"id": row_id},
|
||||
)
|
||||
|
||||
def _upsert_parent_child_relation(self, user_id: int, child_id: int) -> None:
|
||||
updated = self.db.execute(
|
||||
text(
|
||||
@@ -73,7 +122,7 @@ class BindingDAO(BaseDAO):
|
||||
def _bind_device(self, device_id: str, user_id: int, child_id: Optional[int]) -> None:
|
||||
existing_by_device = (
|
||||
self.db.execute(
|
||||
text("SELECT id FROM device_bindings WHERE device_id = :device_id"),
|
||||
text("SELECT id, status FROM device_bindings WHERE device_id = :device_id"),
|
||||
{"device_id": device_id},
|
||||
)
|
||||
.mappings()
|
||||
@@ -110,7 +159,7 @@ class BindingDAO(BaseDAO):
|
||||
|
||||
existing_by_child = (
|
||||
self.db.execute(
|
||||
text("SELECT id FROM device_bindings WHERE child_id = :child_id"),
|
||||
text("SELECT id, status FROM device_bindings WHERE child_id = :child_id"),
|
||||
{"child_id": child_id},
|
||||
)
|
||||
.mappings()
|
||||
@@ -120,20 +169,8 @@ class BindingDAO(BaseDAO):
|
||||
if existing_by_device:
|
||||
device_row_id = int(existing_by_device["id"])
|
||||
if existing_by_child and int(existing_by_child["id"]) != device_row_id:
|
||||
# Release child ownership from a different row before assigning to this device.
|
||||
self.db.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE device_bindings
|
||||
SET child_id = NULL,
|
||||
status = 0,
|
||||
unbound_at = CURRENT_TIMESTAMP,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :id
|
||||
"""
|
||||
),
|
||||
{"id": existing_by_child["id"]},
|
||||
)
|
||||
# Keep the old device row visible; only release its child assignment.
|
||||
self._clear_child_from_binding(existing_by_child)
|
||||
|
||||
self.db.execute(
|
||||
text(
|
||||
@@ -146,27 +183,14 @@ class BindingDAO(BaseDAO):
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE device_id = :device_id
|
||||
"""
|
||||
),
|
||||
{"owner_user_id": user_id, "child_id": child_id, "device_id": device_id},
|
||||
)
|
||||
),
|
||||
{"owner_user_id": user_id, "child_id": child_id, "device_id": device_id},
|
||||
)
|
||||
return
|
||||
|
||||
if existing_by_child:
|
||||
self.db.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE device_bindings
|
||||
SET device_id = :device_id,
|
||||
owner_user_id = :owner_user_id,
|
||||
status = 1,
|
||||
unbound_at = NULL,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :id
|
||||
"""
|
||||
),
|
||||
{"device_id": device_id, "owner_user_id": user_id, "id": existing_by_child["id"]},
|
||||
)
|
||||
return
|
||||
# Do not overwrite the previous device row; keep it as an unbound device.
|
||||
self._clear_child_from_binding(existing_by_child)
|
||||
|
||||
self.db.execute(
|
||||
text(
|
||||
|
||||
Reference in New Issue
Block a user