-- ============================================================
-- 关系图谱固定测试数据
-- 说明：
-- 1. 仅用于开发联调，执行前先执行 03_lx_relation_graph_mysql_ddl.sql。
-- 2. 只写入 lx_rel_node / lx_rel_family_edge / lx_rel_stock_edge / lx_rel_represent_edge。
-- 3. 测试数据以 GRAPH_TEST 标记，可重复执行。
-- 4. 覆盖家庭关系、股东持股、法定代表人三类关系。
-- 5. 家庭关系按身份证 MD5 成点，夫妻双方只保留一条配偶边。
-- ============================================================

SET @staff_a_id := '330101198001010011';
SET @staff_b_id := '330101198202020022';
SET @child_id := '330101201206010033';
SET @staff_a_key := MD5(@staff_a_id);
SET @staff_b_key := MD5(@staff_b_id);
SET @child_key := MD5(@child_id);
SET @family_spouse_from := LEAST(@staff_a_key, @staff_b_key);
SET @family_spouse_to := GREATEST(@staff_a_key, @staff_b_key);
SET @family_child_from := LEAST(@staff_a_key, @child_key);
SET @family_child_to := GREATEST(@staff_a_key, @child_key);

SET @company_key := 'GRAPH_TEST_COMPANY_001';
SET @company_credit_code := '91330100GRAPH00001';
SET @person_holder_key := 'GRAPH_TEST_PERSON_HOLDER_001';
SET @spouse_holder_key := 'GRAPH_TEST_PERSON_SPOUSE_HOLDER_001';
SET @enterprise_holder_key := 'GRAPH_TEST_ENTERPRISE_HOLDER_001';
SET @legal_key := 'GRAPH_TEST_LEGAL_001';

-- 清理上一轮 GRAPH_TEST 数据。只清理测试标记数据，不影响生产/基座数据。
DELETE FROM lx_rel_family_edge
WHERE data_source = 'GRAPH_TEST';

DELETE FROM lx_rel_stock_edge
WHERE object_key LIKE 'GRAPH_TEST_%';

DELETE FROM lx_rel_represent_edge
WHERE object_key LIKE 'GRAPH_TEST_%';

DELETE FROM lx_rel_node
WHERE source_type LIKE 'GRAPH_TEST%';

-- 测试点：家庭关系个人。
INSERT INTO lx_rel_node (
    object_key,
    node_name,
    id_number,
    subject_type,
    source_type,
    detail_ref_type,
    detail_ref_key
)
VALUES
(@staff_a_key, '关系图谱测试员工甲', @staff_a_id, '个人', 'GRAPH_TEST_员工家庭关系-员工', 'STAFF_FAMILY', @staff_a_id),
(@staff_b_key, '关系图谱测试员工乙', @staff_b_id, '个人', 'GRAPH_TEST_员工家庭关系-员工,GRAPH_TEST_员工家庭关系-成员', 'STAFF_FAMILY', @staff_b_id),
(@child_key, '关系图谱测试子女', @child_id, '个人', 'GRAPH_TEST_员工家庭关系-成员', 'STAFF_FAMILY', @child_id)
ON DUPLICATE KEY UPDATE
    node_name = VALUES(node_name),
    id_number = VALUES(id_number),
    subject_type = VALUES(subject_type),
    source_type = VALUES(source_type),
    detail_ref_type = VALUES(detail_ref_type),
    detail_ref_key = VALUES(detail_ref_key),
    updated_time = CURRENT_TIMESTAMP;

-- 测试边：家庭关系，夫妻双方只落一条边。
INSERT INTO lx_rel_family_edge (
    object_key,
    from_key,
    to_key,
    person_id,
    relation_name,
    relation_type,
    relation_cert_no,
    gender,
    birth_date,
    relation_cert_type,
    mobile_phone1,
    mobile_phone2,
    wechat_no1,
    wechat_no2,
    wechat_no3,
    contact_address,
    annual_income,
    relation_desc,
    status,
    effective_date,
    invalid_date,
    remark,
    data_source
)
VALUES
(MD5(CONCAT('family|', @family_spouse_from, '|', @family_spouse_to)),
 CONCAT('rel_node/', @family_spouse_from),
 CONCAT('rel_node/', @family_spouse_to),
 CONCAT(@staff_a_id, '/', @staff_b_id),
 '关系图谱测试员工甲/关系图谱测试员工乙',
 '配偶',
 CONCAT(@staff_a_id, '/', @staff_b_id),
 'F/M',
 '1980-01-01/1982-02-02',
 '身份证',
 '13800000001/13800000002',
 NULL,
 NULL,
 NULL,
 NULL,
 '杭州市测试区家庭路1号',
 320000.00,
 '夫妻双方都是员工，归并为一条家庭边',
 '1',
 '2024-01-01 00:00:00',
 NULL,
 'GRAPH_TEST',
 'GRAPH_TEST'),
(MD5(CONCAT('family|', @family_child_from, '|', @family_child_to)),
 CONCAT('rel_node/', @family_child_from),
 CONCAT('rel_node/', @family_child_to),
 @staff_a_id,
 '关系图谱测试子女',
 '子女',
 @child_id,
 'M',
 '2012-06-01',
 '身份证',
 '13800000003',
 NULL,
 NULL,
 NULL,
 NULL,
 '杭州市测试区家庭路1号',
 NULL,
 '员工甲与子女关系',
 '1',
 '2024-01-01 00:00:00',
 NULL,
 'GRAPH_TEST',
 'GRAPH_TEST')
