# Business Logic — JBSbatKKCourseChgeTstaDayChsht.selectKktKjFinWk() [94 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `eo.business.service.JBSbatKKCourseChgeTstaDayChsht` |
| Layer | Service (Batch processing service layer) |
| Module | `service` (Package: `eo.business.service`) |

## 1. Role

### JBSbatKKCourseChgeTstaDayChsht.selectKktKjFinWk()

This method performs a **construction completion work query** (工事完了ワーク取得), which retrieves and interprets records from the `KK_T_KJ_FIN_WK` (Construction Completion Work) table for a given service contract and construction case. Its primary business purpose is to determine the current status of a construction order — whether it has been completed, corrected, or cancelled — so that downstream batch processing can branch accordingly. The method executes a parameterized SQL query keyed by service contract number (`svcKeiNo`) and construction case number (`kojiakNo`), then iterates through all matching result rows to evaluate two key fields: the new/change division code (`NEW_CHGE_CD`) and the construction case implementation date (`KOJIAK_JSSI_YMD`). Based on these fields, it assigns a `kojiRslt` (work result) value of "1" (Work Completed), "2" (Work Corrected), or "3" (Work Completion Cancelled). Additionally, as of version 20.00.01, the method returns a `kojiFinClInfFlag` (construction completion/cancellation information presence flag) set to `true` whenever at least one matching record exists in the query results, enabling callers to distinguish between "no records found" and "records found but all were processed." The method follows a **routing/dispatch design pattern**, where the constant-based branching logic routes each row to the appropriate work result classification. It serves as a shared utility called by the parent class's `execute()` method during batch processing of course change status day changes.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["selectKktKjFinWk(outmap, svcKeiNo, kojiakNo)"]) --> INIT["Initialize: kojiakJssiYmd = \"\", kojiRslt = \"\", kojiFinClInfFlag = false"]
    INIT --> WHERE["Build whereKjFinParam = {svcKeiNo, kojiakNo}"]
    WHERE --> EXEC[\"executeKK_T_KJ_FIN_WK_KK_SELECT_007(whereKjFinParam)\"]
    EXEC --> SELECT[\"kktKjFinWkMap_007 = db_KK_T_KJ_FIN_WK.selectNext()\"]
    SELECT --> COND_NULL{\"null != kktKjFinWkMap_007?\"}
    COND_NULL -->|false| SET_OUT["outmap.setString(KOJI_RSLT, kojiRslt = \"\")"]
    COND_NULL -->|true| SET_FLAG["kojiFinClInfFlag = true"]
    SET_FLAG --> WHILE_START{\"null != kktKjFinWkMap_007?\"}
    WHILE_START -->|false| SET_OUT
    WHILE_START -->|true| GET_FIELDS["Get newChgeCd, kojiakJssiYmd from row"]
    GET_FIELDS --> COND_NEW{\"JBSbatKKConst.KKIFI023_NEWCHG_DIV_NEW == newChgeCd?\"}
    COND_NEW -->|true| SET_RSLT1["kojiRslt = KKIFM151_KOJI_RSLT_1 = \"1\" (Work Completed)"]
    SET_RSLT1 --> NEXT_ROW
    COND_NEW -->|false| COND_CHG_DATE{\"KKIFI023_NEWCHG_DIV_CHG == newChgeCd AND kojiakJssiYmd != \"20991231\"?\"}
    COND_CHG_DATE -->|true| SET_RSLT2["kojiRslt = KKIFM151_KOJI_RSLT_2 = \"2\" (Work Corrected)"]
    SET_RSLT2 --> NEXT_ROW
    COND_CHG_DATE -->|false| COND_CHG_CANCEL{\"KKIFI023_NEWCHG_DIV_CHG == newChgeCd AND kojiakJssiYmd == \"20991231\"?\"}
    COND_CHG_CANCEL -->|true| SET_RSLT3["kojiRslt = KKIFM151_KOJI_RSLT_3 = \"3\" (Work Completion Cancelled)"]
    COND_CHG_CANCEL -->|false| NEXT_ROW
    SET_RSLT3 --> NEXT_ROW["kktKjFinWkMap_007 = db_KK_T_KJ_FIN_WK.selectNext()"]
    NEXT_ROW --> WHILE_START
    SET_OUT --> SET_OUT2["outmap.setString(KOJIAK_FIN_YMD, kojiakJssiYmd)"]
    SET_OUT2 --> CHK_LOG{"super.logPrint.chkLogLevel(DEBUG)?"}
    CHK_LOG -->|true| DEBUG_LOG["printDebugLog(\"工事完了検索(工事結果): \" + kojiRslt)"]
    CHK_LOG -->|false| RETURN
    DEBUG_LOG --> RETURN["return kojiFinClInfFlag"]
```

**Branch constant resolution:**

| Constant | Value | Business Meaning |
|----------|-------|------------------|
| `KKIFI023_NEWCHG_DIV_NEW` | `"0"` | New Change Division - New (新規) |
| `KKIFI023_NEWCHG_DIV_CHG` | `"1"` | New Change Division - Change (変更) |
| `KKIFM151_KOJI_RSLT_1` | `"1"` | Work Result - Work Completed (工事完了) |
| `KKIFM151_KOJI_RSLT_2` | `"2"` | Work Result - Work Corrected (工事訂正) |
| `KKIFM151_KOJI_RSLT_3` | `"3"` | Work Result - Work Completion Cancelled (工事完了取消) |

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `outmap` | `JBSbatServiceInterfaceMap` | The output map used to pass processed results back to the caller. After this method completes, it contains the `KOJI_RSLT` (work result code) and `KOJIAK_FIN_YMD` (construction completion date) fields. |
| 2 | `svcKeiNo` | `String` | Service contract number — the unique identifier for a customer's service contract line item. Used as the primary lookup key to query construction completion work records in `KK_T_KJ_FIN_WK`. |
| 3 | `kojiakNo` | `String` | Construction case number — the unique identifier for a specific construction case/order associated with the service contract. Combined with `svcKeiNo`, it forms a compound key to narrow the construction completion work record search. |

**Instance fields read by this method:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `db_KK_T_KJ_FIN_WK` | DB access object | The database mapper instance for the `KK_T_KJ_FIN_WK` (Construction Completion Work) table, used to execute the SQL query and iterate over result rows. |

## 4. CRUD Operations / Called Services

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

The following table lists all method calls within `selectKktKjFinWk`, classified by their operation type.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| CALL | `executeKK_T_KJ_FIN_WK_KK_SELECT_007` | EKK1471_KK_SELECT_007 | `KK_T_KJ_FIN_WK` | Executes a parameterized SQL SELECT query against the Construction Completion Work table using `svcKeiNo` and `kojiakNo` as bind parameters. Sets up a `JBSbatCommonDBInterface` parameter list and delegates to the DB layer. |
| R | `db_KK_T_KJ_FIN_WK.selectNext` | EKK1471_KK_SELECT_007 | `KK_T_KJ_FIN_WK` | Iterates to the next row of the result set from the previously executed query. Returns `null` when no more rows exist. Called once before the loop and at the end of each loop iteration. |
| R | `kktKjFinWkMap_007.getString(NEW_CHGE_CD)` | EKK1471_KK_SELECT_007 | `KK_T_KJ_FIN_WK` | Reads the `NEW_CHGE_CD` (New Change Code) column from the current result row. This code indicates whether the record represents a new order (`"0"`) or a change order (`"1"`). |
| R | `kktKjFinWkMap_007.getString(KOJIAK_JSSI_YMD)` | EKK1471_KK_SELECT_007 | `KK_T_KJ_FIN_WK` | Reads the `KOJIAK_JSSI_YMD` (Construction Case Implementation Year-Month-Day) column from the current result row. This date field distinguishes between active corrections and cancellations — a value of `"20991231"` (a far-future sentinel date) indicates a cancelled state. |
| U | `outmap.setString(KOJI_RSLT, kojiRslt)` | - | - | Writes the determined work result code (`kojiRslt`) into the output map under the `KOJI_RSLT` key. Always executed regardless of whether records were found. |
| U | `outmap.setString(KOJIAK_FIN_YMD, kojiakJssiYmd)` | - | - | Writes the construction completion date (`kojiakJssiYmd`) into the output map under the `KOJIAK_FIN_YMD` key. Set to empty string if no records matched. |
| - | `JBSbatStringUtil.Rtrim(String)` | JBSbatStringUtil | - | Utility method that trims trailing whitespace from database-fetched string values. Applied to both `NEW_CHGE_CD` and `KOJIAK_JSSI_YMD` values. |
| - | `JBSbatKKConst.KKIFI023_NEWCHG_DIV_NEW.equals(String)` | - | - | Constant comparison — checks if the new change division code equals `"0"` (New). |
| - | `JBSbatKKConst.KKIFI023_NEWCHG_DIV_CHG.equals(String)` | - | - | Constant comparison — checks if the new change division code equals `"1"` (Change). |
| - | `super.logPrint.chkLogLevel(JBSbatLogUtil.MODE_DEBUG)` | JBSbatLogUtil | - | Checks whether the current logging level is set to DEBUG before emitting debug output. |
| - | `super.logPrint.printDebugLog(String)` | JBSbatLogUtil | - | Emits a debug-level log message showing the determined work result code: "工事完了検索(工事結果): " + kojiRslt. |

## 5. Dependency Trace

No screen/batch entry points found within 8 hops. Direct callers found: 1 methods.
Terminal operations from this method: `printDebugLog` [-], `selectNext` [R], `getString` [R], `setString` [U], `Rtrim` [U].

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: JBSbatKKCourseChgeTstaDayChsht | `JBSbatKKCourseChgeTstaDayChsht.execute()` -> `selectKktKjFinWk(outmap, svcKeiNo, kojiakNo)` | `executeKK_T_KJ_FIN_WK_KK_SELECT_007 [R] KK_T_KJ_FIN_WK`, `db_KK_T_KJ_FIN_WK.selectNext [R] KK_T_KJ_FIN_WK` |

## 6. Per-Branch Detail Blocks

**Block 1** — [VARIABLE DECLARATION] (L1960)

> Initializes local variables for tracking the construction completion status. As of v20.00.01, `kojiakJssiYmd` and `kojiFinClInfFlag` were moved from instance fields to local variables for thread-safety.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String kojiakJssiYmd = ""` // Construction case implementation year-month-day (工事案件実施年月日) |
| 2 | SET | `String kojiRslt = ""` // Work result (工事結果) |
| 3 | SET | `boolean kojiFinClInfFlag = false` // Construction completion/cancellation information presence flag (工事完了取消情報有無フラグ) |
| 4 | SET | `JBSbatCommonDBInterface kktKjFinWkMap_007 = null` // SQL execution result get map (SQL実行結果取得用map) — Construction completion work query (工事完了ワーク情報取得) |

**Block 2** — [VARIABLE DECLARATION] (L1967)

> Builds the WHERE clause parameters as a String array for the SQL query.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String [] whereKjFinParam = {svcKeiNo, kojiakNo}` // WHERE clause parameters for construction search (工事検索) |

**Block 3** — [METHOD CALL] (L1970)

> Executes the SQL SELECT query against `KK_T_KJ_FIN_WK` using the bind parameters. This is a private method that internally creates a `JBSbatCommonDBInterface` parameter list, sets `svcKeiNo` as bind param [0] and `kojiakNo` as bind param [1], then calls `db_KK_T_KJ_FIN_WK.selectBySqlDefine(paramList, KK_T_KJ_FIN_WK_KK_SELECT_007)`.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeKK_T_KJ_FIN_WK_KK_SELECT_007(whereKjFinParam)` // Execute construction completion work query (工事完了検索実行) |

**Block 4** — [VARIABLE ASSIGNMENT] (L1973)

> Fetches the first row of the query result set. Returns `null` if no matching records exist.

| # | Type | Code |
|---|------|------|
| 1 | SET | `kktKjFinWkMap_007 = db_KK_T_KJ_FIN_WK.selectNext()` // Get next row from construction completion search result (工事完了検索結果の次レコード取得) |

**Block 5** — [IF] `(null != kktKjFinWkMap_007)` (L1975)

> Checks whether at least one construction completion work record was found. If records exist, sets the information presence flag to `true` and enters a while loop to process all rows. This is the main processing branch — all row-level logic lives inside this block.

| # | Type | Code |
|---|------|------|
| 1 | SET | `kojiFinClInfFlag = true` // Set information presence flag to true (情報有(true)設定) |

**Block 5.1** — [WHILE] `(null != kktKjFinWkMap_007)` (L1979)

> Iterates through all matching rows from the construction completion work query. For each row, extracts the new/change code and implementation date, then classifies the work result.

| # | Type | Code |
|---|------|------|
| 1 | SET | `newChgeCd = JBSbatStringUtil.Rtrim(kktKjFinWkMap_007.getString(JBSbatKK_T_KJ_FIN_WK.NEW_CHGE_CD))` // New change code (新規変更コード) |
| 2 | SET | `kojiakJssiYmd = JBSbatStringUtil.Rtrim(kktKjFinWkMap_007.getString(JBSbatKK_T_KJ_FIN_WK.KOJIAK_JSSI_YMD))` // Construction case implementation year-month-day (工事案件実施年月日) |

**Block 5.1.1** — [IF] `(JBSbatKKConst.KKIFI023_NEWCHG_DIV_NEW.equals(newChgeCd))` — `KKIFI023_NEWCHG_DIV_NEW = "0" (New Change Division - New)` (L1985)

> When the new change code is `"0"` (New/新規), sets the work result to `"1"` (Work Completed/工事完了). This is a legacy simplification — originally the code compared against the literal string `"0"` directly.

| # | Type | Code |
|---|------|------|
| 1 | SET | `kojiRslt = JBSbatKKConst.KKIFM151_KOJI_RSLT_1 = "1"` // Work result: Work Completed (工事完了) |

**Block 5.1.2** — [ELSE IF] `(JBSbatKKConst.KKIFI023_NEWCHG_DIV_CHG.equals(newChgeCd) && !"20991231".equals(kojiakJssiYmd))` — `KKIFI023_NEWCHG_DIV_CHG = "1" (New Change Division - Change)` (L1997)

> When the new change code is `"1"` (Change/変更) AND the implementation date is NOT the sentinel value `"20991231"`, sets the work result to `"2"` (Work Corrected/工事訂正). As of v20.00.01, the change division code was corrected from `"5"` to `"1"` (OM-2017-0000986). The sentinel date `"20991231"` acts as a cancellation marker — a future date that indicates no actual implementation has occurred.

| # | Type | Code |
|---|------|------|
| 1 | SET | `kojiRslt = JBSbatKKConst.KKIFM151_KOJI_RSLT_2 = "2"` // Work result: Work Corrected (工事訂正) |

**Block 5.1.3** — [ELSE IF] `(JBSbatKKConst.KKIFI023_NEWCHG_DIV_CHG.equals(newChgeCd) && "20991231".equals(kojiakJssiYmd))` — `KKIFI023_NEWCHG_DIV_CHG = "1" (New Change Division - Change)` (L2010)

> When the new change code is `"1"` (Change/変更) AND the implementation date IS the sentinel value `"20991231"`, sets the work result to `"3"` (Work Completion Cancelled/工事完了取消). This branch captures cases where a change order was issued but then cancelled (no implementation occurred, indicated by the far-future date).

| # | Type | Code |
|---|------|------|
| 1 | SET | `kojiRslt = JBSbatKKConst.KKIFM151_KOJI_RSLT_3 = "3"` // Work result: Work Completion Cancelled (工事完了取消) |

**Block 5.1.4** — [END OF WHILE ITERATION] (L2022)

> Advances to the next row in the result set. If no more rows exist, `selectNext()` returns `null` and the while loop terminates. The last non-null `kojiRslt` value from the iteration is used.

| # | Type | Code |
|---|------|------|
| 1 | SET | `kktKjFinWkMap_007 = db_KK_T_KJ_FIN_WK.selectNext()` // Get next row (次レコード取得) |

**Block 6** — [METHOD CALL / SET] (L2027)

> Writes the determined work result into the output map. Always executed regardless of whether any records were found — if no records matched, `kojiRslt` remains `""` (empty string).

| # | Type | Code |
|---|------|------|
| 1 | SET | `outmap.setString(JBSbatKKIFM151.KOJI_RSLT, kojiRslt)` // Work result (工事結果) |

**Block 7** — [METHOD CALL / SET] (L2029)

> Writes the construction case implementation date into the output map. If no records were found, `kojiakJssiYmd` is `""` (empty string).

| # | Type | Code |
|---|------|------|
| 1 | SET | `outmap.setString(JBSbatKKIFM151.KOJIAK_FIN_YMD, kojiakJssiYmd)` // Construction completion date (工事完了日項目) |

**Block 8** — [IF] `(super.logPrint.chkLogLevel(JBSbatLogUtil.MODE_DEBUG))` (L2032)

> Debug logging block — only executes when the application's log level is set to DEBUG. Logs the determined work result for traceability.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `super.logPrint.printDebugLog("工事完了検索(工事結果): " + kojiRslt)` // Log construction completion search result (ログレベルがデバッグモードの場合) |

**Block 9** — [RETURN] (L2035)

> Returns the `kojiFinClInfFlag` value. This flag is `true` when at least one construction completion work record was found (regardless of the work result), and `false` when no records matched the query. As of v20.00.01, this return value was added to provide callers with record-presence information.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return kojiFinClInfFlag` // Construction completion/cancellation information presence flag (工事完了取消情報有無フラグ) |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `KK_T_KJ_FIN_WK` | Table | Construction Completion Work table — stores records of completed, corrected, or cancelled construction orders for service contracts |
| `KKIFI023_NEWCHG_DIV_NEW` | Constant | New Change Division - New (新規) — value `"0"`, indicates a new (first-time) order/change |
| `KKIFI023_NEWCHG_DIV_CHG` | Constant | New Change Division - Change (変更) — value `"1"`, indicates a modification to an existing order |
| `KKIFM151_KOJI_RSLT_1` | Constant | Work Result: Work Completed (工事完了) — value `"1"`, the construction has been fully completed |
| `KKIFM151_KOJI_RSLT_2` | Constant | Work Result: Work Corrected (工事訂正) — value `"2"`, the construction order was corrected/modified and completed |
| `KKIFM151_KOJI_RSLT_3` | Constant | Work Result: Work Completion Cancelled (工事完了取消) — value `"3"`, the construction completion was cancelled (sentinel date `"20991231"` used) |
| `NEW_CHGE_CD` | Field | New Change Code — column in `KK_T_KJ_FIN_WK`, classifies whether a record is a new order (`"0"`) or a change order (`"1"`) |
| `KOJIAK_JSSI_YMD` | Field | Construction Case Implementation Year-Month-Day — the actual date the construction was carried out; `"20991231"` is a sentinel value meaning "not implemented" / cancelled |
| `KOJI_RSLT` | Field | Work Result — output field set in the result map, indicates the final state of the construction order (completed, corrected, or cancelled) |
| `KOJIAK_FIN_YMD` | Field | Construction Completion Date — output field containing the implementation date of the construction |
| `svcKeiNo` | Field | Service Contract Number — the unique identifier for a customer's service contract, used as a primary query key |
| `kojiakNo` | Field | Construction Case Number — the unique identifier for a specific construction case, used as a compound query key with `svcKeiNo` |
| `kojiFinClInfFlag` | Field | Construction Completion/Cancellation Information Presence Flag — `true` if at least one construction completion work record was found, `false` otherwise |
| `kojiRslt` | Field | Work Result — internal variable holding the determined work status code (`"1"`, `"2"`, or `"3"`) |
| `kojiakJssiYmd` | Field | Construction Case Implementation Year-Month-Day — local variable holding the implementation date extracted from the result set |
| `newChgeCd` | Field | New Change Code — local variable holding the `NEW_CHGE_CD` value from the current result row |
| `KK_T_KJ_FIN_WK_KK_SELECT_007` | SQL Key | Named SQL query key for selecting construction completion work records by service contract number and construction case number |
| `db_KK_T_KJ_FIN_WK` | Instance Field | Database mapper instance for the `KK_T_KJ_FIN_WK` table, provides `selectBySqlDefine()` and `selectNext()` operations |
| `JBSbatCommonDBInterface` | Interface | Common database interface used to hold bind parameters (`setValue`) and iterate over query results (`selectNext`) |
| `JBSbatServiceInterfaceMap` | Class | Service interface map used as a key-value container for passing data between batch processing methods |
| `JBSbatStringUtil.Rtrim` | Method | Trailing whitespace removal utility applied to database-fetched string values |
| `"20991231"` | Sentinel | Far-future date sentinel — used in `KOJIAK_JSSI_YMD` to indicate "no actual implementation occurred" (i.e., a cancelled or pending change) |
