@k2works/claude-code-booster 3.2.1 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/lib/assets/docs/article/index.md +4 -1
  2. package/lib/assets/docs/article/practical-database-design/index.md +121 -0
  3. package/lib/assets/docs/article/practical-database-design/part1/chapter01.md +288 -0
  4. package/lib/assets/docs/article/practical-database-design/part1/chapter02.md +518 -0
  5. package/lib/assets/docs/article/practical-database-design/part1/chapter03.md +557 -0
  6. package/lib/assets/docs/article/practical-database-design/part2/chapter04.md +924 -0
  7. package/lib/assets/docs/article/practical-database-design/part2/chapter05.md +1627 -0
  8. package/lib/assets/docs/article/practical-database-design/part2/chapter06.md +2716 -0
  9. package/lib/assets/docs/article/practical-database-design/part2/chapter07.md +2082 -0
  10. package/lib/assets/docs/article/practical-database-design/part2/chapter08.md +2105 -0
  11. package/lib/assets/docs/article/practical-database-design/part2/chapter09.md +2031 -0
  12. package/lib/assets/docs/article/practical-database-design/part2/chapter10.md +1387 -0
  13. package/lib/assets/docs/article/practical-database-design/part2/chapter11.md +1677 -0
  14. package/lib/assets/docs/article/practical-database-design/part2/chapter12.md +1417 -0
  15. package/lib/assets/docs/article/practical-database-design/part2/chapter13.md +1434 -0
  16. package/lib/assets/docs/article/practical-database-design/part3/chapter14.md +667 -0
  17. package/lib/assets/docs/article/practical-database-design/part3/chapter15.md +1625 -0
  18. package/lib/assets/docs/article/practical-database-design/part3/chapter16.md +1915 -0
  19. package/lib/assets/docs/article/practical-database-design/part3/chapter17.md +1708 -0
  20. package/lib/assets/docs/article/practical-database-design/part3/chapter18.md +2095 -0
  21. package/lib/assets/docs/article/practical-database-design/part3/chapter19.md +1123 -0
  22. package/lib/assets/docs/article/practical-database-design/part3/chapter20.md +1031 -0
  23. package/lib/assets/docs/article/practical-database-design/part3/chapter21.md +1382 -0
  24. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter14-orm.md +991 -0
  25. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter15-orm.md +1300 -0
  26. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter16-orm.md +1166 -0
  27. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter17-orm.md +1584 -0
  28. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter18-orm.md +1183 -0
  29. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter19-orm.md +1016 -0
  30. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter20-orm.md +1753 -0
  31. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter21-orm.md +1447 -0
  32. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter22-orm.md +1878 -0
  33. package/lib/assets/docs/article/practical-database-design/part4/chapter22.md +965 -0
  34. package/lib/assets/docs/article/practical-database-design/part4/chapter23.md +2069 -0
  35. package/lib/assets/docs/article/practical-database-design/part4/chapter24.md +2439 -0
  36. package/lib/assets/docs/article/practical-database-design/part4/chapter25.md +3661 -0
  37. package/lib/assets/docs/article/practical-database-design/part4/chapter26.md +2916 -0
  38. package/lib/assets/docs/article/practical-database-design/part4/chapter27.md +3105 -0
  39. package/lib/assets/docs/article/practical-database-design/part4/chapter28.md +2697 -0
  40. package/lib/assets/docs/article/practical-database-design/part4/chapter29.md +2544 -0
  41. package/lib/assets/docs/article/practical-database-design/part4/chapter30.md +2180 -0
  42. package/lib/assets/docs/article/practical-database-design/part4/chapter31.md +1192 -0
  43. package/lib/assets/docs/article/practical-database-design/part4/chapter32.md +2101 -0
  44. package/lib/assets/docs/article/practical-database-design/part5/chapter33.md +1032 -0
  45. package/lib/assets/docs/article/practical-database-design/part5/chapter34.md +1609 -0
  46. package/lib/assets/docs/article/practical-database-design/part5/chapter35.md +1453 -0
  47. package/lib/assets/docs/article/practical-database-design/part5/chapter36.md +1292 -0
  48. package/lib/assets/docs/article/practical-database-design/part5/chapter37.md +1470 -0
  49. package/lib/assets/docs/article/practical-database-design/part5/chapter38.md +1698 -0
  50. package/lib/assets/docs/article/practical-database-design/part5/chapter39.md +2334 -0
  51. package/lib/assets/docs/article/practical-database-design/study/study2-1.md +1693 -0
  52. package/lib/assets/docs/article/practical-database-design/study/study2-2.md +1347 -0
  53. package/lib/assets/docs/article/practical-database-design/study/study2-3.md +2044 -0
  54. package/lib/assets/docs/article/practical-database-design/study/study2-4.md +2229 -0
  55. package/lib/assets/docs/article/practical-database-design/study/study2-5.md +2418 -0
  56. package/lib/assets/docs/article/practical-database-design/study/study3-1.md +2205 -0
  57. package/lib/assets/docs/article/practical-database-design/study/study3-2.md +2221 -0
  58. package/lib/assets/docs/article/practical-database-design/study/study3-3.md +2253 -0
  59. package/lib/assets/docs/article/practical-database-design/study/study3-4.md +2106 -0
  60. package/lib/assets/docs/article/practical-database-design/study/study3-5.md +2507 -0
  61. package/lib/assets/docs/article/practical-database-design/study/study4-1.md +2587 -0
  62. package/lib/assets/docs/article/practical-database-design/study/study4-2.md +2075 -0
  63. package/lib/assets/docs/article/practical-database-design/study/study4-3.md +1805 -0
  64. package/lib/assets/docs/article/practical-database-design/study/study4-4.md +1895 -0
  65. package/lib/assets/docs/article/practical-database-design/study/study4-5.md +2878 -0
  66. package/package.json +1 -1
