# Business Logic — JBSbatKKAdChgSmtvlRnki.insertKK_T_TAJGSWKEI_TGKEI() [57 LOC]

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

## 1. Role

### JBSbatKKAdChgSmtvlRnki.insertKK_T_TAJGSWKEI_TGKEI()

This method inserts a new record into the **other-provider discount contract target contract** table (`KK_T_TAJGSWKEI_TGKEI`) to manage the lifecycle of a service contract change for a carrier discount. It acts as a **routed/dispatching business method** that handles two mutually exclusive service types based on whether a new service contract number is provided:

1. **Application Start** — When `newSvcKeiNo` is non-null, it registers the effective start of the service contract change, generating a new sequence-assigned record with an open-ended validity period (end date set to `20991231`). This represents the activation of the discount under the new service contract.
2. **Application End** — When `newSvcKeiNo` is null, it registers the effective end of the service contract change using the original data pulled from `param`, with the end date set to the day before the operation date. This represents the deactivation or termination of the previous discount record.

The method implements the **builder pattern** by constructing a `JBSbatCommonDBInterface` (insertMap) with all required fields and then delegates the actual database insert to `db_KK_T_TAJGSWKEI_TGKEI.insertByPrimaryKeys()`. It plays a supporting role in the larger service contract change batch process, invoked by `changeTajgswkeiTgkeiSvcKeiNo()` as part of the home-to-messaging change notification flow (ANK-2186-00-00).

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["insertKK_T_TAJGSWKEI_TGKEI"])
    START --> S1["Read TAJGSWKEI_TGKEI_NO from param"]
    S1 --> S2["Read SVC_KEI_NO from param"]
    S2 --> S3["Read TAJGSWKEI_TGKEI_TSTAYMD from param"]
    S3 --> S4["Read DSL_TAJGS_TCH_YMD from param"]
    S4 --> S5["Read PPCHG_TAJGS_TCH_YMD from param"]
    S5 --> S6["Set endYmd = opeDate - 1 day"]
    S6 --> S7["Set sysTimeStamp = systemDateTimeStamp"]
    S7 --> COND1{newSvcKeiNo<br/>is null?}
    COND1 -->|"No"| B1["Generate SEQ_TAJGSWKEI_TGKEI_NO via DB sequence"]
    B1 --> B2["Set svcKeiNo = newSvcKeiNo"]
    B2 --> B3["Set startYmd = opeDate"]
    B3 --> B4["Set endYmd = MAX_DATE"]
    B4 --> B5["Set dslTajgsTchYmd = MAX_DATE"]
    B5 --> B6["Set ppchgTajgsTchYmd = MAX_DATE"]
    COND1 -->|"Yes"| E1["Keep original tajgswkeiTgkeiNo"]
    E1 --> E2["Keep original svcKeiNo"]
    E2 --> E3["Keep original startYmd"]
    E3 --> E4["Keep endYmd = opeDate - 1"]
    E4 --> E5["Keep dslTajgsTchYmd from param"]
    E5 --> E6["Keep ppchgTajgsTchYmd from param"]
    B6 --> P1["Create insertMap"]
    E6 --> P1
    P1 --> P2["Set TAJGS_WRIB_KEI_NO"]
    P2 --> P3["Set TAJGSWKEI_TGKEI_NO"]
    P3 --> P4["Set GENE_ADD_DTM"]
    P4 --> P5["Set SVC_KEI_NO"]
    P5 --> P6["Set DSL_TAJGS_TCH_YMD"]
    P6 --> P7["Set PPCHG_TAJGS_TCH_YMD"]
    P7 --> P8["Set TAJGSWKEI_TGKEI_TSTAYMD"]
    P8 --> P9["Set TAJGSWKEI_TGKEI_TENDYMD"]
    P9 --> P10["Set null for ADD/UPD/DEL audit fields"]
    P10 --> P11["Set null for ADD_UNYO/UPD_UNYO/DEL_UNYO unyo fields"]
    P11 --> P12["Set null for ADD_TRN_ID/UPD_TRN_ID/DEL_TRN_ID trn_id fields"]
    P12 --> INSERT["Insert record into KK_T_TAJGSWKEI_TGKEI"]
    INSERT --> END(["Return void"])
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `param` | `JBSbatCommonDBInterface` | The source record containing the original "other-provider discount contract target contract" data. It provides the baseline values (service contract number, effective start date, DSL/PP change dates) that are used directly when processing an **application end** (i.e., when `newSvcKeiNo` is null). |
| 2 | `newSvcKeiNo` | `String` | The new service contract number for the service change. This is the **branching parameter** — if non-null, it triggers the **application start** flow (generating a new sequence number, setting the effective start date to the operation date, and opening the validity period indefinitely). If null, it triggers the **application end** flow (closing the previous record's validity to the day before the operation date). |

**Instance fields / external state read:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `super.opeDate` | `String` | The operation date of the batch process — used as the effective start date for application start records and as the reference point (minus 1 day) for application end records. |
| `super.commonItem.getConnection()` | `Connection` | The database connection used to generate the next sequence value. |

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `JBSbatDateUtil.adjustDate` | JBSbatDateUtil | - | Subtracts 1 day from `opeDate` to compute the application end date (「適用終了日：運用日の前日を設定」) |
| - | `JBSbatDateUtil.getSystemDateTimeStamp` | JBSbatDateUtil | - | Retrieves the current system date-time stamp for the `GENE_ADD_DTM` field |
| R | `JBSbatOracleSeqUtil.getFormatedNextSeq` | JBSbatOracleSeq | - | Generates the next formatted sequence number (`SEQ_TAJGSWKEI_TGKEI_NO`, 15 digits) for the `TAJGSWKEI_TGKEI_NO` field |
| R | `JBSbatCommonDBInterface.getString` | JBSbatCommonDBInterface | - | Reads values from `param` by field key (e.g., `TAJGSWKEI_TGKEI_NO`, `SVC_KEI_NO`, `DSL_TAJGS_TCH_YMD`) |
| - | `JBSbatCommonDBInterface.setValue` | JBSbatCommonDBInterface | - | Sets values into the `insertMap` for all 20 fields of the target table |
| C | `JBSbatSQLAccess.insertByPrimaryKeys` | JBSbatSQLAccess | `KK_T_TAJGSWKEI_TGKEI` | Performs the final INSERT of the fully-populated record into the "other-provider discount contract target contract" table |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: `JBSbatKKAdChgSmtvlRnki.changeTajgswkeiTgkeiSvcKeiNo()` | `changeTajgswkeiTgkeiSvcKeiNo()` -> `insertKK_T_TAJGSWKEI_TGKEI()` | `insertByPrimaryKeys [C] KK_T_TAJGSWKEI_TGKEI` |

**Terminal operations from this method:**

| Terminal | Operation | Entity / DB |
|----------|-----------|-------------|
| `getFormatedNextSeq` | Read (R) | DB Sequence `SEQ_TAJGSWKEI_TGKEI_NO` |
| `adjustDate` | Utility | - |
| `getSystemDateTimeStamp` | Utility | - |
| `getString` | Read (R) | `param` interface |
| `setValue` | Utility | `insertMap` interface |
| `insertByPrimaryKeys` | Create (C) | `KK_T_TAJGSWKEI_TGKEI` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SET] `param field reads` (L648)

