# Business Logic — JBSbatKKKjFinClDataInTrn.updateKktkSvcKeiDslTv() [68 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `eo.business.service.JBSbatKKKjFinClDataInTrn` |
| Layer | Service (Package: `eo.business.service`) |
| Module | `service` (Package: `eo.business.service`) |

## 1. Role

### JBSbatKKKjFinClDataInTrn.updateKktkSvcKeiDslTv()

This method performs the business operation of cancelling (terminating) an Equipment Provision Service Contract (TV). Specifically, it processes the contract cancellation update for TV (Television) service contracts that are provisioned to customers with rented equipment. The method follows a two-phase pattern: first executing optimistic-exclusive-lock select-then-update, then persisting four field-level updates to the contract table — marking the contract status as "Cancelled" (910), recording the operational date as the cancellation date, setting the contract application code to "Processing" (1), and flipping the invalid flag to "Invalid" (1). It acts as a domain-specific service method within the equipment provisioning subsystem, handling the complete lifecycle transition from "active" to "cancelled" for a single contract identified by its unique contract number and generation timestamp. The method is called internally by the batch `execute()` method of the same class, indicating it is a sub-routine rather than a screen-facing entry point.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["updateKktkSvcKeiDslTv kktkSvcKeiNo geneAddDtm"])

    INIT["Initialize JBSbatCommonDBInterface instances"]

    WH_MAP_SET["where_map setValue KKTK_SVC_KEI_NO kktkSvcKeiNo"]
    GENE_MAP_SET["where_map setValue GENE_ADD_DTM geneAddDtm"]

    SELECT["db_KK_T_KKTK_SVC_KEI selectByPrimaryKeysForUpdateWait where_map"]

    COND{map not null?}

    ARR_INIT["Allocate value and param arrays"]

    V0_SET["value[0] = 910 Cancelled status"]
    V1_SET["value[1] = this.opeDate Cancellation date"]
    V2_SET["value[2] = 1 Contract Processing code"]
    V3_SET["value[3] = 1 Invalid flag"]

    P0_SET["param[0] = kktkSvcKeiNo Contract number"]
    P1_SET["param[1] = geneAddDtm Timestamp"]

    DEBUG_CHK{DEBUG log level?}

    DLOG1["printDebugLog param contract number"]
    DLOG2["printDebugLog param timestamp"]
    DLOG3["printDebugLog value status 910"]
    DLOG4["printDebugLog value cancellation date"]
    DLOG5["printDebugLog value application code"]
    DLOG6["printDebugLog value invalid flag"]

    EXEC_UPDATE["executeKK_T_KKTK_SVC_KEI_PKUPDATE2 value param"]

    ELSE_BRANCH["throw JBSbatBusinessException EKKB0210CE Record not found"]

    END_NODE["Return next"]

    START --> INIT --> WH_MAP_SET --> GENE_MAP_SET --> SELECT --> COND
    COND --> true_branch["true"]
    COND --> false_branch["false"]
    true_branch --> ARR_INIT
    ARR_INIT --> V0_SET --> V1_SET --> V2_SET --> V3_SET --> P0_SET --> P1_SET --> DEBUG_CHK
    DEBUG_CHK -->|true| DLOG1 --> DLOG2 --> DLOG3 --> DLOG4 --> DLOG5 --> DLOG6 --> EXEC_UPDATE
    DEBUG_CHK -->|false| EXEC_UPDATE
    EXEC_UPDATE --> END_NODE
    false_branch --> ELSE_BRANCH
