# Business Logic — JBSbatKKMiStcKikiInfStku.searchKktk2() [43 LOC]

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

## 1. Role

### JBSbatKKMiStcKikiInfStku.searchKktk2()

The `searchKktk2` method performs a two-tier lookup of an **equipment provision service contract** (機器提供サービス契約) in the batch processing system. It implements a fallback strategy: first attempting to find a contract record in a reservation state (contract change in progress, status code "110" with reservation application code `RSV_APLY_CD = '1'`), and if no such record exists, falling back to retrieving the current (active) contract record. This method is specifically used for **STB** (Set-Top Box) and **PLC** (Programmable Logic Controller) equipment provisioning workflows.

The method acts as a shared internal utility within the equipment provision service class, implementing the **strategy/dispatch pattern** — it routes between two distinct database query modes depending on whether a pending reservation record is available. Its role in the larger system is to provide callers with the most up-to-date contract information while respecting the business rule that reservation-stage records take precedence over current records during contract modification operations.

If the service contract number (`svc_no`) is null or empty, the method returns null early (graceful no-op). If both query attempts yield no results, it throws a `JBSbatBusinessException` with message key `EKKB0210CE`, bubbling up to the caller for error handling.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["Start: searchKktk2(svc_no, svc_cd, err_val)"])
    INIT(["Initialize: db_map = null, param[3]"])

    CHECK_SVC_NO{"svc_no is null or empty?"}
    EARLY_RETURN(["Return null (no service contract number)"])

    SET_PARAM_1["Set param[0] = svc_no, param[1] = svc_cd"]
    EXEC_SELECT_240["Call executeKK_T_KKTK_SVC_KEI_KK_SELECT_240(param)"]
    SELECT_NEXT_1["db_map = db_KK_T_KKTK_SVC_KEI2.selectNext()"]

    CHECK_FOUND{"db_map is not null?"}
    RESERVE_RETURN(["Return db_map (reservation record)"])

    SET_PARAM_2["Set param[0] = svc_no, param[1] = svc_cd, param[2] = super.opeDate"]
    EXEC_SELECT_241["Call executeKK_T_KKTK_SVC_KEI_KK_SELECT_241(param)"]
    SELECT_NEXT_2["db_map = db_KK_T_KKTK_SVC_KEI2.selectNext()"]

    CHECK_FINAL{"db_map is null?"}
    THROW_ERROR(["Throw JBSbatBusinessException: KK_T_KKTK_SVC_KEI not found"])

    FINAL_RETURN(["Return db_map (current contract record)"])

    START --> INIT
    INIT --> CHECK_SVC_NO
    CHECK_SVC_NO -->|"Yes"| EARLY_RETURN
    CHECK_SVC_NO -->|"No"| SET_PARAM_1
    SET_PARAM_1 --> EXEC_SELECT_240
    EXEC_SELECT_240 --> SELECT_NEXT_1
    SELECT_NEXT_1 --> CHECK_FOUND
    CHECK_FOUND -->|"Yes"| RESERVE_RETURN
    CHECK_FOUND -->|"No"| SET_PARAM_2
    SET_PARAM_2 --> EXEC_SELECT_241
    EXEC_SELECT_241 --> SELECT_NEXT_2
    SELECT_NEXT_2 --> CHECK_FINAL
    CHECK_FINAL -->|"Yes"| THROW_ERROR
    CHECK_FINAL -->|"No"| FINAL_RETURN
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `svc_no` | `String` | Equipment provision service contract number — the unique identifier for a service contract line item. If null or empty, the method returns null immediately (no-op). |
| 2 | `svc_cd` | `String` | Equipment provision service code — classifies the type of equipment provision service (e.g., STB, PLC). Used alongside `svc_no` to narrow the contract search. |
| 3 | `err_val` | `String` | Error log output text — supplementary text included in the error message when both database lookups fail. Used to contextualize which contract number failed to resolve. |

**Instance fields / external state read:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `db_KK_T_KKTK_SVC_KEI2` | `JBSbatSQLAccess` | Database access object for the `KK_T_KKTK_SVC_KEI` (Equipment Provision Service Contract) table. Initialized in the constructor with the table name constant `D_TBL_NAME_KK_T_KKTK_SVC_KEI`. |
| `super.opeDate` | `String` (inherited) | Operating date — the batch processing date used as a filter criterion in the current-record query (SELECT_241). Represents the as-of date for selecting the active contract. |

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `JBSbatKKMiStcKikiInfStku.executeKK_T_KKTK_SVC_KEI_KK_SELECT_240` | - | `KK_T_KKTK_SVC_KEI` | Calls `executeKK_T_KKTK_SVC_KEI_KK_SELECT_240` in `JBSbatKKMiStcKikiInfStku` — queries for reservation records (status "110" / contract change in progress, RSV_APLY_CD = '1') |
| R | `JBSbatKKMiStcKikiInfStku.executeKK_T_KKTK_SVC_KEI_KK_SELECT_241` | - | `KK_T_KKTK_SVC_KEI` | Calls `executeKK_T_KKTK_SVC_KEI_KK_SELECT_241` in `JBSbatKKMiStcKikiInfStku` — queries for current (active) contract records as a fallback |

