sz-rust-orm-ext-facade 0.6.1

ORM 扩展层 facade(P3)— model/hooks/relation 基于 sz-rust-orm-facade 的框架层抽象
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
//! `MorphMany` / `MorphTo` 多态关联 — PHP 命名约定 + SQL 片段构造器
//!
//! 本模块对齐 PHP `think\Model::morphMany()` /
//! `morphTo()` 行为,提供:
//!
//! 1. [`default_morph_type_column`]:PHP 默认多态类型列名(`morph . '_type'`)
//! 2. [`default_morph_id_column`]:PHP 默认多态外键列名(`morph . '_id'`)
//! 3. [`php_morph_many`]:构造 `MorphMany` 配置(应用 PHP 默认值)
//! 4. [`php_morph_to`]:构造 `MorphTo` 配置(应用 PHP 默认值)
//! 5. [`morph_many_sql`]:生成 MorphMany 单条 SQL 片段
//! 6. [`morph_many_in_sql`]:生成 MorphMany 批量 IN 查询 SQL 片段
//! 7. [`morph_to_sql`]:生成 MorphTo 单条 SQL 片段
//! 8. [`group_by_morph_type`]:按多态类型分桶(对齐 PHP `MorphTo::eagerlyResultSet`)
//!
//! ## PHP 端 `morphMany` 签名(think-orm 2.0.x,RelationShip.php 第 571-591 行)
//!
//! ```php
//! public function morphMany(string $model, $morph = null, string $type = ''): MorphMany
//! {
//!     $model = $this->parseModel($model);
//!
//!     if (is_null($morph)) {
//!         $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
//!         $morph = Str::snake($trace[1]['function']);  // 调用方方法名 snake_case
//!     }
//!
//!     $type = $type ?: get_class($this);  // 默认为父模型类名
//!
//!     if (is_array($morph)) {
//!         [$morphType, $foreignKey] = $morph;
//!     } else {
//!         $morphType  = $morph . '_type';
//!         $foreignKey = $morph . '_id';
//!     }
//!
//!     return new MorphMany($this, $model, $foreignKey, $morphType, $type);
//! }
//! ```
//!
//! ## PHP 端 `morphTo` 签名(think-orm 2.0.x,RelationShip.php 第 600-618 行)
//!
//! ```php
//! public function morphTo($morph = null, array $alias = []): MorphTo
//! {
//!     $trace    = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
//!     $relation = Str::snake($trace[1]['function']);
//!
//!     if (is_null($morph)) {
//!         $morph = $relation;  // 默认为关联名
//!     }
//!
//!     if (is_array($morph)) {
//!         [$morphType, $foreignKey] = $morph;
//!     } else {
//!         $morphType  = $morph . '_type';
//!         $foreignKey = $morph . '_id';
//!     }
//!
//!     return new MorphTo($this, $morphType, $foreignKey, $alias, $relation);
//! }
//! ```
//!
//! ## sz-orm-core vs PHP 命名映射
//!
//! | sz-orm-core 字段 | PHP MorphMany 字段 | 含义 |
//! |------------------|--------------------|------|
//! | `child_model` | `$model` | 子表名 |
//! | `morph_type_column` | `$morphType` | 多态类型列名(如 `commentable_type`) |
//! | `morph_id_column` | `$morphKey` / `$foreignKey` | 多态外键列名(如 `commentable_id`) |
//! | `morph_type_value` | `$type` | 父模型类型标识(如 `Post`) |
//!
//! | sz-orm-core 字段 | PHP MorphTo 字段 | 含义 |
//! |------------------|------------------|------|
//! | `morph_type_column` | `$morphType` | 多态类型列名 |
//! | `morph_id_column` | `$morphKey` / `$foreignKey` | 多态外键列名 |
//!
//! ## 生成的 SQL
//!
//! ### MorphMany 单条查询(sz-orm-core::WithRelation::load MorphMany 分支)
//!
//! ```sql
//! SELECT * FROM {child_table} WHERE {morph_type_col} = '{morph_type_val}' AND {morph_id_col} = {parent_pk}
//! ```
//!
//! ### MorphMany 批量 IN 查询(对齐 PHP `MorphMany::eagerlyResultSet`)
//!
//! ```sql
//! SELECT * FROM {child_table} WHERE {morph_type_col} = '{morph_type_val}' AND {morph_id_col} IN (v1, v2, ...)
//! ```
//!
//! ### MorphTo 单条查询(sz-orm-core::WithRelation::load MorphTo 分支)
//!
//! ```sql
//! SELECT * FROM {morph_type_val_as_table} WHERE id = {morph_id_val}
//! ```
//!
//! **注意**:MorphTo 的目标表名由 `morph_type_value` 动态决定(每行可能不同)。
//!
//! ## PHP 行为对齐
//!
//! ### `morphMany` 默认值推导
//!
//! - `morph` 为 `null`:使用调用方方法名 `Str::snake` 化(Rust 端需调用方显式传入)
//! - `morph` 为字符串:`morphType = morph . '_type'`,`foreignKey = morph . '_id'`
//! - `morph` 为数组:`[morphType, foreignKey] = morph`
//! - `type` 默认:`get_class($this)`(父模型类名)
//!
//! ### `morphTo` 默认值推导
//!
//! - `morph` 为 `null`:使用关联名(Rust 端需调用方显式传入)
//! - `morph` 为字符串:`morphType = morph . '_type'`,`foreignKey = morph . '_id'`
//! - `morph` 为数组:`[morphType, foreignKey] = morph`
//! - **无 `type` 参数**(MorphTo 由子模型数据动态决定)
//!
//! ## 架构说明
//!
//! sz-orm-core::model 模块私有(`mod model;` 非 `pub mod model;`),sz-rust 端无法
//! 实现 `Model`/`RelationLoader` trait,因此本模块不直接执行关联加载,而是提供:
//!
//! - **PHP 命名约定辅助函数**:`default_morph_type_column` / `default_morph_id_column`
//! - **配置构造器**:`php_morph_many` / `php_morph_to` 返回 sz-orm-core 结构体
//! - **SQL 片段构造器**:`morph_many_sql` / `morph_many_in_sql` / `morph_to_sql`
//! - **数据处理纯函数**:`group_by_morph_type`(对齐 PHP `MorphTo::eagerlyResultSet`)
//!
//! 端到端关联加载由 sz-orm-core `WithRelation::load()` 内部实现并测试。

use super::{MorphMany, MorphTo};

// ============================================================================
// PHP 命名约定辅助函数
// ============================================================================

/// PHP `morphMany` / `morphTo` 默认多态类型列名
///
/// 对齐 PHP `RelationShip::morphMany` 第 586 行与 `morphTo` 第 613 行:
///
/// ```php
/// $morphType = $morph . '_type';
/// ```
///
/// ## 示例
///
/// | 输入 | 输出 |
/// |------|------|
/// | `"commentable"` | `"commentable_type"` |
/// | `"imageable"` | `"imageable_type"` |
/// | `"taggable"` | `"taggable_type"` |
pub fn default_morph_type_column(morph: &str) -> String {
    format!("{}_type", morph)
}

