Merge branch 'dev' into runny827
feat(training): 新增精益培训视频与周任务管理 # Conflicts: # backend/app/models/__init__.py
This commit is contained in:
1 parent
85d27e9ffa
commit
91b7233872
812 files changed
+73763
-18908
No files matched your search
@@ -16,7 +16,8 @@ from app import db
|
||||
from app.core.errors import ConflictError, NotFoundError, ValidationError
|
||||
from app.models import (
|
||||
ShippingBusinessSequence,
|
||||
ShippingNotice,
|
||||
ShippingNotice, ShippingOrderTask, ShippingOrderExtraction,
|
||||
ShippingOrderSchema, ShippingOrderSchemaVersion,
|
||||
ShippingCustomer,
|
||||
ShippingCustomerExternalRef,
|
||||
ShippingCustomerIdentifier,
|
||||
@@ -53,6 +54,41 @@ def _context_field() -> ShippingOrderField:
|
||||
return ShippingOrderField.query.filter_by(field_code='bill_to_customer').one()
|
||||
|
||||
|
||||
def _notice_for_customer(customer: ShippingCustomer, task_no: str) -> ShippingNotice:
|
||||
"""按当前事实链构造带客户引用的正式 Notice,避免写入已删除的 Notice.customer_id。"""
|
||||
schema = ShippingOrderSchema(
|
||||
schema_code=f'LIFECYCLE-{task_no}',
|
||||
display_name='生命周期测试 Schema',
|
||||
status='active',
|
||||
)
|
||||
db.session.add(schema)
|
||||
db.session.flush()
|
||||
version = ShippingOrderSchemaVersion(
|
||||
schema_id=schema.id,
|
||||
version=1,
|
||||
status='published',
|
||||
definition_json={'fields': []},
|
||||
checksum='lifecycle-test',
|
||||
)
|
||||
task = ShippingOrderTask(task_no=f'OT-{task_no}', user_id=1, status='completed')
|
||||
db.session.add_all([version, task])
|
||||
db.session.flush()
|
||||
extraction = ShippingOrderExtraction(
|
||||
task_id=task.id,
|
||||
schema_version_id=version.id,
|
||||
input_snapshot_hash=f'{task_no:0<64}'[:64],
|
||||
status='accepted',
|
||||
)
|
||||
db.session.add(extraction)
|
||||
db.session.flush()
|
||||
task.current_order_extraction_id = extraction.id
|
||||
from tests.conftest import confirm_test_customer_resolution
|
||||
confirm_test_customer_resolution(task, customer=customer, extraction=extraction)
|
||||
notice = ShippingNotice(task_no=task_no, order_task_id=task.id)
|
||||
db.session.add(notice)
|
||||
return notice
|
||||
|
||||
|
||||
def test_unused_customer_can_delete_and_identifiers_are_cascade_only(app):
|
||||
"""仅有识别证据的误建客户可删除,证据不应成为业务阻断引用。"""
|
||||
with app.app_context():
|
||||
@@ -74,15 +110,13 @@ def test_unused_customer_can_delete_and_identifiers_are_cascade_only(app):
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reference_kind', [
|
||||
'notice', 'sequence', 'external_ref', 'import_target', 'import_result', 'shadow_top', 'shadow_manual', 'shadow_match',
|
||||
'sequence', 'external_ref', 'import_target', 'import_result', 'shadow_top', 'shadow_manual', 'shadow_match',
|
||||
])
|
||||
def test_business_and_audit_references_block_delete(app, reference_kind: str):
|
||||
"""每类业务或识别审计引用都必须在统一影响结果中阻断永久删除。"""
|
||||
with app.app_context():
|
||||
customer = _customer()
|
||||
if reference_kind == 'notice':
|
||||
db.session.add(ShippingNotice(task_no='SN-LIFECYCLE', customer_id=customer.id))
|
||||
elif reference_kind == 'sequence':
|
||||
if reference_kind == 'sequence':
|
||||
db.session.add(ShippingBusinessSequence(customer_id=customer.id, business_date=date(2026, 7, 16)))
|
||||
elif reference_kind == 'external_ref':
|
||||
db.session.add(ShippingCustomerExternalRef(customer_id=customer.id, source_system='ERP', legacy_customer_key='C-1'))
|
||||
@@ -201,7 +235,7 @@ def test_delete_rechecks_every_lifecycle_blocker(app, reference_kind: str):
|
||||
with app.app_context():
|
||||
customer = _customer('BLK')
|
||||
if reference_kind == 'notice':
|
||||
db.session.add(ShippingNotice(task_no='SN-DELETE-BLOCK', customer_id=customer.id))
|
||||
_notice_for_customer(customer, 'SN-DELETE-BLOCK')
|
||||
elif reference_kind == 'sequence':
|
||||
db.session.add(ShippingBusinessSequence(customer_id=customer.id, business_date=date(2026, 7, 16)))
|
||||
elif reference_kind == 'external_ref':
|
||||
@@ -267,7 +301,7 @@ def test_delete_blocks_reference_created_after_preview(app):
|
||||
preview = get_customer_lifecycle_impact(customer.id)
|
||||
assert preview['can_delete'] is True
|
||||
|
||||
db.session.add(ShippingNotice(task_no='SN-DELETE-RACE', customer_id=customer.id))
|
||||
_notice_for_customer(customer, 'SN-DELETE-RACE')
|
||||
db.session.commit()
|
||||
with pytest.raises(ConflictError) as exc_info:
|
||||
delete_customer(customer.id, confirmation_short_code='RCE', reason='误建', actor_id=None)
|
||||
|
||||
@@ -26,7 +26,8 @@ from app.models import (
|
||||
ShippingCustomerMerge,
|
||||
ShippingCustomerShadowResolution,
|
||||
ShippingOrderField,
|
||||
ShippingOrderSchema,
|
||||
ShippingOrderSchema, ShippingOrderSchemaVersion, ShippingOrderTask,
|
||||
ShippingOrderExtraction,
|
||||
)
|
||||
from app.services.admin.shipping_customer_lifecycle_service import get_customer_lifecycle_impact
|
||||
from app.services.admin.shipping_customer_merge_service import get_customer_merge_impact, merge_customer
|
||||
@@ -52,6 +53,30 @@ def _context_field() -> ShippingOrderField:
|
||||
return ShippingOrderField.query.filter_by(field_code='bill_to_customer').one()
|
||||
|
||||
|
||||
def _notice_with_customer_history(customer: ShippingCustomer) -> ShippingNotice:
|
||||
"""按 OT→Resolution 构造历史 Notice 客户事实。"""
|
||||
schema = ShippingOrderSchema(schema_code='MERGE-HISTORY', display_name='归并历史测试', status='active')
|
||||
db.session.add(schema)
|
||||
db.session.flush()
|
||||
version = ShippingOrderSchemaVersion(
|
||||
schema_id=schema.id, version=1, status='published', definition_json={'fields': []}, checksum='merge-history',
|
||||
)
|
||||
task = ShippingOrderTask(task_no='OT-SN-MERGE-HISTORY', user_id=1, status='completed')
|
||||
db.session.add_all([version, task])
|
||||
db.session.flush()
|
||||
extraction = ShippingOrderExtraction(
|
||||
task_id=task.id, schema_version_id=version.id, input_snapshot_hash='m' * 64, status='accepted',
|
||||
)
|
||||
db.session.add(extraction)
|
||||
db.session.flush()
|
||||
task.current_order_extraction_id = extraction.id
|
||||
from tests.conftest import confirm_test_customer_resolution
|
||||
confirm_test_customer_resolution(task, customer=customer, extraction=extraction)
|
||||
notice = ShippingNotice(task_no='SN-MERGE-HISTORY', business_no='SRC26071601', order_task_id=task.id)
|
||||
db.session.add(notice)
|
||||
return notice
|
||||
|
||||
|
||||
def test_merge_moves_future_configuration_and_preserves_history(app):
|
||||
"""归并迁移证据和来源键,但业务单、流水及其客户快照始终保留源客户。"""
|
||||
with app.app_context():
|
||||
@@ -71,10 +96,7 @@ def test_merge_moves_future_configuration_and_preserves_history(app):
|
||||
normalized_value='same@example.com', status='approved', source='manual',
|
||||
),
|
||||
ShippingCustomerExternalRef(customer_id=source.id, source_system='ERP', legacy_customer_key='LEGACY-SRC'),
|
||||
ShippingNotice(
|
||||
task_no='SN-MERGE-HISTORY', customer_id=source.id, business_no='SRC26071601',
|
||||
customer_name_snapshot=source.standard_name, customer_short_code_snapshot=source.short_code,
|
||||
),
|
||||
_notice_with_customer_history(source),
|
||||
ShippingBusinessSequence(customer_id=source.id, business_date=date(2026, 7, 16), current_value=1),
|
||||
])
|
||||
db.session.commit()
|
||||
@@ -106,9 +128,12 @@ def test_merge_moves_future_configuration_and_preserves_history(app):
|
||||
assert db.session.query(ShippingCustomerIdentifier).filter_by(customer_id=target_id).count() == 2
|
||||
assert db.session.query(ShippingCustomerExternalRef).filter_by(customer_id=target_id).count() == 1
|
||||
historical_notice = db.session.query(ShippingNotice).filter_by(task_no='SN-MERGE-HISTORY').one()
|
||||
assert historical_notice.customer_id == source_id
|
||||
historical_task = db.session.get(ShippingOrderTask, historical_notice.order_task_id)
|
||||
historical_resolution = ShippingCustomerShadowResolution.query.filter_by(
|
||||
candidate_id=historical_task.candidate_id,
|
||||
).one()
|
||||
assert historical_resolution.confirmed_customer_id == source_id
|
||||
assert historical_notice.business_no == 'SRC26071601'
|
||||
assert historical_notice.customer_short_code_snapshot == 'SRC'
|
||||
assert db.session.query(ShippingBusinessSequence).filter_by(customer_id=source_id).count() == 1
|
||||
merge = db.session.get(ShippingCustomerMerge, result['merge_id'])
|
||||
assert merge is not None and merge.source_customer_id_snapshot == source_id
|
||||
|
||||
@@ -13,7 +13,11 @@ from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app import db
|
||||
from app.core.errors import ConflictError, ValidationError
|
||||
from app.models import ShippingCustomer, ShippingCustomerIdentifier, ShippingCustomerLifecycleEvent, ShippingNotice
|
||||
from app.models import (
|
||||
ShippingCustomer, ShippingCustomerIdentifier, ShippingCustomerLifecycleEvent, ShippingNotice,
|
||||
ShippingOrderExtraction, ShippingOrderSchema, ShippingOrderSchemaVersion,
|
||||
ShippingOrderTask,
|
||||
)
|
||||
from app.services.admin.shipping_customer_service import (
|
||||
create_customer,
|
||||
create_identifier,
|
||||
@@ -28,6 +32,30 @@ def _create_customer(*, name: str = '无锡宇辰有限公司', short_code: str
|
||||
return create_customer({'standard_name': name, 'short_code': short_code}, actor_id=None)
|
||||
|
||||
|
||||
def _notice_for_customer(customer_id: int) -> ShippingNotice:
|
||||
"""按正式客户事实链创建 Notice,列表统计不得依赖已删除的 Notice.customer_id。"""
|
||||
schema = ShippingOrderSchema(schema_code='CUSTOMER-LIST', display_name='客户列表测试', status='active')
|
||||
db.session.add(schema)
|
||||
db.session.flush()
|
||||
version = ShippingOrderSchemaVersion(
|
||||
schema_id=schema.id, version=1, status='published', definition_json={'fields': []}, checksum='customer-list',
|
||||
)
|
||||
task = ShippingOrderTask(task_no='OT-CUSTOMER-LIST', user_id=1, status='completed')
|
||||
db.session.add_all([version, task])
|
||||
db.session.flush()
|
||||
extraction = ShippingOrderExtraction(
|
||||
task_id=task.id, schema_version_id=version.id, input_snapshot_hash='c' * 64, status='accepted',
|
||||
)
|
||||
db.session.add(extraction)
|
||||
db.session.flush()
|
||||
task.current_order_extraction_id = extraction.id
|
||||
from tests.conftest import confirm_test_customer_resolution
|
||||
confirm_test_customer_resolution(task, customer=db.session.get(ShippingCustomer, customer_id), extraction=extraction)
|
||||
notice = ShippingNotice(task_no='SN-COUNT-1', order_task_id=task.id)
|
||||
db.session.add(notice)
|
||||
return notice
|
||||
|
||||
|
||||
def test_customer_short_code_is_normalized_and_unique(app):
|
||||
"""简码统一转大写,且标准化后重复必须返回冲突。"""
|
||||
with app.app_context():
|
||||
@@ -219,7 +247,7 @@ def test_customer_list_returns_preaggregated_reference_counts(app):
|
||||
"""列表返回业务单和识别证据数量,避免管理端逐客户补查。"""
|
||||
with app.app_context():
|
||||
customer = _create_customer()
|
||||
db.session.add(ShippingNotice(task_no='SN-COUNT-1', customer_id=customer['id']))
|
||||
_notice_for_customer(customer['id'])
|
||||
db.session.add(ShippingCustomerIdentifier(
|
||||
customer_id=customer['id'],
|
||||
identifier_type='name_alias',
|
||||
|
||||
Reference in new issue
Block a user