# Business Logic — JBSbatKKDelKhChk.canDelMllist() [57 LOC]

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

## 1. Role

### JBSbatKKDelKhChk.canDelMllist()

This method performs a **deletion eligibility check for mailing list records** within the KOPTELM batch processing framework. It determines whether a mailing list entry — identified by the `MLLIST_NM` field, the operation service contract number (`OPSVKEI_NO`), and the operation date (`opeDate`) — can be safely deleted from the system. The method implements a **two-phase gate** design: first, it queries the operation service contract table (`KK_T_OP_SVC_KEI`) to determine if any active service contracts reference the mailing list; if none are found, it passes the first gate and proceeds to a second check. Second, it queries the order set table (`KK_T_ODR_SET`) to verify whether a cancellation order (解約SOD — `SVC_ORDER_CD_ML = "05"`) has already been issued; if no cancellation order exists, deletion is blocked and a business error log is emitted to alert operators. This method serves as a shared validation utility within the batch deletion pipeline, acting as a gatekeeper that prevents premature or conflicting deletion of mailing list data. The conditional branches cover two scenarios: (1) the mailing list has no active service contracts, permitting deletion pending the second check, and (2) the mailing list lacks an associated cancellation order, which blocks deletion to avoid orphaning data.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["canDelMllist method entry"])

    START --> INIT["Initialize: canDel = false"]

    INIT --> SPLIT["Split MLLIST_NM by @ to extract strMLNM"]

    SPLIT --> BUILD1["Build selectWhereParam: OPSVKEI_NO, strMLNM, opeDate"]

    BUILD1 --> EXEC1["executeKK_T_OP_SVC_KEI_KK_SELECT_041(selectWhereParam)"]

    EXEC1 --> QUERY1["db_KK_T_OP_SVC_KEI.selectNext()"]

    QUERY1 --> CHECK1{nextRec == null?}

    CHECK1 -->|true| SET_TRUE["canDel = true"]
    CHECK1 -->|false| EARLY_EXIT{canDel == false?}

    SET_TRUE --> EARLY_EXIT

    EARLY_EXIT -->|true| RETURN_FALSE["Return canDel (false)"]
    EARLY_EXIT -->|false| BUILD2["Build kk1041SelectWhereParam: SVKEI_NO, OPSVKEI_NO, SVC_ORDER_CD_ML"]

    BUILD2 --> EXEC2["executeKK_T_ODR_SET_KK_SELECT_025(kk1041SelectWhereParam)"]

    EXEC2 --> QUERY2["db_KK_T_ODR_SET.selectNext()"]

    QUERY2 --> CHECK2{kk1041NextRec == null?}

    CHECK2 -->|true| SET_FALSE["canDel = false"]
    CHECK2 -->|false| RETURN_TRUE["Return canDel (true)"]

    SET_FALSE --> LOG["Log: 解約オーダーが未発行のため消去オーダーの発行をスキップしました。"]

    LOG --> RETURN_FALSE