/// PHP `morphMany` / `morphTo` 默认多态外键列名
///
/// 对齐 PHP `RelationShip::morphMany` 第 587 行与 `morphTo` 第 614 行:
///
/// ```php
/// $foreignKey = $morph . '_id';
/// ```
///
/// ## 示例
///
/// | 输入 | 输出 |
/// |------|------|
/// | `"commentable"` | `"commentable_id"` |
/// | `"imageable"` | `"imageable_id"` |
/// | `"taggable"` | `"taggable_id"` |
pub fn default_morph_id_column(morph: &str) -> String {
    format!("{}_id", morph)
}

// ============================================================================
// MorphMany / MorphTo 配置构造器
// ============================================================================

/// 构造 `MorphMany` 配置(应用 PHP 默认值)
///
/// 对齐 PHP `think\Model::morphMany($model, $morph = null, $type = '')`:
///
/// - `morph_type_column` 默认:`default_morph_type_column(morph)`
/// - `morph_id_column` 默认:`default_morph_id_column(morph)`
/// - `morph_type_value` 默认:`parent_class`(对齐 PHP `get_class($this)`)
///
/// ## 参数
///
/// - `parent_class`:父模型类名(如 `"Post"` 或 `"app\\model\\Post"`),用于推导默认 `morph_type_value`
/// - `child_table`:子表名(如 `"comments"`)
/// - `morph`:多态字段名前缀(如 `"commentable"`),对应 PHP `$morph` 字符串形态
/// - `morph_type_value`:父模型类型标识(`None` 使用 `parent_class`)
/// - `morph_type_col`:多态类型列名(`None` 使用 `morph . '_type'`,对应 PHP 数组形态显式指定)
/// - `morph_id_col`:多态外键列名(`None` 使用 `morph . '_id'`,对应 PHP 数组形态显式指定)
///
/// ## PHP ↔ sz-orm-core 映射
///
/// | PHP 参数 | sz-orm-core 字段 | 本函数参数 |
/// |---------|------------------|-----------|
/// | `$model` | `child_model` | `child_table` |
/// | `$morphType` | `morph_type_column` | `morph_type_col` |
/// | `$morphKey` / `$foreignKey` | `morph_id_column` | `morph_id_col` |
/// | `$type` | `morph_type_value` | `morph_type_value` |
///
/// ## 示例
///
/// ```ignore
/// use sz_rust_core::relation::morph::php_morph_many;
///
/// // 等价 PHP: $this->morphMany(Comment::class, 'commentable', 'Post')
/// // 在 Post 模型中定义
/// let rel = php_morph_many("Post", "comments", "commentable", None, None, None);
/// assert_eq!(rel.child_model, "comments");
/// assert_eq!(rel.morph_type_column, "commentable_type");
/// assert_eq!(rel.morph_id_column, "commentable_id");
/// assert_eq!(rel.morph_type_value, "Post");
///
/// // 等价 PHP: $this->morphMany(Comment::class, ['c_type', 'c_id'], 'Post')
/// // PHP 数组形态:显式指定 morphType 和 foreignKey
/// let rel = php_morph_many("Post", "comments", "commentable", None, Some("c_type"), Some("c_id"));
/// assert_eq!(rel.morph_type_column, "c_type");
/// assert_eq!(rel.morph_id_column, "c_id");
/// ```
pub fn php_morph_many(
    parent_class: &str,
    child_table: &str,
    morph: &str,
    morph_type_value: Option<&str>,
    morph_type_col: Option<&str>,
    morph_id_col: Option<&str>,
) -> MorphMany {
    MorphMany {
        child_model: child_table.to_string(),
        morph_type_column: morph_type_col
            .map(String::from)
            .unwrap_or_else(|| default_morph_type_column(morph)),
        morph_id_column: morph_id_col
            .map(String::from)
            .unwrap_or_else(|| default_morph_id_column(morph)),
        morph_type_value: morph_type_value
            .map(String::from)
            .unwrap_or_else(|| parent_class.to_string()),
    }
}

/// 构造 `MorphTo` 配置(应用 PHP 默认值)
///
/// 对齐 PHP `think\Model::morphTo($morph = null, array $alias = [])`:
///
/// - `morph_type_column` 默认:`default_morph_type_column(morph)`
/// - `morph_id_column` 默认:`default_morph_id_column(morph)`
///
/// ## 参数
///
/// - `morph`:多态字段名前缀(如 `"commentable"`),对应 PHP `$morph` 字符串形态
/// - `morph_type_col`:多态类型列名(`None` 使用 `morph . '_type'`,对应 PHP 数组形态显式指定)
/// - `morph_id_col`:多态外键列名(`None` 使用 `morph . '_id'`,对应 PHP 数组形态显式指定)
///
/// ## PHP ↔ sz-orm-core 映射
///
/// | PHP 参数 | sz-orm-core 字段 | 本函数参数 |
/// |---------|------------------|-----------|
/// | `$morphType` | `morph_type_column` | `morph_type_col` |
/// | `$morphKey` / `$foreignKey` | `morph_id_column` | `morph_id_col` |
///
/// **注意**:PHP MorphTo 无 `$type` 参数(由子模型数据动态决定),sz-orm-core
/// `MorphTo` struct 也无 `morph_type_value` 字段。
///
/// ## 示例
///
/// ```ignore
/// use sz_rust_core::relation::morph::php_morph_to;
///
/// // 等价 PHP: $this->morphTo('commentable')
/// // 在 Comment 模型中定义
/// let rel = php_morph_to("commentable", None, None);
/// assert_eq!(rel.morph_type_column, "commentable_type");
/// assert_eq!(rel.morph_id_column, "commentable_id");
///
/// // 等价 PHP: $this->morphTo(['c_type', 'c_id'])
/// // PHP 数组形态:显式指定 morphType 和 foreignKey
/// let rel = php_morph_to("commentable", Some("c_type"), Some("c_id"));
/// assert_eq!(rel.morph_type_column, "c_type");
/// assert_eq!(rel.morph_id_column, "c_id");
/// ```
pub fn php_morph_to(
    morph: &str,
    morph_type_col: Option<&str>,
    morph_id_col: Option<&str>,
) -> MorphTo {
    MorphTo {
        morph_type_column: morph_type_col
            .map(String::from)
            .unwrap_or_else(|| default_morph_type_column(morph)),
        morph_id_column: morph_id_col
            .map(String::from)
            .unwrap_or_else(|| default_morph_id_column(morph)),
    }
}

// ============================================================================
// SQL 片段构造器(用于测试验证)
// ============================================================================

