pl_child_references.py 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Deletion guards for stable logistics child identities."""
  2. import json
  3. from fastapi import HTTPException
  4. from sqlalchemy import inspect
  5. from app.models import pl_models as m
  6. def _exists(db, model, tenant_no, *filters):
  7. return db.query(model.id).filter(model.tenant_no == tenant_no, model.del_flag == 0, *filters).first() is not None
  8. def assert_stop_products_removable(db, tenant_no, plan_id, stop_type, stop_id, rows):
  9. if not rows:
  10. return
  11. ids = {row.id for row in rows}
  12. if any(any(getattr(row, field, None) is not None for field in
  13. ('inbound_qty', 'inbound_price', 'inbound_amount', 'outbound_qty', 'outbound_price', 'outbound_amount'))
  14. or bool(getattr(row, 'archive_remark', None)) for row in rows):
  15. raise HTTPException(409, '段产品已有归档数据,不能移除')
  16. if _exists(db, m.PlPlanProductBrandQty, tenant_no, m.PlPlanProductBrandQty.stop_product_id.in_(ids)):
  17. raise HTTPException(409, '段产品已有归档品牌,不能移除')
  18. # Older installations may not have enabled the optional snapshot table.
  19. from app.models.pl_mobile_stop_meta import PlPlanStopProductMeta
  20. if inspect(db.connection()).has_table(PlPlanStopProductMeta.__tablename__):
  21. if _exists(db, PlPlanStopProductMeta, tenant_no, PlPlanStopProductMeta.stop_product_id.in_(ids)):
  22. raise HTTPException(409, '段产品已有业务快照,不能移除')
  23. for model in (m.PlTransportDriverStopFeedback, m.PlTransportSiteStopFeedback):
  24. feedbacks = db.query(model).filter(model.tenant_no == tenant_no, model.plan_id == plan_id,
  25. model.stop_type == stop_type, model.stop_id == stop_id,
  26. model.del_flag == 0).all()
  27. for feedback in feedbacks:
  28. try:
  29. values = json.loads(feedback.product_feedbacks or '[]')
  30. except (ValueError, TypeError):
  31. raise HTTPException(409, '停靠段回传数据无法核对,不能移除产品')
  32. if not isinstance(values, list):
  33. raise HTTPException(409, '停靠段回传数据无法核对,不能移除产品')
  34. if any(isinstance(value, dict) and (value.get('stopProductId') or value.get('stop_product_id')) in ids for value in values):
  35. raise HTTPException(409, '段产品已有司机或现场回传,不能移除')
  36. ppids = {row.plan_product_id for row in rows if row.plan_product_id}
  37. if ppids and _exists(db, m.PlTransportDbLink, tenant_no,
  38. m.PlTransportDbLink.plan_id == plan_id, m.PlTransportDbLink.stop_type == stop_type,
  39. m.PlTransportDbLink.stop_id == stop_id, m.PlTransportDbLink.plan_product_id.in_(ppids)):
  40. raise HTTPException(409, '段产品已关联地磅单,不能移除')
  41. def assert_stop_removable(db, tenant_no, plan_id, stop_type, stop_id, rows):
  42. assert_stop_products_removable(db, tenant_no, plan_id, stop_type, stop_id, rows)
  43. for model in (m.PlTransportDriverStopFeedback, m.PlTransportSiteStopFeedback, m.PlTransportDbLink):
  44. if _exists(db, model, tenant_no, model.plan_id == plan_id, model.stop_type == stop_type, model.stop_id == stop_id):
  45. raise HTTPException(409, '停靠段已有回传或地磅关联,不能移除')
  46. def assert_plan_products_removable(db, tenant_no, plan_id, rows, ignored_stop_ids=None):
  47. if not rows:
  48. return
  49. ids = {row.id for row in rows}
  50. for model in (m.PlPlanLoadProduct, m.PlPlanDeliveryProduct, m.PlPlanProductBrandQty,
  51. m.PlInboundOrderItem, m.PlOutboundOrderItem, m.PlTransportProductFeedback,
  52. m.PlTransportProductBrandQty, m.PlTransportDbLink, m.PlTransportSiteLink):
  53. filters = [model.plan_product_id.in_(ids)]
  54. if model in (m.PlPlanLoadProduct, m.PlPlanDeliveryProduct) and ignored_stop_ids:
  55. filters.append(~model.id.in_(ignored_stop_ids))
  56. if _exists(db, model, tenant_no, *filters):
  57. raise HTTPException(409, '计划产品已有段产品、归档、回传或出入库关联,不能移除')
  58. if any(any(getattr(row, name, None) is not None for name in
  59. ('inbound_qty', 'outbound_qty')) for row in rows):
  60. raise HTTPException(409, '计划产品已有归档数量,不能移除')
  61. product_ids = {row.product_id for row in rows}
  62. if _exists(db, m.PlInventoryTransaction, tenant_no, m.PlInventoryTransaction.plan_id == plan_id,
  63. m.PlInventoryTransaction.product_id.in_(product_ids)):
  64. raise HTTPException(409, '计划产品已有库存流水,不能移除')