Extract the initial values from the source `param` record. These values are used as the "end" record's baseline, and may be overwritten by the conditional branch below.

| # | Type | Code |
|---|------|------|
| 1 | SET | `tajgswkeiTgkeiNo = param.getString("TAJGSWKEI_TGKEI_NO")` // Original discount contract change number |
| 2 | SET | `svcKeiNo = param.getString("SVC_KEI_NO")` // Original service contract number |
| 3 | SET | `startYmd = param.getString("TAJGSWKEI_TGKEI_TSTAYMD")` // Effective start date of the original record |
| 4 | SET | `dslTajgsTchYmd = param.getString("DSL_TAJGS_TCH_YMD")` // DSL change effective date |
| 5 | SET | `ppchgTajgsTchYmd = param.getString("PPCHG_TAJGS_TCH_YMD")` // Product plan change effective date |

**Block 2** — [SET] `date computations` (L649-654)

Compute the end date and system timestamp. These form the initial values that will be overwritten by the "application start" branch.

| # | Type | Code |
|---|------|------|
| 1 | SET | `endYmd = JBSbatDateUtil.adjustDate(super.opeDate, -1)` // Application end date = operation date - 1 day |
| 2 | SET | `sysTimeStamp = JBSbatDateUtil.getSystemDateTimeStamp()` // System date-time stamp for GENE_ADD_DTM |