ON DUPLICATE KEY UPDATE
    from_key = VALUES(from_key),
    to_key = VALUES(to_key),
    person_id = VALUES(person_id),
    relation_name = VALUES(relation_name),
    relation_type = VALUES(relation_type),
    relation_cert_no = VALUES(relation_cert_no),
    gender = VALUES(gender),
    birth_date = VALUES(birth_date),
    relation_cert_type = VALUES(relation_cert_type),
    mobile_phone1 = VALUES(mobile_phone1),
    contact_address = VALUES(contact_address),
    annual_income = VALUES(annual_income),
    relation_desc = VALUES(relation_desc),
    status = VALUES(status),
    effective_date = VALUES(effective_date),
    remark = VALUES(remark),
    data_source = VALUES(data_source),
    updated_time = CURRENT_TIMESTAMP;

-- 测试点：工商企业、股东、法人。
INSERT INTO lx_rel_node (
    object_key,
    node_name,
    id_number,
    subject_type,
    source_type,
    detail_ref_type,
    detail_ref_key
)
VALUES
(@company_key, '关系图谱测试科技有限公司', @company_credit_code, '企业', 'GRAPH_TEST_工商企业', 'ECI_COMPANY', @company_key),
(@person_holder_key, '关系图谱测试自然人股东', NULL, '个人', 'GRAPH_TEST_自然人股东', 'ECI_PARTNER', @person_holder_key),
(@enterprise_holder_key, '关系图谱测试企业股东有限公司', '91330100GRAPH00002', '企业', 'GRAPH_TEST_企业股东', 'ECI_PARTNER', @enterprise_holder_key),
(@legal_key, '关系图谱测试法定代表人', NULL, '个人', 'GRAPH_TEST_法定代表人', 'ECI_COMPANY_OPERATOR', @legal_key)
ON DUPLICATE KEY UPDATE
    node_name = VALUES(node_name),
    id_number = VALUES(id_number),
    subject_type = VALUES(subject_type),
    source_type = VALUES(source_type),
    detail_ref_type = VALUES(detail_ref_type),
    detail_ref_key = VALUES(detail_ref_key),
    updated_time = CURRENT_TIMESTAMP;

-- 测试边：股东持股关系。
INSERT INTO lx_rel_stock_edge (
    object_key,
    from_key,
    to_key,
    company_name,
    stock_name,
    stock_type,
    stock_percent,
    should_capi,
    should_capi_value,
    should_capi_unit,
    shoud_date,
    p_key_no
)
VALUES
('GRAPH_TEST_STOCK_PERSON_001',
 CONCAT('rel_node/', @person_holder_key),
 CONCAT('rel_node/', @company_key),
 '关系图谱测试科技有限公司',
 '关系图谱测试自然人股东',
 '自然人股东',
 '60%',
 '600',
 '600',
 '万元人民币',
 '2024-05-01',
 @person_holder_key),
('GRAPH_TEST_STOCK_SPOUSE_CANDIDATE_001',
 CONCAT('rel_node/', @spouse_holder_key),
 CONCAT('rel_node/', @company_key),
 '关系图谱测试科技有限公司',
 '关系图谱测试员工乙',
 '自然人股东',
 '15%',
 '150',
 '150',
 '万元人民币',
 '2024-05-01',
 @spouse_holder_key),
('GRAPH_TEST_STOCK_ENTERPRISE_001',
 CONCAT('rel_node/', @enterprise_holder_key),
 CONCAT('rel_node/', @company_key),
 '关系图谱测试科技有限公司',
 '关系图谱测试企业股东有限公司',
 '企业股东',
 '40%',
 '400',
 '400',
 '万元人民币',
 '2024-05-01',
 @enterprise_holder_key)
ON DUPLICATE KEY UPDATE
    from_key = VALUES(from_key),
    to_key = VALUES(to_key),
    company_name = VALUES(company_name),
    stock_name = VALUES(stock_name),
    stock_type = VALUES(stock_type),
    stock_percent = VALUES(stock_percent),
    should_capi = VALUES(should_capi),
    should_capi_value = VALUES(should_capi_value),
    should_capi_unit = VALUES(should_capi_unit),
    shoud_date = VALUES(shoud_date),
    p_key_no = VALUES(p_key_no),
    updated_time = CURRENT_TIMESTAMP;

-- 测试边：法定代表人关系。
INSERT INTO lx_rel_represent_edge (
    object_key,
    from_key,
    to_key,
    oper_name,
    oper_key_no
)
VALUES
('GRAPH_TEST_REPRESENT_001',
 CONCAT('rel_node/', @legal_key),
 CONCAT('rel_node/', @company_key),
 '关系图谱测试法定代表人',
 @legal_key)
ON DUPLICATE KEY UPDATE
    from_key = VALUES(from_key),
    to_key = VALUES(to_key),
    oper_name = VALUES(oper_name),
    oper_key_no = VALUES(oper_key_no),
    updated_time = CURRENT_TIMESTAMP;

SELECT 'lx_rel_node' AS table_name, COUNT(1) AS graph_test_rows
FROM lx_rel_node
WHERE source_type LIKE 'GRAPH_TEST%'
UNION ALL
SELECT 'lx_rel_family_edge' AS table_name, COUNT(1) AS graph_test_rows
FROM lx_rel_family_edge
WHERE data_source = 'GRAPH_TEST'
UNION ALL
SELECT 'lx_rel_stock_edge' AS table_name, COUNT(1) AS graph_test_rows
FROM lx_rel_stock_edge
WHERE object_key LIKE 'GRAPH_TEST_%'
UNION ALL
SELECT 'lx_rel_represent_edge' AS table_name, COUNT(1) AS graph_test_rows
FROM lx_rel_represent_edge
WHERE object_key LIKE 'GRAPH_TEST_%';

SELECT
    @staff_a_id AS test_person_id,
    @staff_a_key AS test_object_key,
    @company_key AS test_company_object_key;