/// 生成 `MorphMany` 关联单条查询 SQL 片段
///
/// 对齐 sz-orm-core `WithRelation::load()` 中 `MorphMany` 分支生成的 SQL:
///
/// ```rust,ignore
/// let sql = format!(
///     "SELECT * FROM {} WHERE {} = {} AND {} = {}",
///     config.child_model,
///     config.morph_type_column,
///     value_to_sql_string(&config.morph_type_value),  // 字符串加引号
///     config.morph_id_column,
///     pk_str  // 数值不加引号,字符串加引号
/// );
/// ```
///
/// ## 参数
///
/// - `child_table`:子表名(如 `"comments"`)
/// - `morph_type_col`:多态类型列名(如 `"commentable_type"`)
/// - `morph_type_val`:父模型类型标识(如 `"Post"`,自动加单引号)
/// - `morph_id_col`:多态外键列名(如 `"commentable_id"`)
/// - `parent_pk_value`:父模型主键值(字符串形式,调用方负责转义)
///
/// ## SQL 注入防护
///
/// 本函数仅用于测试验证 SQL 生成模式,**不应直接用于业务代码**。
/// `morph_type_val` 通过 `format!` 直接拼接为带引号字符串字面量,
/// 调用方应对内部单引号进行转义(`'` → `''`)或使用 sz-orm-core 参数化查询 API。
///
/// ## 示例
///
/// ```ignore
/// use sz_rust_core::relation::morph::morph_many_sql;
///
/// let sql = morph_many_sql("comments", "commentable_type", "Post", "commentable_id", "1");
/// assert_eq!(sql, "SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = 1");
/// ```
pub fn morph_many_sql(
    child_table: &str,
    morph_type_col: &str,
    morph_type_val: &str,
    morph_id_col: &str,
    parent_pk_value: &str,
) -> String {
    format!(
        "SELECT * FROM {} WHERE {} = '{}' AND {} = {}",
        child_table, morph_type_col, morph_type_val, morph_id_col, parent_pk_value
    )
}

/// 生成 `MorphMany` 关联批量 IN 查询 SQL 片段
///
/// 对齐 PHP `MorphMany::eagerlyResultSet` 第 138-142 行生成的 SQL:
///
/// ```php
/// $where = [
///     [$morphKey, 'in', $range],
///     [$morphType, '=', $type],
/// ];
/// ```
///
/// 生成的 SQL:
///
/// ```sql
/// SELECT * FROM {child_table} WHERE {morph_id_col} IN (v1, v2, ...) AND {morph_type_col} = '{morph_type_val}'
/// ```
///
/// ## 参数
///
/// - `child_table`:子表名
/// - `morph_type_col`:多态类型列名
/// - `morph_type_val`:父模型类型标识(自动加单引号)
/// - `morph_id_col`:多态外键列名
/// - `parent_pk_values`:父模型主键值列表(字符串形式)
///
/// ## 空列表处理
///
/// 当 `parent_pk_values` 为空时,返回 `... WHERE {morph_id_col} IN (NULL) AND ...`,
/// 对齐 PHP `!empty($range)` 检查为 false 时跳过查询的行为,但本函数返回 SQL 字符串
/// 而非跳过,调用方应自行判断空列表并跳过查询。
///
/// ## PHP 行为差异
///
/// PHP `MorphMany::eagerlyResultSet` 的 WHERE 条件顺序是 `morphKey IN ... AND morphType = ...`,
/// 即多态外键在前、多态类型在后。本函数对齐此顺序。
///
/// ## 示例
///
/// ```ignore
/// use sz_rust_core::relation::morph::morph_many_in_sql;
///
/// let sql = morph_many_in_sql("comments", "commentable_type", "Post", "commentable_id", &["1", "2", "3"]);
/// assert_eq!(sql, "SELECT * FROM comments WHERE commentable_id IN (1, 2, 3) AND commentable_type = 'Post'");
/// ```
pub fn morph_many_in_sql(
    child_table: &str,
    morph_type_col: &str,
    morph_type_val: &str,
    morph_id_col: &str,
    parent_pk_values: &[&str],
) -> String {
    if parent_pk_values.is_empty() {
        return format!(
            "SELECT * FROM {} WHERE {} IN (NULL) AND {} = '{}'",
            child_table, morph_id_col, morph_type_col, morph_type_val
        );
    }
    format!(
        "SELECT * FROM {} WHERE {} IN ({}) AND {} = '{}'",
        child_table,
        morph_id_col,
        parent_pk_values.join(", "),
        morph_type_col,
        morph_type_val
    )
}

/// 生成 `MorphTo` 关联单条查询 SQL 片段
///
/// 对齐 sz-orm-core `WithRelation::load()` 中 `MorphTo` 分支生成的 SQL:
///
/// ```rust,ignore
/// let sql = format!(
///     "SELECT * FROM {} WHERE id = {}",
///     morph_type_value,  // 作为目标表名
///     pk_to_sql_string(&morph_id_value)
/// );
/// ```
///
/// ## 参数
///
/// - `parent_table`:父表名(由 `morph_type_value` 动态决定,如 `"posts"`)
/// - `parent_pk_col`:父表主键字段名(如 `"id"`)
/// - `morph_id_value`:多态外键值(字符串形式,调用方负责转义)
///
/// ## PHP ↔ sz-orm-core 行为差异
///
/// PHP `MorphTo::eagerlyResult` 通过 `parseModel()` 将 morph_type 值映射到模型类名,
/// 再通过 `(new $model)->getTable()` 获取表名。sz-orm-core 简化为直接将 morph_type_value
/// 作为表名(调用方需在 `get_relation_fk_value` 中完成映射)。
///
/// ## SQL 注入防护
///
/// 本函数仅用于测试验证 SQL 生成模式,**不应直接用于业务代码**。
/// `parent_table` 通过 `format!` 直接拼接,调用方应确保其为合法标识符
/// (通常来自白名单映射,非用户输入)。
///
/// ## 示例
///
/// ```ignore
/// use sz_rust_core::relation::morph::morph_to_sql;
///
/// let sql = morph_to_sql("posts", "id", "1");
/// assert_eq!(sql, "SELECT * FROM posts WHERE id = 1");
/// ```
pub fn morph_to_sql(parent_table: &str, parent_pk_col: &str, morph_id_value: &str) -> String {
    format!(
        "SELECT * FROM {} WHERE {} = {}",
        parent_table, parent_pk_col, morph_id_value
    )
}

// ============================================================================
// 数据处理纯函数(对齐 PHP MorphTo::eagerlyResultSet 分组逻辑)
// ============================================================================

