@k2works/claude-code-booster 3.2.0 → 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 (67) 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/lib/assets/docs/reference//351/201/213/345/226/266/347/256/241/347/220/206.md +131 -39
  67. package/package.json +1 -1
@@ -0,0 +1,2916 @@
1
+ # 第26章:外注委託管理の設計
2
+
3
+ 本章では、外注委託業務の DB 設計を解説します。自社で製造できない工程を外部の委託先に依頼し、材料を支給して加工してもらう業務フローをデータベースで表現します。
4
+
5
+ ---
6
+
7
+ ## 26.1 外注委託業務の理解
8
+
9
+ 外注委託は、自社の生産能力を補完し、専門的な加工を外部に依頼するための重要な業務です。
10
+
11
+ ### 外注委託とは
12
+
13
+ ```plantuml
14
+ @startuml
15
+
16
+ title 外部委託の例
17
+
18
+ |自社工場|
19
+ start
20
+ :原材料;
21
+ :プレス品;
22
+ |外部委託工場|
23
+ :プレス品;
24
+ :メッキ品;
25
+ |自社工場|
26
+ fork
27
+ :部品;
28
+ fork again
29
+ :メッキ品;
30
+ fork end
31
+ :製品;
32
+ stop
33
+
34
+ @enduml
35
+ ```
36
+
37
+ 外注委託には以下の特徴があります:
38
+
39
+ | 特徴 | 説明 |
40
+ |-----|------|
41
+ | **専門加工の委託** | メッキ、熱処理、塗装など専門設備が必要な工程を委託 |
42
+ | **生産能力の補完** | 繁忙期の生産量増加への対応 |
43
+ | **コスト最適化** | 内製と外注のコスト比較による最適な生産配分 |
44
+
45
+ ### 有償支給と無償支給
46
+
47
+ 外注委託における材料の支給方法には2種類あります。
48
+
49
+ | 支給方式 | 説明 | 会計処理 |
50
+ |---------|------|---------|
51
+ | **有償支給** | 材料を売却扱いで支給 | 売上計上後、加工品を仕入れ |
52
+ | **無償支給** | 材料を無償で貸与 | 在庫のまま、加工賃のみ支払い |
53
+
54
+ ```plantuml
55
+ @startuml
56
+
57
+ title 有償支給と無償支給の違い
58
+
59
+ |#LightBlue|有償支給|
60
+ start
61
+ :材料を外注先に売却;
62
+ note right: 売上計上
63
+ :外注先で加工;
64
+ :加工品を購入;
65
+ note right: 仕入計上
66
+ stop
67
+
68
+ |#LightGreen|無償支給|
69
+ start
70
+ :材料を外注先に貸与;
71
+ note right: 在庫移動のみ
72
+ :外注先で加工;
73
+ :加工品を受入;
74
+ note right: 加工賃のみ計上
75
+ stop
76
+
77
+ @enduml
78
+ ```
79
+
80
+ ### 外注委託の業務フロー
81
+
82
+ ```plantuml
83
+ @startuml
84
+
85
+ title 外注委託の業務フロー
86
+
87
+ |生産管理部|
88
+ start
89
+ :オーダ情報;
90
+ note right: 製造オーダから\n外注オーダを生成
91
+
92
+ |購買部|
93
+ :発注作成;
94
+ :注文書発行;
95
+
96
+ |外注部|
97
+ :支給品準備;
98
+ :支給伝票作成;
99
+ :支給品出荷;
100
+
101
+ |委託先|
102
+ :支給品受領;
103
+ :加工作業;
104
+ :完成品出荷;
105
+
106
+ |資材倉庫|
107
+ :入荷受入;
108
+
109
+ |品質管理部|
110
+ :受入検査;
111
+
112
+ if (合格?) then (yes)
113
+ |購買部|
114
+ :検収処理;
115
+ :消費データ作成;
116
+ note right: 支給品の消費を記録
117
+ else (no)
118
+ |品質管理部|
119
+ :不良処理;
120
+ endif
121
+
122
+ stop
123
+
124
+ @enduml
125
+ ```
126
+
127
+ ---
128
+
129
+ ## 26.2 外注委託の DB 設計
130
+
131
+ ### 外注オーダの構造
132
+
133
+ 外注オーダは、通常の購買発注と同じテーブル構造を使用します。発注データと発注明細データに外注委託用の情報を追加する形で管理します。
134
+
135
+ ```plantuml
136
+ @startuml
137
+
138
+ title 外注委託のデータ構造
139
+
140
+ entity "発注データ" as po {
141
+ * 発注番号 [PK]
142
+ --
143
+ 取引先コード [FK]
144
+ 発注日
145
+ ステータス
146
+ }
147
+
148
+ entity "発注明細データ" as pod {
149
+ * 発注番号 [PK,FK]
150
+ * 発注行番号 [PK]
151
+ --
152
+ 品目コード [FK]
153
+ 発注数量
154
+ 発注単価
155
+ }
156
+
157
+ entity "支給データ" as sup {
158
+ * 支給番号 [PK]
159
+ --
160
+ 発注番号 [FK]
161
+ 発注行番号 [FK]
162
+ 取引先コード [FK]
163
+ 支給日
164
+ 支給区分
165
+ }
166
+
167
+ entity "支給明細データ" as supd {
168
+ * 支給番号 [PK,FK]
169
+ * 支給行番号 [PK]
170
+ --
171
+ 品目コード [FK]
172
+ 支給数
173
+ 支給単価
174
+ 支給金額
175
+ }
176
+
177
+ po ||--o{ pod : contains
178
+ pod ||--o{ sup : 支給
179
+ sup ||--o{ supd : contains
180
+
181
+ @enduml
182
+ ```
183
+
184
+ ### 支給関連のスキーマ設計
185
+
186
+ <details>
187
+ <summary>DDL: 支給関連テーブル</summary>
188
+
189
+ ```sql
190
+ -- V011__create_supply_tables.sql
191
+
192
+ -- 支給区分
193
+ CREATE TYPE 支給区分 AS ENUM ('有償支給', '無償支給');
194
+
195
+ -- 支給データ
196
+ CREATE TABLE "支給データ" (
197
+ "ID" SERIAL PRIMARY KEY,
198
+ "支給番号" VARCHAR(20) UNIQUE NOT NULL,
199
+ "発注番号" VARCHAR(20) NOT NULL,
200
+ "発注行番号" INTEGER NOT NULL,
201
+ "取引先コード" VARCHAR(20) NOT NULL,
202
+ "支給日" DATE NOT NULL,
203
+ "支給担当者コード" VARCHAR(20) NOT NULL,
204
+ "支給区分" 支給区分 DEFAULT '無償支給' NOT NULL,
205
+ "備考" TEXT,
206
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
207
+ "作成者" VARCHAR(50),
208
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
209
+ "更新者" VARCHAR(50),
210
+ CONSTRAINT "fk_支給データ_発注明細"
211
+ FOREIGN KEY ("発注番号", "発注行番号") REFERENCES "発注明細データ"("発注番号", "発注行番号"),
212
+ CONSTRAINT "fk_支給データ_取引先"
213
+ FOREIGN KEY ("取引先コード") REFERENCES "取引先マスタ"("取引先コード")
214
+ );
215
+
216
+ -- 支給明細データ
217
+ CREATE TABLE "支給明細データ" (
218
+ "ID" SERIAL PRIMARY KEY,
219
+ "支給番号" VARCHAR(20) NOT NULL,
220
+ "支給行番号" INTEGER NOT NULL,
221
+ "品目コード" VARCHAR(20) NOT NULL,
222
+ "支給数" DECIMAL(15, 2) NOT NULL,
223
+ "支給単価" DECIMAL(15, 2) NOT NULL,
224
+ "支給金額" DECIMAL(15, 2) NOT NULL,
225
+ "備考" TEXT,
226
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
227
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
228
+ CONSTRAINT "fk_支給明細_支給"
229
+ FOREIGN KEY ("支給番号") REFERENCES "支給データ"("支給番号"),
230
+ CONSTRAINT "fk_支給明細_品目"
231
+ FOREIGN KEY ("品目コード") REFERENCES "品目マスタ"("品目コード"),
232
+ UNIQUE ("支給番号", "支給行番号")
233
+ );
234
+
235
+ -- インデックス
236
+ CREATE INDEX "idx_支給データ_発注番号" ON "支給データ"("発注番号", "発注行番号");
237
+ CREATE INDEX "idx_支給データ_取引先コード" ON "支給データ"("取引先コード");
238
+ CREATE INDEX "idx_支給データ_支給日" ON "支給データ"("支給日");
239
+ CREATE INDEX "idx_支給明細_品目コード" ON "支給明細データ"("品目コード");
240
+ ```
241
+
242
+ </details>
243
+
244
+ ### 発注データとの紐付け
245
+
246
+ 支給データは発注明細データと紐付けられます。1つの発注明細に対して、複数の支給が発生する場合があります(分割支給)。
247
+
248
+ ### 支給金額の計算(有償支給の場合)
249
+
250
+ 有償支給の場合、支給金額は以下の計算式で算出されます:
251
+
252
+ ```
253
+ 支給金額 = 支給数 × 支給単価
254
+ ```
255
+
256
+ 無償支給の場合も金額は計算されますが、会計処理が異なります。
257
+
258
+ ### Java エンティティの定義
259
+
260
+ <details>
261
+ <summary>支給区分 Enum</summary>
262
+
263
+ ```java
264
+ // src/main/java/com/example/pms/domain/model/subcontract/SupplyType.java
265
+ package com.example.pms.domain.model.subcontract;
266
+
267
+ import lombok.Getter;
268
+ import lombok.RequiredArgsConstructor;
269
+
270
+ @Getter
271
+ @RequiredArgsConstructor
272
+ public enum SupplyType {
273
+ PAID("有償支給"),
274
+ FREE("無償支給");
275
+
276
+ private final String displayName;
277
+
278
+ public static SupplyType fromDisplayName(String displayName) {
279
+ for (SupplyType type : values()) {
280
+ if (type.displayName.equals(displayName)) {
281
+ return type;
282
+ }
283
+ }
284
+ throw new IllegalArgumentException("不正な支給区分: " + displayName);
285
+ }
286
+ }
287
+ ```
288
+
289
+ </details>
290
+
291
+ <details>
292
+ <summary>支給データエンティティ</summary>
293
+
294
+ ```java
295
+ // src/main/java/com/example/pms/domain/model/subcontract/Supply.java
296
+ package com.example.pms.domain.model.subcontract;
297
+
298
+ import com.example.pms.domain.model.supplier.Supplier;
299
+ import com.example.pms.domain.model.purchase.PurchaseOrderDetail;
300
+ import lombok.AllArgsConstructor;
301
+ import lombok.Builder;
302
+ import lombok.Data;
303
+ import lombok.NoArgsConstructor;
304
+
305
+ import java.time.LocalDate;
306
+ import java.time.LocalDateTime;
307
+ import java.util.List;
308
+
309
+ @Data
310
+ @Builder
311
+ @NoArgsConstructor
312
+ @AllArgsConstructor
313
+ public class Supply {
314
+ private Integer id;
315
+ private String supplyNumber;
316
+ private String purchaseOrderNumber;
317
+ private Integer lineNumber;
318
+ private String supplierCode;
319
+ private LocalDate supplyDate;
320
+ private String supplierPersonCode;
321
+ private SupplyType supplyType;
322
+ private String remarks;
323
+ private LocalDateTime createdAt;
324
+ private String createdBy;
325
+ private LocalDateTime updatedAt;
326
+ private String updatedBy;
327
+
328
+ // リレーション
329
+ private PurchaseOrderDetail purchaseOrderDetail;
330
+ private Supplier supplier;
331
+ private List<SupplyDetail> details;
332
+ }
333
+ ```
334
+
335
+ </details>
336
+
337
+ <details>
338
+ <summary>支給明細データエンティティ</summary>
339
+
340
+ ```java
341
+ // src/main/java/com/example/pms/domain/model/subcontract/SupplyDetail.java
342
+ package com.example.pms.domain.model.subcontract;
343
+
344
+ import com.example.pms.domain.model.item.Item;
345
+ import lombok.AllArgsConstructor;
346
+ import lombok.Builder;
347
+ import lombok.Data;
348
+ import lombok.NoArgsConstructor;
349
+
350
+ import java.math.BigDecimal;
351
+ import java.time.LocalDateTime;
352
+
353
+ @Data
354
+ @Builder
355
+ @NoArgsConstructor
356
+ @AllArgsConstructor
357
+ public class SupplyDetail {
358
+ private Integer id;
359
+ private String supplyNumber;
360
+ private Integer lineNumber;
361
+ private String itemCode;
362
+ private BigDecimal quantity;
363
+ private BigDecimal unitPrice;
364
+ private BigDecimal amount;
365
+ private String remarks;
366
+ private LocalDateTime createdAt;
367
+ private LocalDateTime updatedAt;
368
+
369
+ // リレーション
370
+ private Supply supply;
371
+ private Item item;
372
+ }
373
+ ```
374
+
375
+ </details>
376
+
377
+ ### TypeHandler の実装
378
+
379
+ <details>
380
+ <summary>SupplyTypeTypeHandler</summary>
381
+
382
+ ```java
383
+ // src/main/java/com/example/pms/infrastructure/persistence/SupplyTypeTypeHandler.java
384
+ package com.example.pms.infrastructure.persistence;
385
+
386
+ import com.example.pms.domain.model.subcontract.SupplyType;
387
+ import org.apache.ibatis.type.BaseTypeHandler;
388
+ import org.apache.ibatis.type.JdbcType;
389
+ import org.apache.ibatis.type.MappedTypes;
390
+
391
+ import java.sql.CallableStatement;
392
+ import java.sql.PreparedStatement;
393
+ import java.sql.ResultSet;
394
+ import java.sql.SQLException;
395
+
396
+ @MappedTypes(SupplyType.class)
397
+ public class SupplyTypeTypeHandler extends BaseTypeHandler<SupplyType> {
398
+
399
+ @Override
400
+ public void setNonNullParameter(PreparedStatement ps, int i, SupplyType parameter, JdbcType jdbcType)
401
+ throws SQLException {
402
+ ps.setString(i, parameter.getDisplayName());
403
+ }
404
+
405
+ @Override
406
+ public SupplyType getNullableResult(ResultSet rs, String columnName) throws SQLException {
407
+ String value = rs.getString(columnName);
408
+ return value == null ? null : SupplyType.fromDisplayName(value);
409
+ }
410
+
411
+ @Override
412
+ public SupplyType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
413
+ String value = rs.getString(columnIndex);
414
+ return value == null ? null : SupplyType.fromDisplayName(value);
415
+ }
416
+
417
+ @Override
418
+ public SupplyType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
419
+ String value = cs.getString(columnIndex);
420
+ return value == null ? null : SupplyType.fromDisplayName(value);
421
+ }
422
+ }
423
+ ```
424
+
425
+ </details>
426
+
427
+ ### MyBatis Mapper XML
428
+
429
+ <details>
430
+ <summary>SupplyMapper.xml</summary>
431
+
432
+ ```xml
433
+ <!-- src/main/resources/mapper/SupplyMapper.xml -->
434
+ <?xml version="1.0" encoding="UTF-8" ?>
435
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
436
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
437
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.SupplyMapper">
438
+
439
+ <resultMap id="SupplyResultMap" type="com.example.pms.domain.model.subcontract.Supply">
440
+ <id property="id" column="ID"/>
441
+ <result property="supplyNumber" column="支給番号"/>
442
+ <result property="purchaseOrderNumber" column="発注番号"/>
443
+ <result property="lineNumber" column="発注行番号"/>
444
+ <result property="supplierCode" column="取引先コード"/>
445
+ <result property="supplyDate" column="支給日"/>
446
+ <result property="supplierPersonCode" column="支給担当者コード"/>
447
+ <result property="supplyType" column="支給区分"
448
+ typeHandler="com.example.pms.infrastructure.out.persistence.typehandler.SupplyTypeTypeHandler"/>
449
+ <result property="remarks" column="備考"/>
450
+ <result property="createdAt" column="作成日時"/>
451
+ <result property="createdBy" column="作成者"/>
452
+ <result property="updatedAt" column="更新日時"/>
453
+ <result property="updatedBy" column="更新者"/>
454
+ </resultMap>
455
+
456
+ <!-- PostgreSQL用 INSERT -->
457
+ <insert id="insert" parameterType="com.example.pms.domain.model.subcontract.Supply"
458
+ useGeneratedKeys="true" keyProperty="id" keyColumn="ID" databaseId="postgresql">
459
+ INSERT INTO "支給データ" (
460
+ "支給番号", "発注番号", "発注行番号", "取引先コード",
461
+ "支給日", "支給担当者コード", "支給区分", "備考", "作成者", "更新者"
462
+ ) VALUES (
463
+ #{supplyNumber},
464
+ #{purchaseOrderNumber},
465
+ #{lineNumber},
466
+ #{supplierCode},
467
+ #{supplyDate},
468
+ #{supplierPersonCode},
469
+ #{supplyType, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.SupplyTypeTypeHandler}::支給区分,
470
+ #{remarks},
471
+ #{createdBy},
472
+ #{updatedBy}
473
+ )
474
+ </insert>
475
+
476
+ <!-- H2用 INSERT -->
477
+ <insert id="insert" parameterType="com.example.pms.domain.model.subcontract.Supply"
478
+ useGeneratedKeys="true" keyProperty="id" keyColumn="ID" databaseId="h2">
479
+ INSERT INTO "支給データ" (
480
+ "支給番号", "発注番号", "発注行番号", "取引先コード",
481
+ "支給日", "支給担当者コード", "支給区分", "備考", "作成者", "更新者"
482
+ ) VALUES (
483
+ #{supplyNumber},
484
+ #{purchaseOrderNumber},
485
+ #{lineNumber},
486
+ #{supplierCode},
487
+ #{supplyDate},
488
+ #{supplierPersonCode},
489
+ #{supplyType, typeHandler=com.example.pms.infrastructure.out.persistence.typehandler.SupplyTypeTypeHandler},
490
+ #{remarks},
491
+ #{createdBy},
492
+ #{updatedBy}
493
+ )
494
+ </insert>
495
+
496
+ <select id="findById" resultMap="SupplyResultMap">
497
+ SELECT * FROM "支給データ" WHERE "ID" = #{id}
498
+ </select>
499
+
500
+ <select id="findBySupplyNumber" resultMap="SupplyResultMap">
501
+ SELECT * FROM "支給データ" WHERE "支給番号" = #{supplyNumber}
502
+ </select>
503
+
504
+ <select id="findByPurchaseOrderNumber" resultMap="SupplyResultMap">
505
+ SELECT * FROM "支給データ" WHERE "発注番号" = #{purchaseOrderNumber} ORDER BY "支給日" DESC
506
+ </select>
507
+
508
+ <select id="findByPurchaseOrderNumberAndLineNumber" resultMap="SupplyResultMap">
509
+ SELECT * FROM "支給データ"
510
+ WHERE "発注番号" = #{purchaseOrderNumber} AND "発注行番号" = #{lineNumber}
511
+ ORDER BY "支給日" DESC
512
+ </select>
513
+
514
+ <select id="findBySupplierCode" resultMap="SupplyResultMap">
515
+ SELECT * FROM "支給データ" WHERE "取引先コード" = #{supplierCode} ORDER BY "支給日" DESC
516
+ </select>
517
+
518
+ <select id="findAll" resultMap="SupplyResultMap">
519
+ SELECT * FROM "支給データ" ORDER BY "支給日" DESC
520
+ </select>
521
+
522
+ <!-- PostgreSQL用 DELETE -->
523
+ <delete id="deleteAll" databaseId="postgresql">
524
+ TRUNCATE TABLE "支給データ" CASCADE
525
+ </delete>
526
+
527
+ <!-- H2用 DELETE -->
528
+ <delete id="deleteAll" databaseId="h2">
529
+ DELETE FROM "支給データ"
530
+ </delete>
531
+ </mapper>
532
+ ```
533
+
534
+ </details>
535
+
536
+ <details>
537
+ <summary>SupplyDetailMapper.xml</summary>
538
+
539
+ ```xml
540
+ <!-- src/main/resources/mapper/SupplyDetailMapper.xml -->
541
+ <?xml version="1.0" encoding="UTF-8" ?>
542
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
543
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
544
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.SupplyDetailMapper">
545
+
546
+ <resultMap id="SupplyDetailResultMap" type="com.example.pms.domain.model.subcontract.SupplyDetail">
547
+ <id property="id" column="ID"/>
548
+ <result property="supplyNumber" column="支給番号"/>
549
+ <result property="lineNumber" column="支給行番号"/>
550
+ <result property="itemCode" column="品目コード"/>
551
+ <result property="quantity" column="支給数"/>
552
+ <result property="unitPrice" column="支給単価"/>
553
+ <result property="amount" column="支給金額"/>
554
+ <result property="remarks" column="備考"/>
555
+ <result property="createdAt" column="作成日時"/>
556
+ <result property="updatedAt" column="更新日時"/>
557
+ </resultMap>
558
+
559
+ <insert id="insert" parameterType="com.example.pms.domain.model.subcontract.SupplyDetail"
560
+ useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
561
+ INSERT INTO "支給明細データ" (
562
+ "支給番号", "支給行番号", "品目コード", "支給数", "支給単価", "支給金額", "備考", "作成者", "更新者"
563
+ ) VALUES (
564
+ #{supplyNumber},
565
+ #{lineNumber},
566
+ #{itemCode},
567
+ #{quantity},
568
+ #{unitPrice},
569
+ #{amount},
570
+ #{remarks},
571
+ #{createdBy},
572
+ #{updatedBy}
573
+ )
574
+ </insert>
575
+
576
+ <select id="findById" resultMap="SupplyDetailResultMap">
577
+ SELECT * FROM "支給明細データ" WHERE "ID" = #{id}
578
+ </select>
579
+
580
+ <select id="findBySupplyNumberAndLineNumber" resultMap="SupplyDetailResultMap">
581
+ SELECT * FROM "支給明細データ"
582
+ WHERE "支給番号" = #{supplyNumber} AND "支給行番号" = #{lineNumber}
583
+ </select>
584
+
585
+ <select id="findBySupplyNumber" resultMap="SupplyDetailResultMap">
586
+ SELECT * FROM "支給明細データ"
587
+ WHERE "支給番号" = #{supplyNumber}
588
+ ORDER BY "支給行番号"
589
+ </select>
590
+
591
+ <select id="findAll" resultMap="SupplyDetailResultMap">
592
+ SELECT * FROM "支給明細データ" ORDER BY "支給番号", "支給行番号"
593
+ </select>
594
+
595
+ <!-- PostgreSQL用 DELETE -->
596
+ <delete id="deleteAll" databaseId="postgresql">
597
+ TRUNCATE TABLE "支給明細データ" CASCADE
598
+ </delete>
599
+
600
+ <!-- H2用 DELETE -->
601
+ <delete id="deleteAll" databaseId="h2">
602
+ DELETE FROM "支給明細データ"
603
+ </delete>
604
+ </mapper>
605
+ ```
606
+
607
+ </details>
608
+
609
+ ### Mapper インターフェース
610
+
611
+ <details>
612
+ <summary>SupplyMapper</summary>
613
+
614
+ ```java
615
+ // src/main/java/com/example/pms/infrastructure/out/persistence/mapper/SupplyMapper.java
616
+ package com.example.pms.infrastructure.out.persistence.mapper;
617
+
618
+ import com.example.pms.domain.model.subcontract.Supply;
619
+ import org.apache.ibatis.annotations.Mapper;
620
+ import org.apache.ibatis.annotations.Param;
621
+
622
+ @Mapper
623
+ public interface SupplyMapper {
624
+ void insert(Supply supply);
625
+ Supply findBySupplyNumber(String supplyNumber);
626
+ Supply findByPurchaseOrderDetail(@Param("purchaseOrderNumber") String purchaseOrderNumber,
627
+ @Param("lineNumber") Integer lineNumber);
628
+ String findLatestSupplyNumber(String prefix);
629
+ void deleteAll();
630
+ }
631
+ ```
632
+
633
+ </details>
634
+
635
+ <details>
636
+ <summary>SupplyDetailMapper</summary>
637
+
638
+ ```java
639
+ // src/main/java/com/example/pms/infrastructure/out/persistence/mapper/SupplyDetailMapper.java
640
+ package com.example.pms.infrastructure.out.persistence.mapper;
641
+
642
+ import com.example.pms.domain.model.subcontract.SupplyDetail;
643
+ import org.apache.ibatis.annotations.Mapper;
644
+
645
+ import java.util.List;
646
+
647
+ @Mapper
648
+ public interface SupplyDetailMapper {
649
+ void insert(SupplyDetail detail);
650
+ List<SupplyDetail> findBySupplyNumber(String supplyNumber);
651
+ void deleteAll();
652
+ }
653
+ ```
654
+
655
+ </details>
656
+
657
+ ### 支給サービスの実装
658
+
659
+ <details>
660
+ <summary>SupplyService</summary>
661
+
662
+ ```java
663
+ // src/main/java/com/example/pms/application/service/SupplyService.java
664
+ package com.example.pms.application.service;
665
+
666
+ import com.example.pms.application.port.in.command.SupplyCreateCommand;
667
+ import com.example.pms.application.port.in.command.SupplyDetailCommand;
668
+ import com.example.pms.domain.model.subcontract.*;
669
+ import com.example.pms.infrastructure.out.persistence.mapper.*;
670
+ import lombok.RequiredArgsConstructor;
671
+ import org.springframework.stereotype.Service;
672
+ import org.springframework.transaction.annotation.Transactional;
673
+
674
+ import java.time.LocalDate;
675
+ import java.time.format.DateTimeFormatter;
676
+ import java.util.ArrayList;
677
+ import java.util.List;
678
+
679
+ @Service
680
+ @RequiredArgsConstructor
681
+ public class SupplyService {
682
+
683
+ private final SupplyMapper supplyMapper;
684
+ private final SupplyDetailMapper supplyDetailMapper;
685
+
686
+ /**
687
+ * 支給番号を生成する
688
+ */
689
+ private String generateSupplyNumber(LocalDate supplyDate) {
690
+ String prefix = "SUP-" + supplyDate.format(DateTimeFormatter.ofPattern("yyyyMM")) + "-";
691
+ String latestNumber = supplyMapper.findLatestSupplyNumber(prefix + "%");
692
+
693
+ int sequence = 1;
694
+ if (latestNumber != null) {
695
+ int currentSequence = Integer.parseInt(latestNumber.substring(latestNumber.length() - 4));
696
+ sequence = currentSequence + 1;
697
+ }
698
+
699
+ return prefix + String.format("%04d", sequence);
700
+ }
701
+
702
+ /**
703
+ * 支給データを作成する
704
+ */
705
+ @Transactional
706
+ public Supply createSupply(SupplyCreateCommand command) {
707
+ String supplyNumber = generateSupplyNumber(command.getSupplyDate());
708
+ SupplyType supplyType = command.getSupplyType() != null ? command.getSupplyType() : SupplyType.FREE;
709
+
710
+ // 支給ヘッダを作成
711
+ Supply supply = Supply.builder()
712
+ .supplyNumber(supplyNumber)
713
+ .purchaseOrderNumber(command.getPurchaseOrderNumber())
714
+ .lineNumber(command.getLineNumber())
715
+ .supplierCode(command.getSupplierCode())
716
+ .supplyDate(command.getSupplyDate())
717
+ .supplierPersonCode(command.getSupplierPersonCode())
718
+ .supplyType(supplyType)
719
+ .remarks(command.getRemarks())
720
+ .build();
721
+ supplyMapper.insert(supply);
722
+
723
+ // 支給明細を作成
724
+ List<SupplyDetail> details = new ArrayList<>();
725
+ int detailLineNumber = 0;
726
+
727
+ for (SupplyDetailCommand detailCommand : command.getDetails()) {
728
+ detailLineNumber++;
729
+
730
+ SupplyDetail detail = SupplyDetail.builder()
731
+ .supplyNumber(supplyNumber)
732
+ .lineNumber(detailLineNumber)
733
+ .itemCode(detailCommand.getItemCode())
734
+ .quantity(detailCommand.getQuantity())
735
+ .unitPrice(detailCommand.getUnitPrice())
736
+ .amount(detailCommand.getQuantity().multiply(detailCommand.getUnitPrice()))
737
+ .remarks(detailCommand.getRemarks())
738
+ .build();
739
+ supplyDetailMapper.insert(detail);
740
+
741
+ details.add(detail);
742
+ }
743
+
744
+ supply.setDetails(details);
745
+ return supply;
746
+ }
747
+
748
+ /**
749
+ * 支給番号で検索する
750
+ */
751
+ public Supply findBySupplyNumber(String supplyNumber) {
752
+ Supply supply = supplyMapper.findBySupplyNumber(supplyNumber);
753
+ if (supply != null) {
754
+ supply.setDetails(supplyDetailMapper.findBySupplyNumber(supplyNumber));
755
+ }
756
+ return supply;
757
+ }
758
+ }
759
+ ```
760
+
761
+ </details>
762
+
763
+ ### コマンドクラス
764
+
765
+ <details>
766
+ <summary>SupplyCreateCommand</summary>
767
+
768
+ ```java
769
+ // src/main/java/com/example/pms/application/port/in/command/SupplyCreateCommand.java
770
+ package com.example.pms.application.port.in.command;
771
+
772
+ import com.example.pms.domain.model.subcontract.SupplyType;
773
+ import lombok.Builder;
774
+ import lombok.Data;
775
+
776
+ import java.time.LocalDate;
777
+ import java.util.List;
778
+
779
+ @Data
780
+ @Builder
781
+ public class SupplyCreateCommand {
782
+ private String purchaseOrderNumber;
783
+ private Integer lineNumber;
784
+ private String supplierCode;
785
+ private LocalDate supplyDate;
786
+ private String supplierPersonCode;
787
+ private SupplyType supplyType;
788
+ private String remarks;
789
+ private List<SupplyDetailCommand> details;
790
+ }
791
+ ```
792
+
793
+ </details>
794
+
795
+ <details>
796
+ <summary>SupplyDetailCommand</summary>
797
+
798
+ ```java
799
+ // src/main/java/com/example/pms/application/port/in/command/SupplyDetailCommand.java
800
+ package com.example.pms.application.port.in.command;
801
+
802
+ import lombok.Builder;
803
+ import lombok.Data;
804
+
805
+ import java.math.BigDecimal;
806
+
807
+ @Data
808
+ @Builder
809
+ public class SupplyDetailCommand {
810
+ private String itemCode;
811
+ private BigDecimal quantity;
812
+ private BigDecimal unitPrice;
813
+ private String remarks;
814
+ }
815
+ ```
816
+
817
+ </details>
818
+
819
+ ### TDD: 支給データの登録テスト
820
+
821
+ <details>
822
+ <summary>SupplyServiceTest</summary>
823
+
824
+ ```java
825
+ // src/test/java/com/example/pms/application/service/SupplyServiceTest.java
826
+ package com.example.pms.application.service;
827
+
828
+ import com.example.pms.domain.model.item.Item;
829
+ import com.example.pms.domain.model.item.ItemCategory;
830
+ import com.example.pms.domain.model.supplier.Supplier;
831
+ import com.example.pms.domain.model.purchase.*;
832
+ import com.example.pms.domain.model.subcontract.*;
833
+ import com.example.pms.infrastructure.out.persistence.mapper.*;
834
+ import org.junit.jupiter.api.*;
835
+ import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
836
+ import org.springframework.beans.factory.annotation.Autowired;
837
+ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
838
+ import org.springframework.context.annotation.Import;
839
+ import org.springframework.test.context.DynamicPropertyRegistry;
840
+ import org.springframework.test.context.DynamicPropertySource;
841
+ import org.testcontainers.containers.PostgreSQLContainer;
842
+ import org.testcontainers.junit.jupiter.Container;
843
+ import org.testcontainers.junit.jupiter.Testcontainers;
844
+
845
+ import java.math.BigDecimal;
846
+ import java.time.LocalDate;
847
+ import java.util.List;
848
+
849
+ import static org.assertj.core.api.Assertions.*;
850
+
851
+ @MybatisTest
852
+ @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
853
+ @Import({SupplyService.class, PurchaseOrderService.class})
854
+ @Testcontainers
855
+ @DisplayName("支給業務")
856
+ class SupplyServiceTest {
857
+
858
+ @Container
859
+ static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15")
860
+ .withDatabaseName("testdb")
861
+ .withUsername("testuser")
862
+ .withPassword("testpass");
863
+
864
+ @DynamicPropertySource
865
+ static void configureProperties(DynamicPropertyRegistry registry) {
866
+ registry.add("spring.datasource.url", postgres::getJdbcUrl);
867
+ registry.add("spring.datasource.username", postgres::getUsername);
868
+ registry.add("spring.datasource.password", postgres::getPassword);
869
+ }
870
+
871
+ @Autowired
872
+ private SupplyService supplyService;
873
+
874
+ @Autowired
875
+ private PurchaseOrderService purchaseOrderService;
876
+
877
+ @Autowired
878
+ private ItemMapper itemMapper;
879
+
880
+ @Autowired
881
+ private SupplierMapper supplierMapper;
882
+
883
+ @Autowired
884
+ private UnitPriceMapper unitPriceMapper;
885
+
886
+ @Autowired
887
+ private SupplyMapper supplyMapper;
888
+
889
+ @Autowired
890
+ private SupplyDetailMapper supplyDetailMapper;
891
+
892
+ @Autowired
893
+ private PurchaseOrderMapper purchaseOrderMapper;
894
+
895
+ @Autowired
896
+ private PurchaseOrderDetailMapper purchaseOrderDetailMapper;
897
+
898
+ private PurchaseOrder testPurchaseOrder;
899
+
900
+ @BeforeEach
901
+ void setUp() {
902
+ supplyDetailMapper.deleteAll();
903
+ supplyMapper.deleteAll();
904
+ purchaseOrderDetailMapper.deleteAll();
905
+ purchaseOrderMapper.deleteAll();
906
+ unitPriceMapper.deleteAll();
907
+ supplierMapper.deleteAll();
908
+ itemMapper.deleteAll();
909
+
910
+ // マスタデータの準備
911
+ Supplier supplier = Supplier.builder()
912
+ .supplierCode("SUB-001")
913
+ .effectiveFrom(LocalDate.of(2025, 1, 1))
914
+ .supplierName("株式会社メッキ工業")
915
+ .build();
916
+ supplierMapper.insert(supplier);
917
+
918
+ Item parentItem = Item.builder()
919
+ .itemCode("PLATED-001")
920
+ .effectiveFrom(LocalDate.of(2025, 1, 1))
921
+ .itemName("メッキ加工品")
922
+ .itemCategory(ItemCategory.SEMI_PRODUCT)
923
+ .build();
924
+ itemMapper.insert(parentItem);
925
+
926
+ Item supplyItem = Item.builder()
927
+ .itemCode("PRESS-001")
928
+ .effectiveFrom(LocalDate.of(2025, 1, 1))
929
+ .itemName("プレス部品")
930
+ .itemCategory(ItemCategory.PART)
931
+ .build();
932
+ itemMapper.insert(supplyItem);
933
+
934
+ unitPriceMapper.insert(UnitPrice.builder()
935
+ .itemCode("PLATED-001")
936
+ .supplierCode("SUB-001")
937
+ .effectiveFrom(LocalDate.of(2025, 1, 1))
938
+ .unitPrice(new BigDecimal("500"))
939
+ .build());
940
+
941
+ // テスト用発注を作成
942
+ PurchaseOrderCreateInput poInput = PurchaseOrderCreateInput.builder()
943
+ .supplierCode("SUB-001")
944
+ .orderDate(LocalDate.of(2025, 1, 15))
945
+ .details(List.of(
946
+ PurchaseOrderDetailInput.builder()
947
+ .itemCode("PLATED-001")
948
+ .orderQuantity(new BigDecimal("100"))
949
+ .expectedReceivingDate(LocalDate.of(2025, 1, 25))
950
+ .build()
951
+ ))
952
+ .build();
953
+ testPurchaseOrder = purchaseOrderService.createPurchaseOrder(poInput);
954
+ purchaseOrderService.confirmPurchaseOrder(testPurchaseOrder.getPurchaseOrderNumber());
955
+ }
956
+
957
+ @Nested
958
+ @DisplayName("支給データ作成")
959
+ class SupplyCreation {
960
+
961
+ @Test
962
+ @DisplayName("発注に紐づく支給データを作成できる")
963
+ void canCreateSupplyFromPurchaseOrder() {
964
+ // Act
965
+ SupplyCreateCommand input = SupplyCreateCommand.builder()
966
+ .purchaseOrderNumber(testPurchaseOrder.getPurchaseOrderNumber())
967
+ .lineNumber(1)
968
+ .supplierCode("SUB-001")
969
+ .supplyDate(LocalDate.of(2025, 1, 16))
970
+ .supplierPersonCode("EMP001")
971
+ .supplyType(SupplyType.FREE)
972
+ .details(List.of(
973
+ SupplyDetailCommand.builder()
974
+ .itemCode("PRESS-001")
975
+ .quantity(new BigDecimal("100"))
976
+ .unitPrice(new BigDecimal("200"))
977
+ .build()
978
+ ))
979
+ .build();
980
+
981
+ Supply supply = supplyService.createSupply(input);
982
+
983
+ // Assert
984
+ assertThat(supply).isNotNull();
985
+ assertThat(supply.getSupplyNumber()).startsWith("SUP-");
986
+ assertThat(supply.getSupplierCode()).isEqualTo("SUB-001");
987
+ assertThat(supply.getSupplyType()).isEqualTo(SupplyType.FREE);
988
+ assertThat(supply.getDetails()).hasSize(1);
989
+ assertThat(supply.getDetails().get(0).getQuantity())
990
+ .isEqualByComparingTo(new BigDecimal("100"));
991
+ }
992
+
993
+ @Test
994
+ @DisplayName("有償支給を作成できる")
995
+ void canCreatePaidSupply() {
996
+ // Act
997
+ SupplyCreateCommand input = SupplyCreateCommand.builder()
998
+ .purchaseOrderNumber(testPurchaseOrder.getPurchaseOrderNumber())
999
+ .lineNumber(1)
1000
+ .supplierCode("SUB-001")
1001
+ .supplyDate(LocalDate.of(2025, 1, 16))
1002
+ .supplierPersonCode("EMP001")
1003
+ .supplyType(SupplyType.PAID)
1004
+ .details(List.of(
1005
+ SupplyDetailCommand.builder()
1006
+ .itemCode("PRESS-001")
1007
+ .quantity(new BigDecimal("100"))
1008
+ .unitPrice(new BigDecimal("200"))
1009
+ .build()
1010
+ ))
1011
+ .build();
1012
+
1013
+ Supply supply = supplyService.createSupply(input);
1014
+
1015
+ // Assert
1016
+ assertThat(supply.getSupplyType()).isEqualTo(SupplyType.PAID);
1017
+ assertThat(supply.getDetails().get(0).getAmount())
1018
+ .isEqualByComparingTo(new BigDecimal("20000"));
1019
+ }
1020
+ }
1021
+ }
1022
+ ```
1023
+
1024
+ </details>
1025
+
1026
+ ---
1027
+
1028
+ ## 26.3 消費業務の設計
1029
+
1030
+ 外注先が支給品を加工して納品した際、支給した材料がどれだけ消費されたかを記録します。
1031
+
1032
+ ### 消費関連のスキーマ設計
1033
+
1034
+ ```plantuml
1035
+ @startuml
1036
+
1037
+ title 消費データの関連
1038
+
1039
+ entity "入荷受入データ" as rcv {
1040
+ * 入荷番号 [PK]
1041
+ --
1042
+ 発注番号 [FK]
1043
+ 発注行番号 [FK]
1044
+ 入荷日
1045
+ 入荷数量
1046
+ }
1047
+
1048
+ entity "消費データ" as con {
1049
+ * 消費番号 [PK]
1050
+ --
1051
+ 入荷番号 [FK]
1052
+ 消費日
1053
+ 取引先コード [FK]
1054
+ }
1055
+
1056
+ entity "消費明細データ" as cond {
1057
+ * 消費番号 [PK,FK]
1058
+ * 消費行番号 [PK]
1059
+ --
1060
+ 品目コード [FK]
1061
+ 消費数量
1062
+ }
1063
+
1064
+ rcv ||--o{ con : 消費記録
1065
+ con ||--o{ cond : contains
1066
+
1067
+ @enduml
1068
+ ```
1069
+
1070
+ <details>
1071
+ <summary>DDL: 消費関連テーブル</summary>
1072
+
1073
+ ```sql
1074
+ -- V012__create_consumption_tables.sql
1075
+
1076
+ -- 消費データ
1077
+ CREATE TABLE "消費データ" (
1078
+ "ID" SERIAL PRIMARY KEY,
1079
+ "消費番号" VARCHAR(20) UNIQUE NOT NULL,
1080
+ "入荷番号" VARCHAR(20) NOT NULL,
1081
+ "消費日" DATE NOT NULL,
1082
+ "取引先コード" VARCHAR(20) NOT NULL,
1083
+ "備考" TEXT,
1084
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1085
+ "作成者" VARCHAR(50),
1086
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1087
+ "更新者" VARCHAR(50),
1088
+ CONSTRAINT "fk_消費データ_入荷"
1089
+ FOREIGN KEY ("入荷番号") REFERENCES "入荷受入データ"("入荷番号"),
1090
+ CONSTRAINT "fk_消費データ_取引先"
1091
+ FOREIGN KEY ("取引先コード") REFERENCES "取引先マスタ"("取引先コード")
1092
+ );
1093
+
1094
+ -- 消費明細データ
1095
+ CREATE TABLE "消費明細データ" (
1096
+ "ID" SERIAL PRIMARY KEY,
1097
+ "消費番号" VARCHAR(20) NOT NULL,
1098
+ "消費行番号" INTEGER NOT NULL,
1099
+ "品目コード" VARCHAR(20) NOT NULL,
1100
+ "消費数量" DECIMAL(15, 2) NOT NULL,
1101
+ "備考" TEXT,
1102
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1103
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1104
+ CONSTRAINT "fk_消費明細_消費"
1105
+ FOREIGN KEY ("消費番号") REFERENCES "消費データ"("消費番号"),
1106
+ CONSTRAINT "fk_消費明細_品目"
1107
+ FOREIGN KEY ("品目コード") REFERENCES "品目マスタ"("品目コード"),
1108
+ UNIQUE ("消費番号", "消費行番号")
1109
+ );
1110
+
1111
+ -- インデックス
1112
+ CREATE INDEX "idx_消費データ_入荷番号" ON "消費データ"("入荷番号");
1113
+ CREATE INDEX "idx_消費データ_取引先コード" ON "消費データ"("取引先コード");
1114
+ CREATE INDEX "idx_消費データ_消費日" ON "消費データ"("消費日");
1115
+ CREATE INDEX "idx_消費明細_品目コード" ON "消費明細データ"("品目コード");
1116
+ ```
1117
+
1118
+ </details>
1119
+
1120
+ ### 入荷受入データとの紐付け
1121
+
1122
+ 消費データは入荷受入データと紐付けられます。外注先から加工品が入荷した時点で、支給した材料の消費量を記録します。
1123
+
1124
+ ### 消費率の計算
1125
+
1126
+ 消費率(歩留まり率)は以下の計算式で算出されます:
1127
+
1128
+ ```
1129
+ 消費率 = 消費数量 ÷ 支給数量
1130
+ ```
1131
+
1132
+ この値は品質管理や原価計算において重要な指標となります。
1133
+
1134
+ ### Java エンティティの定義
1135
+
1136
+ <details>
1137
+ <summary>消費データエンティティ</summary>
1138
+
1139
+ ```java
1140
+ // src/main/java/com/example/pms/domain/model/subcontract/Consumption.java
1141
+ package com.example.pms.domain.model.subcontract;
1142
+
1143
+ import com.example.pms.domain.model.supplier.Supplier;
1144
+ import com.example.pms.domain.model.purchase.Receiving;
1145
+ import lombok.AllArgsConstructor;
1146
+ import lombok.Builder;
1147
+ import lombok.Data;
1148
+ import lombok.NoArgsConstructor;
1149
+
1150
+ import java.time.LocalDate;
1151
+ import java.time.LocalDateTime;
1152
+ import java.util.List;
1153
+
1154
+ @Data
1155
+ @Builder
1156
+ @NoArgsConstructor
1157
+ @AllArgsConstructor
1158
+ public class Consumption {
1159
+ private Integer id;
1160
+ private String consumptionNumber;
1161
+ private String receivingNumber;
1162
+ private LocalDate consumptionDate;
1163
+ private String supplierCode;
1164
+ private String remarks;
1165
+ private LocalDateTime createdAt;
1166
+ private String createdBy;
1167
+ private LocalDateTime updatedAt;
1168
+ private String updatedBy;
1169
+
1170
+ // リレーション
1171
+ private Receiving receiving;
1172
+ private Supplier supplier;
1173
+ private List<ConsumptionDetail> details;
1174
+ }
1175
+ ```
1176
+
1177
+ </details>
1178
+
1179
+ <details>
1180
+ <summary>消費明細データエンティティ</summary>
1181
+
1182
+ ```java
1183
+ // src/main/java/com/example/pms/domain/model/subcontract/ConsumptionDetail.java
1184
+ package com.example.pms.domain.model.subcontract;
1185
+
1186
+ import com.example.pms.domain.model.item.Item;
1187
+ import lombok.AllArgsConstructor;
1188
+ import lombok.Builder;
1189
+ import lombok.Data;
1190
+ import lombok.NoArgsConstructor;
1191
+
1192
+ import java.math.BigDecimal;
1193
+ import java.time.LocalDateTime;
1194
+
1195
+ @Data
1196
+ @Builder
1197
+ @NoArgsConstructor
1198
+ @AllArgsConstructor
1199
+ public class ConsumptionDetail {
1200
+ private Integer id;
1201
+ private String consumptionNumber;
1202
+ private Integer lineNumber;
1203
+ private String itemCode;
1204
+ private BigDecimal quantity;
1205
+ private String remarks;
1206
+ private LocalDateTime createdAt;
1207
+ private LocalDateTime updatedAt;
1208
+
1209
+ // リレーション
1210
+ private Consumption consumption;
1211
+ private Item item;
1212
+ }
1213
+ ```
1214
+
1215
+ </details>
1216
+
1217
+ ### MyBatis Mapper XML
1218
+
1219
+ <details>
1220
+ <summary>ConsumptionMapper.xml</summary>
1221
+
1222
+ ```xml
1223
+ <!-- src/main/resources/mapper/ConsumptionMapper.xml -->
1224
+ <?xml version="1.0" encoding="UTF-8" ?>
1225
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
1226
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
1227
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.ConsumptionMapper">
1228
+
1229
+ <resultMap id="ConsumptionResultMap" type="com.example.pms.domain.model.subcontract.Consumption">
1230
+ <id property="id" column="ID"/>
1231
+ <result property="consumptionNumber" column="消費番号"/>
1232
+ <result property="receivingNumber" column="入荷番号"/>
1233
+ <result property="consumptionDate" column="消費日"/>
1234
+ <result property="supplierCode" column="取引先コード"/>
1235
+ <result property="remarks" column="備考"/>
1236
+ <result property="createdAt" column="作成日時"/>
1237
+ <result property="createdBy" column="作成者"/>
1238
+ <result property="updatedAt" column="更新日時"/>
1239
+ <result property="updatedBy" column="更新者"/>
1240
+ </resultMap>
1241
+
1242
+ <insert id="insert" parameterType="com.example.pms.domain.model.subcontract.Consumption"
1243
+ useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
1244
+ INSERT INTO "消費データ" (
1245
+ "消費番号", "入荷番号", "消費日", "取引先コード", "備考", "作成者", "更新者"
1246
+ ) VALUES (
1247
+ #{consumptionNumber},
1248
+ #{receivingNumber},
1249
+ #{consumptionDate},
1250
+ #{supplierCode},
1251
+ #{remarks},
1252
+ #{createdBy},
1253
+ #{updatedBy}
1254
+ )
1255
+ </insert>
1256
+
1257
+ <select id="findById" resultMap="ConsumptionResultMap">
1258
+ SELECT * FROM "消費データ" WHERE "ID" = #{id}
1259
+ </select>
1260
+
1261
+ <select id="findByConsumptionNumber" resultMap="ConsumptionResultMap">
1262
+ SELECT * FROM "消費データ" WHERE "消費番号" = #{consumptionNumber}
1263
+ </select>
1264
+
1265
+ <select id="findByReceivingNumber" resultMap="ConsumptionResultMap">
1266
+ SELECT * FROM "消費データ" WHERE "入荷番号" = #{receivingNumber} ORDER BY "消費日" DESC
1267
+ </select>
1268
+
1269
+ <select id="findBySupplierCode" resultMap="ConsumptionResultMap">
1270
+ SELECT * FROM "消費データ" WHERE "取引先コード" = #{supplierCode} ORDER BY "消費日" DESC
1271
+ </select>
1272
+
1273
+ <select id="findAll" resultMap="ConsumptionResultMap">
1274
+ SELECT * FROM "消費データ" ORDER BY "消費日" DESC
1275
+ </select>
1276
+
1277
+ <!-- PostgreSQL用 DELETE -->
1278
+ <delete id="deleteAll" databaseId="postgresql">
1279
+ TRUNCATE TABLE "消費データ" CASCADE
1280
+ </delete>
1281
+
1282
+ <!-- H2用 DELETE -->
1283
+ <delete id="deleteAll" databaseId="h2">
1284
+ DELETE FROM "消費データ"
1285
+ </delete>
1286
+ </mapper>
1287
+ ```
1288
+
1289
+ </details>
1290
+
1291
+ <details>
1292
+ <summary>ConsumptionDetailMapper.xml</summary>
1293
+
1294
+ ```xml
1295
+ <!-- src/main/resources/mapper/ConsumptionDetailMapper.xml -->
1296
+ <?xml version="1.0" encoding="UTF-8" ?>
1297
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
1298
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
1299
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.ConsumptionDetailMapper">
1300
+
1301
+ <resultMap id="ConsumptionDetailResultMap" type="com.example.pms.domain.model.subcontract.ConsumptionDetail">
1302
+ <id property="id" column="ID"/>
1303
+ <result property="consumptionNumber" column="消費番号"/>
1304
+ <result property="lineNumber" column="消費行番号"/>
1305
+ <result property="itemCode" column="品目コード"/>
1306
+ <result property="quantity" column="消費数量"/>
1307
+ <result property="remarks" column="備考"/>
1308
+ <result property="createdAt" column="作成日時"/>
1309
+ <result property="updatedAt" column="更新日時"/>
1310
+ </resultMap>
1311
+
1312
+ <insert id="insert" parameterType="com.example.pms.domain.model.subcontract.ConsumptionDetail"
1313
+ useGeneratedKeys="true" keyProperty="id" keyColumn="ID">
1314
+ INSERT INTO "消費明細データ" (
1315
+ "消費番号", "消費行番号", "品目コード", "消費数量", "備考", "作成者", "更新者"
1316
+ ) VALUES (
1317
+ #{consumptionNumber},
1318
+ #{lineNumber},
1319
+ #{itemCode},
1320
+ #{quantity},
1321
+ #{remarks},
1322
+ #{createdBy},
1323
+ #{updatedBy}
1324
+ )
1325
+ </insert>
1326
+
1327
+ <select id="findById" resultMap="ConsumptionDetailResultMap">
1328
+ SELECT * FROM "消費明細データ" WHERE "ID" = #{id}
1329
+ </select>
1330
+
1331
+ <select id="findByConsumptionNumberAndLineNumber" resultMap="ConsumptionDetailResultMap">
1332
+ SELECT * FROM "消費明細データ"
1333
+ WHERE "消費番号" = #{consumptionNumber} AND "消費行番号" = #{lineNumber}
1334
+ </select>
1335
+
1336
+ <select id="findByConsumptionNumber" resultMap="ConsumptionDetailResultMap">
1337
+ SELECT * FROM "消費明細データ"
1338
+ WHERE "消費番号" = #{consumptionNumber}
1339
+ ORDER BY "消費行番号"
1340
+ </select>
1341
+
1342
+ <select id="findAll" resultMap="ConsumptionDetailResultMap">
1343
+ SELECT * FROM "消費明細データ" ORDER BY "消費番号", "消費行番号"
1344
+ </select>
1345
+
1346
+ <!-- PostgreSQL用 DELETE -->
1347
+ <delete id="deleteAll" databaseId="postgresql">
1348
+ TRUNCATE TABLE "消費明細データ" CASCADE
1349
+ </delete>
1350
+
1351
+ <!-- H2用 DELETE -->
1352
+ <delete id="deleteAll" databaseId="h2">
1353
+ DELETE FROM "消費明細データ"
1354
+ </delete>
1355
+ </mapper>
1356
+ ```
1357
+
1358
+ </details>
1359
+
1360
+ ### Mapper インターフェース
1361
+
1362
+ <details>
1363
+ <summary>ConsumptionMapper</summary>
1364
+
1365
+ ```java
1366
+ // src/main/java/com/example/pms/infrastructure/out/persistence/mapper/ConsumptionMapper.java
1367
+ package com.example.pms.infrastructure.out.persistence.mapper;
1368
+
1369
+ import com.example.pms.domain.model.subcontract.Consumption;
1370
+ import org.apache.ibatis.annotations.Mapper;
1371
+
1372
+ @Mapper
1373
+ public interface ConsumptionMapper {
1374
+ void insert(Consumption consumption);
1375
+ Consumption findByConsumptionNumber(String consumptionNumber);
1376
+ Consumption findByReceivingNumber(String receivingNumber);
1377
+ String findLatestConsumptionNumber(String prefix);
1378
+ void deleteAll();
1379
+ }
1380
+ ```
1381
+
1382
+ </details>
1383
+
1384
+ <details>
1385
+ <summary>ConsumptionDetailMapper</summary>
1386
+
1387
+ ```java
1388
+ // src/main/java/com/example/pms/infrastructure/out/persistence/mapper/ConsumptionDetailMapper.java
1389
+ package com.example.pms.infrastructure.out.persistence.mapper;
1390
+
1391
+ import com.example.pms.domain.model.subcontract.ConsumptionDetail;
1392
+ import org.apache.ibatis.annotations.Mapper;
1393
+ import org.apache.ibatis.annotations.Param;
1394
+
1395
+ import java.math.BigDecimal;
1396
+ import java.util.List;
1397
+
1398
+ @Mapper
1399
+ public interface ConsumptionDetailMapper {
1400
+ void insert(ConsumptionDetail detail);
1401
+ List<ConsumptionDetail> findByConsumptionNumber(String consumptionNumber);
1402
+ BigDecimal sumByPurchaseOrderAndItem(@Param("purchaseOrderNumber") String purchaseOrderNumber,
1403
+ @Param("lineNumber") Integer lineNumber,
1404
+ @Param("itemCode") String itemCode);
1405
+ void deleteAll();
1406
+ }
1407
+ ```
1408
+
1409
+ </details>
1410
+
1411
+ ### 消費サービスの実装
1412
+
1413
+ <details>
1414
+ <summary>ConsumptionService</summary>
1415
+
1416
+ ```java
1417
+ // src/main/java/com/example/pms/application/service/ConsumptionService.java
1418
+ package com.example.pms.application.service;
1419
+
1420
+ import com.example.pms.application.port.in.command.ConsumptionCreateCommand;
1421
+ import com.example.pms.application.port.in.command.ConsumptionDetailCommand;
1422
+ import com.example.pms.domain.model.purchase.Receiving;
1423
+ import com.example.pms.domain.model.subcontract.*;
1424
+ import com.example.pms.infrastructure.out.persistence.mapper.*;
1425
+ import lombok.RequiredArgsConstructor;
1426
+ import org.springframework.stereotype.Service;
1427
+ import org.springframework.transaction.annotation.Transactional;
1428
+
1429
+ import java.math.BigDecimal;
1430
+ import java.math.RoundingMode;
1431
+ import java.time.LocalDate;
1432
+ import java.time.format.DateTimeFormatter;
1433
+ import java.util.ArrayList;
1434
+ import java.util.List;
1435
+
1436
+ @Service
1437
+ @RequiredArgsConstructor
1438
+ public class ConsumptionService {
1439
+
1440
+ private final ConsumptionMapper consumptionMapper;
1441
+ private final ConsumptionDetailMapper consumptionDetailMapper;
1442
+ private final ReceivingMapper receivingMapper;
1443
+ private final SupplyMapper supplyMapper;
1444
+ private final SupplyDetailMapper supplyDetailMapper;
1445
+
1446
+ /**
1447
+ * 消費番号を生成する
1448
+ */
1449
+ private String generateConsumptionNumber(LocalDate consumptionDate) {
1450
+ String prefix = "CON-" + consumptionDate.format(DateTimeFormatter.ofPattern("yyyyMM")) + "-";
1451
+ String latestNumber = consumptionMapper.findLatestConsumptionNumber(prefix + "%");
1452
+
1453
+ int sequence = 1;
1454
+ if (latestNumber != null) {
1455
+ int currentSequence = Integer.parseInt(latestNumber.substring(latestNumber.length() - 4));
1456
+ sequence = currentSequence + 1;
1457
+ }
1458
+
1459
+ return prefix + String.format("%04d", sequence);
1460
+ }
1461
+
1462
+ /**
1463
+ * 消費データを作成する
1464
+ */
1465
+ @Transactional
1466
+ public Consumption createConsumption(ConsumptionCreateCommand command) {
1467
+ // 入荷データの取得
1468
+ Receiving receiving = receivingMapper.findByReceivingNumber(command.getReceivingNumber());
1469
+ if (receiving == null) {
1470
+ throw new IllegalArgumentException("Receiving not found: " + command.getReceivingNumber());
1471
+ }
1472
+
1473
+ // 関連する支給データを取得
1474
+ Supply supply = supplyMapper.findByPurchaseOrderDetail(
1475
+ receiving.getPurchaseOrderNumber(), receiving.getLineNumber());
1476
+ if (supply == null) {
1477
+ throw new IllegalArgumentException("Supply not found for receiving: " + command.getReceivingNumber());
1478
+ }
1479
+
1480
+ List<SupplyDetail> supplyDetails = supplyDetailMapper.findBySupplyNumber(supply.getSupplyNumber());
1481
+
1482
+ // 消費数量のバリデーション
1483
+ for (ConsumptionDetailCommand detailCommand : command.getDetails()) {
1484
+ for (SupplyDetail supplyDetail : supplyDetails) {
1485
+ if (supplyDetail.getItemCode().equals(detailCommand.getItemCode())) {
1486
+ if (detailCommand.getQuantity().compareTo(supplyDetail.getQuantity()) > 0) {
1487
+ throw new IllegalStateException("Consumption quantity exceeds supply quantity");
1488
+ }
1489
+ }
1490
+ }
1491
+ }
1492
+
1493
+ String consumptionNumber = generateConsumptionNumber(command.getConsumptionDate());
1494
+
1495
+ // 消費ヘッダを作成
1496
+ Consumption consumption = Consumption.builder()
1497
+ .consumptionNumber(consumptionNumber)
1498
+ .receivingNumber(command.getReceivingNumber())
1499
+ .consumptionDate(command.getConsumptionDate())
1500
+ .supplierCode(command.getSupplierCode())
1501
+ .remarks(command.getRemarks())
1502
+ .build();
1503
+ consumptionMapper.insert(consumption);
1504
+
1505
+ // 消費明細を作成
1506
+ List<ConsumptionDetail> details = new ArrayList<>();
1507
+ int lineNumber = 0;
1508
+
1509
+ for (ConsumptionDetailCommand detailCommand : command.getDetails()) {
1510
+ lineNumber++;
1511
+
1512
+ ConsumptionDetail detail = ConsumptionDetail.builder()
1513
+ .consumptionNumber(consumptionNumber)
1514
+ .lineNumber(lineNumber)
1515
+ .itemCode(detailCommand.getItemCode())
1516
+ .quantity(detailCommand.getQuantity())
1517
+ .remarks(detailCommand.getRemarks())
1518
+ .build();
1519
+ consumptionDetailMapper.insert(detail);
1520
+
1521
+ details.add(detail);
1522
+ }
1523
+
1524
+ consumption.setDetails(details);
1525
+ return consumption;
1526
+ }
1527
+
1528
+ /**
1529
+ * 消費率を計算する
1530
+ */
1531
+ public BigDecimal calculateConsumptionRate(String supplyNumber, String itemCode) {
1532
+ Supply supply = supplyMapper.findBySupplyNumber(supplyNumber);
1533
+ if (supply == null) {
1534
+ throw new IllegalArgumentException("Supply not found: " + supplyNumber);
1535
+ }
1536
+
1537
+ List<SupplyDetail> supplyDetails = supplyDetailMapper.findBySupplyNumber(supplyNumber);
1538
+ SupplyDetail targetDetail = supplyDetails.stream()
1539
+ .filter(d -> d.getItemCode().equals(itemCode))
1540
+ .findFirst()
1541
+ .orElseThrow(() -> new IllegalArgumentException("Supply detail not found: " + itemCode));
1542
+
1543
+ BigDecimal supplyQuantity = targetDetail.getQuantity();
1544
+
1545
+ BigDecimal totalConsumption = consumptionDetailMapper.sumByPurchaseOrderAndItem(
1546
+ supply.getPurchaseOrderNumber(),
1547
+ supply.getLineNumber(),
1548
+ itemCode
1549
+ );
1550
+
1551
+ if (totalConsumption == null) {
1552
+ totalConsumption = BigDecimal.ZERO;
1553
+ }
1554
+
1555
+ return totalConsumption.divide(supplyQuantity, 2, RoundingMode.HALF_UP);
1556
+ }
1557
+ }
1558
+ ```
1559
+
1560
+ </details>
1561
+
1562
+ ### コマンドクラス
1563
+
1564
+ <details>
1565
+ <summary>ConsumptionCreateCommand</summary>
1566
+
1567
+ ```java
1568
+ // src/main/java/com/example/pms/application/port/in/command/ConsumptionCreateCommand.java
1569
+ package com.example.pms.application.port.in.command;
1570
+
1571
+ import lombok.Builder;
1572
+ import lombok.Data;
1573
+
1574
+ import java.time.LocalDate;
1575
+ import java.util.List;
1576
+
1577
+ @Data
1578
+ @Builder
1579
+ public class ConsumptionCreateCommand {
1580
+ private String receivingNumber;
1581
+ private LocalDate consumptionDate;
1582
+ private String supplierCode;
1583
+ private String remarks;
1584
+ private List<ConsumptionDetailCommand> details;
1585
+ }
1586
+ ```
1587
+
1588
+ </details>
1589
+
1590
+ <details>
1591
+ <summary>ConsumptionDetailCommand</summary>
1592
+
1593
+ ```java
1594
+ // src/main/java/com/example/pms/application/port/in/command/ConsumptionDetailCommand.java
1595
+ package com.example.pms.application.port.in.command;
1596
+
1597
+ import lombok.Builder;
1598
+ import lombok.Data;
1599
+
1600
+ import java.math.BigDecimal;
1601
+
1602
+ @Data
1603
+ @Builder
1604
+ public class ConsumptionDetailCommand {
1605
+ private String itemCode;
1606
+ private BigDecimal quantity;
1607
+ private String remarks;
1608
+ }
1609
+ ```
1610
+
1611
+ </details>
1612
+
1613
+ ### TDD: 消費データのテスト
1614
+
1615
+ <details>
1616
+ <summary>ConsumptionServiceTest</summary>
1617
+
1618
+ ```java
1619
+ // src/test/java/com/example/pms/application/service/ConsumptionServiceTest.java
1620
+ package com.example.pms.application.service;
1621
+
1622
+ import com.example.pms.domain.model.subcontract.*;
1623
+ import org.junit.jupiter.api.*;
1624
+ import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
1625
+ import org.springframework.beans.factory.annotation.Autowired;
1626
+ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
1627
+ import org.springframework.context.annotation.Import;
1628
+ import org.springframework.test.context.DynamicPropertyRegistry;
1629
+ import org.springframework.test.context.DynamicPropertySource;
1630
+ import org.testcontainers.containers.PostgreSQLContainer;
1631
+ import org.testcontainers.junit.jupiter.Container;
1632
+ import org.testcontainers.junit.jupiter.Testcontainers;
1633
+
1634
+ import java.math.BigDecimal;
1635
+ import java.time.LocalDate;
1636
+ import java.util.List;
1637
+
1638
+ import static org.assertj.core.api.Assertions.*;
1639
+
1640
+ @MybatisTest
1641
+ @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
1642
+ @Import({ConsumptionService.class, SupplyService.class, ReceivingService.class, PurchaseOrderService.class})
1643
+ @Testcontainers
1644
+ @DisplayName("消費業務")
1645
+ class ConsumptionServiceTest {
1646
+
1647
+ @Container
1648
+ static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15")
1649
+ .withDatabaseName("testdb")
1650
+ .withUsername("testuser")
1651
+ .withPassword("testpass");
1652
+
1653
+ @DynamicPropertySource
1654
+ static void configureProperties(DynamicPropertyRegistry registry) {
1655
+ registry.add("spring.datasource.url", postgres::getJdbcUrl);
1656
+ registry.add("spring.datasource.username", postgres::getUsername);
1657
+ registry.add("spring.datasource.password", postgres::getPassword);
1658
+ }
1659
+
1660
+ @Autowired
1661
+ private ConsumptionService consumptionService;
1662
+
1663
+ // テストデータ準備は省略
1664
+
1665
+ @Nested
1666
+ @DisplayName("消費データ作成")
1667
+ class ConsumptionCreation {
1668
+
1669
+ @Test
1670
+ @DisplayName("入荷時に支給品の消費を記録できる")
1671
+ void canRecordConsumptionOnReceiving() {
1672
+ // Arrange: 事前に支給・入荷データを作成(省略)
1673
+ String receivingNumber = "RCV-202501-0001";
1674
+
1675
+ // Act
1676
+ ConsumptionCreateCommand input = ConsumptionCreateCommand.builder()
1677
+ .receivingNumber(receivingNumber)
1678
+ .consumptionDate(LocalDate.of(2025, 1, 26))
1679
+ .supplierCode("SUB-001")
1680
+ .details(List.of(
1681
+ ConsumptionDetailCommand.builder()
1682
+ .itemCode("PRESS-001")
1683
+ .quantity(new BigDecimal("95"))
1684
+ .build()
1685
+ ))
1686
+ .build();
1687
+
1688
+ Consumption consumption = consumptionService.createConsumption(input);
1689
+
1690
+ // Assert
1691
+ assertThat(consumption).isNotNull();
1692
+ assertThat(consumption.getConsumptionNumber()).startsWith("CON-");
1693
+ assertThat(consumption.getDetails()).hasSize(1);
1694
+ assertThat(consumption.getDetails().get(0).getQuantity())
1695
+ .isEqualByComparingTo(new BigDecimal("95"));
1696
+ }
1697
+
1698
+ @Test
1699
+ @DisplayName("消費数量は支給数量を超えてはならない")
1700
+ void cannotConsumeMoreThanSupplied() {
1701
+ // Arrange
1702
+ String receivingNumber = "RCV-202501-0001";
1703
+
1704
+ // Act & Assert
1705
+ ConsumptionCreateCommand input = ConsumptionCreateCommand.builder()
1706
+ .receivingNumber(receivingNumber)
1707
+ .consumptionDate(LocalDate.of(2025, 1, 26))
1708
+ .supplierCode("SUB-001")
1709
+ .details(List.of(
1710
+ ConsumptionDetailCommand.builder()
1711
+ .itemCode("PRESS-001")
1712
+ .quantity(new BigDecimal("150"))
1713
+ .build()
1714
+ ))
1715
+ .build();
1716
+
1717
+ assertThatThrownBy(() -> consumptionService.createConsumption(input))
1718
+ .isInstanceOf(IllegalStateException.class)
1719
+ .hasMessageContaining("exceeds supply quantity");
1720
+ }
1721
+ }
1722
+
1723
+ @Nested
1724
+ @DisplayName("消費率の計算")
1725
+ class ConsumptionRateCalculation {
1726
+
1727
+ @Test
1728
+ @DisplayName("支給に対する消費率を計算できる")
1729
+ void canCalculateConsumptionRate() {
1730
+ // Arrange: 消費データを作成(省略)
1731
+ String supplyNumber = "SUP-202501-0001";
1732
+
1733
+ // Act
1734
+ BigDecimal rate = consumptionService.calculateConsumptionRate(supplyNumber, "PRESS-001");
1735
+
1736
+ // Assert: 95/100 = 0.95
1737
+ assertThat(rate).isEqualByComparingTo(new BigDecimal("0.95"));
1738
+ }
1739
+ }
1740
+ }
1741
+ ```
1742
+
1743
+ </details>
1744
+
1745
+ ---
1746
+
1747
+ ## 26.4 外注委託ワークフロー
1748
+
1749
+ ### 外注委託の全体フロー
1750
+
1751
+ ```plantuml
1752
+ @startuml
1753
+
1754
+ title 外注委託ワークフロー
1755
+
1756
+ start
1757
+
1758
+ :発注作成;
1759
+ note right
1760
+ 発注データを作成
1761
+ ステータス:作成中→発注済
1762
+ end note
1763
+
1764
+ :支給品出荷;
1765
+ note right
1766
+ 支給データを作成
1767
+ 支給明細を登録
1768
+ end note
1769
+
1770
+ :外注先で加工;
1771
+
1772
+ :加工品入荷;
1773
+ note right
1774
+ 入荷受入データを作成
1775
+ 入荷数量を記録
1776
+ end note
1777
+
1778
+ :受入検査;
1779
+
1780
+ if (合格?) then (yes)
1781
+ :検収処理;
1782
+ note right
1783
+ 検収データを作成
1784
+ 検収金額を計算
1785
+ end note
1786
+
1787
+ :消費記録;
1788
+ note right
1789
+ 消費データを作成
1790
+ 支給品の消費を記録
1791
+ end note
1792
+ else (no)
1793
+ :不良処理;
1794
+ :返品処理;
1795
+ endif
1796
+
1797
+ stop
1798
+
1799
+ @enduml
1800
+ ```
1801
+
1802
+ ### 外注委託ワークフローサービス
1803
+
1804
+ <details>
1805
+ <summary>SubcontractingWorkflowService</summary>
1806
+
1807
+ ```java
1808
+ // src/main/java/com/example/pms/application/service/SubcontractingWorkflowService.java
1809
+ package com.example.pms.application.service;
1810
+
1811
+ import com.example.pms.application.port.in.command.SubcontractOrderCommand;
1812
+ import com.example.pms.application.port.out.SubcontractStatus;
1813
+ import com.example.pms.domain.model.purchase.*;
1814
+ import com.example.pms.domain.model.subcontract.*;
1815
+ import com.example.pms.infrastructure.out.persistence.mapper.*;
1816
+ import lombok.RequiredArgsConstructor;
1817
+ import org.springframework.stereotype.Service;
1818
+ import org.springframework.transaction.annotation.Transactional;
1819
+
1820
+ import java.math.BigDecimal;
1821
+ import java.math.RoundingMode;
1822
+ import java.time.LocalDate;
1823
+ import java.time.format.DateTimeFormatter;
1824
+ import java.util.List;
1825
+
1826
+ @Service
1827
+ @RequiredArgsConstructor
1828
+ public class SubcontractingWorkflowService {
1829
+
1830
+ private final PurchaseOrderMapper purchaseOrderMapper;
1831
+ private final PurchaseOrderDetailMapper purchaseOrderDetailMapper;
1832
+ private final SupplyMapper supplyMapper;
1833
+ private final SupplyDetailMapper supplyDetailMapper;
1834
+ private final ConsumptionDetailMapper consumptionDetailMapper;
1835
+ private final UnitPriceMapper unitPriceMapper;
1836
+
1837
+ /**
1838
+ * 発注番号を生成する
1839
+ */
1840
+ private String generatePurchaseOrderNumber() {
1841
+ String prefix = "PO-" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM")) + "-";
1842
+ String latestNumber = purchaseOrderMapper.findLatestPurchaseOrderNumber(prefix + "%");
1843
+
1844
+ int sequence = 1;
1845
+ if (latestNumber != null) {
1846
+ int currentSequence = Integer.parseInt(latestNumber.substring(latestNumber.length() - 4));
1847
+ sequence = currentSequence + 1;
1848
+ }
1849
+
1850
+ return prefix + String.format("%04d", sequence);
1851
+ }
1852
+
1853
+ /**
1854
+ * 外注発注を作成する
1855
+ */
1856
+ @Transactional
1857
+ public PurchaseOrder createSubcontractOrder(SubcontractOrderCommand command) {
1858
+ String purchaseOrderNumber = generatePurchaseOrderNumber();
1859
+
1860
+ // 単価を取得
1861
+ BigDecimal unitPrice = command.getUnitPrice();
1862
+ if (unitPrice == null) {
1863
+ UnitPrice priceEntity = unitPriceMapper.findEffectiveUnitPrice(
1864
+ command.getItemCode(), command.getSupplierCode(), LocalDate.now());
1865
+ unitPrice = priceEntity != null ? priceEntity.getUnitPrice() : BigDecimal.ZERO;
1866
+ }
1867
+
1868
+ // 発注ヘッダを作成
1869
+ PurchaseOrder purchaseOrder = PurchaseOrder.builder()
1870
+ .purchaseOrderNumber(purchaseOrderNumber)
1871
+ .orderDate(LocalDate.now())
1872
+ .supplierCode(command.getSupplierCode())
1873
+ .status(PurchaseOrderStatus.CREATING)
1874
+ .build();
1875
+ purchaseOrderMapper.insert(purchaseOrder);
1876
+
1877
+ // 発注明細を作成
1878
+ BigDecimal orderAmount = command.getQuantity().multiply(unitPrice);
1879
+ PurchaseOrderDetail detail = PurchaseOrderDetail.builder()
1880
+ .purchaseOrderNumber(purchaseOrderNumber)
1881
+ .lineNumber(1)
1882
+ .itemCode(command.getItemCode())
1883
+ .orderQuantity(command.getQuantity())
1884
+ .orderUnitPrice(unitPrice)
1885
+ .orderAmount(orderAmount)
1886
+ .expectedReceivingDate(command.getDeliveryDate())
1887
+ .completedFlag(false)
1888
+ .build();
1889
+ purchaseOrderDetailMapper.insert(detail);
1890
+
1891
+ // 発注確定
1892
+ purchaseOrderMapper.updateStatus(purchaseOrderNumber, PurchaseOrderStatus.ORDERED);
1893
+ purchaseOrder.setStatus(PurchaseOrderStatus.ORDERED);
1894
+
1895
+ return purchaseOrder;
1896
+ }
1897
+
1898
+ /**
1899
+ * 外注委託状況を取得する
1900
+ */
1901
+ public SubcontractStatus getSubcontractStatus(String purchaseOrderNumber) {
1902
+ PurchaseOrder purchaseOrder = purchaseOrderMapper.findByPurchaseOrderNumber(purchaseOrderNumber);
1903
+ if (purchaseOrder == null) {
1904
+ throw new IllegalArgumentException("Purchase order not found: " + purchaseOrderNumber);
1905
+ }
1906
+
1907
+ List<PurchaseOrderDetail> details = purchaseOrderDetailMapper.findByPurchaseOrderNumber(purchaseOrderNumber);
1908
+
1909
+ BigDecimal suppliedQuantity = BigDecimal.ZERO;
1910
+ BigDecimal consumedQuantity = BigDecimal.ZERO;
1911
+ BigDecimal acceptedQuantity = BigDecimal.ZERO;
1912
+
1913
+ for (PurchaseOrderDetail detail : details) {
1914
+ Supply supply = supplyMapper.findByPurchaseOrderDetail(purchaseOrderNumber, detail.getLineNumber());
1915
+ if (supply != null) {
1916
+ List<SupplyDetail> supplyDetails = supplyDetailMapper.findBySupplyNumber(supply.getSupplyNumber());
1917
+ for (SupplyDetail supplyDetail : supplyDetails) {
1918
+ suppliedQuantity = suppliedQuantity.add(supplyDetail.getQuantity());
1919
+
1920
+ BigDecimal consumed = consumptionDetailMapper.sumByPurchaseOrderAndItem(
1921
+ purchaseOrderNumber, detail.getLineNumber(), supplyDetail.getItemCode());
1922
+ if (consumed != null) {
1923
+ consumedQuantity = consumedQuantity.add(consumed);
1924
+ }
1925
+ }
1926
+ }
1927
+
1928
+ if (detail.getAcceptedQuantity() != null) {
1929
+ acceptedQuantity = acceptedQuantity.add(detail.getAcceptedQuantity());
1930
+ }
1931
+ }
1932
+
1933
+ BigDecimal yieldRate = suppliedQuantity.compareTo(BigDecimal.ZERO) > 0
1934
+ ? consumedQuantity.divide(suppliedQuantity, 2, RoundingMode.HALF_UP)
1935
+ : BigDecimal.ZERO;
1936
+
1937
+ return SubcontractStatus.builder()
1938
+ .purchaseOrderNumber(purchaseOrderNumber)
1939
+ .status(purchaseOrder.getStatus())
1940
+ .suppliedQuantity(suppliedQuantity)
1941
+ .consumedQuantity(consumedQuantity)
1942
+ .acceptedQuantity(acceptedQuantity)
1943
+ .yieldRate(yieldRate)
1944
+ .build();
1945
+ }
1946
+ }
1947
+ ```
1948
+
1949
+ </details>
1950
+
1951
+ ### コマンド・DTO クラス
1952
+
1953
+ <details>
1954
+ <summary>SubcontractOrderCommand</summary>
1955
+
1956
+ ```java
1957
+ // src/main/java/com/example/pms/application/port/in/command/SubcontractOrderCommand.java
1958
+ package com.example.pms.application.port.in.command;
1959
+
1960
+ import lombok.Builder;
1961
+ import lombok.Data;
1962
+
1963
+ import java.math.BigDecimal;
1964
+ import java.time.LocalDate;
1965
+
1966
+ @Data
1967
+ @Builder
1968
+ public class SubcontractOrderCommand {
1969
+ private String supplierCode;
1970
+ private LocalDate deliveryDate;
1971
+ private String itemCode;
1972
+ private BigDecimal quantity;
1973
+ private BigDecimal unitPrice;
1974
+ }
1975
+ ```
1976
+
1977
+ </details>
1978
+
1979
+ <details>
1980
+ <summary>SubcontractStatus</summary>
1981
+
1982
+ ```java
1983
+ // src/main/java/com/example/pms/application/port/out/SubcontractStatus.java
1984
+ package com.example.pms.application.port.out;
1985
+
1986
+ import com.example.pms.domain.model.purchase.PurchaseOrderStatus;
1987
+ import lombok.Builder;
1988
+ import lombok.Data;
1989
+
1990
+ import java.math.BigDecimal;
1991
+
1992
+ /**
1993
+ * 外注委託状況DTO
1994
+ */
1995
+ @Data
1996
+ @Builder
1997
+ public class SubcontractStatus {
1998
+ private String purchaseOrderNumber;
1999
+ private PurchaseOrderStatus status;
2000
+ private BigDecimal suppliedQuantity;
2001
+ private BigDecimal consumedQuantity;
2002
+ private BigDecimal acceptedQuantity;
2003
+ private BigDecimal yieldRate;
2004
+ }
2005
+ ```
2006
+
2007
+ </details>
2008
+
2009
+ ### 有償支給と無償支給の会計処理の違い
2010
+
2011
+ | 処理 | 有償支給 | 無償支給 |
2012
+ |-----|---------|---------|
2013
+ | **支給時** | 売上計上(材料を売却) | 在庫移動のみ |
2014
+ | **入荷時** | 仕入計上(加工品を購入) | 加工賃のみ計上 |
2015
+ | **消費税** | 売上・仕入両方で課税 | 加工賃のみ課税 |
2016
+ | **在庫管理** | 支給時に在庫減、入荷時に在庫増 | 在庫は自社のまま |
2017
+
2018
+ ```plantuml
2019
+ @startuml
2020
+
2021
+ title 有償支給と無償支給の会計処理
2022
+
2023
+ |#LightBlue|有償支給|
2024
+ start
2025
+ :材料を外注先に売却;
2026
+ note right
2027
+ 【仕訳】
2028
+ 売掛金 / 売上高
2029
+ 売上原価 / 製品
2030
+ end note
2031
+
2032
+ :加工品を購入;
2033
+ note right
2034
+ 【仕訳】
2035
+ 仕入高 / 買掛金
2036
+ end note
2037
+ stop
2038
+
2039
+ |#LightGreen|無償支給|
2040
+ start
2041
+ :材料を外注先に貸与;
2042
+ note right
2043
+ 【仕訳】
2044
+ 預け材料 / 製品
2045
+ (社内振替のみ)
2046
+ end note
2047
+
2048
+ :加工賃を支払い;
2049
+ note right
2050
+ 【仕訳】
2051
+ 外注加工費 / 買掛金
2052
+ end note
2053
+ stop
2054
+
2055
+ @enduml
2056
+ ```
2057
+
2058
+ ---
2059
+
2060
+ ## 26.5 リレーションと楽観ロックの設計
2061
+
2062
+ ### MyBatis ネストした ResultMap によるリレーション設定
2063
+
2064
+ 外注委託データは、支給→支給明細、消費→消費明細 の親子関係と、支給→発注明細、消費→支給 の参照関係を持ちます。MyBatis でこれらの関係を効率的に取得するための設定を実装します。
2065
+
2066
+ #### ネストした ResultMap の定義
2067
+
2068
+ <details>
2069
+ <summary>SupplyMapper.xml(リレーション設定)</summary>
2070
+
2071
+ ```xml
2072
+ <?xml version="1.0" encoding="UTF-8" ?>
2073
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
2074
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
2075
+
2076
+ <!-- src/main/resources/mapper/SupplyMapper.xml -->
2077
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.SupplyMapper">
2078
+
2079
+ <!-- 支給データ ResultMap(明細・発注明細込み) -->
2080
+ <resultMap id="supplyWithRelationsResultMap" type="com.example.pms.domain.model.subcontract.Supply">
2081
+ <id property="id" column="s_ID"/>
2082
+ <result property="supplyNumber" column="s_支給番号"/>
2083
+ <result property="purchaseOrderNumber" column="s_発注番号"/>
2084
+ <result property="lineNumber" column="s_発注行番号"/>
2085
+ <result property="supplierCode" column="s_取引先コード"/>
2086
+ <result property="supplyDate" column="s_支給日"/>
2087
+ <result property="supplierPersonCode" column="s_支給担当者コード"/>
2088
+ <result property="supplyType" column="s_支給区分"
2089
+ typeHandler="com.example.pms.infrastructure.out.persistence.typehandler.SupplyTypeTypeHandler"/>
2090
+ <result property="remarks" column="s_備考"/>
2091
+ <result property="version" column="s_バージョン"/>
2092
+ <result property="createdAt" column="s_作成日時"/>
2093
+ <result property="createdBy" column="s_作成者"/>
2094
+ <result property="updatedAt" column="s_更新日時"/>
2095
+ <result property="updatedBy" column="s_更新者"/>
2096
+ <!-- 発注明細との N:1 関連 -->
2097
+ <association property="purchaseOrderDetail" javaType="com.example.pms.domain.model.purchase.PurchaseOrderDetail">
2098
+ <id property="id" column="pod_ID"/>
2099
+ <result property="purchaseOrderNumber" column="pod_発注番号"/>
2100
+ <result property="lineNumber" column="pod_発注行番号"/>
2101
+ <result property="itemCode" column="pod_品目コード"/>
2102
+ <result property="orderQuantity" column="pod_発注数量"/>
2103
+ <result property="unitPrice" column="pod_発注単価"/>
2104
+ </association>
2105
+ <!-- 取引先との N:1 関連 -->
2106
+ <association property="supplier" javaType="com.example.pms.domain.model.supplier.Supplier">
2107
+ <id property="supplierCode" column="sup_取引先コード"/>
2108
+ <result property="supplierName" column="sup_取引先名"/>
2109
+ </association>
2110
+ <!-- 支給明細との 1:N 関連 -->
2111
+ <collection property="details" ofType="com.example.pms.domain.model.subcontract.SupplyDetail"
2112
+ resultMap="supplyDetailNestedResultMap"/>
2113
+ </resultMap>
2114
+
2115
+ <!-- 支給明細のネスト ResultMap -->
2116
+ <resultMap id="supplyDetailNestedResultMap" type="com.example.pms.domain.model.subcontract.SupplyDetail">
2117
+ <id property="id" column="sd_ID"/>
2118
+ <result property="supplyNumber" column="sd_支給番号"/>
2119
+ <result property="lineNumber" column="sd_支給行番号"/>
2120
+ <result property="itemCode" column="sd_品目コード"/>
2121
+ <result property="quantity" column="sd_支給数"/>
2122
+ <result property="unitPrice" column="sd_支給単価"/>
2123
+ <result property="amount" column="sd_支給金額"/>
2124
+ <result property="consumedQuantity" column="sd_消費済数量"/>
2125
+ <result property="remainingQuantity" column="sd_残数量"/>
2126
+ <result property="remarks" column="sd_備考"/>
2127
+ <result property="version" column="sd_バージョン"/>
2128
+ <!-- 品目マスタとの N:1 関連 -->
2129
+ <association property="item" javaType="com.example.pms.domain.model.item.Item">
2130
+ <id property="itemCode" column="i_品目コード"/>
2131
+ <result property="itemName" column="i_品名"/>
2132
+ <result property="unit" column="i_単位"/>
2133
+ </association>
2134
+ </resultMap>
2135
+
2136
+ <!-- JOIN による一括取得クエリ -->
2137
+ <select id="findWithRelationsBySupplyNumber" resultMap="supplyWithRelationsResultMap">
2138
+ SELECT
2139
+ -- 支給データ
2140
+ s."ID" AS s_ID,
2141
+ s."支給番号" AS s_支給番号,
2142
+ s."発注番号" AS s_発注番号,
2143
+ s."発注行番号" AS s_発注行番号,
2144
+ s."取引先コード" AS s_取引先コード,
2145
+ s."支給日" AS s_支給日,
2146
+ s."支給担当者コード" AS s_支給担当者コード,
2147
+ s."支給区分" AS s_支給区分,
2148
+ s."備考" AS s_備考,
2149
+ s."バージョン" AS s_バージョン,
2150
+ s."作成日時" AS s_作成日時,
2151
+ s."作成者" AS s_作成者,
2152
+ s."更新日時" AS s_更新日時,
2153
+ s."更新者" AS s_更新者,
2154
+ -- 発注明細データ
2155
+ pod."ID" AS pod_ID,
2156
+ pod."発注番号" AS pod_発注番号,
2157
+ pod."発注行番号" AS pod_発注行番号,
2158
+ pod."品目コード" AS pod_品目コード,
2159
+ pod."発注数量" AS pod_発注数量,
2160
+ pod."発注単価" AS pod_発注単価,
2161
+ -- 取引先マスタ
2162
+ sup."取引先コード" AS sup_取引先コード,
2163
+ sup."取引先名" AS sup_取引先名,
2164
+ -- 支給明細データ
2165
+ sd."ID" AS sd_ID,
2166
+ sd."支給番号" AS sd_支給番号,
2167
+ sd."支給行番号" AS sd_支給行番号,
2168
+ sd."品目コード" AS sd_品目コード,
2169
+ sd."支給数" AS sd_支給数,
2170
+ sd."支給単価" AS sd_支給単価,
2171
+ sd."支給金額" AS sd_支給金額,
2172
+ sd."消費済数量" AS sd_消費済数量,
2173
+ sd."残数量" AS sd_残数量,
2174
+ sd."備考" AS sd_備考,
2175
+ sd."バージョン" AS sd_バージョン,
2176
+ -- 品目マスタ
2177
+ i."品目コード" AS i_品目コード,
2178
+ i."品名" AS i_品名,
2179
+ i."単位" AS i_単位
2180
+ FROM "支給データ" s
2181
+ LEFT JOIN "発注明細データ" pod
2182
+ ON s."発注番号" = pod."発注番号" AND s."発注行番号" = pod."発注行番号"
2183
+ LEFT JOIN "取引先マスタ" sup ON s."取引先コード" = sup."取引先コード"
2184
+ LEFT JOIN "支給明細データ" sd ON s."支給番号" = sd."支給番号"
2185
+ LEFT JOIN "品目マスタ" i ON sd."品目コード" = i."品目コード"
2186
+ AND i."適用開始日" = (
2187
+ SELECT MAX("適用開始日") FROM "品目マスタ"
2188
+ WHERE "品目コード" = sd."品目コード"
2189
+ AND "適用開始日" <= CURRENT_DATE
2190
+ )
2191
+ WHERE s."支給番号" = #{supplyNumber}
2192
+ ORDER BY sd."支給行番号"
2193
+ </select>
2194
+
2195
+ </mapper>
2196
+ ```
2197
+
2198
+ </details>
2199
+
2200
+ <details>
2201
+ <summary>ConsumptionMapper.xml(リレーション設定)</summary>
2202
+
2203
+ ```xml
2204
+ <?xml version="1.0" encoding="UTF-8" ?>
2205
+ <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
2206
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
2207
+
2208
+ <!-- src/main/resources/mapper/ConsumptionMapper.xml -->
2209
+ <mapper namespace="com.example.pms.infrastructure.out.persistence.mapper.ConsumptionMapper">
2210
+
2211
+ <!-- 消費データ ResultMap(明細・支給データ込み) -->
2212
+ <resultMap id="consumptionWithRelationsResultMap" type="com.example.pms.domain.model.subcontract.Consumption">
2213
+ <id property="id" column="c_ID"/>
2214
+ <result property="consumptionNumber" column="c_消費番号"/>
2215
+ <result property="supplyNumber" column="c_支給番号"/>
2216
+ <result property="receivingNumber" column="c_入荷受入番号"/>
2217
+ <result property="consumptionDate" column="c_消費日"/>
2218
+ <result property="remarks" column="c_備考"/>
2219
+ <result property="version" column="c_バージョン"/>
2220
+ <result property="createdAt" column="c_作成日時"/>
2221
+ <result property="updatedAt" column="c_更新日時"/>
2222
+ <!-- 支給データとの N:1 関連 -->
2223
+ <association property="supply" javaType="com.example.pms.domain.model.subcontract.Supply">
2224
+ <id property="id" column="s_ID"/>
2225
+ <result property="supplyNumber" column="s_支給番号"/>
2226
+ <result property="supplierCode" column="s_取引先コード"/>
2227
+ <result property="supplyDate" column="s_支給日"/>
2228
+ <result property="supplyType" column="s_支給区分"
2229
+ typeHandler="com.example.pms.infrastructure.out.persistence.typehandler.SupplyTypeTypeHandler"/>
2230
+ </association>
2231
+ <!-- 入荷データとの N:1 関連 -->
2232
+ <association property="receiving" javaType="com.example.pms.domain.model.purchase.Receiving">
2233
+ <id property="id" column="r_ID"/>
2234
+ <result property="receivingNumber" column="r_入荷受入番号"/>
2235
+ <result property="receivingDate" column="r_入荷日"/>
2236
+ <result property="receivedQuantity" column="r_入荷数量"/>
2237
+ </association>
2238
+ <!-- 消費明細との 1:N 関連 -->
2239
+ <collection property="details" ofType="com.example.pms.domain.model.subcontract.ConsumptionDetail"
2240
+ resultMap="consumptionDetailNestedResultMap"/>
2241
+ </resultMap>
2242
+
2243
+ <!-- 消費明細のネスト ResultMap -->
2244
+ <resultMap id="consumptionDetailNestedResultMap" type="com.example.pms.domain.model.subcontract.ConsumptionDetail">
2245
+ <id property="id" column="cd_ID"/>
2246
+ <result property="consumptionNumber" column="cd_消費番号"/>
2247
+ <result property="lineNumber" column="cd_消費行番号"/>
2248
+ <result property="supplyDetailId" column="cd_支給明細ID"/>
2249
+ <result property="itemCode" column="cd_品目コード"/>
2250
+ <result property="consumedQuantity" column="cd_消費数量"/>
2251
+ <result property="yieldRate" column="cd_歩留率"/>
2252
+ <result property="remarks" column="cd_備考"/>
2253
+ <result property="version" column="cd_バージョン"/>
2254
+ <!-- 支給明細との N:1 関連 -->
2255
+ <association property="supplyDetail" javaType="com.example.pms.domain.model.subcontract.SupplyDetail">
2256
+ <id property="id" column="sd_ID"/>
2257
+ <result property="supplyNumber" column="sd_支給番号"/>
2258
+ <result property="lineNumber" column="sd_支給行番号"/>
2259
+ <result property="quantity" column="sd_支給数"/>
2260
+ <result property="remainingQuantity" column="sd_残数量"/>
2261
+ </association>
2262
+ </resultMap>
2263
+
2264
+ <!-- JOIN による一括取得クエリ -->
2265
+ <select id="findWithRelationsByConsumptionNumber" resultMap="consumptionWithRelationsResultMap">
2266
+ SELECT
2267
+ -- 消費データ
2268
+ c."ID" AS c_ID,
2269
+ c."消費番号" AS c_消費番号,
2270
+ c."支給番号" AS c_支給番号,
2271
+ c."入荷受入番号" AS c_入荷受入番号,
2272
+ c."消費日" AS c_消費日,
2273
+ c."備考" AS c_備考,
2274
+ c."バージョン" AS c_バージョン,
2275
+ c."作成日時" AS c_作成日時,
2276
+ c."更新日時" AS c_更新日時,
2277
+ -- 支給データ
2278
+ s."ID" AS s_ID,
2279
+ s."支給番号" AS s_支給番号,
2280
+ s."取引先コード" AS s_取引先コード,
2281
+ s."支給日" AS s_支給日,
2282
+ s."支給区分" AS s_支給区分,
2283
+ -- 入荷データ
2284
+ r."ID" AS r_ID,
2285
+ r."入荷受入番号" AS r_入荷受入番号,
2286
+ r."入荷日" AS r_入荷日,
2287
+ r."入荷数量" AS r_入荷数量,
2288
+ -- 消費明細データ
2289
+ cd."ID" AS cd_ID,
2290
+ cd."消費番号" AS cd_消費番号,
2291
+ cd."消費行番号" AS cd_消費行番号,
2292
+ cd."支給明細ID" AS cd_支給明細ID,
2293
+ cd."品目コード" AS cd_品目コード,
2294
+ cd."消費数量" AS cd_消費数量,
2295
+ cd."歩留率" AS cd_歩留率,
2296
+ cd."備考" AS cd_備考,
2297
+ cd."バージョン" AS cd_バージョン,
2298
+ -- 支給明細データ
2299
+ sd."ID" AS sd_ID,
2300
+ sd."支給番号" AS sd_支給番号,
2301
+ sd."支給行番号" AS sd_支給行番号,
2302
+ sd."支給数" AS sd_支給数,
2303
+ sd."残数量" AS sd_残数量
2304
+ FROM "消費データ" c
2305
+ LEFT JOIN "支給データ" s ON c."支給番号" = s."支給番号"
2306
+ LEFT JOIN "入荷受入データ" r ON c."入荷受入番号" = r."入荷受入番号"
2307
+ LEFT JOIN "消費明細データ" cd ON c."消費番号" = cd."消費番号"
2308
+ LEFT JOIN "支給明細データ" sd ON cd."支給明細ID" = sd."ID"
2309
+ WHERE c."消費番号" = #{consumptionNumber}
2310
+ ORDER BY cd."消費行番号"
2311
+ </select>
2312
+
2313
+ </mapper>
2314
+ ```
2315
+
2316
+ </details>
2317
+
2318
+ #### リレーション設定のポイント
2319
+
2320
+ | 設定項目 | 説明 |
2321
+ |---------|------|
2322
+ | `<collection>` | 支給→支給明細、消費→消費明細 の 1:N 関連 |
2323
+ | `<association>` | 支給→発注明細、消費→支給、消費明細→支給明細 の N:1 関連 |
2324
+ | エイリアス | `s_`(支給)、`sd_`(支給明細)、`c_`(消費)、`cd_`(消費明細) |
2325
+ | 残数量管理 | 支給明細の残数量を消費時に更新 |
2326
+
2327
+ ### 楽観ロックの実装
2328
+
2329
+ 支給と消費は同時に複数ユーザーが操作する可能性があり、特に残数量の整合性を保つために楽観ロックが重要です。
2330
+
2331
+ #### Flyway マイグレーション: バージョンカラム追加
2332
+
2333
+ <details>
2334
+ <summary>V012__add_subcontract_version_columns.sql</summary>
2335
+
2336
+ ```sql
2337
+ -- src/main/resources/db/migration/V012__add_subcontract_version_columns.sql
2338
+
2339
+ -- 支給データテーブルにバージョンカラムを追加
2340
+ ALTER TABLE "支給データ" ADD COLUMN "バージョン" INTEGER DEFAULT 1 NOT NULL;
2341
+
2342
+ -- 支給明細データテーブルにバージョンカラムと残数量を追加
2343
+ ALTER TABLE "支給明細データ" ADD COLUMN "消費済数量" DECIMAL(15, 2) DEFAULT 0 NOT NULL;
2344
+ ALTER TABLE "支給明細データ" ADD COLUMN "残数量" DECIMAL(15, 2);
2345
+ ALTER TABLE "支給明細データ" ADD COLUMN "バージョン" INTEGER DEFAULT 1 NOT NULL;
2346
+
2347
+ -- 残数量の初期値を設定(支給数と同じ)
2348
+ UPDATE "支給明細データ" SET "残数量" = "支給数" WHERE "残数量" IS NULL;
2349
+ ALTER TABLE "支給明細データ" ALTER COLUMN "残数量" SET NOT NULL;
2350
+
2351
+ -- 消費データテーブルにバージョンカラムを追加
2352
+ ALTER TABLE "消費データ" ADD COLUMN "バージョン" INTEGER DEFAULT 1 NOT NULL;
2353
+
2354
+ -- 消費明細データテーブルにバージョンカラムを追加
2355
+ ALTER TABLE "消費明細データ" ADD COLUMN "バージョン" INTEGER DEFAULT 1 NOT NULL;
2356
+
2357
+ -- コメント追加
2358
+ COMMENT ON COLUMN "支給データ"."バージョン" IS '楽観ロック用バージョン番号';
2359
+ COMMENT ON COLUMN "支給明細データ"."消費済数量" IS '消費済の数量';
2360
+ COMMENT ON COLUMN "支給明細データ"."残数量" IS '未消費の残数量';
2361
+ COMMENT ON COLUMN "支給明細データ"."バージョン" IS '楽観ロック用バージョン番号';
2362
+ COMMENT ON COLUMN "消費データ"."バージョン" IS '楽観ロック用バージョン番号';
2363
+ COMMENT ON COLUMN "消費明細データ"."バージョン" IS '楽観ロック用バージョン番号';
2364
+ ```
2365
+
2366
+ </details>
2367
+
2368
+ #### エンティティへのバージョンフィールド追加
2369
+
2370
+ <details>
2371
+ <summary>Supply.java(バージョンフィールド追加)</summary>
2372
+
2373
+ ```java
2374
+ // src/main/java/com/example/pms/domain/model/subcontract/Supply.java
2375
+ package com.example.pms.domain.model.subcontract;
2376
+
2377
+ import com.example.pms.domain.model.supplier.Supplier;
2378
+ import com.example.pms.domain.model.purchase.PurchaseOrderDetail;
2379
+ import lombok.Builder;
2380
+ import lombok.Data;
2381
+
2382
+ import java.time.LocalDate;
2383
+ import java.time.LocalDateTime;
2384
+ import java.util.ArrayList;
2385
+ import java.util.List;
2386
+
2387
+ @Data
2388
+ @Builder
2389
+ public class Supply {
2390
+ private Integer id;
2391
+ private String supplyNumber;
2392
+ private String purchaseOrderNumber;
2393
+ private Integer lineNumber;
2394
+ private String supplierCode;
2395
+ private LocalDate supplyDate;
2396
+ private String supplierPersonCode;
2397
+ private SupplyType supplyType;
2398
+ private String remarks;
2399
+ private LocalDateTime createdAt;
2400
+ private String createdBy;
2401
+ private LocalDateTime updatedAt;
2402
+ private String updatedBy;
2403
+
2404
+ // 楽観ロック用バージョン
2405
+ @Builder.Default
2406
+ private Integer version = 1;
2407
+
2408
+ // リレーション
2409
+ private PurchaseOrderDetail purchaseOrderDetail;
2410
+ private Supplier supplier;
2411
+ @Builder.Default
2412
+ private List<SupplyDetail> details = new ArrayList<>();
2413
+ }
2414
+ ```
2415
+
2416
+ </details>
2417
+
2418
+ <details>
2419
+ <summary>SupplyDetail.java(バージョン・残数量追加)</summary>
2420
+
2421
+ ```java
2422
+ // src/main/java/com/example/pms/domain/model/subcontract/SupplyDetail.java
2423
+ package com.example.pms.domain.model.subcontract;
2424
+
2425
+ import com.example.pms.domain.model.item.Item;
2426
+ import lombok.AllArgsConstructor;
2427
+ import lombok.Builder;
2428
+ import lombok.Data;
2429
+ import lombok.NoArgsConstructor;
2430
+
2431
+ import java.math.BigDecimal;
2432
+ import java.time.LocalDateTime;
2433
+
2434
+ @Data
2435
+ @Builder
2436
+ @NoArgsConstructor
2437
+ @AllArgsConstructor
2438
+ public class SupplyDetail {
2439
+ private Integer id;
2440
+ private String supplyNumber;
2441
+ private Integer lineNumber;
2442
+ private String itemCode;
2443
+ private BigDecimal quantity;
2444
+ private BigDecimal unitPrice;
2445
+ private BigDecimal amount;
2446
+ private BigDecimal consumedQuantity;
2447
+ private BigDecimal remainingQuantity;
2448
+ private String remarks;
2449
+ private LocalDateTime createdAt;
2450
+ private LocalDateTime updatedAt;
2451
+
2452
+ // 楽観ロック用バージョン
2453
+ @Builder.Default
2454
+ private Integer version = 1;
2455
+
2456
+ // リレーション
2457
+ private Supply supply;
2458
+ private Item item;
2459
+
2460
+ /**
2461
+ * 消費可能かどうかを判定
2462
+ */
2463
+ public boolean canConsume(BigDecimal requestedQuantity) {
2464
+ return remainingQuantity.compareTo(requestedQuantity) >= 0;
2465
+ }
2466
+ }
2467
+ ```
2468
+
2469
+ </details>
2470
+
2471
+ #### MyBatis Mapper: 楽観ロック対応の更新
2472
+
2473
+ <details>
2474
+ <summary>SupplyDetailMapper.xml(楽観ロック対応 UPDATE)</summary>
2475
+
2476
+ ```xml
2477
+ <!-- 消費数量更新(楽観ロック対応) -->
2478
+ <update id="updateConsumedQuantityWithOptimisticLock">
2479
+ UPDATE "支給明細データ"
2480
+ SET
2481
+ "消費済数量" = "消費済数量" + #{consumedQuantity},
2482
+ "残数量" = "残数量" - #{consumedQuantity},
2483
+ "更新日時" = CURRENT_TIMESTAMP,
2484
+ "バージョン" = "バージョン" + 1
2485
+ WHERE "ID" = #{id}
2486
+ AND "バージョン" = #{version}
2487
+ AND "残数量" >= #{consumedQuantity}
2488
+ </update>
2489
+
2490
+ <!-- 残数量チェック付き更新(楽観ロック + 業務制約) -->
2491
+ <update id="consumeWithValidation">
2492
+ UPDATE "支給明細データ"
2493
+ SET
2494
+ "消費済数量" = "消費済数量" + #{consumedQuantity},
2495
+ "残数量" = "残数量" - #{consumedQuantity},
2496
+ "更新日時" = CURRENT_TIMESTAMP,
2497
+ "バージョン" = "バージョン" + 1
2498
+ WHERE "ID" = #{id}
2499
+ AND "バージョン" = #{version}
2500
+ AND "残数量" >= #{consumedQuantity}
2501
+ </update>
2502
+
2503
+ <!-- 現在のバージョン取得 -->
2504
+ <select id="findVersionById" resultType="java.lang.Integer">
2505
+ SELECT "バージョン" FROM "支給明細データ" WHERE "ID" = #{id}
2506
+ </select>
2507
+
2508
+ <!-- 残数量取得 -->
2509
+ <select id="findRemainingQuantityById" resultType="java.math.BigDecimal">
2510
+ SELECT "残数量" FROM "支給明細データ" WHERE "ID" = #{id}
2511
+ </select>
2512
+ ```
2513
+
2514
+ </details>
2515
+
2516
+ <details>
2517
+ <summary>ConsumptionMapper.xml(楽観ロック対応 UPDATE)</summary>
2518
+
2519
+ ```xml
2520
+ <!-- 楽観ロック対応の更新 -->
2521
+ <update id="updateWithOptimisticLock" parameterType="com.example.pms.domain.model.subcontract.Consumption">
2522
+ UPDATE "消費データ"
2523
+ SET
2524
+ "消費日" = #{consumptionDate},
2525
+ "備考" = #{remarks},
2526
+ "更新日時" = CURRENT_TIMESTAMP,
2527
+ "バージョン" = "バージョン" + 1
2528
+ WHERE "ID" = #{id}
2529
+ AND "バージョン" = #{version}
2530
+ </update>
2531
+
2532
+ <!-- 現在のバージョン取得 -->
2533
+ <select id="findVersionById" resultType="java.lang.Integer">
2534
+ SELECT "バージョン" FROM "消費データ" WHERE "ID" = #{id}
2535
+ </select>
2536
+ ```
2537
+
2538
+ </details>
2539
+
2540
+ #### Repository 実装: 楽観ロック対応
2541
+
2542
+ <details>
2543
+ <summary>SupplyDetailRepositoryImpl.java(楽観ロック対応)</summary>
2544
+
2545
+ ```java
2546
+ // src/main/java/com/example/pms/infrastructure/persistence/repository/SupplyDetailRepositoryImpl.java
2547
+ package com.example.pms.infrastructure.out.persistence.repository;
2548
+
2549
+ import com.example.pms.application.port.out.SupplyDetailRepository;
2550
+ import com.example.pms.domain.exception.InsufficientQuantityException;
2551
+ import com.example.pms.domain.exception.OptimisticLockException;
2552
+ import com.example.pms.domain.model.subcontract.SupplyDetail;
2553
+ import com.example.pms.infrastructure.out.persistence.mapper.SupplyDetailMapper;
2554
+ import lombok.RequiredArgsConstructor;
2555
+ import org.springframework.stereotype.Repository;
2556
+ import org.springframework.transaction.annotation.Transactional;
2557
+
2558
+ import java.math.BigDecimal;
2559
+ import java.util.Optional;
2560
+
2561
+ @Repository
2562
+ @RequiredArgsConstructor
2563
+ public class SupplyDetailRepositoryImpl implements SupplyDetailRepository {
2564
+
2565
+ private final SupplyDetailMapper mapper;
2566
+
2567
+ @Override
2568
+ @Transactional
2569
+ public void consumeQuantity(Integer id, BigDecimal consumedQuantity, Integer version) {
2570
+ int updatedCount = mapper.updateConsumedQuantityWithOptimisticLock(id, consumedQuantity, version);
2571
+
2572
+ if (updatedCount == 0) {
2573
+ // 更新失敗の原因を特定
2574
+ Integer currentVersion = mapper.findVersionById(id);
2575
+ if (currentVersion == null) {
2576
+ throw new OptimisticLockException("支給明細", id);
2577
+ }
2578
+
2579
+ BigDecimal remainingQuantity = mapper.findRemainingQuantityById(id);
2580
+ if (remainingQuantity.compareTo(consumedQuantity) < 0) {
2581
+ throw new InsufficientQuantityException(
2582
+ String.format("支給明細ID %d の残数量が不足しています(残数: %s, 要求: %s)",
2583
+ id, remainingQuantity, consumedQuantity));
2584
+ }
2585
+
2586
+ throw new OptimisticLockException("支給明細", id, version, currentVersion);
2587
+ }
2588
+ }
2589
+
2590
+ @Override
2591
+ public Optional<SupplyDetail> findById(Integer id) {
2592
+ return Optional.ofNullable(mapper.findById(id));
2593
+ }
2594
+
2595
+ // その他のメソッド...
2596
+ }
2597
+ ```
2598
+
2599
+ </details>
2600
+
2601
+ #### TDD: 楽観ロックのテスト
2602
+
2603
+ <details>
2604
+ <summary>SupplyDetailRepositoryOptimisticLockTest.java</summary>
2605
+
2606
+ ```java
2607
+ // src/test/java/com/example/pms/infrastructure/persistence/repository/SupplyDetailRepositoryOptimisticLockTest.java
2608
+ package com.example.pms.infrastructure.out.persistence.repository;
2609
+
2610
+ import com.example.pms.application.port.out.SupplyDetailRepository;
2611
+ import com.example.pms.application.port.out.SupplyRepository;
2612
+ import com.example.pms.domain.exception.InsufficientQuantityException;
2613
+ import com.example.pms.domain.exception.OptimisticLockException;
2614
+ import com.example.pms.domain.model.subcontract.Supply;
2615
+ import com.example.pms.domain.model.subcontract.SupplyDetail;
2616
+ import com.example.pms.domain.model.subcontract.SupplyType;
2617
+ import com.example.pms.testsetup.BaseIntegrationTest;
2618
+ import org.junit.jupiter.api.*;
2619
+ import org.springframework.beans.factory.annotation.Autowired;
2620
+
2621
+ import java.math.BigDecimal;
2622
+ import java.time.LocalDate;
2623
+
2624
+ import static org.assertj.core.api.Assertions.*;
2625
+
2626
+ @DisplayName("支給明細リポジトリ - 楽観ロック")
2627
+ class SupplyDetailRepositoryOptimisticLockTest extends BaseIntegrationTest {
2628
+
2629
+ @Autowired
2630
+ private SupplyRepository supplyRepository;
2631
+
2632
+ @Autowired
2633
+ private SupplyDetailRepository supplyDetailRepository;
2634
+
2635
+ @BeforeEach
2636
+ void setUp() {
2637
+ supplyRepository.deleteAll();
2638
+ }
2639
+
2640
+ @Nested
2641
+ @DisplayName("消費数量更新の楽観ロック")
2642
+ class ConsumeQuantityOptimisticLock {
2643
+
2644
+ @Test
2645
+ @DisplayName("同じバージョンで消費できる")
2646
+ void canConsumeWithSameVersion() {
2647
+ // Arrange
2648
+ var supply = createSupply("SUP-2025-0001");
2649
+ var detail = createSupplyDetail(supply.getSupplyNumber(), 1, new BigDecimal("100"));
2650
+
2651
+ // Act
2652
+ var fetched = supplyDetailRepository.findById(detail.getId()).get();
2653
+ supplyDetailRepository.consumeQuantity(
2654
+ fetched.getId(),
2655
+ new BigDecimal("30"),
2656
+ fetched.getVersion()
2657
+ );
2658
+
2659
+ // Assert
2660
+ var updated = supplyDetailRepository.findById(detail.getId()).get();
2661
+ assertThat(updated.getConsumedQuantity()).isEqualByComparingTo(new BigDecimal("30"));
2662
+ assertThat(updated.getRemainingQuantity()).isEqualByComparingTo(new BigDecimal("70"));
2663
+ assertThat(updated.getVersion()).isEqualTo(2);
2664
+ }
2665
+
2666
+ @Test
2667
+ @DisplayName("異なるバージョンで消費すると楽観ロック例外が発生する")
2668
+ void throwsExceptionWhenVersionMismatch() {
2669
+ // Arrange
2670
+ var supply = createSupply("SUP-2025-0002");
2671
+ var detail = createSupplyDetail(supply.getSupplyNumber(), 1, new BigDecimal("100"));
2672
+
2673
+ // ユーザーAが取得
2674
+ var detailA = supplyDetailRepository.findById(detail.getId()).get();
2675
+ // ユーザーBが取得
2676
+ var detailB = supplyDetailRepository.findById(detail.getId()).get();
2677
+
2678
+ // ユーザーAが消費(成功)
2679
+ supplyDetailRepository.consumeQuantity(
2680
+ detailA.getId(),
2681
+ new BigDecimal("30"),
2682
+ detailA.getVersion()
2683
+ );
2684
+
2685
+ // Act & Assert: ユーザーBが古いバージョンで消費(失敗)
2686
+ assertThatThrownBy(() ->
2687
+ supplyDetailRepository.consumeQuantity(
2688
+ detailB.getId(),
2689
+ new BigDecimal("40"),
2690
+ detailB.getVersion()
2691
+ ))
2692
+ .isInstanceOf(OptimisticLockException.class)
2693
+ .hasMessageContaining("他のユーザーによって更新されています");
2694
+ }
2695
+
2696
+ @Test
2697
+ @DisplayName("残数量を超える消費は残数量不足例外が発生する")
2698
+ void throwsExceptionWhenInsufficientQuantity() {
2699
+ // Arrange
2700
+ var supply = createSupply("SUP-2025-0003");
2701
+ var detail = createSupplyDetail(supply.getSupplyNumber(), 1, new BigDecimal("100"));
2702
+
2703
+ var fetched = supplyDetailRepository.findById(detail.getId()).get();
2704
+
2705
+ // Act & Assert
2706
+ assertThatThrownBy(() ->
2707
+ supplyDetailRepository.consumeQuantity(
2708
+ fetched.getId(),
2709
+ new BigDecimal("150"), // 残数量100を超える
2710
+ fetched.getVersion()
2711
+ ))
2712
+ .isInstanceOf(InsufficientQuantityException.class)
2713
+ .hasMessageContaining("残数量が不足しています");
2714
+ }
2715
+ }
2716
+
2717
+ private Supply createSupply(String supplyNumber) {
2718
+ var supply = Supply.builder()
2719
+ .supplyNumber(supplyNumber)
2720
+ .purchaseOrderNumber("PO-2025-0001")
2721
+ .lineNumber(1)
2722
+ .supplierCode("SUP-001")
2723
+ .supplyDate(LocalDate.of(2025, 1, 20))
2724
+ .supplierPersonCode("EMP-001")
2725
+ .supplyType(SupplyType.FREE)
2726
+ .build();
2727
+ supplyRepository.save(supply);
2728
+ return supply;
2729
+ }
2730
+
2731
+ private SupplyDetail createSupplyDetail(String supplyNumber, int lineNumber, BigDecimal quantity) {
2732
+ var detail = SupplyDetail.builder()
2733
+ .supplyNumber(supplyNumber)
2734
+ .lineNumber(lineNumber)
2735
+ .itemCode("MAT-001")
2736
+ .quantity(quantity)
2737
+ .unitPrice(new BigDecimal("500"))
2738
+ .amount(quantity.multiply(new BigDecimal("500")))
2739
+ .consumedQuantity(BigDecimal.ZERO)
2740
+ .remainingQuantity(quantity)
2741
+ .build();
2742
+ supplyDetailRepository.save(detail);
2743
+ return detail;
2744
+ }
2745
+ }
2746
+ ```
2747
+
2748
+ </details>
2749
+
2750
+ ### 消費処理における楽観ロックの考慮
2751
+
2752
+ 消費処理では、支給明細の残数量を更新するため、楽観ロックと業務制約(残数量チェック)を組み合わせた処理が必要です。
2753
+
2754
+ ```plantuml
2755
+ @startuml
2756
+
2757
+ title 消費処理と楽観ロック
2758
+
2759
+ participant "消費サービス" as ConsumeSvc
2760
+ participant "支給明細リポジトリ" as SDRepo
2761
+ participant "消費リポジトリ" as ConRepo
2762
+ database "DB" as DB
2763
+
2764
+ ConsumeSvc -> SDRepo: findById(supplyDetailId)
2765
+ SDRepo -> DB: SELECT ... WHERE ID = ?
2766
+ DB --> SDRepo: 支給明細(バージョン・残数量込み)
2767
+ SDRepo --> ConsumeSvc: SupplyDetail
2768
+
2769
+ ConsumeSvc -> ConsumeSvc: 残数量チェック
2770
+ alt 残数量 >= 消費数量
2771
+ ConsumeSvc -> ConRepo: save(consumption)
2772
+ ConRepo -> DB: INSERT INTO 消費データ
2773
+ DB --> ConRepo: OK
2774
+
2775
+ ConsumeSvc -> SDRepo: consumeQuantity(id, quantity, version)
2776
+ SDRepo -> DB: UPDATE ... WHERE ID = ? AND バージョン = ? AND 残数量 >= ?
2777
+ alt 更新成功
2778
+ DB --> SDRepo: 更新成功(1件)
2779
+ SDRepo --> ConsumeSvc: OK
2780
+ else バージョン不一致 or 残数量不足
2781
+ DB --> SDRepo: 更新失敗(0件)
2782
+ SDRepo --> ConsumeSvc: OptimisticLockException / InsufficientQuantityException
2783
+ ConsumeSvc -> ConsumeSvc: ロールバック
2784
+ end
2785
+ else 残数量 < 消費数量
2786
+ ConsumeSvc -> ConsumeSvc: InsufficientQuantityException
2787
+ end
2788
+
2789
+ @enduml
2790
+ ```
2791
+
2792
+ #### 消費サービスの実装
2793
+
2794
+ <details>
2795
+ <summary>ConsumptionService.java(楽観ロック対応)</summary>
2796
+
2797
+ ```java
2798
+ /**
2799
+ * 消費処理(楽観ロック対応)
2800
+ */
2801
+ @Transactional
2802
+ public Consumption processConsumption(ConsumptionRequest request) {
2803
+ // 支給明細を取得
2804
+ var supplyDetail = supplyDetailRepository.findById(request.getSupplyDetailId())
2805
+ .orElseThrow(() -> new IllegalArgumentException("支給明細が見つかりません"));
2806
+
2807
+ // 残数量の事前チェック(楽観的)
2808
+ if (!supplyDetail.canConsume(request.getConsumedQuantity())) {
2809
+ throw new InsufficientQuantityException(
2810
+ String.format("残数量が不足しています(残数: %s, 要求: %s)",
2811
+ supplyDetail.getRemainingQuantity(), request.getConsumedQuantity()));
2812
+ }
2813
+
2814
+ // 歩留率の計算
2815
+ var yieldRate = calculateYieldRate(
2816
+ request.getConsumedQuantity(),
2817
+ request.getProducedQuantity()
2818
+ );
2819
+
2820
+ // 消費データ作成
2821
+ var consumption = Consumption.builder()
2822
+ .consumptionNumber(generateConsumptionNumber())
2823
+ .supplyNumber(supplyDetail.getSupplyNumber())
2824
+ .receivingNumber(request.getReceivingNumber())
2825
+ .consumptionDate(request.getConsumptionDate())
2826
+ .build();
2827
+ consumptionRepository.save(consumption);
2828
+
2829
+ // 消費明細作成
2830
+ var consumptionDetail = ConsumptionDetail.builder()
2831
+ .consumptionNumber(consumption.getConsumptionNumber())
2832
+ .lineNumber(1)
2833
+ .supplyDetailId(supplyDetail.getId())
2834
+ .itemCode(supplyDetail.getItemCode())
2835
+ .consumedQuantity(request.getConsumedQuantity())
2836
+ .yieldRate(yieldRate)
2837
+ .build();
2838
+ consumptionDetailRepository.save(consumptionDetail);
2839
+
2840
+ // 支給明細の残数量を更新(楽観ロック)
2841
+ try {
2842
+ supplyDetailRepository.consumeQuantity(
2843
+ supplyDetail.getId(),
2844
+ request.getConsumedQuantity(),
2845
+ supplyDetail.getVersion()
2846
+ );
2847
+ } catch (OptimisticLockException e) {
2848
+ throw new ConcurrentUpdateException(
2849
+ "他のユーザーが同時に消費処理を行いました。画面を更新して再度お試しください。", e);
2850
+ } catch (InsufficientQuantityException e) {
2851
+ throw new ConcurrentUpdateException(
2852
+ "他のユーザーが先に消費処理を行ったため、残数量が不足しています。", e);
2853
+ }
2854
+
2855
+ return consumption;
2856
+ }
2857
+
2858
+ private BigDecimal calculateYieldRate(BigDecimal consumed, BigDecimal produced) {
2859
+ if (consumed.compareTo(BigDecimal.ZERO) == 0) {
2860
+ return BigDecimal.ZERO;
2861
+ }
2862
+ return produced.divide(consumed, 4, RoundingMode.HALF_UP)
2863
+ .multiply(new BigDecimal("100"));
2864
+ }
2865
+ ```
2866
+
2867
+ </details>
2868
+
2869
+ #### 楽観ロックのベストプラクティス(外注委託向け)
2870
+
2871
+ | ポイント | 説明 |
2872
+ |---------|------|
2873
+ | **残数量と楽観ロックの組み合わせ** | WHERE 条件で両方をチェック |
2874
+ | **エラー原因の特定** | バージョン不一致か残数量不足かを判別 |
2875
+ | **歩留率の記録** | 消費時に歩留率を計算・記録 |
2876
+ | **有償/無償支給の区別** | 会計処理が異なるため支給区分を保持 |
2877
+ | **トランザクション境界** | 消費→消費明細→支給明細更新を1トランザクションで |
2878
+
2879
+ ---
2880
+
2881
+ ## 26.6 まとめ
2882
+
2883
+ 本章では、外注委託管理(支給から消費まで)の DB 設計と実装について学びました。
2884
+
2885
+ ### 学んだこと
2886
+
2887
+ 1. **支給業務の設計**
2888
+ - 支給データと支給明細データの親子関係
2889
+ - 有償支給と無償支給の区別
2890
+ - 発注明細との紐付け
2891
+
2892
+ 2. **消費業務の設計**
2893
+ - 入荷時の消費記録
2894
+ - 歩留まり率の計算
2895
+ - 支給数量を超えない消費のバリデーション
2896
+
2897
+ 3. **命名規則のパターン**
2898
+ - **DB(日本語)**: テーブル名、カラム名、ENUM 型・値は日本語
2899
+ - **Java(英語)**: クラス名、フィールド名、enum 値は英語
2900
+ - **MyBatis resultMap**: 日本語カラムと英語プロパティのマッピング
2901
+ - **TypeHandler**: 日本語 ENUM 値と英語 enum の相互変換
2902
+
2903
+ ### テーブル一覧
2904
+
2905
+ | テーブル名(日本語) | Java エンティティ | 説明 |
2906
+ |---|---|---|
2907
+ | 支給データ | Supply | 外注先への支給情報 |
2908
+ | 支給明細データ | SupplyDetail | 支給品目の明細 |
2909
+ | 消費データ | Consumption | 支給品の消費報告 |
2910
+ | 消費明細データ | ConsumptionDetail | 消費品目の明細 |
2911
+
2912
+ ### ENUM 一覧
2913
+
2914
+ | DB ENUM 型(日本語) | Java Enum | 値 |
2915
+ |---|---|---|
2916
+ | 支給区分 | SupplyType | 有償支給→PAID, 無償支給→FREE |