# Business Logic — JBSbatKKKjhKapZnskIktSikyAdchg.addWrisvcTgKei() [72 LOC]

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

## 1. Role

### JBSbatKKKjhKapZnskIktSikyAdchg.addWrisvcTgKei()

This method registers a discount service target contract (`割引サービス対象契約`) into the `T_WRISVC_TG_KEI` table. In the telecom billing domain, when a customer subscribes to a promotional discount plan, the system must create a formal contract record that tracks which service contract line is eligible for the discount, the effective date range, and the specific service code the discount applies to. The method constructs a fully populated SQL parameter array with 29 fields spanning primary keys, timestamps, operational audit metadata, date validity ranges, and deletion flags, then delegates to `executeKK_T_WRISVC_TG_KEI_PKINSERT` for the actual database insert. It acts as a builder-and-delegate within the batch processing pipeline called from `addWrib`, serving as a focused CRUD registration handler for a single database entity. The method generates a system-managed sequence number (`wribSvcTrtgKeiNo`) as the discount service target contract number and uses a `judgeYmd` instance field (determined by prior batch evaluation logic) as the application start date instead of the standard operation date — a post-refactor change from OM-2019-0000601 that introduced more precise date judgment for discount applicability.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["addWrisvcTgKei(wribAddData, firstSeikySvcKeiNo)"])

    START --> CREATE_SET["Create setParam array (KK_T_WRISVC_TG_KEI_CNT elements)"]
    CREATE_SET --> GEN_SEQ["Generate wribSvcTrtgKeiNo via JCCBatCommon.getFormatedNextSeq"]
    GEN_SEQ --> POPULATE["Populate setParam[0-28] with contract and service data"]
    POPULATE --> EXECUTE["executeKK_T_WRISVC_TG_KEI_PKINSERT(setParam)"]
    EXECUTE --> END_NODE(["Return / Next"])