/// 按多态类型分桶(对齐 PHP `MorphTo::eagerlyResultSet` 第 223-230 行)
///
/// PHP `MorphTo::eagerlyResultSet` 按 `morphType` 列值分组,每组收集对应的
/// `morphKey` 值列表,然后对每组发起一次 IN 查询:
///
/// ```php
/// $range = [];
/// foreach ($resultSet as $result) {
///     if (!empty($result->$morphKey)) {
///         $range[$result->$morphType][] = $result->$morphKey;
///     }
/// }
/// // $range 形如: ['Post' => [1, 2, 3], 'Video' => [10, 20]]
/// ```
///
/// ## 参数
///
/// - `rows`:当前模型结果集(JSON 对象数组)
/// - `morph_type_field`:多态类型字段名(如 `"commentable_type"`)
/// - `morph_id_field`:多态外键字段名(如 `"commentable_id"`)
///
/// ## 返回
///
/// `HashMap<morph_type, Vec<morph_id>>`,仅包含 `morph_id` 非空的条目。
///
/// ## 示例
///
/// ```ignore
/// use sz_rust_core::relation::morph::group_by_morph_type;
/// use serde_json::json;
///
/// let rows = vec![
///     json!({"commentable_type": "Post", "commentable_id": 1}),
///     json!({"commentable_type": "Post", "commentable_id": 2}),
///     json!({"commentable_type": "Video", "commentable_id": 10}),
/// ];
/// let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
/// assert_eq!(grouped.get("Post").unwrap(), &vec!["1".to_string(), "2".to_string()]);
/// assert_eq!(grouped.get("Video").unwrap(), &vec!["10".to_string()]);
/// ```
pub fn group_by_morph_type(
    rows: &[serde_json::Value],
    morph_type_field: &str,
    morph_id_field: &str,
) -> std::collections::HashMap<String, Vec<String>> {
    let mut grouped: std::collections::HashMap<String, Vec<String>> =
        std::collections::HashMap::new();

    for row in rows {
        let obj = match row.as_object() {
            Some(o) => o,
            None => continue,
        };

        let morph_type = match obj.get(morph_type_field).and_then(|v| v.as_str()) {
            Some(t) => t.to_string(),
            None => continue,
        };

        let morph_id = match obj.get(morph_id_field) {
            Some(v) => value_to_string(v),
            None => continue,
        };

        if morph_id.is_empty() {
            continue;
        }

        grouped.entry(morph_type).or_default().push(morph_id);
    }

    grouped
}

/// 将 `serde_json::Value` 转换为字符串表示
///
/// - 整数 / 浮点数:原样返回(如 `1` → `"1"`,`3.14` → `"3.14"`)
/// - 字符串:原样返回(不加引号)
/// - 布尔:`true` → `"1"`,`false` → `"0"`(对齐 PHP 弱类型)
/// - null / 其他:空字符串
fn value_to_string(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Bool(b) => {
            if *b {
                "1".to_string()
            } else {
                "0".to_string()
            }
        }
        _ => String::new(),
    }
}