**Detailed breakdown:**

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `executeKK_T_KKTK_SVC_KEI_KK_SELECT_240` | (internal) | `KK_T_KKTK_SVC_KEI` | Reservation record query — retrieves the most recent equipment provision service contract record where status = "110" (contract change in progress) and RSV_APLY_CD = '1'. Takes `svc_no` and `svc_cd` as bind parameters. Executes SQL key `KK_T_KKTK_SVC_KEI_KK_SELECT_240`. |
| R | `executeKK_T_KKTK_SVC_KEI_KK_SELECT_241` | (internal) | `KK_T_KKTK_SVC_KEI` | Current record query (fallback) — retrieves the current (active) equipment provision service contract record for the given `svc_no` and `svc_cd` as of `opeDate`. Takes `svc_no`, `svc_cd`, and `opeDate` as bind parameters. Executes SQL key `KK_T_KKTK_SVC_KEI_KK_SELECT_241`. |
| R | `db_KK_T_KKTK_SVC_KEI2.selectNext()` | (internal) | `KK_T_KKTK_SVC_KEI` | Fetches the next row from the result set populated by the preceding `selectBySqlDefine` call. Called twice — once after each SELECT_240 and SELECT_241 query. |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Method: `JBSbatKKMiStcKikiInfStku.searchKktk()` | `searchKktk()` -> `searchKktk2()` | `executeKK_T_KKTK_SVC_KEI_KK_SELECT_241 [R] KK_T_KKTK_SVC_KEI`, `executeKK_T_KKTK_SVC_KEI_KK_SELECT_240 [R] KK_T_KKTK_SVC_KEI` |

**Terminal operations reachable from this method:**

| Terminal | Type | Entity / DB | Description |
|----------|------|-------------|-------------|
| `executeKK_T_KKTK_SVC_KEI_KK_SELECT_240` | Read | `KK_T_KKTK_SVC_KEI` | Queries reservation-stage equipment provision service contract records |
| `executeKK_T_KKTK_SVC_KEI_KK_SELECT_241` | Read | `KK_T_KKTK_SVC_KEI` | Queries current (active) equipment provision service contract records |

**Call Chain Details:**

- **`JBSbatKKMiStcKikiInfStku.searchKktk()`** — The sole caller of `searchKktk2`. This method delegates to `searchKktk2` by passing through `svc_no`, `svc_cd`, and `err_val` parameters. The calling code at line 897 shows: `return searchKktk2(svc_no, svc_cd, err_val);`

## 6. Per-Branch Detail Blocks

**Block 1** — [ASSIGNMENT] (L950-L951)

> Initialize local variables for the search operation.

| # | Type | Code |
|---|------|------|
| 1 | SET | `db_map = null` // Initialize return variable |
| 2 | SET | `param = new String[3]` // Bind parameter array (3 slots) |

**Block 2** — [IF] `(svc_no is null or empty)` (L954-L957)

> Service contract number validation guard. Japanese comment: 機器提供サービス契約番号チェック (Equipment provision service contract number check). Returns null immediately if no contract number is provided, treating it as a no-op rather than an error.

| # | Type | Code |
|---|------|------|
| 1 | SET | `db_map = null` // Return value already null |
| 2 | RETURN | `return db_map` // Returns null — early exit when svc_no is null or empty |

**Block 3** — [ASSIGNMENT] (L962-L963)

> Prepare bind parameters for the reservation record query. Japanese comment: 指定された機器提供サービス契約番号、機器提供サービスコードで、機器提供サービス契約ステータスが"110"（契約変更中）、RSV_APLY_CD = '1'の世代最新を取得します。 (With the specified equipment provision service contract number and service code, acquire the latest generation record where the service contract status is "110" (contract change in progress) and RSV_APLY_CD = '1'.)

| # | Type | Code |
|---|------|------|
| 1 | SET | `param[0] = svc_no` // Equipment provision service contract number |
| 2 | SET | `param[1] = svc_cd` // Equipment provision service code |

**Block 4** — [CALL] `executeKK_T_KKTK_SVC_KEI_KK_SELECT_240` (L965)

> Execute reservation record query. Japanese comment: (See above — this query searches for records in contract change-in-progress status).

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeKK_T_KKTK_SVC_KEI_KK_SELECT_240(param)` // Executes SQL key KK_T_KKTK_SVC_KEI_KK_SELECT_240 |

**Block 5** — [CALL] `selectNext()` (L966)

> Fetch the next row from the result set.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `db_map = db_KK_T_KKTK_SVC_KEI2.selectNext()` // Fetches reservation record from result set |

**Block 6** — [IF] `(db_map != null)` (L969-L972)

> Reservation record found branch. Japanese comment: 検索できた場合、返却します。 (When a record is found, return it.) If the reservation query returned a record, this is returned immediately — reservation-stage records take priority over current records.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return db_map` // Returns the reservation record |