```

**Processing Steps:**

1. **Array Creation** — Creates an `Object[]` named `setParam` with size `KK_T_WRISVC_TG_KEI_CNT` (the number of columns in the `T_WRISVC_TG_KEI` table). This array serves as the SQL bind variable holder for the insert statement.

2. **Sequence Generation** — Calls `JCCBatCommon.getFormatedNextSeq()` to generate a 12-digit formatted sequence number for the discount service target contract number (`wribSvcTrtgKeiNo`). The sequence key used is `SEQ_WRIB_SVC_TRGT_KEI_NO`.

3. **Parameter Population** — Populates all 29 positions in the `setParam` array:
   - `setParam[0]` — Discount service contract number (`wribSvcKeiNo`) from the enclosing scope
   - `setParam[1]` — Generated discount service target contract number
   - `setParam[2]` — System timestamp (generation time of the registration)
   - `setParam[3]` — Target contract identification code = `TG_SKBT_SVC`
   - `setParam[4]` — First billing service contract number (passed as parameter)
   - `setParam[5]` through `setParam[9]` — Service contract detail number, option service contract number, sub-option service contract number, machine provider service contract number, and billing option service contract number (all initialized to empty strings, reserved for future expansion)
   - `setParam[10]` — Discount service target service code, extracted from `wribAddData.getString(JBSbatKK_T_WRISVC_TG_KEI.WRIB_SVC_TRGT_SVC_CD)`
   - `setParam[11]` — Deletion date (empty string, reserved)
   - `setParam[12]` — Setup registration date from `commonItem.getOpeDate()`
   - `setParam[13]` — Discount service target contract application start date from `judgeYmd` (post-OM-2019-0000601 change; was previously `commonItem.getOpeDate()`)
   - `setParam[14]` — Application end date set to `S_MAX_DATE` (maximum representable date, indicating no planned end)
   - `setParam[15]` through `setParam[18]` — Registration timestamp, registration operator, update timestamp, update operator
   - `setParam[19]` through `setParam[20]` — Delete timestamp, delete operator (both empty)
   - `setParam[21]` — Valid flag = `MK_FLG_YK` (0 = Valid)
   - `setParam[22]` through `setParam[27]` — Operation dates and process IDs for registration, update, and deletion (all empty strings)
   - `setParam[28]` — Machine option service contract number (empty string, reserved)

4. **Database Insert** — Calls `executeKK_T_WRISVC_TG_KEI_PKINSERT(setParam)` to execute the SQL INSERT statement against the `T_WRISVC_TG_KEI` table.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `wribAddData` | `JBSbatCommonDBInterface` | The discount registration data carrier carrying service-specific attributes for the discount being registered. It provides access to the discount service target service code (`WRIB_SVC_TRGT_SVC_CD`) which identifies what type of service the discount applies to (e.g., standard service, post-sale discount). |
| 2 | `firstSeikySvcKeiNo` | `String` | The first billing service contract number — the primary contract identifier associated with the customer's initial billing cycle. This links the discount target contract to the customer's main service contract line. |

**Instance fields read:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `judgeYmd` | `String` | Judgment date (YYYYMMDD format) — the business-determined start date for discount applicability. Set by prior batch processing logic (e.g., promotional evaluation), replacing the standard operation date since OM-2019-0000601. |
| `commonItem` | `JBSbatCommonDBInterface` | Common batch item carrier providing the operation date (`getOpeDate()`) for the setup registration date field. |
| `batchUserId` | `String` (via `super`) | The batch operator/user ID used for audit tracking (registration and update operator fields). |

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `JCCBatCommon.getFormatedNextSeq` | JCCBatCommon | - | Generates a formatted 12-digit sequence number for the discount service target contract number using the `SEQ_WRIB_SVC_TRGT_KEI_NO` sequence key |
| R | `JBSbatDateUtil.getSystemDateTimeStamp` | JBSbatDateUtil | - | Retrieves the current system date-time stamp for audit trail timestamps (generation time, registration time, update time) |
| R | `wribAddData.getString(JBSbatKK_T_WRISVC_TG_KEI.WRIB_SVC_TRGT_SVC_CD)` | JBSbatCommonDBInterface | - | Extracts the discount service target service code from the input data carrier |
| R | `commonItem.getOpeDate()` | JBSbatCommonDBInterface | - | Retrieves the operation date (YYYYMMDD format) for the setup registration date |
| C | `JBSbatKKKjhKapZnskIktSikyAdchg.executeKK_T_WRISVC_TG_KEI_PKINSERT` | JBSbatKKKjhKapZnskIktSikyAdchg | `T_WRISVC_TG_KEI` | Inserts a new row into the discount service target contract table (`T_WRISVC_TG_KEI`) with all 29 parameter fields bound |

**CRUD Classification Details:**

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `JCCBatCommon.getFormatedNextSeq` | JCCBatCommon | Oracle Sequence (`SEQ_WRIB_SVC_TRGT_KEI_NO`) | Fetches the next formatted (zero-padded, 12-digit) sequence value to serve as the discount service target contract number |
| R | `JBSbatDateUtil.getSystemDateTimeStamp` | JBSbatDateUtil | System | Returns current system date-time stamp in standardized format for audit trail fields |
| R | `wribAddData.getString(JBSbatKK_T_WRISVC_TG_KEI.WRIB_SVC_TRGT_SVC_CD)` | JBSbatCommonDBInterface | In-memory data carrier | Reads the service code identifying which service type the discount applies to |
| R | `commonItem.getOpeDate()` | JBSbatCommonDBInterface | In-memory data carrier | Returns the batch operation date used for the setup registration date field |
| C | `executeKK_T_WRISVC_TG_KEI_PKINSERT(setParam)` | JBSbatKKKjhKapZnskIktSikyAdchg | `T_WRISVC_TG_KEI` | Executes SQL INSERT to register the discount service target contract record |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Method: `JBSbatKKKjhKapZnskIktSikyAdchg.addWrib` | `addWrib` -> `addWrisvcTgKei` | `executeKK_T_WRISVC_TG_KEI_PKINSERT [C] T_WRISVC_TG_KEI`, `getSystemDateTimeStamp [R]`, `getFormatedNextSeq [R]`, `getOpeDate [R]`, `getString [R]` |

**Terminal operations from this method (all DB/Service endpoints it ultimately reaches):**

| # | Method | CRUD | Entity |
|---|--------|------|--------|
| 1 | `executeKK_T_WRISVC_TG_KEI_PKINSERT` | C | `T_WRISVC_TG_KEI` |
| 2 | `JBSbatDateUtil.getSystemDateTimeStamp` | R | System clock (audit timestamp) |
| 3 | `JCCBatCommon.getFormatedNextSeq` | R | Oracle Sequence (`SEQ_WRIB_SVC_TRGT_KEI_NO`) |
| 4 | `commonItem.getOpeDate()` | R | Operation date context |
| 5 | `wribAddData.getString()` | R | In-memory data carrier |

No screen/batch entry points were found within 8 hops. This method is called exclusively from the sibling method `addWrib` within the same class, indicating it is an internal helper method in the batch registration workflow.

## 6. Per-Branch Detail Blocks

**Block 1** — [VARIABLE DECLARATION] `setParam = new Object[KK_T_WRISVC_TG_KEI_CNT]` (L1861)

> Creates the SQL bind parameter array for the insert operation. The array size equals the column count of the `T_WRISVC_TG_KEI` table.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Object[] setParam = new Object[KK_T_WRISVC_TG_KEI_CNT]` // SQL parameter container for T_WRISVC_TG_KEI insert |

