|
|
@@ -17,6 +17,8 @@ from app.core.tenant_context import get_tenant_no
|
|
|
from app.crud.crm.data_scope_helpers import (
|
|
|
crm_user_info,
|
|
|
ensure_crm_create_by_row_in_scope,
|
|
|
+ ensure_crm_contact_row_in_scope,
|
|
|
+ ensure_crm_customer_linked_row_in_scope,
|
|
|
ensure_crm_owner_row_in_scope,
|
|
|
)
|
|
|
from app.utils.crm_export_columns import (
|
|
|
@@ -329,6 +331,8 @@ async def category_tree(
|
|
|
"""获取产品分类树"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
payload = body or {}
|
|
|
category_name = payload.get("categoryName") or payload.get("category_name")
|
|
|
status = payload.get("status")
|
|
|
@@ -337,6 +341,7 @@ async def category_tree(
|
|
|
tenant_no,
|
|
|
category_name=category_name,
|
|
|
status=status,
|
|
|
+ user_info=user_info,
|
|
|
)
|
|
|
return success_response(rows)
|
|
|
except ValueError as e:
|
|
|
@@ -516,6 +521,7 @@ async def category_add(
|
|
|
"""新增产品分类"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
data = body.model_dump(exclude_unset=True)
|
|
|
obj = create_category(db, tenant_no=tenant_no, user_id=user_id, **data)
|
|
|
@@ -537,7 +543,7 @@ async def category_update(
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
data = body.model_dump(exclude_unset=True)
|
|
|
category_id = data.pop("id")
|
|
|
- obj = update_category(db, category_id, user_id=user_id, **data)
|
|
|
+ obj = update_category(db, category_id, user_id=user_id, user_info=user_info, **data)
|
|
|
return success_response(_category_row_to_dict(obj), message="更新成功")
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -553,7 +559,7 @@ async def category_delete(
|
|
|
):
|
|
|
"""删除产品分类"""
|
|
|
try:
|
|
|
- soft_delete_category(db, body.id)
|
|
|
+ soft_delete_category(db, body.id, user_info=user_info)
|
|
|
return success_response(None, message="删除成功")
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -573,6 +579,7 @@ async def product_list(
|
|
|
"""分页查询产品列表"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
rows, total = list_products(
|
|
|
db,
|
|
|
tenant_no,
|
|
|
@@ -581,6 +588,7 @@ async def product_list(
|
|
|
keyword=body.keyword,
|
|
|
category_id=body.category_id,
|
|
|
status=body.status,
|
|
|
+ user_info=user_info,
|
|
|
)
|
|
|
tp = total_pages(total, body.pageSize)
|
|
|
return success_response(
|
|
|
@@ -606,9 +614,12 @@ async def product_detail(
|
|
|
):
|
|
|
"""获取产品详情"""
|
|
|
try:
|
|
|
+ tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
obj = get_product(db, body.id)
|
|
|
if not obj:
|
|
|
return error_response(404, "产品不存在")
|
|
|
+ ensure_crm_create_by_row_in_scope(db, obj, user_info, tenant_no, message="无权访问该产品")
|
|
|
return success_response(_product_row_to_dict(obj))
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -625,6 +636,7 @@ async def product_add(
|
|
|
"""新增产品"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
obj = create_product(
|
|
|
db,
|
|
|
@@ -650,7 +662,9 @@ async def product_update(
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
data = body.model_dump(exclude_unset=True)
|
|
|
product_id = data.pop("id")
|
|
|
- obj = update_product(db, product_id, user_id=user_id, **data)
|
|
|
+ tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
+ obj = update_product(db, product_id, user_id=user_id, user_info=user_info, **data)
|
|
|
return success_response(_product_row_to_dict(obj), message="更新成功")
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -666,6 +680,8 @@ async def product_delete(
|
|
|
):
|
|
|
"""删除产品(支持单条 body.id 和批量 body.ids)"""
|
|
|
try:
|
|
|
+ tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
ids_to_delete: List[str] = []
|
|
|
if body.ids:
|
|
|
ids_to_delete = body.ids
|
|
|
@@ -675,7 +691,7 @@ async def product_delete(
|
|
|
return error_response(400, "请传入 id 或 ids")
|
|
|
|
|
|
for pid in ids_to_delete:
|
|
|
- soft_delete_product(db, pid)
|
|
|
+ soft_delete_product(db, pid, user_info=user_info)
|
|
|
|
|
|
return success_response(None, message="删除成功")
|
|
|
except ValueError as e:
|
|
|
@@ -692,10 +708,12 @@ async def product_update_status(
|
|
|
):
|
|
|
"""批量上架/下架"""
|
|
|
try:
|
|
|
+ tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
status_label = "上架" if body.status == "on" else "下架"
|
|
|
for pid in body.ids:
|
|
|
- update_product(db, pid, user_id=user_id, status=body.status)
|
|
|
+ update_product(db, pid, user_id=user_id, user_info=user_info, status=body.status)
|
|
|
return success_response(None, message=f"批量{status_label}成功")
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -712,11 +730,13 @@ async def product_options(
|
|
|
"""获取产品下拉选项"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
options = list_product_options(
|
|
|
db,
|
|
|
tenant_no,
|
|
|
keyword=body.keyword,
|
|
|
category_id=body.category_id,
|
|
|
+ user_info=user_info,
|
|
|
)
|
|
|
return success_response(options)
|
|
|
except ValueError as e:
|
|
|
@@ -739,6 +759,7 @@ async def product_import(
|
|
|
from openpyxl import load_workbook
|
|
|
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
|
|
|
content = await file.read()
|
|
|
@@ -833,6 +854,7 @@ async def product_export(
|
|
|
"""Excel 导出产品列表"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
rows, _ = list_products(
|
|
|
db,
|
|
|
tenant_no,
|
|
|
@@ -1424,10 +1446,11 @@ async def contact_list(
|
|
|
"""联系人分页列表"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
page = body.page if body else 1
|
|
|
page_size = body.pageSize if body else 10
|
|
|
rows, total = list_contacts(
|
|
|
- db, tenant_no, page=page, page_size=page_size,
|
|
|
+ db, tenant_no, page=page, page_size=page_size, user_info=user_info,
|
|
|
keyword=body.keyword if body else None,
|
|
|
contact_name=body.contact_name if body else None,
|
|
|
mobile=body.mobile if body else None,
|
|
|
@@ -1461,9 +1484,12 @@ async def contact_detail(
|
|
|
contact_id = body.get("id")
|
|
|
if not contact_id:
|
|
|
return error_response(400, "缺少联系人ID")
|
|
|
+ tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
row = get_contact(db, contact_id)
|
|
|
if not row:
|
|
|
return error_response(404, "联系人不存在")
|
|
|
+ ensure_crm_contact_row_in_scope(db, row, user_info, tenant_no, message="无权访问该联系人")
|
|
|
return success_response(contact_row_to_dict(row))
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -1480,10 +1506,11 @@ async def contact_by_customer(
|
|
|
"""按客户ID查询联系人"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
customer_id = body.get("customerId", "")
|
|
|
if not customer_id:
|
|
|
return error_response(400, "缺少客户ID")
|
|
|
- rows = list_contacts_by_customer(db, tenant_no, customer_id)
|
|
|
+ rows = list_contacts_by_customer(db, tenant_no, customer_id, user_info=user_info)
|
|
|
return success_response(rows)
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -1500,9 +1527,11 @@ async def contact_add(
|
|
|
"""新增联系人"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
obj = create_contact(
|
|
|
- db, tenant_no=tenant_no, user_id=user_id,
|
|
|
+ db, tenant_no=tenant_no, user_id=user_id, user_info=user_info,
|
|
|
**body.model_dump(exclude_unset=True),
|
|
|
)
|
|
|
return success_response(contact_row_to_dict(obj), message="创建成功")
|
|
|
@@ -1523,7 +1552,9 @@ async def contact_update(
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
data = body.model_dump(exclude_unset=True)
|
|
|
contact_id = data.pop("id")
|
|
|
- obj = update_contact(db, contact_id, user_id=user_id, **data)
|
|
|
+ tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
+ obj = update_contact(db, contact_id, user_id=user_id, tenant_no=tenant_no, user_info=user_info, **data)
|
|
|
return success_response(contact_row_to_dict(obj), message="更新成功")
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -1539,8 +1570,11 @@ async def contact_delete(
|
|
|
):
|
|
|
"""删除联系人"""
|
|
|
try:
|
|
|
+ tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
+ user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
for cid in body.ids:
|
|
|
- soft_delete_contact(db, cid)
|
|
|
+ soft_delete_contact(db, cid, user_id=user_id, tenant_no=tenant_no, user_info=user_info)
|
|
|
return success_response(None, message="删除成功")
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -1560,7 +1594,9 @@ async def contact_set_primary(
|
|
|
if not contact_id:
|
|
|
return error_response(400, "缺少联系人ID")
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
- obj = set_primary_contact(db, contact_id, user_id=user_id)
|
|
|
+ tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
+ obj = set_primary_contact(db, contact_id, user_id=user_id, tenant_no=tenant_no, user_info=user_info)
|
|
|
return success_response(contact_row_to_dict(obj), message="设置成功")
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -1577,8 +1613,9 @@ async def contact_export(
|
|
|
"""导出联系人列表"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
rows, _ = list_contacts(
|
|
|
- db, tenant_no, page=1, page_size=10000,
|
|
|
+ db, tenant_no, page=1, page_size=10000, user_info=user_info,
|
|
|
keyword=body.keyword if body else None,
|
|
|
customer_id=body.customer_id if body else None,
|
|
|
)
|
|
|
@@ -1606,10 +1643,11 @@ async def contact_decision(
|
|
|
"""决策人联系人列表"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
page = body.page if body else 1
|
|
|
page_size = body.pageSize if body else 10
|
|
|
rows, total = list_contacts(
|
|
|
- db, tenant_no, page=page, page_size=page_size,
|
|
|
+ db, tenant_no, page=page, page_size=page_size, user_info=user_info,
|
|
|
is_decision=True,
|
|
|
keyword=body.keyword if body else None,
|
|
|
)
|
|
|
@@ -1636,10 +1674,11 @@ async def contact_primary(
|
|
|
"""主联系人列表"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
page = body.page if body else 1
|
|
|
page_size = body.pageSize if body else 10
|
|
|
rows, total = list_contacts(
|
|
|
- db, tenant_no, page=page, page_size=page_size,
|
|
|
+ db, tenant_no, page=page, page_size=page_size, user_info=user_info,
|
|
|
is_primary=True,
|
|
|
keyword=body.keyword if body else None,
|
|
|
)
|
|
|
@@ -1666,10 +1705,11 @@ async def contact_birthday(
|
|
|
"""本月生日联系人列表"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
page = body.page if body else 1
|
|
|
page_size = body.pageSize if body else 10
|
|
|
rows, total = list_contacts(
|
|
|
- db, tenant_no, page=page, page_size=page_size,
|
|
|
+ db, tenant_no, page=page, page_size=page_size, user_info=user_info,
|
|
|
birthday_month=datetime.now().month,
|
|
|
keyword=body.keyword if body else None,
|
|
|
)
|
|
|
@@ -3044,10 +3084,12 @@ async def supplier_info_list(
|
|
|
"""分页查询供方列表 — Java: CrmSupplierInfoController.list"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
list_body = body or SupplierInfoListBody()
|
|
|
rows, total = list_supplier_info(
|
|
|
db,
|
|
|
tenant_no,
|
|
|
+ user_info=user_info,
|
|
|
page=list_body.page,
|
|
|
page_size=list_body.pageSize,
|
|
|
supplier_name=list_body.supplier_name,
|
|
|
@@ -3077,10 +3119,12 @@ async def supplier_info_list_all(
|
|
|
"""供方全量列表(下拉)— Java: CrmSupplierInfoController.listAll"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
list_body = body or SupplierInfoListAllBody()
|
|
|
rows = list_all_supplier_info(
|
|
|
db,
|
|
|
tenant_no,
|
|
|
+ user_info=user_info,
|
|
|
supplier_name=list_body.supplier_name,
|
|
|
status=list_body.status,
|
|
|
)
|
|
|
@@ -3100,9 +3144,13 @@ async def supplier_info_detail(
|
|
|
"""供方详情 — Java: CrmSupplierInfoController.getInfo"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
obj = get_supplier_info(db, body.id, tenant_no=tenant_no)
|
|
|
if not obj:
|
|
|
return error_response(404, "供方信息不存在")
|
|
|
+ ensure_crm_create_by_row_in_scope(
|
|
|
+ db, obj, user_info, tenant_no, message="无权访问该供方信息"
|
|
|
+ )
|
|
|
return success_response(supplier_info_row_to_dict(obj))
|
|
|
except ValueError as e:
|
|
|
return error_response(400, str(e))
|
|
|
@@ -3119,6 +3167,7 @@ async def supplier_info_add(
|
|
|
"""新增供方 — Java: CrmSupplierInfoController.add"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
dept_id = _user.get("dept_id", "") or _user.get("create_dept_id", "") or ""
|
|
|
obj = create_supplier_info(
|
|
|
@@ -3144,6 +3193,7 @@ async def supplier_info_update(
|
|
|
"""修改供方 — Java: CrmSupplierInfoController.edit"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
data = body.model_dump(exclude_unset=True)
|
|
|
supplier_id = data.pop("id")
|
|
|
@@ -3152,6 +3202,7 @@ async def supplier_info_update(
|
|
|
supplier_id,
|
|
|
tenant_no=tenant_no,
|
|
|
user_id=user_id,
|
|
|
+ user_info=user_info,
|
|
|
**data,
|
|
|
)
|
|
|
return success_response(supplier_info_row_to_dict(obj), message="修改成功")
|
|
|
@@ -3170,6 +3221,7 @@ async def supplier_info_delete(
|
|
|
"""删除供方 — Java: CrmSupplierInfoController.remove"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
ids_to_delete: List[str] = []
|
|
|
if body.ids:
|
|
|
@@ -3198,10 +3250,12 @@ async def supplier_info_export(
|
|
|
"""导出供方列表 — Java: CrmSupplierInfoController.export"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
list_body = body or SupplierInfoListBody()
|
|
|
rows, _ = list_supplier_info(
|
|
|
db,
|
|
|
tenant_no,
|
|
|
+ user_info=user_info,
|
|
|
page=1,
|
|
|
page_size=10000,
|
|
|
supplier_name=list_body.supplier_name,
|
|
|
@@ -3231,7 +3285,10 @@ async def _invoice_info_by_customer_impl(
|
|
|
db: Session,
|
|
|
):
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
- rows = list_invoice_info_by_customer(db, tenant_no, body.customer_id)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
+ rows = list_invoice_info_by_customer(
|
|
|
+ db, tenant_no, body.customer_id, user_info=user_info
|
|
|
+ )
|
|
|
return success_response(rows)
|
|
|
|
|
|
|
|
|
@@ -3274,11 +3331,13 @@ async def invoice_info_add(
|
|
|
"""新增开票信息"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
obj = create_invoice_info(
|
|
|
db,
|
|
|
tenant_no=tenant_no,
|
|
|
user_id=user_id,
|
|
|
+ user_info=user_info,
|
|
|
**body.model_dump(exclude_unset=True),
|
|
|
)
|
|
|
return success_response(invoice_info_row_to_dict(obj), message="创建成功")
|
|
|
@@ -3297,6 +3356,7 @@ async def invoice_info_update(
|
|
|
"""更新开票信息"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
data = body.model_dump(exclude_unset=True)
|
|
|
invoice_id = data.pop("id")
|
|
|
@@ -3305,6 +3365,7 @@ async def invoice_info_update(
|
|
|
invoice_id,
|
|
|
tenant_no=tenant_no,
|
|
|
user_id=user_id,
|
|
|
+ user_info=user_info,
|
|
|
**data,
|
|
|
)
|
|
|
return success_response(invoice_info_row_to_dict(obj), message="更新成功")
|
|
|
@@ -3346,6 +3407,7 @@ async def invoice_info_set_default(
|
|
|
"""设置默认开票信息"""
|
|
|
try:
|
|
|
tenant_no = get_tenant_no(db, _user)
|
|
|
+ user_info = crm_user_info(_user)
|
|
|
user_id = _user.get("user_id", "") or _user.get("id", "")
|
|
|
obj = set_default_invoice_info(
|
|
|
db,
|