// ============================================================================
// 单元测试
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // ====================================================================
    // 组 1:default_morph_type_column / default_morph_id_column
    // ====================================================================

    #[test]
    fn test_default_morph_type_column_simple() {
        // PHP: $morphType = $morph . '_type'
        assert_eq!(default_morph_type_column("commentable"), "commentable_type");
        assert_eq!(default_morph_type_column("imageable"), "imageable_type");
        assert_eq!(default_morph_type_column("taggable"), "taggable_type");
    }

    #[test]
    fn test_default_morph_type_column_multi_word() {
        // 多单词 morph 名(snake_case)
        assert_eq!(
            default_morph_type_column("comment_able"),
            "comment_able_type"
        );
    }

    #[test]
    fn test_default_morph_id_column_simple() {
        // PHP: $foreignKey = $morph . '_id'
        assert_eq!(default_morph_id_column("commentable"), "commentable_id");
        assert_eq!(default_morph_id_column("imageable"), "imageable_id");
        assert_eq!(default_morph_id_column("taggable"), "taggable_id");
    }

    #[test]
    fn test_default_morph_id_column_multi_word() {
        assert_eq!(default_morph_id_column("comment_able"), "comment_able_id");
    }

    #[test]
    fn test_default_columns_consistent_with_php_convention() {
        // 验证 morph_type_column 与 morph_id_column 前缀一致
        let morph = "commentable";
        let type_col = default_morph_type_column(morph);
        let id_col = default_morph_id_column(morph);
        assert_eq!(
            type_col.strip_suffix("_type").unwrap(),
            id_col.strip_suffix("_id").unwrap()
        );
    }

    // ====================================================================
    // 组 2:php_morph_many 配置构造器
    // ====================================================================

    #[test]
    fn test_php_morph_many_all_defaults() {
        // PHP: $this->morphMany(Comment::class, 'commentable') on Post
        // 等价:morph_type_column = "commentable_type"
        //       morph_id_column = "commentable_id"
        //       morph_type_value = "Post"(get_class($this))
        let rel = php_morph_many("Post", "comments", "commentable", None, None, None);
        assert_eq!(rel.child_model, "comments");
        assert_eq!(rel.morph_type_column, "commentable_type");
        assert_eq!(rel.morph_id_column, "commentable_id");
        assert_eq!(rel.morph_type_value, "Post");
    }

    #[test]
    fn test_php_morph_many_explicit_morph_type_value() {
        // PHP: $this->morphMany(Comment::class, 'commentable', 'CustomPost')
        let rel = php_morph_many(
            "Post",
            "comments",
            "commentable",
            Some("CustomPost"),
            None,
            None,
        );
        assert_eq!(rel.morph_type_value, "CustomPost");
        assert_eq!(rel.morph_type_column, "commentable_type"); // 仍使用默认
        assert_eq!(rel.morph_id_column, "commentable_id"); // 仍使用默认
    }

    #[test]
    fn test_php_morph_many_explicit_columns() {
        // PHP: $this->morphMany(Comment::class, ['c_type', 'c_id'], 'Post')
        // PHP 数组形态:[morphType, foreignKey] = ['c_type', 'c_id']
        let rel = php_morph_many(
            "Post",
            "comments",
            "commentable",
            None,
            Some("c_type"),
            Some("c_id"),
        );
        assert_eq!(rel.morph_type_column, "c_type");
        assert_eq!(rel.morph_id_column, "c_id");
        assert_eq!(rel.morph_type_value, "Post"); // 仍使用默认
    }

    #[test]
    fn test_php_morph_many_all_explicit() {
        // 全部显式指定
        let rel = php_morph_many(
            "Post",
            "comments",
            "commentable",
            Some("CustomType"),
            Some("c_type"),
            Some("c_id"),
        );
        assert_eq!(rel.child_model, "comments");
        assert_eq!(rel.morph_type_column, "c_type");
        assert_eq!(rel.morph_id_column, "c_id");
        assert_eq!(rel.morph_type_value, "CustomType");
    }

    #[test]
    fn test_php_morph_many_with_namespace_parent() {
        // PHP: $this->morphMany(Comment::class, 'commentable') on \app\model\Post
        // get_class($this) 返回完整类名(PHP 行为)
        let rel = php_morph_many(
            "app\\model\\Post",
            "comments",
            "commentable",
            None,
            None,
            None,
        );
        assert_eq!(rel.morph_type_value, "app\\model\\Post");
    }

    #[test]
    fn test_php_morph_many_multi_word_morph() {
        // 多单词 morph 名
        let rel = php_morph_many("Post", "comments", "comment_able", None, None, None);
        assert_eq!(rel.morph_type_column, "comment_able_type");
        assert_eq!(rel.morph_id_column, "comment_able_id");
    }

    // ====================================================================
    // 组 3:php_morph_to 配置构造器
    // ====================================================================

    #[test]
    fn test_php_morph_to_all_defaults() {
        // PHP: $this->morphTo('commentable') on Comment
        let rel = php_morph_to("commentable", None, None);
        assert_eq!(rel.morph_type_column, "commentable_type");
        assert_eq!(rel.morph_id_column, "commentable_id");
    }

    #[test]
    fn test_php_morph_to_explicit_columns() {
        // PHP: $this->morphTo(['c_type', 'c_id'])
        // PHP 数组形态:[morphType, foreignKey] = ['c_type', 'c_id']
        let rel = php_morph_to("commentable", Some("c_type"), Some("c_id"));
        assert_eq!(rel.morph_type_column, "c_type");
        assert_eq!(rel.morph_id_column, "c_id");
    }

    #[test]
    fn test_php_morph_to_only_type_col_explicit() {
        // 仅显式指定 morph_type_col
        let rel = php_morph_to("commentable", Some("c_type"), None);
        assert_eq!(rel.morph_type_column, "c_type");
        assert_eq!(rel.morph_id_column, "commentable_id"); // 仍使用默认
    }

    #[test]
    fn test_php_morph_to_only_id_col_explicit() {
        // 仅显式指定 morph_id_col
        let rel = php_morph_to("commentable", None, Some("c_id"));
        assert_eq!(rel.morph_type_column, "commentable_type"); // 仍使用默认
        assert_eq!(rel.morph_id_column, "c_id");
    }

    #[test]
    fn test_php_morph_to_multi_word_morph() {
        // 多单词 morph 名
        let rel = php_morph_to("comment_able", None, None);
        assert_eq!(rel.morph_type_column, "comment_able_type");
        assert_eq!(rel.morph_id_column, "comment_able_id");
    }

    #[test]
    fn test_php_morph_to_no_type_value_field() {
        // 关键差异:MorphTo struct 无 morph_type_value 字段
        // PHP MorphTo 也无 $type 参数(由子模型数据动态决定)
        let rel = php_morph_to("commentable", None, None);
        // 验证 MorphTo struct 仅有两个字段
        let _morph_type_column: &String = &rel.morph_type_column;
        let _morph_id_column: &String = &rel.morph_id_column;
        // 编译时验证:无 morph_type_value 字段
    }

    // ====================================================================
    // 组 4:morph_many_sql SQL 片段构造器
    // ====================================================================

    #[test]
    fn test_morph_many_sql_numeric_pk() {
        // 数值型主键
        let sql = morph_many_sql(
            "comments",
            "commentable_type",
            "Post",
            "commentable_id",
            "1",
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = 1"
        );
    }

    #[test]
    fn test_morph_many_sql_string_pk() {
        // 字符串型主键(如 UUID),调用方负责加引号
        let sql = morph_many_sql(
            "comments",
            "commentable_type",
            "Post",
            "commentable_id",
            "'abc-123'",
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = 'abc-123'"
        );
    }

    #[test]
    fn test_morph_many_sql_custom_columns() {
        // 自定义列名(PHP 数组形态)
        let sql = morph_many_sql("comments", "c_type", "Post", "c_id", "1");
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE c_type = 'Post' AND c_id = 1"
        );
    }

    #[test]
    fn test_morph_many_sql_custom_type_value() {
        // 自定义类型值(含命名空间)
        let sql = morph_many_sql(
            "comments",
            "commentable_type",
            "app\\model\\Post",
            "commentable_id",
            "1",
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_type = 'app\\model\\Post' AND commentable_id = 1"
        );
    }

    #[test]
    fn test_morph_many_sql_aligns_sz_orm_core_pattern() {
        // 验证 SQL 模式与 sz-orm-core::WithRelation::load MorphMany 分支一致
        // sz-orm-core 源码:
        //   format!(
        //     "SELECT * FROM {} WHERE {} = {} AND {} = {}",
        //     config.child_model, config.morph_type_column,
        //     value_to_sql_string(&config.morph_type_value),  // 加引号
        //     config.morph_id_column, pk_str
        //   );
        let sql = morph_many_sql(
            "comments",
            "commentable_type",
            "Post",
            "commentable_id",
            "1",
        );
        assert!(sql.starts_with(
            "SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = "
        ));
    }

    // ====================================================================
    // 组 5:morph_many_in_sql 批量 IN 查询 SQL
    // ====================================================================

    #[test]
    fn test_morph_many_in_sql_numeric_pks() {
        // 数值型主键列表
        let sql = morph_many_in_sql(
            "comments",
            "commentable_type",
            "Post",
            "commentable_id",
            &["1", "2", "3"],
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_id IN (1, 2, 3) AND commentable_type = 'Post'"
        );
    }

    #[test]
    fn test_morph_many_in_sql_single_pk() {
        // 单个主键
        let sql = morph_many_in_sql(
            "comments",
            "commentable_type",
            "Post",
            "commentable_id",
            &["1"],
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_id IN (1) AND commentable_type = 'Post'"
        );
    }

    #[test]
    fn test_morph_many_in_sql_empty_list() {
        // 空列表:返回 IN (NULL) 模式
        let sql = morph_many_in_sql(
            "comments",
            "commentable_type",
            "Post",
            "commentable_id",
            &[],
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_id IN (NULL) AND commentable_type = 'Post'"
        );
    }

    #[test]
    fn test_morph_many_in_sql_custom_columns() {
        // 自定义列名
        let sql = morph_many_in_sql("comments", "c_type", "Post", "c_id", &["1", "2"]);
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE c_id IN (1, 2) AND c_type = 'Post'"
        );
    }

    #[test]
    fn test_morph_many_in_sql_aligns_php_eagerly_result_set() {
        // 验证 SQL 模式对齐 PHP MorphMany::eagerlyResultSet
        // PHP 源码(第 138-142 行):
        //   $where = [
        //     [$morphKey, 'in', $range],      // morph_id IN (...)
        //     [$morphType, '=', $type],       // morph_type = '...'
        //   ];
        // 注意:PHP WHERE 条件顺序是 morphKey IN 在前,morphType = 在后
        let sql = morph_many_in_sql(
            "comments",
            "commentable_type",
            "Post",
            "commentable_id",
            &["1", "2"],
        );
        // 验证 IN 条件在 = 条件之前
        let in_pos = sql.find("IN (1, 2)").unwrap();
        let eq_pos = sql.find("= 'Post'").unwrap();
        assert!(in_pos < eq_pos, "IN 条件应在 = 条件之前,对齐 PHP 顺序");
    }

    // ====================================================================
    // 组 6:morph_to_sql SQL 片段构造器
    // ====================================================================

    #[test]
    fn test_morph_to_sql_numeric_id() {
        // 数值型 morph_id
        let sql = morph_to_sql("posts", "id", "1");
        assert_eq!(sql, "SELECT * FROM posts WHERE id = 1");
    }

    #[test]
    fn test_morph_to_sql_string_id() {
        // 字符串型 morph_id(如 UUID)
        let sql = morph_to_sql("posts", "id", "'abc-123'");
        assert_eq!(sql, "SELECT * FROM posts WHERE id = 'abc-123'");
    }

    #[test]
    fn test_morph_to_sql_custom_pk_col() {
        // 自定义父表主键列名
        let sql = morph_to_sql("posts", "pk", "1");
        assert_eq!(sql, "SELECT * FROM posts WHERE pk = 1");
    }

    #[test]
    fn test_morph_to_sql_dynamic_parent_table() {
        // 关键:MorphTo 的父表名由 morph_type_value 动态决定
        // 同一条 Comment 可能关联 Post / Video / Image 等不同父表
        let sql_post = morph_to_sql("posts", "id", "1");
        let sql_video = morph_to_sql("videos", "id", "10");
        let sql_image = morph_to_sql("images", "id", "100");

        assert_eq!(sql_post, "SELECT * FROM posts WHERE id = 1");
        assert_eq!(sql_video, "SELECT * FROM videos WHERE id = 10");
        assert_eq!(sql_image, "SELECT * FROM images WHERE id = 100");
    }

    #[test]
    fn test_morph_to_sql_aligns_sz_orm_core_pattern() {
        // 验证 SQL 模式与 sz-orm-core::WithRelation::load MorphTo 分支一致
        // sz-orm-core 源码:
        //   format!("SELECT * FROM {} WHERE id = {}", morph_type_value, pk_to_sql_string(&morph_id_value));
        let sql = morph_to_sql("posts", "id", "1");
        assert!(sql.starts_with("SELECT * FROM posts WHERE id = "));
    }

    // ====================================================================
    // 组 7:group_by_morph_type 数据处理纯函数
    // ====================================================================

    #[test]
    fn test_group_by_morph_type_single_type() {
        // 单一多态类型
        let rows = vec![
            json!({"commentable_type": "Post", "commentable_id": 1}),
            json!({"commentable_type": "Post", "commentable_id": 2}),
            json!({"commentable_type": "Post", "commentable_id": 3}),
        ];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        assert_eq!(grouped.len(), 1);
        assert_eq!(
            grouped.get("Post").unwrap(),
            &vec!["1".to_string(), "2".to_string(), "3".to_string()]
        );
    }

    #[test]
    fn test_group_by_morph_type_multiple_types() {
        // 多种多态类型(PHP MorphTo 核心场景)
        let rows = vec![
            json!({"commentable_type": "Post", "commentable_id": 1}),
            json!({"commentable_type": "Video", "commentable_id": 10}),
            json!({"commentable_type": "Post", "commentable_id": 2}),
            json!({"commentable_type": "Image", "commentable_id": 100}),
            json!({"commentable_type": "Video", "commentable_id": 20}),
        ];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        assert_eq!(grouped.len(), 3);
        assert_eq!(
            grouped.get("Post").unwrap(),
            &vec!["1".to_string(), "2".to_string()]
        );
        assert_eq!(
            grouped.get("Video").unwrap(),
            &vec!["10".to_string(), "20".to_string()]
        );
        assert_eq!(grouped.get("Image").unwrap(), &vec!["100".to_string()]);
    }

    #[test]
    fn test_group_by_morph_type_skip_empty_id() {
        // 跳过 morph_id 为空的行(对齐 PHP !empty($result->$morphKey) 检查)
        let rows = vec![
            json!({"commentable_type": "Post", "commentable_id": 1}),
            json!({"commentable_type": "Post", "commentable_id": null}),
            json!({"commentable_type": "Video", "commentable_id": 10}),
        ];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        assert_eq!(grouped.len(), 2);
        assert_eq!(grouped.get("Post").unwrap(), &vec!["1".to_string()]);
        assert_eq!(grouped.get("Video").unwrap(), &vec!["10".to_string()]);
    }

    #[test]
    fn test_group_by_morph_type_skip_missing_fields() {
        // 跳过缺少字段的行
        let rows = vec![
            json!({"commentable_type": "Post", "commentable_id": 1}),
            json!({"commentable_type": "Post"}), // 缺少 commentable_id
            json!({"commentable_id": 10}),       // 缺少 commentable_type
            json!({}),                           // 完全空
        ];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        assert_eq!(grouped.len(), 1);
        assert_eq!(grouped.get("Post").unwrap(), &vec!["1".to_string()]);
    }

    #[test]
    fn test_group_by_morph_type_empty_rows() {
        // 空结果集
        let rows: Vec<serde_json::Value> = vec![];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        assert!(grouped.is_empty());
    }

    #[test]
    fn test_group_by_morph_type_string_ids() {
        // 字符串型 morph_id(如 UUID)
        let rows = vec![
            json!({"commentable_type": "Post", "commentable_id": "abc-123"}),
            json!({"commentable_type": "Post", "commentable_id": "def-456"}),
        ];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        assert_eq!(
            grouped.get("Post").unwrap(),
            &vec!["abc-123".to_string(), "def-456".to_string()]
        );
    }

    #[test]
    fn test_group_by_morph_type_non_object_rows() {
        // 非对象行(数组、字符串等)应被跳过
        let rows = vec![
            json!({"commentable_type": "Post", "commentable_id": 1}),
            json!([1, 2, 3]), // 数组
            json!("string"),  // 字符串
            json!(42),        // 数字
            json!(null),      // null
        ];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        assert_eq!(grouped.len(), 1);
        assert_eq!(grouped.get("Post").unwrap(), &vec!["1".to_string()]);
    }

    #[test]
    fn test_group_by_morph_type_preserves_insertion_order_within_group() {
        // 验证组内顺序保持(对齐 PHP $range[$type][] 顺序追加)
        let rows = vec![
            json!({"commentable_type": "Post", "commentable_id": 3}),
            json!({"commentable_type": "Post", "commentable_id": 1}),
            json!({"commentable_type": "Post", "commentable_id": 2}),
        ];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        // 顺序应为 [3, 1, 2](按出现顺序)
        assert_eq!(
            grouped.get("Post").unwrap(),
            &vec!["3".to_string(), "1".to_string(), "2".to_string()]
        );
    }

    // ====================================================================
    // 组 8:value_to_string 辅助函数
    // ====================================================================

    #[test]
    fn test_value_to_string_integer() {
        assert_eq!(value_to_string(&json!(42)), "42");
        assert_eq!(value_to_string(&json!(0)), "0");
        assert_eq!(value_to_string(&json!(-5)), "-5");
    }

    #[test]
    fn test_value_to_string_float() {
        assert_eq!(value_to_string(&json!(2.5)), "2.5");
    }

    #[test]
    fn test_value_to_string_string() {
        assert_eq!(value_to_string(&json!("abc")), "abc");
        assert_eq!(value_to_string(&json!("")), "");
    }

    #[test]
    fn test_value_to_string_bool() {
        // 对齐 PHP 弱类型:true → "1",false → "0"
        assert_eq!(value_to_string(&json!(true)), "1");
        assert_eq!(value_to_string(&json!(false)), "0");
    }

    #[test]
    fn test_value_to_string_null_and_complex() {
        // null / 对象 / 数组 → 空字符串
        assert_eq!(value_to_string(&json!(null)), "");
        assert_eq!(value_to_string(&json!({"a": 1})), "");
        assert_eq!(value_to_string(&json!([1, 2])), "");
    }

    // ====================================================================
    // 组 9:R5 PHP 行为对齐验证(硬约束)
    // ====================================================================

    #[test]
    fn test_r5_php_morph_many_default_column_convention() {
        // R5-1:PHP morphMany 默认列名 `morph . '_type'` / `morph . '_id'`
        // PHP 源码:$morphType = $morph . '_type'; $foreignKey = $morph . '_id';
        assert_eq!(default_morph_type_column("commentable"), "commentable_type");
        assert_eq!(default_morph_id_column("commentable"), "commentable_id");
        assert_eq!(default_morph_type_column("imageable"), "imageable_type");
        assert_eq!(default_morph_id_column("imageable"), "imageable_id");
    }

    #[test]
    fn test_r5_php_morph_many_default_type_is_parent_class() {
        // R5-2:PHP morphMany 默认 type = get_class($this)
        // sz-orm-core::MorphMany.morph_type_value 对应 PHP $type
        let rel = php_morph_many("Post", "comments", "commentable", None, None, None);
        assert_eq!(rel.morph_type_value, "Post");
    }

    #[test]
    fn test_r5_php_morph_many_explicit_overrides_default() {
        // R5-3:显式参数覆盖默认值
        let rel = php_morph_many(
            "Post",
            "comments",
            "commentable",
            Some("CustomType"),
            Some("c_type"),
            Some("c_id"),
        );
        assert_eq!(rel.morph_type_value, "CustomType");
        assert_eq!(rel.morph_type_column, "c_type");
        assert_eq!(rel.morph_id_column, "c_id");
    }

    #[test]
    fn test_r5_php_morph_to_no_type_parameter() {
        // R5-4:PHP morphTo 无 $type 参数(与 morphMany 关键差异)
        // sz-orm-core::MorphTo struct 也无 morph_type_value 字段
        // 编译时验证:MorphTo 仅有 morph_type_column 和 morph_id_column 两个字段
        let rel = php_morph_to("commentable", None, None);
        let _type_col: String = rel.morph_type_column.clone();
        let _id_col: String = rel.morph_id_column.clone();
        // 若 MorphTo 有 morph_type_value 字段,下方代码会编译失败
        // (Rust 编译时保证类型对齐)
    }

    #[test]
    fn test_r5_php_morph_many_sql_pattern_matches_sz_orm_core() {
        // R5-5:MorphMany SQL 模式对齐 sz-orm-core
        // sz-orm-core::WithRelation::load MorphMany 分支:
        //   SELECT * FROM {child} WHERE {morph_type_col} = '{morph_type_val}' AND {morph_id_col} = {pk}
        let sql = morph_many_sql(
            "comments",
            "commentable_type",
            "Post",
            "commentable_id",
            "1",
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = 1"
        );
    }

    #[test]
    fn test_r5_php_morph_to_sql_pattern_matches_sz_orm_core() {
        // R5-6:MorphTo SQL 模式对齐 sz-orm-core
        // sz-orm-core::WithRelation::load MorphTo 分支:
        //   SELECT * FROM {morph_type_val_as_table} WHERE id = {morph_id_val}
        let sql = morph_to_sql("posts", "id", "1");
        assert_eq!(sql, "SELECT * FROM posts WHERE id = 1");
    }

    #[test]
    fn test_r5_php_morph_many_in_sql_eagerly_pattern() {
        // R5-7:MorphMany 批量 IN 查询对齐 PHP eagerlyResultSet
        // PHP 源码(第 138-142 行):
        //   $where = [[$morphKey, 'in', $range], [$morphType, '=', $type]];
        let sql = morph_many_in_sql(
            "comments",
            "commentable_type",
            "Post",
            "commentable_id",
            &["1", "2", "3"],
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_id IN (1, 2, 3) AND commentable_type = 'Post'"
        );
    }

    #[test]
    fn test_r5_php_morph_to_group_by_morph_type_pattern() {
        // R5-8:MorphTo 按 morphType 分组对齐 PHP eagerlyResultSet
        // PHP 源码(第 223-230 行):
        //   foreach ($resultSet as $result) {
        //     if (!empty($result->$morphKey)) {
        //       $range[$result->$morphType][] = $result->$morphKey;
        //     }
        //   }
        let rows = vec![
            json!({"commentable_type": "Post", "commentable_id": 1}),
            json!({"commentable_type": "Video", "commentable_id": 10}),
            json!({"commentable_type": "Post", "commentable_id": 2}),
        ];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        assert_eq!(grouped.len(), 2);
        assert_eq!(
            grouped.get("Post").unwrap(),
            &vec!["1".to_string(), "2".to_string()]
        );
        assert_eq!(grouped.get("Video").unwrap(), &vec!["10".to_string()]);
    }

    #[test]
    fn test_r5_php_morph_to_empty_morph_key_skipped() {
        // R5-9:PHP !empty($result->$morphKey) 检查跳过空值
        let rows = vec![
            json!({"commentable_type": "Post", "commentable_id": 1}),
            json!({"commentable_type": "Post", "commentable_id": null}),
            json!({"commentable_type": "Post", "commentable_id": ""}),
        ];
        let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
        // 仅第一条应被收集
        assert_eq!(grouped.get("Post").unwrap(), &vec!["1".to_string()]);
    }

    #[test]
    fn test_r5_php_morph_many_delegates_to_sz_orm_core() {
        // R5-10:sz-rust 端复用 sz-orm-core::WithRelation::load 进行端到端关联加载
        // 验证 php_morph_many 返回 sz-orm-core::MorphMany 类型
        let rel = php_morph_many("Post", "comments", "commentable", None, None, None);
        // 验证类型为 sz_orm_core::MorphMany(编译时类型检查)
        let _: &MorphMany = &rel;
        // 验证字段可被 sz-orm-core::Relation::MorphMany 包装
        let relation = sz_orm_core::Relation::MorphMany(rel.clone());
        assert!(matches!(relation, sz_orm_core::Relation::MorphMany(_)));
    }

    #[test]
    fn test_r5_php_morph_to_delegates_to_sz_orm_core() {
        // R5-11:sz-rust 端复用 sz-orm-core::WithRelation::load 进行端到端关联加载
        // 验证 php_morph_to 返回 sz-orm-core::MorphTo 类型
        let rel = php_morph_to("commentable", None, None);
        // 验证类型为 sz_orm_core::MorphTo(编译时类型检查)
        let _: &MorphTo = &rel;
        // 验证字段可被 sz-orm-core::Relation::MorphTo 包装
        let relation = sz_orm_core::Relation::MorphTo(rel.clone());
        assert!(matches!(relation, sz_orm_core::Relation::MorphTo(_)));
    }

    // ====================================================================
    // 组 10:集成测试(PHP 业务场景)
    // ====================================================================

    #[test]
    fn test_integration_post_morph_many_comments() {
        // PHP 业务场景:Post morphMany Comments
        // ```php
        // class Post extends Model {
        //     public function comments() {
        //         return $this->morphMany(Comment::class, 'commentable');
        //     }
        // }
        // ```
        let rel = php_morph_many("Post", "comments", "commentable", None, None, None);
        assert_eq!(rel.child_model, "comments");
        assert_eq!(rel.morph_type_column, "commentable_type");
        assert_eq!(rel.morph_id_column, "commentable_id");
        assert_eq!(rel.morph_type_value, "Post");

        // 单条查询 SQL
        let sql = morph_many_sql(
            &rel.child_model,
            &rel.morph_type_column,
            &rel.morph_type_value,
            &rel.morph_id_column,
            "1",
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = 1"
        );

        // 批量 IN 查询 SQL
        let in_sql = morph_many_in_sql(
            &rel.child_model,
            &rel.morph_type_column,
            &rel.morph_type_value,
            &rel.morph_id_column,
            &["1", "2", "3"],
        );
        assert_eq!(
            in_sql,
            "SELECT * FROM comments WHERE commentable_id IN (1, 2, 3) AND commentable_type = 'Post'"
        );
    }

    #[test]
    fn test_integration_video_morph_many_comments() {
        // PHP 业务场景:Video morphMany Comments(与 Post 共用 comments 表)
        let rel = php_morph_many("Video", "comments", "commentable", None, None, None);
        assert_eq!(rel.morph_type_value, "Video");
        assert_eq!(rel.morph_type_column, "commentable_type");

        let sql = morph_many_sql(
            &rel.child_model,
            &rel.morph_type_column,
            &rel.morph_type_value,
            &rel.morph_id_column,
            "10",
        );
        assert_eq!(
            sql,
            "SELECT * FROM comments WHERE commentable_type = 'Video' AND commentable_id = 10"
        );
    }

    #[test]
    fn test_integration_comment_morph_to_post_or_video() {
        // PHP 业务场景:Comment morphTo Post 或 Video
        // ```php
        // class Comment extends Model {
        //     public function commentable() {
        //         return $this->morphTo();
        //     }
        // }
        // ```
        let rel = php_morph_to("commentable", None, None);
        assert_eq!(rel.morph_type_column, "commentable_type");
        assert_eq!(rel.morph_id_column, "commentable_id");

        // 根据 morph_type 动态路由到不同父表
        // 假设 morph_type_value = "Post" → 表名 "posts"
        let sql_post = morph_to_sql("posts", "id", "1");
        assert_eq!(sql_post, "SELECT * FROM posts WHERE id = 1");

        // 假设 morph_type_value = "Video" → 表名 "videos"
        let sql_video = morph_to_sql("videos", "id", "10");
        assert_eq!(sql_video, "SELECT * FROM videos WHERE id = 10");
    }

    #[test]
    fn test_integration_morph_to_batch_loading() {
        // PHP 业务场景:批量加载 Comments 的 morphTo 关联
        // 步骤 1:收集 morph_type + morph_id 分组
        let comments = vec![
            json!({"id": 1, "commentable_type": "Post", "commentable_id": 1}),
            json!({"id": 2, "commentable_type": "Video", "commentable_id": 10}),
            json!({"id": 3, "commentable_type": "Post", "commentable_id": 2}),
            json!({"id": 4, "commentable_type": "Image", "commentable_id": 100}),
        ];

        let grouped = group_by_morph_type(&comments, "commentable_type", "commentable_id");
        assert_eq!(grouped.len(), 3);

        // 步骤 2:对每种类型发起 IN 查询
        // Post: SELECT * FROM posts WHERE id IN (1, 2)
        let post_ids: Vec<&str> = grouped
            .get("Post")
            .unwrap()
            .iter()
            .map(|s| s.as_str())
            .collect();
        let post_sql = format!("SELECT * FROM posts WHERE id IN ({})", post_ids.join(", "));
        assert_eq!(post_sql, "SELECT * FROM posts WHERE id IN (1, 2)");

        // Video: SELECT * FROM videos WHERE id IN (10)
        let video_ids: Vec<&str> = grouped
            .get("Video")
            .unwrap()
            .iter()
            .map(|s| s.as_str())
            .collect();
        let video_sql = format!(
            "SELECT * FROM videos WHERE id IN ({})",
            video_ids.join(", ")
        );
        assert_eq!(video_sql, "SELECT * FROM videos WHERE id IN (10)");

        // Image: SELECT * FROM images WHERE id IN (100)
        let image_ids: Vec<&str> = grouped
            .get("Image")
            .unwrap()
            .iter()
            .map(|s| s.as_str())
            .collect();
        let image_sql = format!(
            "SELECT * FROM images WHERE id IN ({})",
            image_ids.join(", ")
        );
        assert_eq!(image_sql, "SELECT * FROM images WHERE id IN (100)");
    }

    #[test]
    fn test_integration_image_morph_many_tags() {
        // PHP 业务场景:Image morphMany Tags(使用自定义列名)
        // ```php
        // class Image extends Model {
        //     public function tags() {
        //         return $this->morphMany(Tag::class, ['tag_type', 'tag_id'], 'Image');
        //     }
        // }
        // ```
        let rel = php_morph_many(
            "Image",
            "tags",
            "taggable",
            None,
            Some("tag_type"),
            Some("tag_id"),
        );
        assert_eq!(rel.child_model, "tags");
        assert_eq!(rel.morph_type_column, "tag_type"); // 显式覆盖
        assert_eq!(rel.morph_id_column, "tag_id"); // 显式覆盖
        assert_eq!(rel.morph_type_value, "Image");

        let sql = morph_many_sql(
            &rel.child_model,
            &rel.morph_type_column,
            &rel.morph_type_value,
            &rel.morph_id_column,
            "100",
        );
        assert_eq!(
            sql,
            "SELECT * FROM tags WHERE tag_type = 'Image' AND tag_id = 100"
        );
    }
}