**Block 2** — [VARIABLE DECLARATION] `wribSvcTrtgKeiNo = getFormatedNextSeq(..., SEQ_WRIB_SVC_TRGT_KEI_NO, "", 12)` (L1863)

> Generates the discount service target contract number as a 12-digit zero-padded sequence.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String wribSvcTrtgKeiNo = JCCBatCommon.getFormatedNextSeq(super.commonItem, SEQ_WRIB_SVC_TRGT_KEI_NO, "", 12)` // Generate 12-digit sequence number [-> SEQ_WRIB_SVC_TRGT_KEI_NO = sequence key] |

**Block 3** — [SET BLOCK] `setParam[0-2]` — Contract identity and timestamps (L1865–L1868)

> Sets the primary contract identifiers and generation timestamp.

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[0] = wribSvcKeiNo` // Discount service contract number [from enclosing scope] |
| 2 | SET | `setParam[1] = wribSvcTrtgKeiNo` // Discount service target contract number [-> generated in Block 2] |
| 3 | SET | `setParam[2] = JBSbatDateUtil.getSystemDateTimeStamp()` // System date-time stamp of registration generation |

**Block 4** — [SET BLOCK] `setParam[3-9]` — Contract identification and billing references (L1870–L1883)

> Sets the target contract identification code and various service contract number references. Most future-use fields are initialized to empty strings.

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[3] = JBSbatKKConst.TG_SKBT_SVC` // Target contract identification code [-> TG_SKBT_SVC = "target service contract"] |
| 2 | SET | `setParam[4] = firstSeikySvcKeiNo` // Service contract number [-> passed as method parameter] |
| 3 | SET | `setParam[5] = ""` // Service contract detail number (empty, reserved) |
| 4 | SET | `setParam[6] = ""` // Option service contract number (empty, reserved) |
| 5 | SET | `setParam[7] = ""` // Sub-option service contract number (empty, reserved) |
| 6 | SET | `setParam[8] = ""` // Machine provider service contract number (empty, reserved) |
| 7 | SET | `setParam[9] = ""` // Billing option service contract number (empty, reserved) |

**Block 5** — [SET BLOCK] `setParam[10]` — Discount service target service code (L1885–L1886)

> Extracts the service code from the input data carrier, identifying which service type the discount applies to.

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[10] = wribAddData.getString(JBSbatKK_T_WRISVC_TG_KEI.WRIB_SVC_TRGT_SVC_CD)` // Discount service target service code [-> extracted from input data] |

**Block 6** — [SET BLOCK] `setParam[11-12]` — Deletion date and setup registration date (L1888–L1890)

> Sets the deletion date (reserved empty) and the setup registration date from the operation date context.

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[11] = ""` // Deletion date (empty, reserved) |
| 2 | SET | `setParam[12] = commonItem.getOpeDate()` // Setup registration date [-> operation date from batch context] |

**Block 7** — [SET BLOCK with conditional refactoring] `setParam[13]` — Application start date (L1892–L1894)

> **Post-OM-2019-0000601 change:** The start date for discount applicability now uses `judgeYmd` instead of `commonItem.getOpeDate()`. This allows batch processing to assign a business-judged effective date rather than using the batch run date, enabling retroactive or future-dated discount registrations.

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[13] = judgeYmd` // Discount service target contract application start date [-> judgment date from batch context] |

**Block 8** — [SET BLOCK] `setParam[14]` — Application end date (L1896–L1897)

> Sets the application end date to the system maximum date, indicating the discount has no planned expiration.

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[14] = JBSbatKKConst.S_MAX_DATE` // Discount service target contract application end date [-> S_MAX_DATE = system maximum date] |

**Block 9** — [SET BLOCK] `setParam[15-18]` — Audit timestamps and operators (L1899–L1906)

> Sets all registration and update metadata including timestamps and operator IDs for audit compliance.

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[15] = JBSbatDateUtil.getSystemDateTimeStamp()` // Registration date-time stamp |
| 2 | SET | `setParam[16] = super.batchUserId` // Registration operator account [-> batch user ID] |
| 3 | SET | `setParam[17] = JBSbatDateUtil.getSystemDateTimeStamp()` // Update date-time stamp |
| 4 | SET | `setParam[18] = super.batchUserId` // Update operator account [-> batch user ID] |

**Block 10** — [SET BLOCK] `setParam[19-21]` — Deletion metadata and valid flag (L1908–L1912)