```

**Constant Resolution:**

| Constant | Resolved Value | Business Meaning |
|----------|---------------|-----------------|
| `JBSbatKKConst.CD00056_KKTK_SVC_KEI_STAT_910` | `"910"` | Contract Status — Cancelled (解約済) |
| `JBSbatKKConst.RSV_APLY_CD_TTDK` | `"1"` | Reservation Application Code — Contract Processing (予約手続中) |
| `JBSbatKKConst.MK_FLG_MK` | `"1"` | Invalid Flag — Invalid (無効) |

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `kktkSvcKeiNo` | `String` | Equipment Provision Service Contract Number (機器提供サービス契約番号) — the unique identifier for a TV equipment provision contract record. Used as the primary key (WHERE clause) to locate the exact contract row to cancel. The value is padded to 12 characters with spaces in the underlying SQL. |
| 2 | `geneAddDtm` | `String` | Generation Registration Timestamp (世代登録年月日時分秒) — the generation timestamp used as a secondary primary key for optimistic concurrency control. Ensures the update targets the most recent version of the contract record, preventing stale-data overwrites in batch processing. |

**Instance fields read:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `this.opeDate` | `String` | Operational date (予約取消年月日) — the system operation date set as the cancellation date on the contract record |

## 4. CRUD Operations / Called Services

### Pre-computed evidence from code analysis graph:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `db_KK_T_KKTK_SVC_KEI.selectByPrimaryKeysForUpdateWait` | - | `KK_T_KKTK_SVC_KEI` | SELECT FOR UPDATE — retrieves the target contract row with an exclusive lock for optimistic concurrency control (排他検索) |
| U | `executeKK_T_KKTK_SVC_KEI_PKUPDATE2` | - | `KK_T_KKTK_SVC_KEI` | UPDATE by primary key — updates 4 fields: contract status to "910", cancellation date, application code to "1", and invalid flag to "1" (where clause: contract number + timestamp) |
| - | `JBSbatCommonDBInterface.setValue` | - | - | Sets key-value pairs in the in-memory data interface for WHERE clause and SET clause construction |
| - | `JBSbatCommonDBInterface.<init>` | - | - | Instantiates new data interface maps for query conditions and results |
| - | `logPrint.chkLogLevel` | - | - | Checks if DEBUG-level logging is enabled |
| - | `logPrint.printDebugLog` | - | - | Emits debug log lines for param and value arrays |
| - | `JBSbatBusinessException.<init>` | - | - | Constructs business exception with error code `EKKB0210CE` and localized message arguments |

### Detailed CRUD Analysis:

**Step 1 — SELECT FOR UPDATE (R):**
- `db_KK_T_KKTK_SVC_KEI.selectByPrimaryKeysForUpdateWait(where_map)`
- Entity/DB: `KK_T_KKTK_SVC_KEI` (Equipment Provision Service Contract table)
- WHERE: `KKTK_SVC_KEI_NO = ? AND GENE_ADD_DTM = ?`
- Purpose: 排他検索 (exclusive search) — acquires a row-level lock to prevent concurrent updates. If no record matches, returns `null`.

**Step 2 — UPDATE (U):**
- `executeKK_T_KKTK_SVC_KEI_PKUPDATE2(value, param)` (private method, lines 1377–1396)
- Entity/DB: `KK_T_KKTK_SVC_KEI`
- SET: `KKTK_SVC_KEI_STAT = '910'`, `RSV_CL_YMD = opeDate`, `RSV_APLY_CD = '1'`, `MK_FLG = '1'`
- WHERE: `KKTK_SVC_KEI_NO = ? AND GENE_ADD_DTM = ?` (using same primary keys as the SELECT)
- Terminal operation: `db_KK_T_KKTK_SVC_KEI.updateByPrimaryKeys(whereMap, setMap)`

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: JBSbatKKKjFinClDataInTrn | `JBSbatKKKjFinClDataInTrn.execute()` → `JBSbatKKKjFinClDataInTrn.updateKktkSvcKeiDslTv(kktkSvcKeiNo, geneAddDtm)` | `executeKK_T_KKTK_SVC_KEI_PKUPDATE2 [U] KK_T_KKTK_SVC_KEI` |

**Terminal operations from this method:**
- `executeKK_T_KKTK_SVC_KEI_PKUPDATE2 [U] KK_T_KKTK_SVC_KEI` — Updates contract fields (status, cancellation date, application code, invalid flag)
- `printDebugLog [-]` — Debug logging calls (multiple, for param and value arrays)

**Call chain explanation:**
The batch entry point `JBSbatKKKjFinClDataInTrn.execute()` iterates over target records and invokes `updateKktkSvcKeiDslTv()` for each equipment provision service contract that needs cancellation processing. This method is not exposed to screens — it is purely a batch-processing service method called within its own class.

## 6. Per-Branch Detail Blocks

### Block 1 — [VARIABLE DECLARATION] (L2418–L2424)

> Initializes local variables: two `JBSbatCommonDBInterface` objects for the result map and WHERE clause map, and two null String arrays for later use.

| # | Type | Code |
|---|------|------|
| 1 | SET | `JBSbatCommonDBInterface map = new JBSbatCommonDBInterface();` // Result container |
| 2 | SET | `JBSbatCommonDBInterface where_map = new JBSbatCommonDBInterface();` // WHERE clause map |
| 3 | SET | `String[] value = null;` // SET clause value array (4 elements) |
| 4 | SET | `String[] param = null;` // WHERE clause parameter array (2 elements) |

### Block 2 — [SET] WHERE Clause Construction (L2426–L2427)

> Populates the WHERE clause map with the primary key fields to identify the target contract record.

| # | Type | Code |
|---|------|------|
| 1 | SET | `where_map.setValue(KKTK_SVC_KEI_NO, kktkSvcKeiNo)` // Contract number |
| 2 | SET | `where_map.setValue(GENE_ADD_DTM, geneAddDtm)` // Generation timestamp |

### Block 3 — [CALL — SELECT FOR UPDATE] (L2429–L2430)

> Executes an exclusive row-level lock query. 排他検索を行います (Performs exclusive search). If the record exists, returns the populated map; otherwise returns `null`.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `map = db_KK_T_KKTK_SVC_KEI.selectByPrimaryKeysForUpdateWait(where_map)` // SELECT FOR UPDATE |

### Block 4 — [IF] Record Existence Check (L2432)

> 排他検索結果がある場合 (If exclusive search result exists) — verifies the contract record was found.

| # | Type | Code |
|---|------|------|
| 1 | COND | `if (map != null)` // Record found? |

#### Block 4.1 — [IF-TRUE] Array Allocation and Value Setup (L2434–L2452)

> Allocates value and param arrays, sets the 4 update values and 2 WHERE parameters, conditionally emits debug logs, and executes the update.

| # | Type | Code |
|---|------|------|
| 1 | SET | `value = new String[4];` // Initialize SET clause array |
| 2 | SET | `param = new String[2];` // Initialize WHERE parameter array |
| 3 | SET | `value[0] = CD00056_KKTK_SVC_KEI_STAT_910 = "910"` // [-> KKTK_SVC_KEI_STAT = "910"] (Cancelled status: 解約済) |
| 4 | SET | `value[1] = this.opeDate` // [-> RSV_CL_YMD = opeDate] (Reservation Cancellation Date: 予約取消年月日) |
| 5 | SET | `value[2] = RSV_APLY_CD_TTDK = "1"` // [-> RSV_APLY_CD = "1"] (Contract Processing: 予約手続中) |
| 6 | SET | `value[3] = MK_FLG_MK = "1"` // [-> MK_FLG = "1"] (Invalid Flag: 無効) |
| 7 | SET | `param[0] = kktkSvcKeiNo` // Contract Number: 機器提供サービス契約番号 |
| 8 | SET | `param[1] = geneAddDtm` // Timestamp: 世代登録年月日時分秒 |

##### Block 4.1.1 — [IF] DEBUG Log Level Check (L2454)

> ログレベルがデバッグモードの場合 (If log level is DEBUG mode) — conditionally logs all 6 parameter/value pairs for troubleshooting.

| # | Type | Code |
|---|------|------|
| 1 | COND | `if (super.logPrint.chkLogLevel(JBSbatLogUtil.MODE_DEBUG))` // DEBUG logging enabled? |

###### Block 4.1.1.1 — [IF-TRUE] Debug Logging (L2456–L2461)

> Emits 6 debug log lines documenting the contract number, timestamp, and all 4 update values.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `super.logPrint.printDebugLog("param(機器提供サービス契約番号): " + param[0])` // Logs contract number |
| 2 | EXEC | `super.logPrint.printDebugLog("param(世代登録年月日時分秒): " + param[1])` // Logs timestamp |
| 3 | EXEC | `super.logPrint.printDebugLog("value(機器提供サービス契約ステータス(910:解約済)): " + value[0])` // Logs status 910 |
| 4 | EXEC | `super.logPrint.printDebugLog("value(予約取消年月日(運行日設定)): " + value[1])` // Logs cancellation date |
| 5 | EXEC | `super.logPrint.printDebugLog("value(予約適用コード(1:予約手続中)): " + value[2])` // Logs application code 1 |
| 6 | EXEC | `super.logPrint.printDebugLog("value(予無効フラグ(1:無効)): " + value[3])` // Logs invalid flag 1 |

##### Block 4.1.2 — [CALL — UPDATE] (L2465)

> Executes the database update by primary key with the prepared SET values and WHERE parameters.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeKK_T_KKTK_SVC_KEI_PKUPDATE2(value, param)` // UPDATE 4 fields on KK_T_KKTK_SVC_KEI |