**Block 3** — [IF] `newSvcKeiNo != null` — **Application Start** (L657)

When a new service contract number is provided, the method registers the start of the service contract change. It generates a new sequence-assigned record and opens the validity period indefinitely.

| # | Type | Code |
|---|------|------|
| 1 | SET | `tajgswkeiTgkeiNo = JBSbatOracleSeqUtil.getFormatedNextSeq(connection, "SEQ_TAJGSWKEI_TGKEI_NO", "", 15)` // [-> Generate 15-digit sequence number] |
| 2 | SET | `svcKeiNo = newSvcKeiNo` // [-> Set the new service contract number from parameter] |
| 3 | SET | `startYmd = super.opeDate` // [-> Set effective start date to operation date] |
| 4 | SET | `endYmd = MAX_DATE` // [-> 20991231, open-ended validity period for the new record] |
| 5 | SET | `dslTajgsTchYmd = MAX_DATE` // [-> 20991231, no DSL change limit] |
| 6 | SET | `ppchgTajgsTchYmd = MAX_DATE` // [-> 20991231, no product plan change limit] |

**Block 4** — [ELSE] `newSvcKeiNo == null` — **Application End** (implicit L670)

When no new service contract number is provided, the method retains all original values and keeps the end date as "operation date - 1" to close the validity of the previous record. No overrides are applied — the values from Blocks 1 and 2 remain in effect.

**Block 5** — [SET] `insertMap construction` (L674-701)

Build the complete insert record with all 20 fields. This is the same regardless of whether it's an application start or end.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `insertMap = new JBSbatCommonDBInterface()` |
| 2 | SET | `insertMap.setValue("TAJGS_WRIB_KEI_NO", param.getString("TAJGS_WRIB_KEI_NO"))` // Discount eligibility contract number |
| 3 | SET | `insertMap.setValue("TAJGSWKEI_TGKEI_NO", tajgswkeiTgkeiNo)` // [-> Sequence-assigned or original record number] |
| 4 | SET | `insertMap.setValue("GENE_ADD_DTM", sysTimeStamp)` // [-> Generated add date-time stamp] |
| 5 | SET | `insertMap.setValue("SVC_KEI_NO", svcKeiNo)` // [-> New or original service contract number] |
| 6 | SET | `insertMap.setValue("DSL_TAJGS_TCH_YMD", dslTajgsTchYmd)` // [-> Overridden to MAX_DATE for start, original for end] |
| 7 | SET | `insertMap.setValue("PPCHG_TAJGS_TCH_YMD", ppchgTajgsTchYmd)` // [-> Overridden to MAX_DATE for start, original for end] |
| 8 | SET | `insertMap.setValue("TAJGSWKEI_TGKEI_TSTAYMD", startYmd)` // [-> Original or operation date for start] |
| 9 | SET | `insertMap.setValue("TAJGSWKEI_TGKEI_TENDYMD", endYmd)` // [-> Operation date - 1 (end) or 20991231 (start)] |
| 10 | SET | `insertMap.setValue("ADD_DTM", null)` // Audit: add date-time |
| 11 | SET | `insertMap.setValue("ADD_OPEACNT", null)` // Audit: add operator account |
| 12 | SET | `insertMap.setValue("UPD_DTM", null)` // Audit: update date-time |
| 13 | SET | `insertMap.setValue("UPD_OPEACNT", null)` // Audit: update operator account |
| 14 | SET | `insertMap.setValue("DEL_DTM", null)` // Audit: delete date-time |
| 15 | SET | `insertMap.setValue("DEL_OPEACNT", null)` // Audit: delete operator account |
| 16 | SET | `insertMap.setValue("MK_FLG", null)` // Audit: master key flag |
| 17 | SET | `insertMap.setValue("ADD_UNYO_YMD", null)` // Audit: add unyo (related process) date |
| 18 | SET | `insertMap.setValue("ADD_TRN_ID", null)` // Audit: add transaction ID |
| 19 | SET | `insertMap.setValue("UPD_UNYO_YMD", null)` // Audit: update unyo date |
| 20 | SET | `insertMap.setValue("UPD_TRN_ID", null)` // Audit: update transaction ID |
| 21 | SET | `insertMap.setValue("DEL_UNYO_YMD", null)` // Audit: delete unyo date |
| 22 | SET | `insertMap.setValue("DEL_TRN_ID", null)` // Audit: delete transaction ID |