```

**Constant Resolution:**
- `JBSbatKKConst.SVC_ORDER_CD_ML = "05"` (サービスオーダーコード ML — Service Order Code for Mail List service type)

**Processing flow:**
1. **Initialize**: Set `canDel = false` (消去可否 — deletion eligibility flag).
2. **Extract mailing list name**: Retrieve `MLLIST_NM` from the input map and split by `"@"` to isolate the base mailing list name (`strMLNM`). This handles cases where the mailing list name may contain compound identifiers.
3. **First gate query**: Search the `KK_T_OP_SVC_KEI` table using the operation service contract number, extracted mailing list name, and operation date. If no matching record is found (`nextRec == null`), deletion is tentatively allowed (`canDel = true`). If a record exists, `canDel` remains `false` and the method exits early.
4. **Second gate query**: If the first gate passed, search the `KK_T_ODR_SET` table for any order set record matching the service contract number, operation service contract number, and service order code `"05"` (ML cancellation). If no such record exists (`kk1041NextRec == null`), it means no cancellation order (解約SOD) has been issued. In this case, deletion is blocked (`canDel = false`) and a business error log is emitted via `printBusinessErrorLog` to notify operators that the deletion order was skipped because no cancellation order exists.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `inMap` | `JBSbatServiceInterfaceMap` | The input transaction map carrying batch processing parameters. It contains the mailing list name (`MLLIST_NM`), operation service contract number (`OPSVKEI_NO`), and service contract number (`SVKEI_NO`) — all used as lookup keys to determine deletion eligibility. |

| No | Instance Field | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `opeDate` | (String/date) | Operation date — the business date used as a filter in the first gate query to locate service contract records as of a specific date. |
| 2 | `db_KK_T_OP_SVC_KEI` | `JBSbatCommonDBInterface` | Database interface for the `KK_T_OP_SVC_KEI` table — operation service contract master. Used by the first gate to check for active contracts. |
| 3 | `db_KK_T_ODR_SET` | `JBSbatCommonDBInterface` | Database interface for the `KK_T_ODR_SET` table — order set master. Used by the second gate to check for cancellation orders. |
| 4 | `commonItem` | (object) | Common item accessor providing logging infrastructure. Used to emit business error logs when deletion is blocked. |

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `executeKK_T_OP_SVC_KEI_KK_SELECT_041` | EKK1041D010SC (inferred from method name pattern) | KK_T_OP_SVC_KEI | Queries the operation service contract master table to check if any active service contracts reference the mailing list. Used as the first deletion eligibility gate. |
| R | `executeKK_T_ODR_SET_KK_SELECT_025` | EKK1041D010SC (inferred from method name pattern) | KK_T_ODR_SET | Queries the order set master table to check if a cancellation order (解約SOD, service order code "05") has been issued for the service contract. Used as the second deletion eligibility gate. |
| - | `JKKBatOneTimeLogWriter.printBusinessErrorLog` | JKKBatOneTimeLogWriter | - | Emits a business error log when deletion is blocked due to missing cancellation order (解約SOD). Log message: "解約オーダーが未発行のため消去オーダーの発行をスキップしました。" |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: `JBSbatKKDelKhChk.execute()` | `execute()` → `canDelMllist()` | `executeKK_T_OP_SVC_KEI_KK_SELECT_041 [R] KK_T_OP_SVC_KEI`, `executeKK_T_ODR_SET_KK_SELECT_025 [R] KK_T_ODR_SET`, `printBusinessErrorLog [-]` |

## 6. Per-Branch Detail Blocks

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

Initialize the deletion eligibility flag and supporting variables.

| # | Type | Code |
|---|------|------|
| 1 | SET | `canDel = false` // 消去可否 (deletion eligibility — defaults to not allowed) |
| 2 | SET | `nextRec = null` // 次レコード (next record holder) |

**Block 2** — [VARIABLE ASSIGNMENT] (L752)

Split the mailing list name by "@" to extract the base name. This handles compound mailing list identifiers where the base name precedes "@".

| # | Type | Code |
|---|------|------|
| 1 | SET | `strMLLIST_NM = inMap.getString(JBSbatKKIFM160.MLLIST_NM).split("@")` // メーリングリスト名 (mailing list name) split by "@" |
| 2 | SET | `strMLNM = strMLLIST_NM[0]` // Extract the first part as the base mailing list name |

**Block 3** — [ARRAY BUILD] (L755)

Build the WHERE parameter array for the first gate query on `KK_T_OP_SVC_KEI`.

| # | Type | Code |
|---|------|------|
| 1 | SET | `selectWhereParam = {inMap.getString(JBSbatKKIFM160.OPSVKEI_NO), strMLNM, opeDate}` |
| 2 | SET | `inMap.getString(JBSbatKKIFM160.OPSVKEI_NO)` // オプショнсサービス契約番号 (option service contract number) |
| 3 | SET | `strMLNM` // メールリスト名 (mailing list name — extracted above) |
| 4 | SET | `opeDate` // 運用日 (operation date) |

**Block 4** — [EXEC] (L763)

Execute the first gate query against the operation service contract table.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeKK_T_OP_SVC_KEI_KK_SELECT_041(selectWhereParam)` |

**Block 5** — [EXEC] (L764)

Fetch the next record from the query result.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `nextRec = db_KK_T_OP_SVC_KEI.selectNext()` |

**Block 6** — [IF] `(nextRec == null)` (L765)

First gate: check if any active service contracts reference the mailing list. If no matching record is found, the mailing list is not in use — deletion is tentatively allowed.

| # | Type | Code |
|---|------|------|
| 1 | SET | `canDel = true` // Deletion is allowed — no active service contracts found |

**Block 7** — [IF] `(!canDel)` (L771)

