# Business Logic — JBSbatKKIntrCdIkaAdd.checkMain() [39 LOC]

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

## 1. Role

### JBSbatKKIntrCdIkaAdd.checkMain()

This method performs comprehensive validation of a single record in the referral code bulk registration batch job (紹介コード一括登録 — Referral Code Bulk Registration). It is the central orchestrator of pre-insert business rule checks, ensuring that each customer and service contract referenced by the batch input data is valid before any referral codes are registered into `KK_T_INTR`. The method follows a sequential gating pattern: first delegating to `singleCheck()` for item-level field validation, then performing three layered entity existence and state checks against the customer master (`CK_T_CUST`) and service contract header (`KK_T_SVC_KEI`) tables. Each check independently short-circuits with a distinct error code (E100, E110, E120), allowing the batch job to fail fast and precisely without unnecessary downstream processing. Its role in the larger system is as a shared validation step invoked once per record within the batch's main `execute()` loop — it does not modify data, only asserts preconditions for safe registration.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["checkMain(recordMap)"])
    CHECK_SINGLE["singleCheck(recordMap.getMap())"]
    CHECK_SINGLE_RESULT{result != null?}
    RETURN_SINGLE["Return singleCheck error"]
    GET_SYSID["Get sysid from recordMap<br>recordMap.getString(SYSID)"]
    QUERY_CUST["executeCK_T_CUST_KK_SELECT_020(sysid, opeDate)"]
    FETCH_CUST["custInfo = db_CK_T_CUST.selectNext()"]
    CUST_EXISTS{custInfo != null?}
    RETURN_E100["Return ErrCdEnum.E100<br/>Customer not found"]
    GET_NTAIKAI["custNtaikaiCd = custInfo.getString(CUST_NTAIKAI_CD)"]
    NTAIKAI_CHECK{CUST_NTAIKAI_CD equals 1?<br/>(Cancelled?)}
    RETURN_E110["Return ErrCdEnum.E110<br/>Customer cancelled"]
    GET_SVC_KEI_NO["Get svcKeiNo from recordMap<br>recordMap.getString(SVC_KEI_NO)"]
    QUERY_SVC["executeKK_T_SVC_KEI_KK_SELECT_373(svcKeiNo, opeDate)"]
    FETCH_SVC["svcKeiInfo = db_KK_T_SVC_KEI.selectNext()"]
    SVC_CHECK{svcKeiInfo == null<br/>or sysid mismatch?}
    RETURN_E120["Return ErrCdEnum.E120<br/>Contract not found or mismatch"]
    RETURN_OK["Return null (success)"]

    START --> CHECK_SINGLE
    CHECK_SINGLE --> CHECK_SINGLE_RESULT
    CHECK_SINGLE_RESULT -- "true" --> RETURN_SINGLE
    CHECK_SINGLE_RESULT -- "false" --> GET_SYSID
    GET_SYSID --> QUERY_CUST
    QUERY_CUST --> FETCH_CUST
    FETCH_CUST --> CUST_EXISTS
    CUST_EXISTS -- "false" --> RETURN_E100
    CUST_EXISTS -- "true" --> GET_NTAIKAI
    GET_NTAIKAI --> NTAIKAI_CHECK
    NTAIKAI_CHECK -- "true" --> RETURN_E110
    NTAIKAI_CHECK -- "false" --> GET_SVC_KEI_NO
    GET_SVC_KEI_NO --> QUERY_SVC
    QUERY_SVC --> FETCH_SVC
    FETCH_SVC --> SVC_CHECK
    SVC_CHECK -- "true" --> RETURN_E120
    SVC_CHECK -- "false" --> RETURN_OK
```

```mermaid
sequenceDiagram
    participant CLI as checkMain caller
    participant CLI2 as checkMain
    participant DB1 as CK_T_CUST
    participant DB2 as KK_T_SVC_KEI

    CLI->>CLI2: checkMain(recordMap)
    CLI2->>CLI2: singleCheck(recordMap.getMap())
    alt Single check error
        CLI2-->>CLI: return ErrCdEnum.E100-E999
    end
    CLI2->>DB1: executeCK_T_CUST_KK_SELECT_020(sysid, opeDate)
    DB1-->>CLI2: custInfo (CK_T_CUST)
    alt Customer not found
        CLI2-->>CLI: return E100
    end
    alt Customer cancelled (NTAIKAI_CD="1")
        CLI2-->>CLI: return E110
    end
    CLI2->>DB2: executeKK_T_SVC_KEI_KK_SELECT_373(svcKeiNo, opeDate)
    DB2-->>CLI2: svcKeiInfo (KK_T_SVC_KEI)
    alt Contract not found or sysid mismatch
        CLI2-->>CLI: return E120
    end
    CLI2-->>CLI: return null (success)
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `recordMap` | `JBSbatServiceInterfaceMap` | A single record from the referral code bulk registration list (紹介コード一括登録リストの１レコードの情報). It carries all input fields for one batch entry, including the customer SYSID, service detail number (SVC_KEI_NO), and other referral code registration fields read from the input file (TXT or DB). |