**Block 6** — [CALL] `db insert` (L702)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `db_KK_T_TAJGSWKEI_TGKEI.insertByPrimaryKeys(insertMap)` // Insert the fully constructed record into KK_T_TAJGSWKEI_TGKEI table |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `TAJGSWKEI_TGKEI_NO` | Field | Contract change number — the unique sequence-assigned identifier for a record in the other-provider discount contract target contract table |
| `TAJGS` | Field (Abbrev) | 他事業者割引 (Ta-jigyousha waribiki) — Other-provider discount; a discount applicable to contracts from competing telecom carriers |
| `SVC_KEI_NO` | Field | Service contract number — the unique identifier of a service contract line item |
| `TAJGSWKEI` | Field (Abbrev) | 他事業者割引契約対象契約 (Ta-jigyousha waribiki keiyaku taishou keiyaku) — Other-provider discount contract target contract |
| `TSTAYMD` | Field (Abbrev) | 適用開始日 (Tekiyou kaishihi) — Effective start date of the discount applicability |
| `TENDYMD` | Field (Abbrev) | 適用終了日 (Tekiyou shuuryouhi) — Effective end date of the discount applicability |
| `newSvcKeiNo` | Parameter | New service contract number for the contract change. Non-null triggers an application start; null triggers an application end |
| `opeDate` | Field | Operation date — the batch processing date that serves as the reference for computing effective date ranges |
| `GENE_ADD_DTM` | Field | Generated add date-time — the timestamp when the record was created in the database |
| `DSL_TAJGS_TCH_YMD` | Field | DSL (Digital Subscriber Line) discount change effective date |
| `PPCHG_TAJGS_TCH_YMD` | Field | Product Plan Change discount effective date |
| `MAX_DATE` | Constant | `"20991231"` — A sentinel date representing "open-ended" or "no expiry" used in Japanese enterprise systems |
| `SEQ_TAJGSWKEI_TGKEI_NO` | Constant | `"SEQ_TAJGSWKEI_TGKEI_NO"` — The database sequence name used to generate unique 15-digit contract change numbers |
| `KK_T_TAJGSWKEI_TGKEI` | DB Table | Other-provider discount contract target contract table — stores the lifecycle records (start/end) of discount applicability for contract changes |
| `insertByPrimaryKeys` | Method | Inserts a record into a table using primary key fields from the provided interface map |
| `adjustDate` | Method | Utility that adds or subtracts days from a date string (format: `yyyyMMdd`) |
| `getSystemDateTimeStamp` | Method | Utility that returns the current system date-time as a formatted timestamp string |
| `getFormatedNextSeq` | Method | Retrieves the next formatted (zero-padded) value from a database sequence |
| `TAJGS_WRIB_KEI_NO` | Field | Discount eligibility contract number — identifies the parent eligibility contract |
| UNYO | Abbrev | 関連処理 (Kanren shori) — Related process / associated business process |
| ANK-2186-00-00 | Issue ID | Smart balance home-to-messaging change notification to KDDI — the business requirement that added this notification flow |
| OM-2014-0002549 | Issue ID | Change to support DSL and product plan change effective dates (2014/08/02) |