Early exit check: if the first gate failed (`canDel` is false, meaning contracts exist for this mailing list), return immediately without proceeding to the second gate.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return canDel` // Early exit — deletion not permitted (active contracts exist) |

**Block 8** — [VARIABLE DECLARATION] (L777)

Initialize variables for the second gate query on `KK_T_ODR_SET`.

| # | Type | Code |
|---|------|------|
| 1 | SET | `kk1041NextRec = null` // 次レコード (next record holder for second gate) |

**Block 9** — [ARRAY BUILD] (L780)

Build the WHERE parameter array for the second gate query on `KK_T_ODR_SET`, including the service order code for ML cancellation.

| # | Type | Code |
|---|------|------|
| 1 | SET | `kk1041SelectWhereParam = {inMap.getString(JBSbatKKIFM160.SVKEI_NO), inMap.getString(JBSbatKKIFM160.OPSVKEI_NO), JBSbatKKConst.SVC_ORDER_CD_ML}` |
| 2 | SET | `inMap.getString(JBSbatKKIFM160.SVKEI_NO)` // サービス契約番号 (service contract number) |
| 3 | SET | `inMap.getString(JBSbatKKIFM160.OPSVKEI_NO)` // オプションスサービス契約番号 (option service contract number) |
| 4 | SET | `JBSbatKKConst.SVC_ORDER_CD_ML = "05"` // サービスオーダーコード ML (service order code for Mail List cancellation) |

**Block 10** — [EXEC] (L786)

Execute the second gate query against the order set table.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeKK_T_ODR_SET_KK_SELECT_025(kk1041SelectWhereParam)` |

**Block 11** — [EXEC] (L787)

Fetch the next record from the second query.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `kk1041NextRec = db_KK_T_ODR_SET.selectNext()` |

**Block 12** — [IF] `(kk1041NextRec == null)` (L790)

Second gate: check if a cancellation order (解約SOD) has been issued for this service contract. If no order set record is found, it means the cancellation order has NOT been issued yet. In this case, deletion is blocked (`canDel = false`) and a business error log is emitted to inform operators that the deletion order was skipped because no cancellation order exists.

| # | Type | Code |
|---|------|------|
| 1 | SET | `canDel = false` // Deletion blocked — no cancellation order exists |
| 2 | SET | `log = "解約オーダーが未発行のため消去オーダーの発行をスキップしました。..."` // 解約オーダーが未発行のため消去オーダーの発行をスキップしました。(Deletion order issuance skipped because cancellation order has not been issued) + OPSVKEI_NO |
| 3 | CALL | `commonItem.getLogPrint().printBusinessErrorLog("EKKB1200AI", new String[] {log})` |

**Block 13** — [RETURN] (L805)

Return the final deletion eligibility result to the caller.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return canDel` // true = deletion allowed, false = deletion blocked |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `MLLIST_NM` | Field | Mailing list name — the identifier for a mailing list entry in the batch system. May contain a compound format separated by "@". |
| `OPSVKEI_NO` | Field | Option service contract number — identifies an option-level service contract under a parent service contract. Used as a lookup key to find active contracts. |
| `SVKEI_NO` | Field | Service contract number — the primary identifier for a service contract in the KOPTELM system. |
| `opeDate` | Field | Operation date — the business date used as a temporal filter in contract lookups, ensuring records are evaluated as of a specific date. |
| `canDel` | Field | Deletion eligibility flag — internal boolean tracking whether the mailing list can be safely deleted. |
| `strMLNM` | Field | Extracted mailing list name — the base portion of `MLLIST_NM` obtained by splitting on "@". |
| SVC_ORDER_CD_ML | Constant | Service order code "05" — identifies a Mail List service order type, specifically used to query for cancellation (解約) orders. |
| KK_T_OP_SVC_KEI | DB Table | Operation Service Contract Master — stores operation-level service contract records. Used to verify if a mailing list is actively in use. |
| KK_T_ODR_SET | DB Table | Order Set Master — stores order set records including cancellation orders. Used to verify if a cancellation order has been issued before allowing deletion. |
| 解約SOD | Business term | Cancellation Order (Service Order Data) — an order record indicating that a service contract has been cancelled. Must exist before deletion of mailing list data is permitted. |
| 消去可否 | Business term | Deletion eligibility — the determination of whether a record can be safely deleted. This method returns `true` only when both gates pass. |
| 消去オーダー | Business term | Deletion order — the batch order to remove mailing list data. This method gates when such orders can be issued. |
| JBSbatServiceInterfaceMap | Class | Service interface map — the standard transaction data carrier for batch processing, holding input parameters as key-value pairs. |
| JBSbatCommonDBInterface | Class | Common database interface — the standard data access wrapper used across KOPTELM batch services for querying database tables. |
| JBSbatKKIFM160 | Class | Input map field constant class — defines all field name constants (keys) for the batch input file format. |
| JBSbatKKConst | Class | Common batch constant class — defines shared constant values such as service order codes across batch services. |