**Inherited/instance fields read by this method:**

| No | Field Name | Type | Business Description |
|----|-----------|------|---------------------|
| 2 | `super.opeDate` | `String` | The operation date (業務日) inherited from the parent class `JBSbatBusinessService`. Used as the effective date parameter when querying customer and service contract records from the database. |

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `JBSbatKKIntrCdIkaAdd.executeCK_T_CUST_KK_SELECT_020` | KK_SELECT_020 | `CK_T_CUST` | Queries the customer master table by SYSID and operation date to retrieve the customer record. Used to validate customer existence and cancellation status. |
| R | `JBSbatKKIntrCdIkaAdd.executeKK_T_SVC_KEI_KK_SELECT_373` | KK_SELECT_373 | `KK_T_SVC_KEI` | Queries the service contract header table by service detail number (SVC_KEI_NO) and operation date to retrieve the service contract record. Used to validate contract existence and SYSID cross-reference. |

### Internal method calls:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `JBSbatKKIntrCdIkaAdd.singleCheck` | - | - | Performs single-item field-level validation on the record's data map. Returns an error code if any field fails validation, or null if all fields are valid. |
| - | `recordMap.getMap()` | - | - | Retrieves the underlying `Map` object from the service interface map for delegation to `singleCheck`. |
| - | `recordMap.getString(SYSID)` | - | - | Extracts the customer SYSID string from the batch input record. |
| - | `recordMap.getString(SVC_KEI_NO)` | - | - | Extracts the service detail number string from the batch input record. |
| - | `custInfo.getString(CUST_NTAIKAI_CD)` | - | - | Reads the customer cancellation status code from the `CK_T_CUST` row. Value "1" indicates cancelled (退会). |
| - | `svcKeiInfo.getString(SYSID)` | - | - | Reads the SYSID from the `KK_T_SVC_KEI` row for cross-referencing against the input SYSID. |

## 5. Dependency Trace

No screen/batch entry points found within 8 hops. Direct callers found: 1 methods.
Terminal operations from this method: `getString` [R], `getString` [R], `getString` [R], `executeKK_T_SVC_KEI_KK_SELECT_373` [-], `getString` [R], `getString` [R], `getString` [R], `executeCK_T_CUST_KK_SELECT_020` [-], `getString` [R], `getString` [R], `getString` [R]  # NOSONAR

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: JBSbatKKIntrCdIkaAdd | `JBSbatKKIntrCdIkaAdd.execute()` -> `JBSbatKKIntrCdIkaAdd.checkMain` | `executeCK_T_CUST_KK_SELECT_020 [R] CK_T_CUST`, `executeKK_T_SVC_KEI_KK_SELECT_373 [R] KK_T_SVC_KEI` |

## 6. Per-Branch Detail Blocks

**Block 1** — IF `(singleCheck result != null)` (L799)

> Delegates to single-item field validation. If any field fails validation, returns the error code immediately without proceeding to entity checks.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `result = singleCheck(recordMap.getMap())` // Perform single-item field check (単項目チェック) [-> CK_T_CUST] |
| 2 | SET | `result = result` // Holds error code if validation fails, or null if valid |
| 3 | IF | `result != null` [-> Early exit on field error] (L800) |

**Block 1.1** — IF BRANCH `(result != null == true)` (L800-802)

> Single-item validation found an error. Return the error code to the caller immediately.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return result;` // Return singleCheck error code (E020-E090 range from ErrCdEnum) |

**Block 2** — SET `(Extract SYSID from record)` (L804)

> Reads the customer SYSID from the batch input record. This SYSID is used as the key to look up the customer in the customer master table.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `sysid = recordMap.getString(JBSbatKKIFM875.SYSID)` // Get SYSID from input record [-> SYSID="SYSID"] |

**Block 3** — SET `(Query customer master)` (L806-808)

> Executes a database query to retrieve the customer record matching the SYSID and operation date.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeCK_T_CUST_KK_SELECT_020(new String[]{sysid, super.opeDate})` // Query customer master (お客様をコントロール取得) [-> KK_SELECT_020, CK_T_CUST] |
| 2 | CALL | `custInfo = db_CK_T_CUST.selectNext()` // Fetch customer record from result set [-> R, CK_T_CUST] |

**Block 4** — IF `(Customer not found)` (L811-813)

> If no customer record exists for the given SYSID, return error E100. This ensures the batch does not attempt to register referral codes for a non-existent customer.

| # | Type | Code |
|---|------|------|
| 1 | IF | `null == custInfo` [-> Customer does not exist in CK_T_CUST] (L811) |

**Block 4.1** — IF BRANCH `(custInfo == null == true)` (L812-813)