@@ -0,0 +1,2544 @@
1
+ # 第29章:品質管理の設計
2
+
3
+ ## 29.1 品質管理の概要
4
+
5
+ ### 品質管理の目的と重要性
6
+
7
+ 品質管理は、製品が顧客の要求仕様を満たしていることを保証するための重要な業務領域です。製造業において品質管理は、以下の目的で実施されます。
8
+
9
+ ```plantuml
10
+ @startuml
11
+
12
+ title 品質管理の目的
13
+
14
+ package "品質管理の目的" {
15
+ [顧客満足の確保] as CS
16
+ [不良品の流出防止] as DP
17
+ [製造コストの最適化] as CO
18
+ [法規制・規格への適合] as CC
19
+ [継続的改善の推進] as CI
20
+ }
21
+
22
+ CS --> DP : 要求品質の確保
23
+ DP --> CO : 手直し・廃棄の削減
24
+ CO --> CI : 原因分析と対策
25
+ CC --> CS : 信頼性の担保
26
+
27
+ note right of CS : 品質クレームの削減
28
+ note right of DP : 検査による品質保証
29
+ note right of CO : 良品率の向上
30
+ note right of CI : PDCA サイクル
31
+
32
+ @enduml
33
+ ```
34
+
35
+ 品質管理の重要性は以下の点にあります:
36
+
37
+ | 観点 | 説明 |
38
+ |-----|------|
39
+ | **顧客視点** | 不良品の流出は顧客の信頼を失い、ブランド価値を毀損する |
40
+ | **コスト視点** | 早期発見による手直しコスト削減、廃棄ロスの最小化 |
41
+ | **法的視点** | PL 法(製造物責任法)への対応、各種規格への適合 |
42
+ | **競争力視点** | 高品質は差別化要因となり、競争優位性を確保 |
43
+
44
+ ### 検査の種類と位置づけ
45
+
46
+ 製造プロセスにおける検査は、実施タイミングによって以下の3種類に分類されます。
47
+
48
+ ```plantuml
49
+ @startuml
50
+
51
+ title 品質管理のスコープ
52
+
53
+ |購買管理|
54
+ start
55
+ :入荷;
56
+
57
+ |品質管理|
58
+ :受入検査;
59
+ if (合格?) then (yes)
60
+ :合格処理;
61
+ else (no)
62
+ :不合格処理;
63
+ :返品/手直し;
64
+ endif
65
+
66
+ |工程管理|
67
+ :製造;
68
+
69
+ |品質管理|
70
+ :工程内検査;
71
+
72
+ |工程管理|
73
+ :完成;
74
+
75
+ |品質管理|
76
+ :出荷検査;
77
+ if (合格?) then (yes)
78
+ :出荷許可;
79
+ else (no)
80
+ :再検査/手直し;
81
+ endif
82
+
83
+ stop
84
+
85
+ @enduml
86
+ ```
87
+
88
+ 各検査の特徴と目的は以下の通りです:
89
+
90
+ | 検査種別 | タイミング | 目的 | 関連テーブル |
91
+ |---------|----------|------|------------|
92
+ | **受入検査** | 入荷後 | 購買品の品質確認、不良品の受入防止 | 受入検査データ |
93
+ | **工程内検査** | 製造中 | 製造工程での品質確認、早期不良発見 | 完成検査結果データ |
94
+ | **出荷検査** | 出荷前 | 最終品質確認、不良品の流出防止 | 出荷検査データ |
95
+
96
+ ---
97
+
98
+ ## 29.2 受入検査・工程検査・出荷検査
99
+
100
+ ### 共通の検査判定
101
+
102
+ すべての検査で共通して使用する検査判定の列挙型を定義します。
103
+
104
+ #### 検査判定 Enum
105
+
106
+ <details>
107
+ <summary>InspectionJudgment.java</summary>
108
+
109
+ ```java
110
+ // src/main/java/com/example/sms/domain/model/quality/InspectionJudgment.java
111
+ package com.example.pms.domain.model.quality;
112
+
113
+ /**
114
+ * 検査判定
115
+ */
116
+ public enum InspectionJudgment {
117
+ PASSED("合格"),
118
+ FAILED("不合格"),
119
+ HOLD("保留");
120
+
121
+ private final String displayName;
122
+
123
+ InspectionJudgment(String displayName) {
124
+ this.displayName = displayName;
125
+ }
126
+
127
+ public String getDisplayName() {
128
+ return displayName;
129
+ }
130
+
131
+ public static InspectionJudgment fromDisplayName(String displayName) {
132
+ for (InspectionJudgment judgment : values()) {
133
+ if (judgment.displayName.equals(displayName)) {
134
+ return judgment;
135
+ }
136
+ }
137
+ throw new IllegalArgumentException("Unknown display name: " + displayName);
138
+ }
139
+ }
140
+ ```
141
+
142
+ </details>
143
+
144
+ #### 検査判定 TypeHandler
145
+
146
+ <details>
147
+ <summary>InspectionJudgmentTypeHandler.java</summary>
148
+
149
+ ```java
150
+ // src/main/java/com/example/sms/infrastructure/out/persistence/typehandler/InspectionJudgmentTypeHandler.java
151
+ package com.example.pms.infrastructure.out.persistence.typehandler;
152
+
153
+ import com.example.pms.domain.model.quality.InspectionJudgment;
154
+ import org.apache.ibatis.type.BaseTypeHandler;
155
+ import org.apache.ibatis.type.JdbcType;
156
+ import org.apache.ibatis.type.MappedTypes;
157
+
158
+ import java.sql.CallableStatement;
159
+ import java.sql.PreparedStatement;
160
+ import java.sql.ResultSet;
161
+ import java.sql.SQLException;
162
+
163
+ /**
164
+ * 検査判定のTypeHandler
165
+ */
166
+ @MappedTypes(InspectionJudgment.class)
167
+ public class InspectionJudgmentTypeHandler extends BaseTypeHandler<InspectionJudgment> {
168
+
169
+ @Override
170
+ public void setNonNullParameter(PreparedStatement ps, int i,
171
+ InspectionJudgment parameter, JdbcType jdbcType) throws SQLException {
172
+ ps.setString(i, parameter.getDisplayName());
173
+ }
174
+
175
+ @Override
176
+ public InspectionJudgment getNullableResult(ResultSet rs, String columnName) throws SQLException {
177
+ String value = rs.getString(columnName);
178
+ return value == null ? null : InspectionJudgment.fromDisplayName(value);
179
+ }
180
+
181
+ @Override
182
+ public InspectionJudgment getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
183
+ String value = rs.getString(columnIndex);
184
+ return value == null ? null : InspectionJudgment.fromDisplayName(value);
185
+ }
186
+
187
+ @Override
188
+ public InspectionJudgment getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
189
+ String value = cs.getString(columnIndex);
190
+ return value == null ? null : InspectionJudgment.fromDisplayName(value);
191
+ }
192
+ }
193
+ ```
194
+
195
+ </details>
196
+
197
+ ### 欠点マスタ
198
+
199
+ 検査で発見された不良の種類を管理するマスタです。
200
+
201
+ #### 欠点マスタエンティティ
202
+
203
+ <details>
204
+ <summary>DefectMaster.java</summary>
205
+
206
+ ```java
207
+ // src/main/java/com/example/sms/domain/model/quality/DefectMaster.java
208
+ package com.example.pms.domain.model.quality;
209
+
210
+ import lombok.*;
211
+ import java.time.LocalDateTime;
212
+
213
+ /**
214
+ * 欠点マスタエンティティ
215
+ */
216
+ @Data
217
+ @Builder
218
+ @NoArgsConstructor
219
+ @AllArgsConstructor
220
+ public class DefectMaster {
221
+ private String defectCode;
222
+ private String defectName;
223
+ private String defectCategory;
224
+ private LocalDateTime createdAt;
225
+ private LocalDateTime updatedAt;
226
+ }
227
+ ```
228
+
229
+ </details>
230
+
231
+ ### 受入検査の設計(購買品の品質確認)
232
+
233
+ 受入検査は、購買品が仕入先から入荷した際に実施する品質検査です。
234
+
235
+ ```plantuml
236
+ @startuml
237
+
238
+ title 受入検査の業務フロー
239
+
240
+ |購買管理|
241
+ start
242
+ :入荷処理;
243
+ :入荷データ登録;
244
+
245
+ |品質管理|
246
+ :受入検査実施;
247
+ :検査結果記録;
248
+
249
+ if (判定) then (合格)
250
+ :合格数量を在庫計上;
251
+ |在庫管理|
252
+ :在庫増加処理;
253
+ else (不合格)
254
+ :不合格処理;
255
+ if (処置) then (返品)
256
+ |購買管理|
257
+ :返品処理;
258
+ else (手直し)
259
+ :手直し指示;
260
+ :再検査;
261
+ endif
262
+ endif
263
+
264
+ stop
265
+
266
+ @enduml
267
+ ```
268
+
269
+ #### 受入検査データエンティティ
270
+
271
+ <details>
272
+ <summary>ReceivingInspection.java</summary>
273
+
274
+ ```java
275
+ // src/main/java/com/example/sms/domain/model/quality/ReceivingInspection.java
276
+ package com.example.pms.domain.model.quality;
277
+
278
+ import lombok.*;
279
+ import java.math.BigDecimal;
280
+ import java.time.LocalDate;
281
+ import java.time.LocalDateTime;
282
+ import java.util.List;
283
+
284
+ /**
285
+ * 受入検査データエンティティ
286
+ */
287
+ @Data
288
+ @Builder
289
+ @NoArgsConstructor
290
+ @AllArgsConstructor
291
+ public class ReceivingInspection {
292
+ private Integer id;
293
+ private String inspectionNumber;
294
+ private String receivingNumber;
295
+ private String purchaseOrderNumber;
296
+ private String itemCode;
297
+ private String supplierCode;
298
+ private LocalDate inspectionDate;
299
+ private String inspectorCode;
300
+ private BigDecimal inspectionQuantity;
301
+ private BigDecimal passedQuantity;
302
+ private BigDecimal failedQuantity;
303
+ private InspectionJudgment judgment;
304
+ private String remarks;
305
+ private LocalDateTime createdAt;
306
+ private LocalDateTime updatedAt;
307
+
308
+ // 楽観ロック用バージョン
309
+ @Builder.Default
310
+ private Integer version = 1;
311
+
312
+ // リレーション
313
+ private Item item;
314
+ private Supplier supplier;
315
+ @Builder.Default
316
+ private List<ReceivingInspectionResult> results = new ArrayList<>();
317
+ }
318
+ ```
319
+
320
+ </details>
321
+
322
+ #### 受入検査結果データエンティティ
323
+
324
+ <details>
325
+ <summary>ReceivingInspectionResult.java</summary>
326
+
327
+ ```java
328
+ // src/main/java/com/example/sms/domain/model/quality/ReceivingInspectionResult.java
329
+ package com.example.pms.domain.model.quality;
330
+
331
+ import lombok.*;
332
+ import java.math.BigDecimal;
333
+
334
+ /**
335
+ * 受入検査結果データエンティティ.
336
+ */
337
+ @Data
338
+ @Builder
339
+ @NoArgsConstructor
340
+ @AllArgsConstructor
341
+ public class ReceivingInspectionResult {
342
+ private Integer id;
343
+ private String inspectionNumber;
344
+ private String defectCode;
345
+ private BigDecimal quantity;
346
+ private String remarks;
347
+
348
+ // リレーション
349
+ private Defect defect;
350
+ }
351
+ ```
352
+
353
+ </details>
354
+
355
+ #### MyBatis Mapper XML:受入検査
356
+
357
+ <details>
358
+ <summary>ReceivingInspectionMapper.xml</summary>
359
+
360
+ ```xml
361
+ <?xml version="1.0" encoding="UTF-8" ?>
362
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
363
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
364
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.ReceivingInspectionMapper">
365
+
366
+ <resultMap id="ReceivingInspectionResultMap"
367
+ type="com.example.pms.domain.model.quality.ReceivingInspection">
368
+ <id property="id" column="ID"/>
369
+ <result property="inspectionNumber" column="受入検査番号"/>
370
+ <result property="receivingNumber" column="入荷番号"/>
371
+ <result property="purchaseOrderNumber" column="発注番号"/>
372
+ <result property="itemCode" column="品目コード"/>
373
+ <result property="supplierCode" column="仕入先コード"/>
374
+ <result property="inspectionDate" column="検査日"/>
375
+ <result property="inspectorCode" column="検査担当者コード"/>
376
+ <result property="inspectionQuantity" column="検査数量"/>
377
+ <result property="passedQuantity" column="合格数"/>
378
+ <result property="failedQuantity" column="不合格数"/>
379
+ <result property="judgment" column="判定"
380
+ typeHandler="com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler"/>
381
+ <result property="remarks" column="備考"/>
382
+ <result property="createdAt" column="作成日時"/>
383
+ <result property="updatedAt" column="更新日時"/>
384
+ <collection property="results" ofType="com.example.pms.domain.model.quality.ReceivingInspectionResult"
385
+ resultMap="ReceivingInspectionResultResultMap"/>
386
+ </resultMap>
387
+
388
+ <resultMap id="ReceivingInspectionResultResultMap"
389
+ type="com.example.pms.domain.model.quality.ReceivingInspectionResult">
390
+ <id property="id" column="RESULT_ID"/>
391
+ <result property="inspectionNumber" column="受入検査番号"/>
392
+ <result property="defectCode" column="欠点コード"/>
393
+ <result property="quantity" column="数量"/>
394
+ <result property="remarks" column="結果備考"/>
395
+ </resultMap>
396
+
397
+ <select id="findByInspectionNumber" resultMap="ReceivingInspectionResultMap">
398
+ SELECT
399
+ ri.*,
400
+ rir."ID" AS RESULT_ID,
401
+ rir."欠点コード",
402
+ rir."数量",
403
+ rir."備考" AS 結果備考
404
+ FROM "受入検査データ" ri
405
+ LEFT JOIN "受入検査結果データ" rir ON ri."受入検査番号" = rir."受入検査番号"
406
+ WHERE ri."受入検査番号" = #{inspectionNumber}
407
+ </select>
408
+
409
+ <select id="findByReceivingNumber" resultMap="ReceivingInspectionResultMap">
410
+ SELECT * FROM "受入検査データ"
411
+ WHERE "入荷番号" = #{receivingNumber}
412
+ </select>
413
+
414
+ <select id="findBySupplierCode" resultMap="ReceivingInspectionResultMap">
415
+ SELECT * FROM "受入検査データ"
416
+ WHERE "仕入先コード" = #{supplierCode}
417
+ ORDER BY "検査日" DESC
418
+ </select>
419
+
420
+ <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
421
+ INSERT INTO "受入検査データ" (
422
+ "受入検査番号", "入荷番号", "発注番号", "品目コード",
423
+ "仕入先コード", "検査日", "検査担当者コード",
424
+ "検査数量", "合格数", "不合格数", "判定", "備考"
425
+ ) VALUES (
426
+ #{inspectionNumber}, #{receivingNumber}, #{purchaseOrderNumber}, #{itemCode},
427
+ #{supplierCode}, #{inspectionDate}, #{inspectorCode},
428
+ #{inspectionQuantity}, #{passedQuantity}, #{failedQuantity},
429
+ #{judgment, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler},
430
+ #{remarks}
431
+ )
432
+ </insert>
433
+
434
+ <insert id="insertResult" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
435
+ INSERT INTO "受入検査結果データ" (
436
+ "受入検査番号", "欠点コード", "数量", "備考"
437
+ ) VALUES (
438
+ #{inspectionNumber}, #{defectCode}, #{quantity}, #{remarks}
439
+ )
440
+ </insert>
441
+
442
+ <update id="update">
443
+ UPDATE "受入検査データ" SET
444
+ "検査数量" = #{inspectionQuantity},
445
+ "合格数" = #{passedQuantity},
446
+ "不合格数" = #{failedQuantity},
447
+ "判定" = #{judgment, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler},
448
+ "備考" = #{remarks},
449
+ "更新日時" = CURRENT_TIMESTAMP
450
+ WHERE "受入検査番号" = #{inspectionNumber}
451
+ </update>
452
+
453
+ </mapper>
454
+ ```
455
+
456
+ </details>
457
+
458
+ #### Flyway マイグレーション:受入検査
459
+
460
+ <details>
461
+ <summary>V029_1__create_receiving_inspection_tables.sql</summary>
462
+
463
+ ```sql
464
+ -- V029_1__create_receiving_inspection_tables.sql
465
+
466
+ -- 検査判定 ENUM(共通)
467
+ CREATE TYPE "検査判定" AS ENUM ('合格', '不合格', '保留');
468
+
469
+ -- 欠点マスタ
470
+ CREATE TABLE "欠点マスタ" (
471
+ "欠点コード" VARCHAR(20) PRIMARY KEY,
472
+ "欠点名" VARCHAR(100) NOT NULL,
473
+ "欠点分類" VARCHAR(50),
474
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
475
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
476
+ );
477
+
478
+ COMMENT ON TABLE "欠点マスタ" IS '欠点マスタ';
479
+ COMMENT ON COLUMN "欠点マスタ"."欠点コード" IS '欠点コード';
480
+ COMMENT ON COLUMN "欠点マスタ"."欠点名" IS '欠点名';
481
+ COMMENT ON COLUMN "欠点マスタ"."欠点分類" IS '欠点分類(外観/寸法/機能など)';
482
+
483
+ -- 受入検査データ
484
+ CREATE TABLE "受入検査データ" (
485
+ "ID" SERIAL PRIMARY KEY,
486
+ "受入検査番号" VARCHAR(20) UNIQUE NOT NULL,
487
+ "入荷番号" VARCHAR(20) NOT NULL,
488
+ "発注番号" VARCHAR(20) NOT NULL,
489
+ "品目コード" VARCHAR(20) NOT NULL,
490
+ "仕入先コード" VARCHAR(20) NOT NULL,
491
+ "検査日" DATE NOT NULL,
492
+ "検査担当者コード" VARCHAR(20) NOT NULL,
493
+ "検査数量" DECIMAL(15, 2) NOT NULL,
494
+ "合格数" DECIMAL(15, 2) NOT NULL,
495
+ "不合格数" DECIMAL(15, 2) NOT NULL,
496
+ "判定" "検査判定" NOT NULL,
497
+ "備考" VARCHAR(500),
498
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
499
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
500
+ CONSTRAINT "FK_受入検査_品目" FOREIGN KEY ("品目コード")
501
+ REFERENCES "品目マスタ"("品目コード"),
502
+ CONSTRAINT "FK_受入検査_仕入先" FOREIGN KEY ("仕入先コード")
503
+ REFERENCES "仕入先マスタ"("仕入先コード")
504
+ );
505
+
506
+ COMMENT ON TABLE "受入検査データ" IS '受入検査データ';
507
+ COMMENT ON COLUMN "受入検査データ"."受入検査番号" IS '受入検査番号';
508
+ COMMENT ON COLUMN "受入検査データ"."入荷番号" IS '入荷番号';
509
+ COMMENT ON COLUMN "受入検査データ"."判定" IS '検査判定(合格/不合格/保留)';
510
+
511
+ -- 受入検査結果データ
512
+ CREATE TABLE "受入検査結果データ" (
513
+ "ID" SERIAL PRIMARY KEY,
514
+ "受入検査番号" VARCHAR(20) NOT NULL,
515
+ "欠点コード" VARCHAR(20) NOT NULL,
516
+ "数量" DECIMAL(15, 2) NOT NULL,
517
+ "備考" VARCHAR(500),
518
+ CONSTRAINT "UK_受入検査結果" UNIQUE ("受入検査番号", "欠点コード"),
519
+ CONSTRAINT "FK_受入検査結果_受入検査" FOREIGN KEY ("受入検査番号")
520
+ REFERENCES "受入検査データ"("受入検査番号"),
521
+ CONSTRAINT "FK_受入検査結果_欠点" FOREIGN KEY ("欠点コード")
522
+ REFERENCES "欠点マスタ"("欠点コード")
523
+ );
524
+
525
+ COMMENT ON TABLE "受入検査結果データ" IS '受入検査結果データ';
526
+
527
+ -- インデックス
528
+ CREATE INDEX "IDX_受入検査_入荷番号" ON "受入検査データ" ("入荷番号");
529
+ CREATE INDEX "IDX_受入検査_仕入先" ON "受入検査データ" ("仕入先コード");
530
+ CREATE INDEX "IDX_受入検査_検査日" ON "受入検査データ" ("検査日");
531
+ ```
532
+
533
+ </details>
534
+
535
+ #### 受入検査の ER 図
536
+
537
+ ```plantuml
538
+ @startuml
539
+
540
+ entity "受入検査データ" as receiving_inspection {
541
+ * ID [PK]
542
+ --
543
+ * 受入検査番号 [UK]
544
+ * 入荷番号
545
+ * 発注番号
546
+ * 品目コード [FK]
547
+ * 仕入先コード [FK]
548
+ * 検査日
549
+ * 検査担当者コード
550
+ * 検査数量
551
+ * 合格数
552
+ * 不合格数
553
+ * 判定
554
+ 備考
555
+ * 作成日時
556
+ * 更新日時
557
+ }
558
+
559
+ entity "受入検査結果データ" as receiving_inspection_result {
560
+ * ID [PK]
561
+ --
562
+ * 受入検査番号 [FK]
563
+ * 欠点コード [FK]
564
+ * 数量
565
+ 備考
566
+ }
567
+
568
+ entity "欠点マスタ" as defect_master {
569
+ * 欠点コード [PK]
570
+ --
571
+ * 欠点名
572
+ 欠点分類
573
+ * 作成日時
574
+ * 更新日時
575
+ }
576
+
577
+ receiving_inspection ||--o{ receiving_inspection_result
578
+ defect_master ||--o{ receiving_inspection_result
579
+
580
+ @enduml
581
+ ```
582
+
583
+ ### 工程検査の設計(製造中の品質確認)
584
+
585
+ 工程検査は、製造工程の途中で実施する品質検査です。製造工程で品質問題を早期発見し、不良品の大量発生を防止します。
586
+
587
+ ```plantuml
588
+ @startuml
589
+
590
+ title 工程検査の業務フロー
591
+
592
+ |工程管理|
593
+ start
594
+ :作業実施;
595
+ :工程完了報告;
596
+
597
+ |品質管理|
598
+ :工程内検査実施;
599
+ :検査結果記録;
600
+
601
+ if (判定) then (合格)
602
+ :次工程へ進行;
603
+ |工程管理|
604
+ :次工程開始;
605
+ else (不合格)
606
+ :不合格処理;
607
+ if (処置) then (手直し)
608
+ |工程管理|
609
+ :手直し作業;
610
+ :再検査;
611
+ else (廃棄)
612
+ :廃棄処理;
613
+ :在庫減算;
614
+ endif
615
+ endif
616
+
617
+ stop
618
+
619
+ @enduml
620
+ ```
621
+
622
+ #### 工程検査データエンティティ
623
+
624
+ <details>
625
+ <summary>ProcessInspection.java</summary>
626
+
627
+ ```java
628
+ // src/main/java/com/example/sms/domain/model/quality/ProcessInspection.java
629
+ package com.example.pms.domain.model.quality;
630
+
631
+ import lombok.*;
632
+ import java.math.BigDecimal;
633
+ import java.time.LocalDate;
634
+ import java.time.LocalDateTime;
635
+ import java.util.List;
636
+
637
+ /**
638
+ * 工程検査データエンティティ.
639
+ */
640
+ @Data
641
+ @Builder
642
+ @NoArgsConstructor
643
+ @AllArgsConstructor
644
+ public class ProcessInspection {
645
+ private Integer id;
646
+ private String inspectionNumber;
647
+ private String workOrderNumber;
648
+ private String processCode;
649
+ private String itemCode;
650
+ private LocalDate inspectionDate;
651
+ private String inspectorCode;
652
+ private BigDecimal inspectionQuantity;
653
+ private BigDecimal passedQuantity;
654
+ private BigDecimal failedQuantity;
655
+ private InspectionJudgment judgment;
656
+ private String remarks;
657
+ private LocalDateTime createdAt;
658
+ private LocalDateTime updatedAt;
659
+
660
+ // 楽観ロック用バージョン
661
+ @Builder.Default
662
+ private Integer version = 1;
663
+
664
+ // リレーション
665
+ private Item item;
666
+ private Process process;
667
+ @Builder.Default
668
+ private List<ProcessInspectionResult> results = new ArrayList<>();
669
+ }
670
+ ```
671
+
672
+ </details>
673
+
674
+ #### 工程検査結果データエンティティ
675
+
676
+ <details>
677
+ <summary>ProcessInspectionResult.java</summary>
678
+
679
+ ```java
680
+ // src/main/java/com/example/sms/domain/model/quality/ProcessInspectionResult.java
681
+ package com.example.pms.domain.model.quality;
682
+
683
+ import lombok.*;
684
+ import java.math.BigDecimal;
685
+
686
+ /**
687
+ * 工程検査結果データエンティティ.
688
+ */
689
+ @Data
690
+ @Builder
691
+ @NoArgsConstructor
692
+ @AllArgsConstructor
693
+ public class ProcessInspectionResult {
694
+ private Integer id;
695
+ private String inspectionNumber;
696
+ private String defectCode;
697
+ private BigDecimal quantity;
698
+ private String remarks;
699
+
700
+ // リレーション
701
+ private Defect defect;
702
+ }
703
+ ```
704
+
705
+ </details>
706
+
707
+ #### MyBatis Mapper XML:工程検査
708
+
709
+ <details>
710
+ <summary>ProcessInspectionMapper.xml</summary>
711
+
712
+ ```xml
713
+ <?xml version="1.0" encoding="UTF-8" ?>
714
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
715
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
716
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.ProcessInspectionMapper">
717
+
718
+ <resultMap id="ProcessInspectionResultMap"
719
+ type="com.example.pms.domain.model.quality.ProcessInspection">
720
+ <id property="id" column="ID"/>
721
+ <result property="inspectionNumber" column="工程検査番号"/>
722
+ <result property="workOrderNumber" column="作業指示番号"/>
723
+ <result property="processCode" column="工程コード"/>
724
+ <result property="itemCode" column="品目コード"/>
725
+ <result property="inspectionDate" column="検査日"/>
726
+ <result property="inspectorCode" column="検査担当者コード"/>
727
+ <result property="inspectionQuantity" column="検査数量"/>
728
+ <result property="passedQuantity" column="合格数"/>
729
+ <result property="failedQuantity" column="不合格数"/>
730
+ <result property="judgment" column="判定"
731
+ typeHandler="com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler"/>
732
+ <result property="remarks" column="備考"/>
733
+ <result property="createdAt" column="作成日時"/>
734
+ <result property="updatedAt" column="更新日時"/>
735
+ <collection property="results" ofType="com.example.pms.domain.model.quality.ProcessInspectionResult"
736
+ resultMap="ProcessInspectionResultResultMap"/>
737
+ </resultMap>
738
+
739
+ <resultMap id="ProcessInspectionResultResultMap"
740
+ type="com.example.pms.domain.model.quality.ProcessInspectionResult">
741
+ <id property="id" column="RESULT_ID"/>
742
+ <result property="inspectionNumber" column="工程検査番号"/>
743
+ <result property="defectCode" column="欠点コード"/>
744
+ <result property="quantity" column="数量"/>
745
+ <result property="remarks" column="結果備考"/>
746
+ </resultMap>
747
+
748
+ <select id="findByInspectionNumber" resultMap="ProcessInspectionResultMap">
749
+ SELECT
750
+ pi.*,
751
+ pir."ID" AS RESULT_ID,
752
+ pir."欠点コード",
753
+ pir."数量",
754
+ pir."備考" AS 結果備考
755
+ FROM "工程検査データ" pi
756
+ LEFT JOIN "工程検査結果データ" pir ON pi."工程検査番号" = pir."工程検査番号"
757
+ WHERE pi."工程検査番号" = #{inspectionNumber}
758
+ </select>
759
+
760
+ <select id="findByWorkOrderNumber" resultMap="ProcessInspectionResultMap">
761
+ SELECT * FROM "工程検査データ"
762
+ WHERE "作業指示番号" = #{workOrderNumber}
763
+ ORDER BY "検査日" ASC
764
+ </select>
765
+
766
+ <select id="findByProcessCode" resultMap="ProcessInspectionResultMap">
767
+ SELECT * FROM "工程検査データ"
768
+ WHERE "工程コード" = #{processCode}
769
+ ORDER BY "検査日" DESC
770
+ </select>
771
+
772
+ <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
773
+ INSERT INTO "工程検査データ" (
774
+ "工程検査番号", "作業指示番号", "工程コード", "品目コード",
775
+ "検査日", "検査担当者コード",
776
+ "検査数量", "合格数", "不合格数", "判定", "備考"
777
+ ) VALUES (
778
+ #{inspectionNumber}, #{workOrderNumber}, #{processCode}, #{itemCode},
779
+ #{inspectionDate}, #{inspectorCode},
780
+ #{inspectionQuantity}, #{passedQuantity}, #{failedQuantity},
781
+ #{judgment, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler},
782
+ #{remarks}
783
+ )
784
+ </insert>
785
+
786
+ <insert id="insertResult" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
787
+ INSERT INTO "工程検査結果データ" (
788
+ "工程検査番号", "欠点コード", "数量", "備考"
789
+ ) VALUES (
790
+ #{inspectionNumber}, #{defectCode}, #{quantity}, #{remarks}
791
+ )
792
+ </insert>
793
+
794
+ <update id="update">
795
+ UPDATE "工程検査データ" SET
796
+ "検査数量" = #{inspectionQuantity},
797
+ "合格数" = #{passedQuantity},
798
+ "不合格数" = #{failedQuantity},
799
+ "判定" = #{judgment, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler},
800
+ "備考" = #{remarks},
801
+ "更新日時" = CURRENT_TIMESTAMP
802
+ WHERE "工程検査番号" = #{inspectionNumber}
803
+ </update>
804
+
805
+ </mapper>
806
+ ```
807
+
808
+ </details>
809
+
810
+ #### Flyway マイグレーション:工程検査
811
+
812
+ <details>
813
+ <summary>V029_2__create_process_inspection_tables.sql</summary>
814
+
815
+ ```sql
816
+ -- V029_2__create_process_inspection_tables.sql
817
+
818
+ -- 工程検査データ
819
+ CREATE TABLE "工程検査データ" (
820
+ "ID" SERIAL PRIMARY KEY,
821
+ "工程検査番号" VARCHAR(20) UNIQUE NOT NULL,
822
+ "作業指示番号" VARCHAR(20) NOT NULL,
823
+ "工程コード" VARCHAR(20) NOT NULL,
824
+ "品目コード" VARCHAR(20) NOT NULL,
825
+ "検査日" DATE NOT NULL,
826
+ "検査担当者コード" VARCHAR(20) NOT NULL,
827
+ "検査数量" DECIMAL(15, 2) NOT NULL,
828
+ "合格数" DECIMAL(15, 2) NOT NULL,
829
+ "不合格数" DECIMAL(15, 2) NOT NULL,
830
+ "判定" "検査判定" NOT NULL,
831
+ "備考" VARCHAR(500),
832
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
833
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
834
+ CONSTRAINT "FK_工程検査_作業指示" FOREIGN KEY ("作業指示番号")
835
+ REFERENCES "作業指示データ"("作業指示番号"),
836
+ CONSTRAINT "FK_工程検査_工程" FOREIGN KEY ("工程コード")
837
+ REFERENCES "工程マスタ"("工程コード"),
838
+ CONSTRAINT "FK_工程検査_品目" FOREIGN KEY ("品目コード")
839
+ REFERENCES "品目マスタ"("品目コード")
840
+ );
841
+
842
+ COMMENT ON TABLE "工程検査データ" IS '工程検査データ';
843
+ COMMENT ON COLUMN "工程検査データ"."工程検査番号" IS '工程検査番号';
844
+ COMMENT ON COLUMN "工程検査データ"."作業指示番号" IS '作業指示番号';
845
+ COMMENT ON COLUMN "工程検査データ"."工程コード" IS '検査対象工程';
846
+
847
+ -- 工程検査結果データ
848
+ CREATE TABLE "工程検査結果データ" (
849
+ "ID" SERIAL PRIMARY KEY,
850
+ "工程検査番号" VARCHAR(20) NOT NULL,
851
+ "欠点コード" VARCHAR(20) NOT NULL,
852
+ "数量" DECIMAL(15, 2) NOT NULL,
853
+ "備考" VARCHAR(500),
854
+ CONSTRAINT "UK_工程検査結果" UNIQUE ("工程検査番号", "欠点コード"),
855
+ CONSTRAINT "FK_工程検査結果_工程検査" FOREIGN KEY ("工程検査番号")
856
+ REFERENCES "工程検査データ"("工程検査番号"),
857
+ CONSTRAINT "FK_工程検査結果_欠点" FOREIGN KEY ("欠点コード")
858
+ REFERENCES "欠点マスタ"("欠点コード")
859
+ );
860
+
861
+ COMMENT ON TABLE "工程検査結果データ" IS '工程検査結果データ';
862
+
863
+ -- インデックス
864
+ CREATE INDEX "IDX_工程検査_作業指示" ON "工程検査データ" ("作業指示番号");
865
+ CREATE INDEX "IDX_工程検査_工程" ON "工程検査データ" ("工程コード");
866
+ CREATE INDEX "IDX_工程検査_検査日" ON "工程検査データ" ("検査日");
867
+ ```
868
+
869
+ </details>
870
+
871
+ #### 工程検査の ER 図
872
+
873
+ ```plantuml
874
+ @startuml
875
+
876
+ entity "工程検査データ" as process_inspection {
877
+ * ID [PK]
878
+ --
879
+ * 工程検査番号 [UK]
880
+ * 作業指示番号 [FK]
881
+ * 工程コード [FK]
882
+ * 品目コード [FK]
883
+ * 検査日
884
+ * 検査担当者コード
885
+ * 検査数量
886
+ * 合格数
887
+ * 不合格数
888
+ * 判定
889
+ 備考
890
+ * 作成日時
891
+ * 更新日時
892
+ }
893
+
894
+ entity "工程検査結果データ" as process_inspection_result {
895
+ * ID [PK]
896
+ --
897
+ * 工程検査番号 [FK]
898
+ * 欠点コード [FK]
899
+ * 数量
900
+ 備考
901
+ }
902
+
903
+ entity "作業指示データ" as work_order {
904
+ * 作業指示番号 [PK]
905
+ --
906
+ ...
907
+ }
908
+
909
+ entity "工程マスタ" as process_master {
910
+ * 工程コード [PK]
911
+ --
912
+ ...
913
+ }
914
+
915
+ work_order ||--o{ process_inspection
916
+ process_master ||--o{ process_inspection
917
+ process_inspection ||--o{ process_inspection_result
918
+
919
+ @enduml
920
+ ```
921
+
922
+ ### 出荷検査の設計(出荷前の品質確認)
923
+
924
+ 出荷検査は、製品を顧客に出荷する前に実施する最終品質検査です。不良品の流出を防止する最後の砦となります。
925
+
926
+ ```plantuml
927
+ @startuml
928
+
929
+ title 出荷検査の業務フロー
930
+
931
+ |出荷管理|
932
+ start
933
+ :出荷指示;
934
+ :ピッキング;
935
+
936
+ |品質管理|
937
+ :出荷検査実施;
938
+ :検査結果記録;
939
+
940
+ if (判定) then (合格)
941
+ :出荷許可;
942
+ |出荷管理|
943
+ :梱包・発送;
944
+ :出荷完了;
945
+ else (不合格)
946
+ :出荷保留;
947
+ if (処置) then (再検査)
948
+ :再検査実施;
949
+ else (差替)
950
+ :代替品手配;
951
+ :再出荷検査;
952
+ endif
953
+ endif
954
+
955
+ stop
956
+
957
+ @enduml
958
+ ```
959
+
960
+ #### 出荷検査データエンティティ
961
+
962
+ <details>
963
+ <summary>ShipmentInspection.java</summary>
964
+
965
+ ```java
966
+ // src/main/java/com/example/sms/domain/model/quality/ShipmentInspection.java
967
+ package com.example.pms.domain.model.quality;
968
+
969
+ import lombok.*;
970
+ import java.math.BigDecimal;
971
+ import java.time.LocalDate;
972
+ import java.time.LocalDateTime;
973
+ import java.util.List;
974
+
975
+ /**
976
+ * 出荷検査データエンティティ.
977
+ */
978
+ @Data
979
+ @Builder
980
+ @NoArgsConstructor
981
+ @AllArgsConstructor
982
+ public class ShipmentInspection {
983
+ private Integer id;
984
+ private String inspectionNumber;
985
+ private String shipmentNumber;
986
+ private String itemCode;
987
+ private LocalDate inspectionDate;
988
+ private String inspectorCode;
989
+ private BigDecimal inspectionQuantity;
990
+ private BigDecimal passedQuantity;
991
+ private BigDecimal failedQuantity;
992
+ private InspectionJudgment judgment;
993
+ private String remarks;
994
+ private LocalDateTime createdAt;
995
+ private LocalDateTime updatedAt;
996
+
997
+ // 楽観ロック用バージョン
998
+ @Builder.Default
999
+ private Integer version = 1;
1000
+
1001
+ // リレーション
1002
+ private Item item;
1003
+ @Builder.Default
1004
+ private List<ShipmentInspectionResult> results = new ArrayList<>();
1005
+ }
1006
+ ```
1007
+
1008
+ </details>
1009
+
1010
+ #### 出荷検査結果データエンティティ
1011
+
1012
+ <details>
1013
+ <summary>ShipmentInspectionResult.java</summary>
1014
+
1015
+ ```java
1016
+ // src/main/java/com/example/sms/domain/model/quality/ShipmentInspectionResult.java
1017
+ package com.example.pms.domain.model.quality;
1018
+
1019
+ import lombok.*;
1020
+ import java.math.BigDecimal;
1021
+
1022
+ /**
1023
+ * 出荷検査結果データエンティティ.
1024
+ */
1025
+ @Data
1026
+ @Builder
1027
+ @NoArgsConstructor
1028
+ @AllArgsConstructor
1029
+ public class ShipmentInspectionResult {
1030
+ private Integer id;
1031
+ private String inspectionNumber;
1032
+ private String defectCode;
1033
+ private BigDecimal quantity;
1034
+ private String remarks;
1035
+
1036
+ // リレーション
1037
+ private Defect defect;
1038
+ }
1039
+ ```
1040
+
1041
+ </details>
1042
+
1043
+ #### MyBatis Mapper XML:出荷検査
1044
+
1045
+ <details>
1046
+ <summary>ShipmentInspectionMapper.xml</summary>
1047
+
1048
+ ```xml
1049
+ <?xml version="1.0" encoding="UTF-8" ?>
1050
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
1051
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
1052
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.ShipmentInspectionMapper">
1053
+
1054
+ <resultMap id="ShipmentInspectionResultMap"
1055
+ type="com.example.pms.domain.model.quality.ShipmentInspection">
1056
+ <id property="id" column="ID"/>
1057
+ <result property="inspectionNumber" column="出荷検査番号"/>
1058
+ <result property="shipmentNumber" column="出荷番号"/>
1059
+ <result property="itemCode" column="品目コード"/>
1060
+ <result property="inspectionDate" column="検査日"/>
1061
+ <result property="inspectorCode" column="検査担当者コード"/>
1062
+ <result property="inspectionQuantity" column="検査数量"/>
1063
+ <result property="passedQuantity" column="合格数"/>
1064
+ <result property="failedQuantity" column="不合格数"/>
1065
+ <result property="judgment" column="判定"
1066
+ typeHandler="com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler"/>
1067
+ <result property="remarks" column="備考"/>
1068
+ <result property="createdAt" column="作成日時"/>
1069
+ <result property="updatedAt" column="更新日時"/>
1070
+ <collection property="results" ofType="com.example.pms.domain.model.quality.ShipmentInspectionResult"
1071
+ resultMap="ShipmentInspectionResultResultMap"/>
1072
+ </resultMap>
1073
+
1074
+ <resultMap id="ShipmentInspectionResultResultMap"
1075
+ type="com.example.pms.domain.model.quality.ShipmentInspectionResult">
1076
+ <id property="id" column="RESULT_ID"/>
1077
+ <result property="inspectionNumber" column="出荷検査番号"/>
1078
+ <result property="defectCode" column="欠点コード"/>
1079
+ <result property="quantity" column="数量"/>
1080
+ <result property="remarks" column="結果備考"/>
1081
+ </resultMap>
1082
+
1083
+ <select id="findByInspectionNumber" resultMap="ShipmentInspectionResultMap">
1084
+ SELECT
1085
+ si.*,
1086
+ sir."ID" AS RESULT_ID,
1087
+ sir."欠点コード",
1088
+ sir."数量",
1089
+ sir."備考" AS 結果備考
1090
+ FROM "出荷検査データ" si
1091
+ LEFT JOIN "出荷検査結果データ" sir ON si."出荷検査番号" = sir."出荷検査番号"
1092
+ WHERE si."出荷検査番号" = #{inspectionNumber}
1093
+ </select>
1094
+
1095
+ <select id="findByShipmentNumber" resultMap="ShipmentInspectionResultMap">
1096
+ SELECT * FROM "出荷検査データ"
1097
+ WHERE "出荷番号" = #{shipmentNumber}
1098
+ </select>
1099
+
1100
+ <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
1101
+ INSERT INTO "出荷検査データ" (
1102
+ "出荷検査番号", "出荷番号", "品目コード", "検査日",
1103
+ "検査担当者コード", "検査数量", "合格数", "不合格数", "判定", "備考"
1104
+ ) VALUES (
1105
+ #{inspectionNumber}, #{shipmentNumber}, #{itemCode}, #{inspectionDate},
1106
+ #{inspectorCode}, #{inspectionQuantity}, #{passedQuantity}, #{failedQuantity},
1107
+ #{judgment, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler},
1108
+ #{remarks}
1109
+ )
1110
+ </insert>
1111
+
1112
+ <insert id="insertResult" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
1113
+ INSERT INTO "出荷検査結果データ" (
1114
+ "出荷検査番号", "欠点コード", "数量", "備考"
1115
+ ) VALUES (
1116
+ #{inspectionNumber}, #{defectCode}, #{quantity}, #{remarks}
1117
+ )
1118
+ </insert>
1119
+
1120
+ <update id="update">
1121
+ UPDATE "出荷検査データ" SET
1122
+ "検査数量" = #{inspectionQuantity},
1123
+ "合格数" = #{passedQuantity},
1124
+ "不合格数" = #{failedQuantity},
1125
+ "判定" = #{judgment, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler},
1126
+ "備考" = #{remarks},
1127
+ "更新日時" = CURRENT_TIMESTAMP
1128
+ WHERE "出荷検査番号" = #{inspectionNumber}
1129
+ </update>
1130
+
1131
+ </mapper>
1132
+ ```
1133
+
1134
+ </details>
1135
+
1136
+ #### Flyway マイグレーション:出荷検査
1137
+
1138
+ <details>
1139
+ <summary>V029_3__create_shipment_inspection_tables.sql</summary>
1140
+
1141
+ ```sql
1142
+ -- V029_3__create_shipment_inspection_tables.sql
1143
+
1144
+ -- 出荷検査データ
1145
+ CREATE TABLE "出荷検査データ" (
1146
+ "ID" SERIAL PRIMARY KEY,
1147
+ "出荷検査番号" VARCHAR(20) UNIQUE NOT NULL,
1148
+ "出荷番号" VARCHAR(20) NOT NULL,
1149
+ "品目コード" VARCHAR(20) NOT NULL,
1150
+ "検査日" DATE NOT NULL,
1151
+ "検査担当者コード" VARCHAR(20) NOT NULL,
1152
+ "検査数量" DECIMAL(15, 2) NOT NULL,
1153
+ "合格数" DECIMAL(15, 2) NOT NULL,
1154
+ "不合格数" DECIMAL(15, 2) NOT NULL,
1155
+ "判定" "検査判定" NOT NULL,
1156
+ "備考" VARCHAR(500),
1157
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1158
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1159
+ CONSTRAINT "FK_出荷検査_品目" FOREIGN KEY ("品目コード")
1160
+ REFERENCES "品目マスタ"("品目コード")
1161
+ );
1162
+
1163
+ COMMENT ON TABLE "出荷検査データ" IS '出荷検査データ';
1164
+ COMMENT ON COLUMN "出荷検査データ"."出荷検査番号" IS '出荷検査番号';
1165
+ COMMENT ON COLUMN "出荷検査データ"."出荷番号" IS '出荷番号';
1166
+ COMMENT ON COLUMN "出荷検査データ"."判定" IS '検査判定(合格/不合格/保留)';
1167
+
1168
+ -- 出荷検査結果データ
1169
+ CREATE TABLE "出荷検査結果データ" (
1170
+ "ID" SERIAL PRIMARY KEY,
1171
+ "出荷検査番号" VARCHAR(20) NOT NULL,
1172
+ "欠点コード" VARCHAR(20) NOT NULL,
1173
+ "数量" DECIMAL(15, 2) NOT NULL,
1174
+ "備考" VARCHAR(500),
1175
+ CONSTRAINT "UK_出荷検査結果" UNIQUE ("出荷検査番号", "欠点コード"),
1176
+ CONSTRAINT "FK_出荷検査結果_出荷検査" FOREIGN KEY ("出荷検査番号")
1177
+ REFERENCES "出荷検査データ"("出荷検査番号"),
1178
+ CONSTRAINT "FK_出荷検査結果_欠点" FOREIGN KEY ("欠点コード")
1179
+ REFERENCES "欠点マスタ"("欠点コード")
1180
+ );
1181
+
1182
+ COMMENT ON TABLE "出荷検査結果データ" IS '出荷検査結果データ';
1183
+
1184
+ -- インデックス
1185
+ CREATE INDEX "IDX_出荷検査_出荷番号" ON "出荷検査データ" ("出荷番号");
1186
+ CREATE INDEX "IDX_出荷検査_検査日" ON "出荷検査データ" ("検査日");
1187
+ ```
1188
+
1189
+ </details>
1190
+
1191
+ #### 出荷検査の ER 図
1192
+
1193
+ ```plantuml
1194
+ @startuml
1195
+
1196
+ entity "出荷検査データ" as shipment_inspection {
1197
+ * ID [PK]
1198
+ --
1199
+ * 出荷検査番号 [UK]
1200
+ * 出荷番号
1201
+ * 品目コード [FK]
1202
+ * 検査日
1203
+ * 検査担当者コード
1204
+ * 検査数量
1205
+ * 合格数
1206
+ * 不合格数
1207
+ * 判定
1208
+ 備考
1209
+ * 作成日時
1210
+ * 更新日時
1211
+ }
1212
+
1213
+ entity "出荷検査結果データ" as shipment_inspection_result {
1214
+ * ID [PK]
1215
+ --
1216
+ * 出荷検査番号 [FK]
1217
+ * 欠点コード [FK]
1218
+ * 数量
1219
+ 備考
1220
+ }
1221
+
1222
+ entity "欠点マスタ" as defect_master {
1223
+ * 欠点コード [PK]
1224
+ --
1225
+ * 欠点名
1226
+ 欠点分類
1227
+ * 作成日時
1228
+ * 更新日時
1229
+ }
1230
+
1231
+ shipment_inspection ||--o{ shipment_inspection_result
1232
+ defect_master ||--o{ shipment_inspection_result
1233
+
1234
+ @enduml
1235
+ ```
1236
+
1237
+ ### トレーサビリティ(ロット追跡・履歴管理)
1238
+
1239
+ 製造業におけるトレーサビリティは、製品の製造履歴を追跡可能にする仕組みです。品質問題が発生した際に、原因究明と影響範囲の特定を可能にします。
1240
+
1241
+ ```plantuml
1242
+ @startuml
1243
+
1244
+ title トレーサビリティの概念図
1245
+
1246
+ |トレースフォワード|
1247
+ note right: 材料 → 製品 の追跡
1248
+ :原材料ロット;
1249
+ :製造ロット;
1250
+ :製品ロット;
1251
+ :出荷先;
1252
+
1253
+ |トレースバック|
1254
+ note right: 製品 → 材料 の追跡
1255
+ :不良品発覚;
1256
+ :製造ロット特定;
1257
+ :使用材料特定;
1258
+ :影響範囲調査;
1259
+
1260
+ @enduml
1261
+ ```
1262
+
1263
+ #### トレーサビリティの種類
1264
+
1265
+ | 種類 | 説明 | 用途 |
1266
+ |-----|------|-----|
1267
+ | **トレースフォワード** | 材料から製品への追跡 | 不良材料を使用した製品の特定 |
1268
+ | **トレースバック** | 製品から材料への追跡 | 不良製品の原因究明 |
1269
+
1270
+ #### ロット種別 Enum
1271
+
1272
+ <details>
1273
+ <summary>LotType.java</summary>
1274
+
1275
+ ```java
1276
+ // src/main/java/com/example/sms/domain/model/quality/LotType.java
1277
+ package com.example.pms.domain.model.quality;
1278
+
1279
+ /**
1280
+ * ロット種別
1281
+ */
1282
+ public enum LotType {
1283
+ PURCHASED("購入ロット"),
1284
+ MANUFACTURED("製造ロット");
1285
+
1286
+ private final String displayName;
1287
+
1288
+ LotType(String displayName) {
1289
+ this.displayName = displayName;
1290
+ }
1291
+
1292
+ public String getDisplayName() {
1293
+ return displayName;
1294
+ }
1295
+
1296
+ public static LotType fromDisplayName(String displayName) {
1297
+ for (LotType type : values()) {
1298
+ if (type.displayName.equals(displayName)) {
1299
+ return type;
1300
+ }
1301
+ }
1302
+ throw new IllegalArgumentException("Unknown display name: " + displayName);
1303
+ }
1304
+ }
1305
+ ```
1306
+
1307
+ </details>
1308
+
1309
+ #### ロット種別 TypeHandler
1310
+
1311
+ <details>
1312
+ <summary>LotTypeTypeHandler.java</summary>
1313
+
1314
+ ```java
1315
+ // src/main/java/com/example/sms/infrastructure/out/persistence/typehandler/LotTypeTypeHandler.java
1316
+ package com.example.pms.infrastructure.out.persistence.typehandler;
1317
+
1318
+ import com.example.pms.domain.model.quality.LotType;
1319
+ import org.apache.ibatis.type.BaseTypeHandler;
1320
+ import org.apache.ibatis.type.JdbcType;
1321
+ import org.apache.ibatis.type.MappedTypes;
1322
+
1323
+ import java.sql.CallableStatement;
1324
+ import java.sql.PreparedStatement;
1325
+ import java.sql.ResultSet;
1326
+ import java.sql.SQLException;
1327
+
1328
+ /**
1329
+ * ロット種別のTypeHandler
1330
+ */
1331
+ @MappedTypes(LotType.class)
1332
+ public class LotTypeTypeHandler extends BaseTypeHandler<LotType> {
1333
+
1334
+ @Override
1335
+ public void setNonNullParameter(PreparedStatement ps, int i,
1336
+ LotType parameter, JdbcType jdbcType) throws SQLException {
1337
+ ps.setString(i, parameter.getDisplayName());
1338
+ }
1339
+
1340
+ @Override
1341
+ public LotType getNullableResult(ResultSet rs, String columnName) throws SQLException {
1342
+ String value = rs.getString(columnName);
1343
+ return value == null ? null : LotType.fromDisplayName(value);
1344
+ }
1345
+
1346
+ @Override
1347
+ public LotType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
1348
+ String value = rs.getString(columnIndex);
1349
+ return value == null ? null : LotType.fromDisplayName(value);
1350
+ }
1351
+
1352
+ @Override
1353
+ public LotType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
1354
+ String value = cs.getString(columnIndex);
1355
+ return value == null ? null : LotType.fromDisplayName(value);
1356
+ }
1357
+ }
1358
+ ```
1359
+
1360
+ </details>
1361
+
1362
+ #### ロットマスタエンティティ
1363
+
1364
+ <details>
1365
+ <summary>LotMaster.java</summary>
1366
+
1367
+ ```java
1368
+ // src/main/java/com/example/sms/domain/model/quality/LotMaster.java
1369
+ package com.example.pms.domain.model.quality;
1370
+
1371
+ import lombok.*;
1372
+ import java.math.BigDecimal;
1373
+ import java.time.LocalDate;
1374
+ import java.time.LocalDateTime;
1375
+ import java.util.List;
1376
+
1377
+ /**
1378
+ * ロットマスタエンティティ.
1379
+ */
1380
+ @Data
1381
+ @Builder
1382
+ @NoArgsConstructor
1383
+ @AllArgsConstructor
1384
+ public class LotMaster {
1385
+ private Integer id;
1386
+ private String lotNumber;
1387
+ private String itemCode;
1388
+ private LotType lotType;
1389
+ private LocalDate manufactureDate;
1390
+ private LocalDate expirationDate;
1391
+ private BigDecimal quantity;
1392
+ private String warehouseCode;
1393
+ private String remarks;
1394
+ private LocalDateTime createdAt;
1395
+ private LocalDateTime updatedAt;
1396
+
1397
+ // 楽観ロック用バージョン
1398
+ @Builder.Default
1399
+ private Integer version = 1;
1400
+
1401
+ // リレーション
1402
+ private Item item;
1403
+ @Builder.Default
1404
+ private List<LotComposition> parentLotRelations = new ArrayList<>();
1405
+ @Builder.Default
1406
+ private List<LotComposition> childLotRelations = new ArrayList<>();
1407
+
1408
+ /**
1409
+ * 有効期限が切れているかチェック.
1410
+ *
1411
+ * @return 有効期限切れの場合 true
1412
+ */
1413
+ public boolean isExpired() {
1414
+ return expirationDate != null && expirationDate.isBefore(LocalDate.now());
1415
+ }
1416
+ }
1417
+ ```
1418
+
1419
+ </details>
1420
+
1421
+ #### ロット構成エンティティ
1422
+
1423
+ <details>
1424
+ <summary>LotComposition.java</summary>
1425
+
1426
+ ```java
1427
+ // src/main/java/com/example/sms/domain/model/quality/LotComposition.java
1428
+ package com.example.pms.domain.model.quality;
1429
+
1430
+ import lombok.*;
1431
+ import java.math.BigDecimal;
1432
+ import java.time.LocalDateTime;
1433
+
1434
+ /**
1435
+ * ロット構成エンティティ.
1436
+ * 親子ロット間の関係を管理(トレーサビリティ用)
1437
+ */
1438
+ @Data
1439
+ @Builder
1440
+ @NoArgsConstructor
1441
+ @AllArgsConstructor
1442
+ public class LotComposition {
1443
+ private Integer id;
1444
+ private String parentLotNumber;
1445
+ private String childLotNumber;
1446
+ private BigDecimal usedQuantity;
1447
+ private LocalDateTime createdAt;
1448
+ }
1449
+ ```
1450
+
1451
+ </details>
1452
+
1453
+ #### MyBatis Mapper XML:ロット管理
1454
+
1455
+ <details>
1456
+ <summary>LotMapper.xml</summary>
1457
+
1458
+ ```xml
1459
+ <?xml version="1.0" encoding="UTF-8" ?>
1460
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
1461
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
1462
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.LotMapper">
1463
+
1464
+ <resultMap id="LotMasterResultMap" type="com.example.pms.domain.model.quality.LotMaster">
1465
+ <id property="id" column="ID"/>
1466
+ <result property="lotNumber" column="ロット番号"/>
1467
+ <result property="itemCode" column="品目コード"/>
1468
+ <result property="lotType" column="ロット種別"
1469
+ typeHandler="com.example.pms.infrastructure.out.persistence.typehandler.LotTypeTypeHandler"/>
1470
+ <result property="manufactureDate" column="製造日"/>
1471
+ <result property="expirationDate" column="有効期限"/>
1472
+ <result property="quantity" column="数量"/>
1473
+ <result property="warehouseCode" column="倉庫コード"/>
1474
+ <result property="remarks" column="備考"/>
1475
+ <result property="createdAt" column="作成日時"/>
1476
+ <result property="updatedAt" column="更新日時"/>
1477
+ </resultMap>
1478
+
1479
+ <resultMap id="LotCompositionResultMap" type="com.example.pms.domain.model.quality.LotComposition">
1480
+ <id property="id" column="ID"/>
1481
+ <result property="parentLotNumber" column="親ロット番号"/>
1482
+ <result property="childLotNumber" column="子ロット番号"/>
1483
+ <result property="usedQuantity" column="使用数量"/>
1484
+ <result property="createdAt" column="作成日時"/>
1485
+ </resultMap>
1486
+
1487
+ <select id="findByLotNumber" resultMap="LotMasterResultMap">
1488
+ SELECT * FROM "ロットマスタ"
1489
+ WHERE "ロット番号" = #{lotNumber}
1490
+ </select>
1491
+
1492
+ <select id="findByItemCode" resultMap="LotMasterResultMap">
1493
+ SELECT * FROM "ロットマスタ"
1494
+ WHERE "品目コード" = #{itemCode}
1495
+ ORDER BY "製造日" DESC
1496
+ </select>
1497
+
1498
+ <select id="findChildLots" resultMap="LotCompositionResultMap">
1499
+ SELECT * FROM "ロット構成"
1500
+ WHERE "親ロット番号" = #{parentLotNumber}
1501
+ </select>
1502
+
1503
+ <select id="findParentLots" resultMap="LotCompositionResultMap">
1504
+ SELECT * FROM "ロット構成"
1505
+ WHERE "子ロット番号" = #{childLotNumber}
1506
+ </select>
1507
+
1508
+ <!-- トレースフォワード: 子ロットから製造ロット、出荷先を追跡 -->
1509
+ <select id="traceForward" resultMap="LotMasterResultMap">
1510
+ WITH RECURSIVE lot_tree AS (
1511
+ SELECT lm.*, 0 AS level
1512
+ FROM "ロットマスタ" lm
1513
+ WHERE lm."ロット番号" = #{lotNumber}
1514
+
1515
+ UNION ALL
1516
+
1517
+ SELECT lm.*, lt.level + 1
1518
+ FROM "ロットマスタ" lm
1519
+ JOIN "ロット構成" lc ON lm."ロット番号" = lc."親ロット番号"
1520
+ JOIN lot_tree lt ON lc."子ロット番号" = lt."ロット番号"
1521
+ WHERE lt.level &lt; 10
1522
+ )
1523
+ SELECT * FROM lot_tree
1524
+ ORDER BY level ASC
1525
+ </select>
1526
+
1527
+ <!-- トレースバック: 親ロットから材料ロットを追跡 -->
1528
+ <select id="traceBack" resultMap="LotMasterResultMap">
1529
+ WITH RECURSIVE lot_tree AS (
1530
+ SELECT lm.*, 0 AS level
1531
+ FROM "ロットマスタ" lm
1532
+ WHERE lm."ロット番号" = #{lotNumber}
1533
+
1534
+ UNION ALL
1535
+
1536
+ SELECT lm.*, lt.level + 1
1537
+ FROM "ロットマスタ" lm
1538
+ JOIN "ロット構成" lc ON lm."ロット番号" = lc."子ロット番号"
1539
+ JOIN lot_tree lt ON lc."親ロット番号" = lt."ロット番号"
1540
+ WHERE lt.level &lt; 10
1541
+ )
1542
+ SELECT * FROM lot_tree
1543
+ ORDER BY level ASC
1544
+ </select>
1545
+
1546
+ <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
1547
+ INSERT INTO "ロットマスタ" (
1548
+ "ロット番号", "品目コード", "ロット種別",
1549
+ "製造日", "有効期限", "数量", "倉庫コード", "備考"
1550
+ ) VALUES (
1551
+ #{lotNumber}, #{itemCode},
1552
+ #{lotType, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.LotTypeTypeHandler},
1553
+ #{manufactureDate}, #{expirationDate}, #{quantity}, #{warehouseCode}, #{remarks}
1554
+ )
1555
+ </insert>
1556
+
1557
+ <insert id="insertComposition" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
1558
+ INSERT INTO "ロット構成" (
1559
+ "親ロット番号", "子ロット番号", "使用数量"
1560
+ ) VALUES (
1561
+ #{parentLotNumber}, #{childLotNumber}, #{usedQuantity}
1562
+ )
1563
+ </insert>
1564
+
1565
+ <update id="update">
1566
+ UPDATE "ロットマスタ" SET
1567
+ "数量" = #{quantity},
1568
+ "倉庫コード" = #{warehouseCode},
1569
+ "備考" = #{remarks},
1570
+ "更新日時" = CURRENT_TIMESTAMP
1571
+ WHERE "ロット番号" = #{lotNumber}
1572
+ </update>
1573
+
1574
+ </mapper>
1575
+ ```
1576
+
1577
+ </details>
1578
+
1579
+ #### Flyway マイグレーション:ロット管理
1580
+
1581
+ <details>
1582
+ <summary>V029_4__create_lot_tables.sql</summary>
1583
+
1584
+ ```sql
1585
+ -- V029_4__create_lot_tables.sql
1586
+
1587
+ -- ロット種別 ENUM
1588
+ CREATE TYPE "ロット種別" AS ENUM ('購入ロット', '製造ロット');
1589
+
1590
+ -- ロットマスタ
1591
+ CREATE TABLE "ロットマスタ" (
1592
+ "ID" SERIAL PRIMARY KEY,
1593
+ "ロット番号" VARCHAR(30) UNIQUE NOT NULL,
1594
+ "品目コード" VARCHAR(20) NOT NULL,
1595
+ "ロット種別" "ロット種別" NOT NULL,
1596
+ "製造日" DATE,
1597
+ "有効期限" DATE,
1598
+ "数量" DECIMAL(15, 2) NOT NULL,
1599
+ "倉庫コード" VARCHAR(20),
1600
+ "備考" VARCHAR(500),
1601
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1602
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1603
+ CONSTRAINT "FK_ロット_品目" FOREIGN KEY ("品目コード")
1604
+ REFERENCES "品目マスタ"("品目コード")
1605
+ );
1606
+
1607
+ COMMENT ON TABLE "ロットマスタ" IS 'ロットマスタ';
1608
+ COMMENT ON COLUMN "ロットマスタ"."ロット番号" IS 'ロット番号';
1609
+ COMMENT ON COLUMN "ロットマスタ"."ロット種別" IS 'ロット種別(購入ロット/製造ロット)';
1610
+ COMMENT ON COLUMN "ロットマスタ"."製造日" IS '製造日(購入ロットの場合は入荷日)';
1611
+ COMMENT ON COLUMN "ロットマスタ"."有効期限" IS '有効期限(賞味期限、使用期限など)';
1612
+
1613
+ -- ロット構成(トレーサビリティ用)
1614
+ CREATE TABLE "ロット構成" (
1615
+ "ID" SERIAL PRIMARY KEY,
1616
+ "親ロット番号" VARCHAR(30) NOT NULL,
1617
+ "子ロット番号" VARCHAR(30) NOT NULL,
1618
+ "使用数量" DECIMAL(15, 2) NOT NULL,
1619
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1620
+ CONSTRAINT "UK_ロット構成" UNIQUE ("親ロット番号", "子ロット番号"),
1621
+ CONSTRAINT "FK_ロット構成_親" FOREIGN KEY ("親ロット番号")
1622
+ REFERENCES "ロットマスタ"("ロット番号"),
1623
+ CONSTRAINT "FK_ロット構成_子" FOREIGN KEY ("子ロット番号")
1624
+ REFERENCES "ロットマスタ"("ロット番号")
1625
+ );
1626
+
1627
+ COMMENT ON TABLE "ロット構成" IS 'ロット構成(トレーサビリティ用)';
1628
+ COMMENT ON COLUMN "ロット構成"."親ロット番号" IS '製造ロット(製品側)';
1629
+ COMMENT ON COLUMN "ロット構成"."子ロット番号" IS '消費ロット(材料側)';
1630
+ COMMENT ON COLUMN "ロット構成"."使用数量" IS '製造に使用した数量';
1631
+
1632
+ -- インデックス
1633
+ CREATE INDEX "IDX_ロット_品目" ON "ロットマスタ" ("品目コード");
1634
+ CREATE INDEX "IDX_ロット_製造日" ON "ロットマスタ" ("製造日");
1635
+ CREATE INDEX "IDX_ロット_有効期限" ON "ロットマスタ" ("有効期限");
1636
+ CREATE INDEX "IDX_ロット構成_親" ON "ロット構成" ("親ロット番号");
1637
+ CREATE INDEX "IDX_ロット構成_子" ON "ロット構成" ("子ロット番号");
1638
+ ```
1639
+
1640
+ </details>
1641
+
1642
+ #### ロット管理の ER 図
1643
+
1644
+ ```plantuml
1645
+ @startuml
1646
+
1647
+ entity "ロットマスタ" as lot_master {
1648
+ * ID [PK]
1649
+ --
1650
+ * ロット番号 [UK]
1651
+ * 品目コード [FK]
1652
+ * ロット種別
1653
+ 製造日
1654
+ 有効期限
1655
+ * 数量
1656
+ 倉庫コード
1657
+ 備考
1658
+ * 作成日時
1659
+ * 更新日時
1660
+ }
1661
+
1662
+ entity "ロット構成" as lot_composition {
1663
+ * ID [PK]
1664
+ --
1665
+ * 親ロット番号 [FK]
1666
+ * 子ロット番号 [FK]
1667
+ * 使用数量
1668
+ * 作成日時
1669
+ }
1670
+
1671
+ entity "品目マスタ" as item_master {
1672
+ * 品目コード [PK]
1673
+ --
1674
+ ...
1675
+ }
1676
+
1677
+ item_master ||--o{ lot_master
1678
+ lot_master ||--o{ lot_composition : 親ロット
1679
+ lot_master ||--o{ lot_composition : 子ロット
1680
+
1681
+ note right of lot_composition
1682
+ 親ロット = 製造ロット(製品)
1683
+ 子ロット = 消費ロット(材料)
1684
+ end note
1685
+
1686
+ @enduml
1687
+ ```
1688
+
1689
+ #### トレーサビリティサービス
1690
+
1691
+ <details>
1692
+ <summary>TraceabilityService.java</summary>
1693
+
1694
+ ```java
1695
+ // src/main/java/com/example/sms/application/service/quality/TraceabilityService.java
1696
+ package com.example.pms.application.service.quality;
1697
+
1698
+ import com.example.pms.domain.model.quality.LotMaster;
1699
+ import com.example.pms.infrastructure.out.persistence.mapper.LotMapper;
1700
+ import lombok.RequiredArgsConstructor;
1701
+ import org.springframework.stereotype.Service;
1702
+ import org.springframework.transaction.annotation.Transactional;
1703
+
1704
+ import java.util.List;
1705
+
1706
+ /**
1707
+ * トレーサビリティサービス
1708
+ */
1709
+ @Service
1710
+ @RequiredArgsConstructor
1711
+ public class TraceabilityService {
1712
+
1713
+ private final LotMapper lotMapper;
1714
+
1715
+ /**
1716
+ * トレースフォワード
1717
+ * 指定されたロットが使用された製品ロットを追跡する
1718
+ *
1719
+ * @param lotNumber 追跡対象のロット番号
1720
+ * @return 製品ロットのリスト
1721
+ */
1722
+ @Transactional(readOnly = true)
1723
+ public List<LotMaster> traceForward(String lotNumber) {
1724
+ return lotMapper.traceForward(lotNumber);
1725
+ }
1726
+
1727
+ /**
1728
+ * トレースバック
1729
+ * 指定された製品ロットに使用された材料ロットを追跡する
1730
+ *
1731
+ * @param lotNumber 追跡対象のロット番号
1732
+ * @return 材料ロットのリスト
1733
+ */
1734
+ @Transactional(readOnly = true)
1735
+ public List<LotMaster> traceBack(String lotNumber) {
1736
+ return lotMapper.traceBack(lotNumber);
1737
+ }
1738
+
1739
+ /**
1740
+ * 影響範囲の調査
1741
+ * 不良ロットが使用された製品と出荷先を特定する
1742
+ *
1743
+ * @param defectiveLotNumber 不良ロット番号
1744
+ * @return 影響を受けたロットのリスト
1745
+ */
1746
+ @Transactional(readOnly = true)
1747
+ public List<LotMaster> investigateImpactRange(String defectiveLotNumber) {
1748
+ // トレースフォワードで影響範囲を特定
1749
+ return traceForward(defectiveLotNumber);
1750
+ }
1751
+
1752
+ /**
1753
+ * 原因究明
1754
+ * 不良製品の原因となった材料ロットを特定する
1755
+ *
1756
+ * @param defectiveProductLotNumber 不良製品ロット番号
1757
+ * @return 原因となりうる材料ロットのリスト
1758
+ */
1759
+ @Transactional(readOnly = true)
1760
+ public List<LotMaster> investigateCause(String defectiveProductLotNumber) {
1761
+ // トレースバックで原因を追跡
1762
+ return traceBack(defectiveProductLotNumber);
1763
+ }
1764
+ }
1765
+ ```
1766
+
1767
+ </details>
1768
+
1769
+ ### 品質管理の全体 ER 図
1770
+
1771
+ ```plantuml
1772
+ @startuml
1773
+
1774
+ title 品質管理の全体 ER 図
1775
+
1776
+ entity "欠点マスタ" as defect_master {
1777
+ * 欠点コード [PK]
1778
+ --
1779
+ * 欠点名
1780
+ 欠点分類
1781
+ }
1782
+
1783
+ entity "受入検査データ" as receiving_inspection {
1784
+ * ID [PK]
1785
+ --
1786
+ * 受入検査番号 [UK]
1787
+ * 入荷番号
1788
+ * 品目コード [FK]
1789
+ * 仕入先コード [FK]
1790
+ * 判定
1791
+ }
1792
+
1793
+ entity "受入検査結果データ" as receiving_inspection_result {
1794
+ * ID [PK]
1795
+ --
1796
+ * 受入検査番号 [FK]
1797
+ * 欠点コード [FK]
1798
+ * 数量
1799
+ }
1800
+
1801
+ entity "工程検査データ" as process_inspection {
1802
+ * ID [PK]
1803
+ --
1804
+ * 工程検査番号 [UK]
1805
+ * 作業指示番号 [FK]
1806
+ * 工程コード [FK]
1807
+ * 品目コード [FK]
1808
+ * 判定
1809
+ }
1810
+
1811
+ entity "工程検査結果データ" as process_inspection_result {
1812
+ * ID [PK]
1813
+ --
1814
+ * 工程検査番号 [FK]
1815
+ * 欠点コード [FK]
1816
+ * 数量
1817
+ }
1818
+
1819
+ entity "出荷検査データ" as shipment_inspection {
1820
+ * ID [PK]
1821
+ --
1822
+ * 出荷検査番号 [UK]
1823
+ * 出荷番号
1824
+ * 品目コード [FK]
1825
+ * 判定
1826
+ }
1827
+
1828
+ entity "出荷検査結果データ" as shipment_inspection_result {
1829
+ * ID [PK]
1830
+ --
1831
+ * 出荷検査番号 [FK]
1832
+ * 欠点コード [FK]
1833
+ * 数量
1834
+ }
1835
+
1836
+ entity "ロットマスタ" as lot_master {
1837
+ * ID [PK]
1838
+ --
1839
+ * ロット番号 [UK]
1840
+ * 品目コード [FK]
1841
+ * ロット種別
1842
+ * 数量
1843
+ }
1844
+
1845
+ entity "ロット構成" as lot_composition {
1846
+ * ID [PK]
1847
+ --
1848
+ * 親ロット番号 [FK]
1849
+ * 子ロット番号 [FK]
1850
+ * 使用数量
1851
+ }
1852
+
1853
+ receiving_inspection ||--o{ receiving_inspection_result
1854
+ process_inspection ||--o{ process_inspection_result
1855
+ shipment_inspection ||--o{ shipment_inspection_result
1856
+
1857
+ defect_master ||--o{ receiving_inspection_result
1858
+ defect_master ||--o{ process_inspection_result
1859
+ defect_master ||--o{ shipment_inspection_result
1860
+
1861
+ lot_master ||--o{ lot_composition : 親ロット
1862
+ lot_master ||--o{ lot_composition : 子ロット
1863
+
1864
+ @enduml
1865
+ ```
1866
+
1867
+ ---
1868
+
1869
+ ## 29.3 リレーションと楽観ロックの設計
1870
+
1871
+ ### MyBatis ネストした select によるリレーション設定
1872
+
1873
+ 品質管理では、検査データ→検査結果、ロットマスタ→ロット構成といった親子関係があります。MyBatis でこれらの関係を効率的に取得するために、ネストした select(Nested Select)方式を採用します。
1874
+
1875
+ #### ネストした select 方式の利点
1876
+
1877
+ | 観点 | 説明 |
1878
+ |-----|------|
1879
+ | **シンプルなクエリ** | 親テーブルのみを SELECT し、関連データは別クエリで取得 |
1880
+ | **遅延ロード対応** | 必要な時のみ関連データを取得可能 |
1881
+ | **N+1 問題への対応** | MyBatis のキャッシュ機能と組み合わせて最適化 |
1882
+ | **H2/PostgreSQL 両対応** | 複雑な JOIN を避けることで DB 互換性を確保 |
1883
+
1884
+ #### 受入検査データのネスト select(検査結果を含む)
1885
+
1886
+ <details>
1887
+ <summary>ReceivingInspectionMapper.xml(リレーション設定)</summary>
1888
+
1889
+ ```xml
1890
+ <?xml version="1.0" encoding="UTF-8" ?>
1891
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
1892
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
1893
+
1894
+ <!-- src/main/resources/mapper/ReceivingInspectionMapper.xml -->
1895
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.ReceivingInspectionMapper">
1896
+
1897
+ <!-- 基本 ResultMap -->
1898
+ <resultMap id="ReceivingInspectionResultMap" type="com.example.pms.domain.model.quality.ReceivingInspection">
1899
+ <id property="id" column="ID"/>
1900
+ <result property="inspectionNumber" column="受入検査番号"/>
1901
+ <result property="receivingNumber" column="入荷番号"/>
1902
+ <result property="purchaseOrderNumber" column="発注番号"/>
1903
+ <result property="itemCode" column="品目コード"/>
1904
+ <result property="supplierCode" column="仕入先コード"/>
1905
+ <result property="inspectionDate" column="検査日"/>
1906
+ <result property="inspectorCode" column="検査担当者コード"/>
1907
+ <result property="inspectionQuantity" column="検査数量"/>
1908
+ <result property="passedQuantity" column="合格数"/>
1909
+ <result property="failedQuantity" column="不合格数"/>
1910
+ <result property="judgment" column="判定"
1911
+ typeHandler="com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler"/>
1912
+ <result property="remarks" column="備考"/>
1913
+ <result property="version" column="バージョン"/>
1914
+ <result property="createdAt" column="作成日時"/>
1915
+ <result property="updatedAt" column="更新日時"/>
1916
+ </resultMap>
1917
+
1918
+ <!-- 検査結果を含む ResultMap(ネスト select 方式) -->
1919
+ <resultMap id="ReceivingInspectionWithResultsResultMap" type="com.example.pms.domain.model.quality.ReceivingInspection"
1920
+ extends="ReceivingInspectionResultMap">
1921
+ <collection property="results" ofType="com.example.pms.domain.model.quality.ReceivingInspectionResult"
1922
+ column="受入検査番号" select="com.example.pms.infrastructure.out.persistence.mapper.ReceivingInspectionResultMapper.findByInspectionNumber"/>
1923
+ </resultMap>
1924
+
1925
+ <!-- 検査結果を含めて取得 -->
1926
+ <select id="findByInspectionNumberWithResults" resultMap="ReceivingInspectionWithResultsResultMap">
1927
+ SELECT * FROM "受入検査データ"
1928
+ WHERE "受入検査番号" = #{inspectionNumber}
1929
+ </select>
1930
+
1931
+ </mapper>
1932
+ ```
1933
+
1934
+ </details>
1935
+
1936
+ #### ロットマスタのネスト select(親子ロット構成を含む)
1937
+
1938
+ <details>
1939
+ <summary>LotMasterMapper.xml(リレーション設定)</summary>
1940
+
1941
+ ```xml
1942
+ <?xml version="1.0" encoding="UTF-8" ?>
1943
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
1944
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
1945
+
1946
+ <!-- src/main/resources/mapper/LotMasterMapper.xml -->
1947
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.LotMasterMapper">
1948
+
1949
+ <!-- 基本 ResultMap -->
1950
+ <resultMap id="LotMasterResultMap" type="com.example.pms.domain.model.quality.LotMaster">
1951
+ <id property="id" column="ID"/>
1952
+ <result property="lotNumber" column="ロット番号"/>
1953
+ <result property="itemCode" column="品目コード"/>
1954
+ <result property="lotType" column="ロット種別"
1955
+ typeHandler="com.example.pms.infrastructure.out.persistence.typehandler.LotTypeTypeHandler"/>
1956
+ <result property="manufactureDate" column="製造日"/>
1957
+ <result property="expirationDate" column="有効期限"/>
1958
+ <result property="quantity" column="数量"/>
1959
+ <result property="warehouseCode" column="倉庫コード"/>
1960
+ <result property="remarks" column="備考"/>
1961
+ <result property="version" column="バージョン"/>
1962
+ <result property="createdAt" column="作成日時"/>
1963
+ <result property="updatedAt" column="更新日時"/>
1964
+ </resultMap>
1965
+
1966
+ <!-- ロット構成を含む ResultMap(ネスト select 方式) -->
1967
+ <resultMap id="LotMasterWithCompositionsResultMap" type="com.example.pms.domain.model.quality.LotMaster"
1968
+ extends="LotMasterResultMap">
1969
+ <!-- 親ロット構成(このロットを材料として使用した製造ロット) -->
1970
+ <collection property="parentLotRelations" ofType="com.example.pms.domain.model.quality.LotComposition"
1971
+ column="ロット番号" select="com.example.pms.infrastructure.out.persistence.mapper.LotCompositionMapper.findByChildLotNumber"/>
1972
+ <!-- 子ロット構成(このロットが使用した材料ロット) -->
1973
+ <collection property="childLotRelations" ofType="com.example.pms.domain.model.quality.LotComposition"
1974
+ column="ロット番号" select="com.example.pms.infrastructure.out.persistence.mapper.LotCompositionMapper.findByParentLotNumber"/>
1975
+ </resultMap>
1976
+
1977
+ <!-- ロット構成を含めて取得 -->
1978
+ <select id="findByLotNumberWithCompositions" resultMap="LotMasterWithCompositionsResultMap">
1979
+ SELECT * FROM "ロットマスタ"
1980
+ WHERE "ロット番号" = #{lotNumber}
1981
+ </select>
1982
+
1983
+ <!-- トレースフォワード: 子ロットから製造ロットを追跡(PostgreSQL用) -->
1984
+ <select id="traceForward" resultMap="LotMasterResultMap" databaseId="postgresql">
1985
+ WITH RECURSIVE lot_tree AS (
1986
+ SELECT lm.*, 0 AS level
1987
+ FROM "ロットマスタ" lm
1988
+ WHERE lm."ロット番号" = #{lotNumber}
1989
+
1990
+ UNION ALL
1991
+
1992
+ SELECT lm.*, lt.level + 1
1993
+ FROM "ロットマスタ" lm
1994
+ JOIN "ロット構成" lc ON lm."ロット番号" = lc."親ロット番号"
1995
+ JOIN lot_tree lt ON lc."子ロット番号" = lt."ロット番号"
1996
+ WHERE lt.level &lt; 10
1997
+ )
1998
+ SELECT * FROM lot_tree
1999
+ ORDER BY level ASC
2000
+ </select>
2001
+
2002
+ <!-- トレースバック: 親ロットから材料ロットを追跡(PostgreSQL用) -->
2003
+ <select id="traceBack" resultMap="LotMasterResultMap" databaseId="postgresql">
2004
+ WITH RECURSIVE lot_tree AS (
2005
+ SELECT lm.*, 0 AS level
2006
+ FROM "ロットマスタ" lm
2007
+ WHERE lm."ロット番号" = #{lotNumber}
2008
+
2009
+ UNION ALL
2010
+
2011
+ SELECT lm.*, lt.level + 1
2012
+ FROM "ロットマスタ" lm
2013
+ JOIN "ロット構成" lc ON lm."ロット番号" = lc."子ロット番号"
2014
+ JOIN lot_tree lt ON lc."親ロット番号" = lt."ロット番号"
2015
+ WHERE lt.level &lt; 10
2016
+ )
2017
+ SELECT * FROM lot_tree
2018
+ ORDER BY level ASC
2019
+ </select>
2020
+
2021
+ </mapper>
2022
+ ```
2023
+
2024
+ </details>
2025
+
2026
+ #### リレーション設定のポイント
2027
+
2028
+ | 設定項目 | 説明 |
2029
+ |---------|------|
2030
+ | **ネスト select 方式** | `column` と `select` 属性で関連データを別クエリで取得 |
2031
+ | **extends 属性** | 基本 ResultMap を継承してリレーション付き ResultMap を定義 |
2032
+ | **双方向ロット構成** | 親・子両方向のロット構成を別々の collection で取得 |
2033
+ | **databaseId** | PostgreSQL と H2 で異なるクエリを使い分け |
2034
+
2035
+ ### 楽観ロックの実装
2036
+
2037
+ 品質管理では、検査結果の再判定やロット情報の更新時に、データの整合性を保つために楽観ロックを実装します。
2038
+
2039
+ #### Flyway マイグレーション: バージョンカラム追加
2040
+
2041
+ <details>
2042
+ <summary>V029_5__add_quality_version_columns.sql</summary>
2043
+
2044
+ ```sql
2045
+ -- src/main/resources/db/migration/V029_5__add_quality_version_columns.sql
2046
+
2047
+ -- 受入検査データテーブルにバージョンカラムを追加
2048
+ ALTER TABLE "受入検査データ" ADD COLUMN "バージョン" INTEGER DEFAULT 1 NOT NULL;
2049
+
2050
+ -- 工程検査データテーブルにバージョンカラムを追加
2051
+ ALTER TABLE "工程検査データ" ADD COLUMN "バージョン" INTEGER DEFAULT 1 NOT NULL;
2052
+
2053
+ -- 出荷検査データテーブルにバージョンカラムを追加
2054
+ ALTER TABLE "出荷検査データ" ADD COLUMN "バージョン" INTEGER DEFAULT 1 NOT NULL;
2055
+
2056
+ -- ロットマスタテーブルにバージョンカラムを追加
2057
+ ALTER TABLE "ロットマスタ" ADD COLUMN "バージョン" INTEGER DEFAULT 1 NOT NULL;
2058
+
2059
+ -- コメント追加
2060
+ COMMENT ON COLUMN "受入検査データ"."バージョン" IS '楽観ロック用バージョン番号';
2061
+ COMMENT ON COLUMN "工程検査データ"."バージョン" IS '楽観ロック用バージョン番号';
2062
+ COMMENT ON COLUMN "出荷検査データ"."バージョン" IS '楽観ロック用バージョン番号';
2063
+ COMMENT ON COLUMN "ロットマスタ"."バージョン" IS '楽観ロック用バージョン番号';
2064
+ ```
2065
+
2066
+ </details>
2067
+
2068
+ #### エンティティへのバージョンフィールド追加
2069
+
2070
+ <details>
2071
+ <summary>ReceivingInspection.java(バージョンフィールド追加)</summary>
2072
+
2073
+ ```java
2074
+ // src/main/java/com/example/pms/domain/model/quality/ReceivingInspection.java
2075
+ package com.example.pms.domain.model.quality;
2076
+
2077
+ import com.example.pms.domain.model.item.Item;
2078
+ import com.example.pms.domain.model.supplier.Supplier;
2079
+ import lombok.*;
2080
+ import java.math.BigDecimal;
2081
+ import java.time.LocalDate;
2082
+ import java.time.LocalDateTime;
2083
+ import java.util.ArrayList;
2084
+ import java.util.List;
2085
+
2086
+ /**
2087
+ * 受入検査データエンティティ.
2088
+ */
2089
+ @Data
2090
+ @Builder
2091
+ @NoArgsConstructor
2092
+ @AllArgsConstructor
2093
+ public class ReceivingInspection {
2094
+ private Integer id;
2095
+ private String inspectionNumber;
2096
+ private String receivingNumber;
2097
+ private String purchaseOrderNumber;
2098
+ private String itemCode;
2099
+ private String supplierCode;
2100
+ private LocalDate inspectionDate;
2101
+ private String inspectorCode;
2102
+ private BigDecimal inspectionQuantity;
2103
+ private BigDecimal passedQuantity;
2104
+ private BigDecimal failedQuantity;
2105
+ private InspectionJudgment judgment;
2106
+ private String remarks;
2107
+ private LocalDateTime createdAt;
2108
+ private LocalDateTime updatedAt;
2109
+
2110
+ // 楽観ロック用バージョン
2111
+ @Builder.Default
2112
+ private Integer version = 1;
2113
+
2114
+ // リレーション
2115
+ private Item item;
2116
+ private Supplier supplier;
2117
+ @Builder.Default
2118
+ private List<ReceivingInspectionResult> results = new ArrayList<>();
2119
+
2120
+ /**
2121
+ * 再検査可能かどうかをチェック.
2122
+ *
2123
+ * @return 再検査可能な場合 true
2124
+ */
2125
+ public boolean canReinspect() {
2126
+ return judgment == InspectionJudgment.HOLD;
2127
+ }
2128
+
2129
+ /**
2130
+ * 不合格率を計算.
2131
+ *
2132
+ * @return 不合格率(%)
2133
+ */
2134
+ public BigDecimal getFailureRate() {
2135
+ if (inspectionQuantity == null || inspectionQuantity.compareTo(BigDecimal.ZERO) == 0) {
2136
+ return BigDecimal.ZERO;
2137
+ }
2138
+ return failedQuantity.divide(inspectionQuantity, 4, java.math.RoundingMode.HALF_UP)
2139
+ .multiply(new BigDecimal("100"));
2140
+ }
2141
+ }
2142
+ ```
2143
+
2144
+ </details>
2145
+
2146
+ #### MyBatis Mapper: 楽観ロック対応の更新
2147
+
2148
+ 検査データの判定更新や数量修正時に楽観ロックを適用します。
2149
+
2150
+ <details>
2151
+ <summary>ReceivingInspectionMapper.xml(楽観ロック対応 UPDATE)</summary>
2152
+
2153
+ ```xml
2154
+ <!-- 判定更新(楽観ロック対応) -->
2155
+ <update id="updateJudgmentWithOptimisticLock">
2156
+ UPDATE "受入検査データ"
2157
+ SET
2158
+ "合格数" = #{passedQuantity},
2159
+ "不合格数" = #{failedQuantity},
2160
+ "判定" = #{judgment, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler}::"検査判定",
2161
+ "備考" = #{remarks},
2162
+ "更新日時" = CURRENT_TIMESTAMP,
2163
+ "バージョン" = "バージョン" + 1
2164
+ WHERE "受入検査番号" = #{inspectionNumber}
2165
+ AND "バージョン" = #{version}
2166
+ </update>
2167
+
2168
+ <!-- 再検査による判定変更(楽観ロック + 保留チェック) -->
2169
+ <update id="reinspectWithOptimisticLock">
2170
+ UPDATE "受入検査データ"
2171
+ SET
2172
+ "合格数" = #{passedQuantity},
2173
+ "不合格数" = #{failedQuantity},
2174
+ "判定" = #{judgment, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.InspectionJudgmentTypeHandler}::"検査判定",
2175
+ "備考" = #{remarks},
2176
+ "更新日時" = CURRENT_TIMESTAMP,
2177
+ "バージョン" = "バージョン" + 1
2178
+ WHERE "受入検査番号" = #{inspectionNumber}
2179
+ AND "バージョン" = #{version}
2180
+ AND "判定" = '保留'
2181
+ </update>
2182
+
2183
+ <!-- バージョン取得 -->
2184
+ <select id="findVersionByInspectionNumber" resultType="java.lang.Integer">
2185
+ SELECT "バージョン" FROM "受入検査データ"
2186
+ WHERE "受入検査番号" = #{inspectionNumber}
2187
+ </select>
2188
+
2189
+ <!-- 判定状態取得 -->
2190
+ <select id="findJudgmentByInspectionNumber" resultType="java.lang.String">
2191
+ SELECT "判定"::text FROM "受入検査データ"
2192
+ WHERE "受入検査番号" = #{inspectionNumber}
2193
+ </select>
2194
+ ```
2195
+
2196
+ </details>
2197
+
2198
+ #### Repository 実装: 楽観ロック対応
2199
+
2200
+ <details>
2201
+ <summary>ReceivingInspectionRepositoryImpl.java(楽観ロック対応)</summary>
2202
+
2203
+ ```java
2204
+ // src/main/java/com/example/sms/infrastructure/out/persistence/repository/ReceivingInspectionRepositoryImpl.java
2205
+ package com.example.pms.infrastructure.out.persistence.repository;
2206
+
2207
+ import com.example.pms.application.port.out.ReceivingInspectionRepository;
2208
+ import com.example.pms.domain.exception.OptimisticLockException;
2209
+ import com.example.pms.domain.model.quality.InspectionJudgment;
2210
+ import com.example.pms.domain.model.quality.ReceivingInspection;
2211
+ import com.example.pms.infrastructure.out.persistence.mapper.ReceivingInspectionMapper;
2212
+ import lombok.RequiredArgsConstructor;
2213
+ import org.springframework.stereotype.Repository;
2214
+ import org.springframework.transaction.annotation.Transactional;
2215
+
2216
+ import java.math.BigDecimal;
2217
+ import java.util.Optional;
2218
+
2219
+ @Repository
2220
+ @RequiredArgsConstructor
2221
+ public class ReceivingInspectionRepositoryImpl implements ReceivingInspectionRepository {
2222
+
2223
+ private final ReceivingInspectionMapper mapper;
2224
+
2225
+ @Override
2226
+ @Transactional
2227
+ public void updateJudgment(String inspectionNumber, Integer version,
2228
+ BigDecimal passedQuantity, BigDecimal failedQuantity,
2229
+ InspectionJudgment judgment, String remarks) {
2230
+ int updatedCount = mapper.updateJudgmentWithOptimisticLock(
2231
+ inspectionNumber, version, passedQuantity, failedQuantity, judgment, remarks);
2232
+
2233
+ if (updatedCount == 0) {
2234
+ handleOptimisticLockFailure(inspectionNumber, version);
2235
+ }
2236
+ }
2237
+
2238
+ @Override
2239
+ @Transactional
2240
+ public void reinspect(String inspectionNumber, Integer version,
2241
+ BigDecimal passedQuantity, BigDecimal failedQuantity,
2242
+ InspectionJudgment judgment, String remarks) {
2243
+ int updatedCount = mapper.reinspectWithOptimisticLock(
2244
+ inspectionNumber, version, passedQuantity, failedQuantity, judgment, remarks);
2245
+
2246
+ if (updatedCount == 0) {
2247
+ // 再検査失敗の原因を特定
2248
+ Integer currentVersion = mapper.findVersionByInspectionNumber(inspectionNumber);
2249
+ if (currentVersion == null) {
2250
+ throw new IllegalArgumentException("検査データが見つかりません");
2251
+ } else if (!currentVersion.equals(version)) {
2252
+ throw new OptimisticLockException("受入検査", inspectionNumber,
2253
+ version, currentVersion);
2254
+ } else {
2255
+ // バージョンは一致しているので保留状態ではない
2256
+ String currentJudgment = mapper.findJudgmentByInspectionNumber(inspectionNumber);
2257
+ throw new IllegalStateException(
2258
+ String.format("保留状態の検査のみ再検査可能です。現在の判定: %s", currentJudgment));
2259
+ }
2260
+ }
2261
+ }
2262
+
2263
+ private void handleOptimisticLockFailure(String inspectionNumber, Integer expectedVersion) {
2264
+ Integer currentVersion = mapper.findVersionByInspectionNumber(inspectionNumber);
2265
+ if (currentVersion == null) {
2266
+ throw new IllegalArgumentException("検査データが見つかりません");
2267
+ } else {
2268
+ throw new OptimisticLockException("受入検査", inspectionNumber,
2269
+ expectedVersion, currentVersion);
2270
+ }
2271
+ }
2272
+
2273
+ @Override
2274
+ public Optional<ReceivingInspection> findFullByInspectionNumber(String inspectionNumber) {
2275
+ return Optional.ofNullable(mapper.findFullByInspectionNumber(inspectionNumber));
2276
+ }
2277
+ }
2278
+ ```
2279
+
2280
+ </details>
2281
+
2282
+ #### TDD: 楽観ロックのテスト
2283
+
2284
+ <details>
2285
+ <summary>ReceivingInspectionRepositoryOptimisticLockTest.java</summary>
2286
+
2287
+ ```java
2288
+ // src/test/java/com/example/sms/infrastructure/out/persistence/repository/ReceivingInspectionRepositoryOptimisticLockTest.java
2289
+ package com.example.pms.infrastructure.out.persistence.repository;
2290
+
2291
+ import com.example.pms.application.port.out.ReceivingInspectionRepository;
2292
+ import com.example.pms.domain.exception.OptimisticLockException;
2293
+ import com.example.pms.domain.model.quality.InspectionJudgment;
2294
+ import com.example.pms.domain.model.quality.ReceivingInspection;
2295
+ import com.example.pms.testsetup.BaseIntegrationTest;
2296
+ import org.junit.jupiter.api.*;
2297
+ import org.springframework.beans.factory.annotation.Autowired;
2298
+
2299
+ import java.math.BigDecimal;
2300
+
2301
+ import static org.assertj.core.api.Assertions.*;
2302
+
2303
+ @DisplayName("受入検査リポジトリ - 楽観ロック")
2304
+ class ReceivingInspectionRepositoryOptimisticLockTest extends BaseIntegrationTest {
2305
+
2306
+ @Autowired
2307
+ private ReceivingInspectionRepository receivingInspectionRepository;
2308
+
2309
+ @BeforeEach
2310
+ void setUp() {
2311
+ // テストデータのセットアップ
2312
+ }
2313
+
2314
+ @Nested
2315
+ @DisplayName("判定更新の楽観ロック")
2316
+ class JudgmentUpdateOptimisticLocking {
2317
+
2318
+ @Test
2319
+ @DisplayName("同じバージョンで判定を更新できる")
2320
+ void canUpdateJudgmentWithSameVersion() {
2321
+ // Arrange
2322
+ ReceivingInspection inspection = createTestInspection("RI-TEST-001", InspectionJudgment.HOLD);
2323
+ Integer initialVersion = inspection.getVersion();
2324
+
2325
+ // Act
2326
+ receivingInspectionRepository.updateJudgment(
2327
+ inspection.getInspectionNumber(),
2328
+ initialVersion,
2329
+ new BigDecimal("95"),
2330
+ new BigDecimal("5"),
2331
+ InspectionJudgment.PASSED,
2332
+ "再検査により合格");
2333
+
2334
+ // Assert
2335
+ var updated = receivingInspectionRepository
2336
+ .findFullByInspectionNumber("RI-TEST-001").get();
2337
+ assertThat(updated.getJudgment()).isEqualTo(InspectionJudgment.PASSED);
2338
+ assertThat(updated.getPassedQuantity()).isEqualByComparingTo(new BigDecimal("95"));
2339
+ assertThat(updated.getVersion()).isEqualTo(initialVersion + 1);
2340
+ }
2341
+
2342
+ @Test
2343
+ @DisplayName("異なるバージョンで更新すると楽観ロック例外が発生する")
2344
+ void throwsExceptionWhenVersionMismatch() {
2345
+ // Arrange
2346
+ ReceivingInspection inspection = createTestInspection("RI-TEST-002", InspectionJudgment.HOLD);
2347
+ Integer initialVersion = inspection.getVersion();
2348
+
2349
+ // 検査担当者Aが更新(成功)
2350
+ receivingInspectionRepository.updateJudgment(
2351
+ inspection.getInspectionNumber(),
2352
+ initialVersion,
2353
+ new BigDecimal("90"),
2354
+ new BigDecimal("10"),
2355
+ InspectionJudgment.PASSED,
2356
+ "担当者Aによる判定");
2357
+
2358
+ // Act & Assert: 検査担当者Bが古いバージョンで更新(失敗)
2359
+ assertThatThrownBy(() -> receivingInspectionRepository.updateJudgment(
2360
+ inspection.getInspectionNumber(),
2361
+ initialVersion, // 古いバージョン
2362
+ new BigDecimal("80"),
2363
+ new BigDecimal("20"),
2364
+ InspectionJudgment.FAILED,
2365
+ "担当者Bによる判定"))
2366
+ .isInstanceOf(OptimisticLockException.class)
2367
+ .hasMessageContaining("他のユーザーによって更新されています");
2368
+ }
2369
+ }
2370
+
2371
+ @Nested
2372
+ @DisplayName("再検査の楽観ロック")
2373
+ class ReinspectOptimisticLocking {
2374
+
2375
+ @Test
2376
+ @DisplayName("保留状態の検査を再検査できる")
2377
+ void canReinspectHoldInspection() {
2378
+ // Arrange
2379
+ ReceivingInspection inspection = createTestInspection("RI-TEST-003", InspectionJudgment.HOLD);
2380
+
2381
+ // Act
2382
+ receivingInspectionRepository.reinspect(
2383
+ inspection.getInspectionNumber(),
2384
+ inspection.getVersion(),
2385
+ new BigDecimal("100"),
2386
+ BigDecimal.ZERO,
2387
+ InspectionJudgment.PASSED,
2388
+ "再検査により合格");
2389
+
2390
+ // Assert
2391
+ var updated = receivingInspectionRepository
2392
+ .findFullByInspectionNumber("RI-TEST-003").get();
2393
+ assertThat(updated.getJudgment()).isEqualTo(InspectionJudgment.PASSED);
2394
+ }
2395
+
2396
+ @Test
2397
+ @DisplayName("保留状態でない検査は再検査できない")
2398
+ void cannotReinspectNonHoldInspection() {
2399
+ // Arrange: 合格状態の検査を作成
2400
+ ReceivingInspection inspection = createTestInspection("RI-TEST-004", InspectionJudgment.PASSED);
2401
+
2402
+ // Act & Assert
2403
+ assertThatThrownBy(() -> receivingInspectionRepository.reinspect(
2404
+ inspection.getInspectionNumber(),
2405
+ inspection.getVersion(),
2406
+ new BigDecimal("80"),
2407
+ new BigDecimal("20"),
2408
+ InspectionJudgment.FAILED,
2409
+ "再検査"))
2410
+ .isInstanceOf(IllegalStateException.class)
2411
+ .hasMessageContaining("保留状態の検査のみ再検査可能です");
2412
+ }
2413
+ }
2414
+
2415
+ private ReceivingInspection createTestInspection(String inspectionNumber, InspectionJudgment judgment) {
2416
+ // テスト用検査データの作成
2417
+ return ReceivingInspection.builder()
2418
+ .inspectionNumber(inspectionNumber)
2419
+ .receivingNumber("RC-TEST-001")
2420
+ .purchaseOrderNumber("PO-TEST-001")
2421
+ .itemCode("MAT-001")
2422
+ .supplierCode("SUP-001")
2423
+ .inspectionDate(java.time.LocalDate.now())
2424
+ .inspectorCode("INS-001")
2425
+ .inspectionQuantity(new BigDecimal("100"))
2426
+ .passedQuantity(judgment == InspectionJudgment.PASSED ? new BigDecimal("100") : BigDecimal.ZERO)
2427
+ .failedQuantity(judgment == InspectionJudgment.FAILED ? new BigDecimal("100") : BigDecimal.ZERO)
2428
+ .judgment(judgment)
2429
+ .build();
2430
+ }
2431
+ }
2432
+ ```
2433
+
2434
+ </details>
2435
+
2436
+ ### 検査再判定処理のシーケンス図
2437
+
2438
+ 検査の再判定では、同一検査に対する複数担当者の同時更新を楽観ロックで制御します。
2439
+
2440
+ ```plantuml
2441
+ @startuml
2442
+
2443
+ title 検査再判定処理シーケンス(楽観ロック対応)
2444
+
2445
+ actor 検査担当者A
2446
+ actor 検査担当者B
2447
+ participant "InspectionService" as Service
2448
+ participant "ReceivingInspectionRepository" as Repo
2449
+ database "受入検査データ" as InspTable
2450
+
2451
+ == 同時再判定シナリオ(検査番号: RI-001, 判定: 保留) ==
2452
+
2453
+ 検査担当者A -> Service: 再判定(RI-001, 合格)
2454
+ activate Service
2455
+ Service -> Repo: findFullByInspectionNumber(RI-001)
2456
+ Repo -> InspTable: SELECT ... WHERE 受入検査番号 = 'RI-001'
2457
+ InspTable --> Repo: 検査データ(判定=保留, version=1)
2458
+ Repo --> Service: 検査データ(version=1)
2459
+
2460
+ 検査担当者B -> Service: 再判定(RI-001, 不合格)
2461
+ activate Service
2462
+ Service -> Repo: findFullByInspectionNumber(RI-001)
2463
+ Repo -> InspTable: SELECT
2464
+ InspTable --> Repo: 検査データ(判定=保留, version=1)
2465
+ Repo --> Service: 検査データ(version=1)
2466
+
2467
+ note over 検査担当者A,InspTable: 検査担当者Aが先に更新
2468
+
2469
+ Service -> Repo: reinspect(version=1, 判定=合格)
2470
+ Repo -> InspTable: UPDATE SET 判定='合格', バージョン += 1\nWHERE バージョン = 1 AND 判定 = '保留'
2471
+ InspTable --> Repo: 1 row updated
2472
+ Repo --> Service: 成功
2473
+ Service --> 検査担当者A: 再判定完了(合格)
2474
+ deactivate Service
2475
+
2476
+ note over 検査担当者A,InspTable: 検査担当者Bの更新(楽観ロック失敗)
2477
+
2478
+ Service -> Repo: reinspect(version=1, 判定=不合格)
2479
+ Repo -> InspTable: UPDATE SET 判定='不合格', バージョン += 1\nWHERE バージョン = 1 AND 判定 = '保留'
2480
+ InspTable --> Repo: 0 rows updated
2481
+ Repo -> InspTable: SELECT バージョン, 判定
2482
+ InspTable --> Repo: version=2, 判定='合格'
2483
+ Repo --> Service: OptimisticLockException
2484
+ Service --> 検査担当者B: エラー: 他の担当者が更新済み
2485
+ deactivate Service
2486
+
2487
+ note over 検査担当者B: 担当者Bは最新状態を確認
2488
+
2489
+ 検査担当者B -> Service: 検査情報取得(RI-001)
2490
+ activate Service
2491
+ Service -> Repo: findFullByInspectionNumber(RI-001)
2492
+ Repo -> InspTable: SELECT
2493
+ InspTable --> Repo: 検査データ(判定=合格, version=2)
2494
+ Repo --> Service: 検査データ
2495
+ Service --> 検査担当者B: 判定: 合格(担当者Aが更新済み)
2496
+ deactivate Service
2497
+
2498
+ @enduml
2499
+ ```
2500
+
2501
+ ### 品質管理向け楽観ロックのベストプラクティス
2502
+
2503
+ | ポイント | 説明 |
2504
+ |---------|------|
2505
+ | **状態チェック併用** | `AND 判定 = '保留'` で再検査可能状態と楽観ロック失敗を同時に検出 |
2506
+ | **エラー原因の特定** | 更新失敗時はバージョンと状態を確認してエラー種別を判定 |
2507
+ | **監査証跡の考慮** | 検査判定の変更履歴を別テーブルに記録することも検討 |
2508
+ | **ロット追跡の整合性** | ロット構成追加時は親子両方のロットをロックして整合性を保証 |
2509
+ | **再検査ワークフロー** | 保留→合格/不合格の遷移のみ許可し、確定後の変更は別プロセスで管理 |
2510
+ | **品質記録の永続性** | 検査結果は物理削除せず、バージョン管理で履歴を保持 |
2511
+
2512
+ ---
2513
+
2514
+ ## 29.4 まとめ
2515
+
2516
+ 本章では、品質管理の設計について解説しました。
2517
+
2518
+ ### 設計のポイント
2519
+
2520
+ 1. **検査の種類と目的の明確化**
2521
+ - 受入検査:購買品の品質確認
2522
+ - 工程検査:製造中の品質確認
2523
+ - 出荷検査:出荷前の最終品質確認
2524
+
2525
+ 2. **共通の検査判定**
2526
+ - InspectionJudgment Enum で合格/不合格/保留を統一管理
2527
+ - TypeHandler による PostgreSQL ENUM 型との連携
2528
+
2529
+ 3. **検査データ構造の統一**
2530
+ - ヘッダー(検査データ)と明細(検査結果データ)の分離
2531
+ - 欠点マスタによる不良種類の管理
2532
+
2533
+ 4. **トレーサビリティの実現**
2534
+ - ロットマスタによるロット情報の管理
2535
+ - ロット構成テーブルによる親子関係の管理
2536
+ - 再帰クエリによるトレースフォワード/トレースバック
2537
+
2538
+ ### 次章への橋渡し
2539
+
2540
+ 次章では、製造原価管理の設計について解説します。標準原価と実際原価の計算、原価差異分析など、製造業の原価管理に必要なデータベース設計を取り上げます。
2541
+
2542
+ ---
2543
+
2544
+ [← 第28章:在庫管理の設計](chapter28.md) | [第30章:製造原価管理の設計 →](chapter30.md)