> Sets deletion audit fields as empty (record is active) and the valid flag as "valid" (0).

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[19] = ""` // Deletion date-time stamp (empty — record is active) |
| 2 | SET | `setParam[20] = ""` // Deletion operator account (empty — record is active) |
| 3 | SET | `setParam[21] = JBSbatKKConst.MK_FLG_YK` // Valid flag (0 = Valid) [-> MK_FLG_YK = 0, indicating record is active/valid] |

**Block 11** — [SET BLOCK] `setParam[22-27]` — Operation dates and process IDs (L1914–L1925)

> All operation dates and process IDs for registration, update, and deletion are left empty. These fields are used for operational tracking when records are modified outside this batch process.

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[22] = ""` // Registration operation date (empty) |
| 2 | SET | `setParam[23] = ""` // Registration process ID (empty) |
| 3 | SET | `setParam[24] = ""` // Update operation date (empty) |
| 4 | SET | `setParam[25] = ""` // Update process ID (empty) |
| 5 | SET | `setParam[26] = ""` // Deletion operation date (empty) |
| 6 | SET | `setParam[27] = ""` // Deletion process ID (empty) |

**Block 12** — [SET BLOCK] `setParam[28]` — Machine option service contract number (L1927–L1928)

> Reserved field for future machine-based option service contract linkage, initialized to empty string.

| # | Type | Code |
|---|------|------|
| 1 | SET | `setParam[28] = ""` // Machine option service contract number (empty, reserved) |

**Block 13** — [CALL] `executeKK_T_WRISVC_TG_KEI_PKINSERT(setParam)` (L1930–L1931)

> Executes the SQL INSERT statement against the `T_WRISVC_TG_KEI` table. This is the terminal operation that commits the discount service target contract record to the database. The method name `PKINSERT` indicates the statement is keyed by primary key fields.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `executeKK_T_WRISVC_TG_KEI_PKINSERT(setParam)` // Insert discount service target contract into T_WRISVC_TG_KEI |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `wribSvcKeiNo` | Field | Discount service contract number — the unique identifier for a discount service contract line item assigned during the batch registration process |
| `wribSvcTrtgKeiNo` | Field | Discount service target contract number — a generated 12-digit sequence number identifying the specific contract line that the discount targets |
| `firstSeikySvcKeiNo` | Field | First billing service contract number — the primary contract number associated with the customer's initial billing cycle; links discount records to the customer's main contract |
| `judgeYmd` | Field | Judgment date (YYYYMMDD format) — the business-determined effective start date for discount applicability, assigned by prior batch evaluation logic rather than the system's current date |
| `wribAddData` | Field | Discount registration data carrier — input parameter holding service-specific attributes (e.g., service code) for the discount being registered |
| `TG_SKBT_SVC` | Constant | Target contract identification code — a constant identifying that this contract record represents a target service contract for discount purposes |
| `MK_FLG_YK` | Constant | Valid flag = "0" (valid) — indicates the record is active and not deleted |
| `S_MAX_DATE` | Constant | System maximum date — a sentinel date value used to indicate no planned end date for the discount applicability period |
| `WRIB_SVC_TRGT_SVC_CD` | Field | Discount service target service code — identifies which service type the discount applies to (e.g., standard, post-sale, promotional) |
| `T_WRISVC_TG_KEI` | Table | Discount Service Target Contract table — the database table storing discount service target contract records with full audit trail |
| `T_WRISVC_KEI_UCWK` | Table | Discount Service Contract Detail table — the database table storing detail records for discount service contracts (referenced by sibling method `addWrisvcKeiUcwk`) |
| `getFormatedNextSeq` | Method | Generates a zero-padded, fixed-length sequence number from a named Oracle sequence, used for primary key generation |
| `getOpeDate` | Method | Returns the batch operation date (YYYYMMDD format) from the common item context |
| `getSystemDateTimeStamp` | Method | Returns the current system date and time in standardized timestamp format for audit trail purposes |
| `executeKK_T_WRISVC_TG_KEI_PKINSERT` | Method | Executes the SQL INSERT statement against `T_WRISVC_TG_KEI`, binding the 29-element parameter array to the prepared statement |
| `commonItem` | Field | Common batch item carrier — instance field providing shared batch context including operation date and other common metadata |
| `batchUserId` | Field | Batch operator user ID — the system user identifier for the batch process, used in audit tracking fields |
| OM-2019-0000601 | Change ticket | A modification request that changed the discount application start date from the batch operation date (`getOpeDate()`) to a business-judged date (`judgeYmd`) for more precise effective dating |
| `KK_T_WRISVC_TG_KEI_CNT` | Constant | The total number of columns in the `T_WRISVC_TG_KEI` table (29), used to size the parameter array |
| `SEQ_WRIB_SVC_TRGT_KEI_NO` | Constant | Sequence key identifier used to request the next value from the database sequence for discount service target contract numbers |