> Customer not found. Return E100 error.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return ErrCdEnum.E100;` // 存在しないSYSIDです。(SYSID does not exist) |

**Block 5** — SET `(Read cancellation status)` (L816)

> Reads the customer cancellation status code from the fetched customer record.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `custNtaikaiCd = custInfo.getString(JBSbatCK_T_CUST.CUST_NTAIKAI_CD)` // Get customer cancellation status code (お客様の退会コード) [-> CUST_NTAIKAI_CD="CUST_NTAIKAI_CD"] |

**Block 6** — IF `(Customer is cancelled)` (L817-819)

> If the customer's cancellation status code is "1" (退会 = cancelled), return error E110. This prevents referral code registration for cancelled customers.

| # | Type | Code |
|---|------|------|
| 1 | IF | `"1".equals(custNtaikaiCd)` [CUST_NTAIKAI_CD = "1" (退会 / Cancelled)] (L817) |

**Block 6.1** — IF BRANCH `(CUST_NTAIKAI_CD == "1")` (L818-819)

> Customer has cancelled. Return E110 error.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return ErrCdEnum.E110;` // 既に退会しています。(Customer has already cancelled) |

**Block 7** — SET `(Extract SVC_KEI_NO from record)` (L822)

> Reads the service detail number from the batch input record. This is used to look up the service contract header.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `svcKeiNo = recordMap.getString(JBSbatKKIFM875.SVC_KEI_NO)` // Get service detail number from input record [-> SVC_KEI_NO="SVC_KEI_NO"] |

**Block 8** — SET `(Query service contract header)` (L823-825)

> Executes a database query to retrieve the service contract record matching the service detail number and operation date. Then cross-references the SYSID from the contract against the input SYSID to ensure consistency.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeKK_T_SVC_KEI_KK_SELECT_373(new String[]{svcKeiNo, super.opeDate})` // Query service contract (サービス契約をコントロール取得) [-> KK_SELECT_373, KK_T_SVC_KEI] |
| 2 | CALL | `svcKeiInfo = db_KK_T_SVC_KEI.selectNext()` // Fetch service contract record [-> R, KK_T_SVC_KEI] |

**Block 9** — IF `(Contract not found or SYSID mismatch)` (L826-829)

> If no service contract record is found for the given SVC_KEI_NO, or if the SYSID stored in the service contract does not match the input SYSID, return error E120. This cross-reference ensures the input record's SYSID and service contract are consistent with each other.

| # | Type | Code |
|---|------|------|
| 1 | IF | `null == svcKeiInfo \|\| !sysid.equals(svcKeiInfo.getString(JBSbatKK_T_SVC_KEI.SYSID))` [Contract not found or SYSID/customer ID mismatch] (L826) |

**Block 9.1** — IF BRANCH `(svcKeiInfo == null || SYSID mismatch)` (L827-829)

> Service contract not found or SYSIDs do not match. Return E120 error.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return ErrCdEnum.E120;` // SYSIDとお客户IDが一致しません。(SYSID and customer ID do not match) |

**Block 10** — RETURN `(All checks passed)` (L831)

> All validation checks have passed: single-item validation succeeded, customer exists and is active, and service contract exists with matching SYSID. The batch can proceed with referral code registration for this record.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return null;` // Success — no errors detected |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `SYSID` | Field | System ID — unique identifier for a customer, used to link customer records with service contracts across tables |
| `SVC_KEI_NO` | Field | Service detail number (サービス詳細番号) — internal tracking number for a service contract line item |
| `CUST_NTAIKAI_CD` | Field | Customer cancellation status code (お客様退会コード) — "1" means the customer has cancelled their account (退会), "0" or other values mean active |
| `opeDate` | Field | Operation date (業務日) — the business date used for querying time-variant master records from the database |
| `recordMap` | Field | Referral code bulk registration record map (紹介コード一括登録リストの１レコードの情報) — holds all input fields for one batch entry from TXT file or DB source |
| `singleCheck` | Method | Single-item field validation — checks individual fields (SYSID format, length, character type, expiry date, etc.) for validity |
| `CK_T_CUST` | Table | Customer master table (お客様マスタ) — stores customer information including SYSID and cancellation status |
| `KK_T_SVC_KEI` | Table | Service contract header table (サービス契約) — stores service contract details including SYSID cross-reference and service detail numbers |
| `KK_T_INTR` | Table | Referral code registration table (紹介) — target table where referral codes are inserted during batch processing |
| `KK_SELECT_020` | SQL Key | SQL definition key for customer master query — selects CK_T_CUST by SYSID |
| `KK_SELECT_373` | SQL Key | SQL definition key for service contract query — selects KK_T_SVC_KEI by SVC_KEI_NO |
| `E100` | Error code | Customer not found — the SYSID in the input record does not exist in the customer master table (存在しないSYSIDです。) |
| `E110` | Error code | Customer cancelled — the customer has already cancelled their account (既に退会しています。) |
| `E120` | Error code | SYSID/customer ID mismatch — the service contract does not exist or its SYSID does not match the input SYSID (SYSIDとお客户IDが一致しません。) |
| `ErrCdEnum` | Enum | Local error code enumeration specific to this batch service, defining E020–E999 error conditions |
| JBSbat | Prefix | Japan Batch — batch processing module naming convention for the customer backbone system (顧客基幹システム) |
| IntrCd | Abbreviation | Referral code (紹介コード) — a marketing referral/discount code registered in bulk by this batch job |
| 退会 | Japanese term | Cancellation — customer has terminated their account/service contract |