### Block 5 — [ELSE] Record Not Found — Exception (L2469–L2472)

> 排他エラーの場合 / 該当レコード無しエラー (Exclusive error / Record not found error) — if the SELECT FOR UPDATE returns null (no matching contract record), throws a business exception with error code `EKKB0210CE` and a message identifying "Equipment Provision Service Contract" and the specific contract number.

| # | Type | Code |
|---|------|------|
| 1 | THROW | `throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0210CE, new String[]{"機器提供サービス契約", "機器提供サービス契約番号" + kktkSvcKeiNo})` // Error: contract record not found |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `KKTK_SVC_KEI_NO` | Field | Equipment Provision Service Contract Number (機器提供サービス契約番号) — primary key identifying a specific TV equipment contract |
| `GENE_ADD_DTM` | Field | Generation Registration Timestamp (世代登録年月日時分秒) — timestamp for optimistic concurrency control and version tracking |
| `KKTK_SVC_KEI_STAT` | Field | Equipment Provision Service Contract Status (機器提供サービス契約ステータス) — current status code of the contract |
| `RSV_CL_YMD` | Field | Reservation Cancellation Date (予約取消年月日) — the date the contract cancellation was processed |
| `RSV_APLY_CD` | Field | Reservation Application Code (予約適用コード) — application processing state code |
| `MK_FLG` | Field | Invalid Flag (無効フラグ) — soft-delete marker; "1" indicates the record is invalid/deactivated |
| `opeDate` | Field | Operational Date (運行日設定) — the batch system's operational date, used as the cancellation date in updates |
| `CD00056_KKTK_SVC_KEI_STAT_910` | Constant | Contract Status Code "910" — Cancelled (解約済) |
| `RSV_APLY_CD_TTDK` | Constant | Reservation Application Code "1" — Contract Processing (予約手続中) |
| `MK_FLG_MK` | Constant | Invalid Flag "1" — Invalid (無効) |
| `EKKB0210CE` | Constant | Error Code — Record Not Found business exception |
| `KK_T_KKTK_SVC_KEI` | Entity/DB Table | Equipment Provision Service Contract table (機器提供サービス契約テーブル) — stores TV equipment contract records |
| `selectByPrimaryKeysForUpdateWait` | Method | SELECT FOR UPDATE — acquires a row-level exclusive lock during read |
| `updateByPrimaryKeys` | Method | UPDATE by primary key — updates specific fields while matching on all primary key columns |
| TV | Business term | Television service — a television broadcast service provisioned with rented equipment to customers |
| 機器提供サービス契約 | Business term | Equipment Provision Service Contract — the contract between the provider and customer for rented TV equipment service |
| 解約更新処理 | Business term | Cancellation Update Processing — the business process of updating a contract record to mark it as cancelled |
| 排他制御 | Business term | Exclusive Control (Concurrency Control) — mechanism to prevent concurrent updates to the same record using SELECT FOR UPDATE |
| JBSbatBusinessException | Class | Business Exception — throws structured business-level errors with codes and localized messages |
| JBSbatCommonDBInterface | Class | Common Database Interface — in-memory key-value map for constructing SQL SET and WHERE clauses |