**Block 7** — [ASSIGNMENT] (L976-L979)

> Fallback: prepare bind parameters for current record query. Japanese comment: 指定された機器提供サービス契約番号、機器提供サービスコードで、通常のカレントを取得します。 (With the specified equipment provision service contract number and service code, acquire the normal current record.)

| # | Type | Code |
|---|------|------|
| 1 | SET | `param[0] = svc_no` // Equipment provision service contract number |
| 2 | SET | `param[1] = svc_cd` // Equipment provision service code |
| 3 | SET | `param[2] = super.opeDate` // Batch processing date (as-of date for current record) |

**Block 8** — [CALL] `executeKK_T_KKTK_SVC_KEI_KK_SELECT_241` (L982)

> Execute current record query (fallback).

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeKK_T_KKTK_SVC_KEI_KK_SELECT_241(param)` // Executes SQL key KK_T_KKTK_SVC_KEI_KK_SELECT_241 |

**Block 9** — [CALL] `selectNext()` (L983)

> Fetch the next row from the current record result set.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `db_map = db_KK_T_KKTK_SVC_KEI2.selectNext()` // Fetches current record from result set |

**Block 10** — [IF] `(db_map == null)` (L986-L990)

> Record not found error handling branch. Japanese comment: 検索結果なしの場合、エラーと処理中断します。 (When no search result, treat as error and abort processing.) Throws a business exception with message key `EKKB0210CE` and includes the table name `KK_T_KKTK_SVC_KEI` plus a formatted error value string.

| # | Type | Code |
|---|------|------|
| 1 | SET | `value = "機器提供サービス契約番号（" + err_val + "）：" + svc_no + ", 機器提供サービスコード=" + svc_cd` // Error message: "Equipment provision service contract number (err_val): svc_no, Equipment provision service code=svc_cd" |
| 2 | EXEC | `throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0210CE, new String[]{"KK_T_KKTK_SVC_KEI", value})` // Throws business exception |

**Block 11** — [RETURN] (L991)

> Successful current record return.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return db_map` // Returns the current contract record |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `svc_no` | Field | Service contract number — unique identifier for an equipment provision service contract line item. |
| `svc_cd` | Field | Service code — classifies the type of equipment provision service (e.g., STB, PLC). |
| `err_val` | Field | Error value — supplementary text appended to error log messages for contextual identification of the failing contract. |
| `kk_svc_kei_no` | Field | Equipment provision service contract number — internal ID for a service contract (shortened as KK = 機器提供). |
| STB | Business term | Set-Top Box — cable television / broadband equipment used for service delivery. |
| PLC | Business term | Programmable Logic Controller — networking equipment used for service provisioning. |
| RSV_APLY_CD | Field | Reservation Application Code — a flag indicating a record is in reservation/pending state. Value '1' means "reserved application." |
| `opeDate` | Field | Operating Date — the batch processing date used as an as-of filter for retrieving active/current records. |
| `KK_T_KKTK_SVC_KEI` | Entity / Table | Equipment Provision Service Contract Table — the database table storing equipment provision service contract records. |
| `db_KK_T_KKTK_SVC_KEI2` | Field | SQL Access Object — a database access wrapper for the `KK_T_KKTK_SVC_KEI` table, initialized in the class constructor. |
| `JBSbatSQLAccess` | Class | SQL Access Base Class — provides `selectBySqlDefine()` and `selectNext()` methods for executing SQL queries defined in SQL definition files. |
| `JBSbatCommonDBInterface` | Interface | Common Database Interface — the interface returned by SQL access methods, representing a row of query results as a key-value map. |
| `JBSbatBusinessException` | Class | Business Exception — thrown when a business rule violation occurs (e.g., record not found). Carries message keys and parameterized error values. |
| `EKKB0210CE` | Constant | Error message key — indicates "record not found" in the `KK_T_KKTK_SVC_KEI` table. Includes table name and contextual value in its message parameters. |
| `KK_T_KKTK_SVC_KEI_KK_SELECT_240` | SQL Key | SQL definition key for querying reservation-stage contract records (status "110" / contract change in progress). |
| `KK_T_KKTK_SVC_KEI_KK_SELECT_241` | SQL Key | SQL definition key for querying current (active) contract records as of the operating date. |
| 機器提供サービス契約 | Japanese term | Equipment Provision Service Contract — the business entity representing a contract for providing equipment (STB, PLC) to a customer. |
| 契約変更中 | Japanese term | Contract Change in Progress — a status state indicating a service contract modification is pending (status code "110"). |
| カレントレコード | Japanese term | Current Record — the active/standard contract record (as opposed to a reservation/pending-stage record). |
| 予約中 | Japanese term | Reservation Period — the state where a record is reserved for change, representing a pending or in-progress operation. |
| バインド変数 | Japanese term | Bind Variable — a parameter value substituted into a SQL query at execution time to prevent injection and enable query plan caching. |
