# Business Blueprint Draft Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Add a safe business blueprint draft flow so AI can return business actions such as borrow and return alongside generated table design.

**Architecture:** Extend the existing `DatabaseDesignResponse` draft contract with `businessActions`, normalize and validate action metadata in the backend, persist it on `front_project`, expose it to the Vue workbench, and pass it into generated project Velocity context. The MVP stores blueprint JSON on the project draft and keeps actual code expansion template-ready rather than hardcoding generated business methods in this pass.

**Tech Stack:** Java 8, Spring Boot, MyBatis, Fastjson2/Jackson, JUnit 4/Mockito, Vue 3, Element Plus, Velocity.

---

## File Structure

- `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/front/dto/BusinessActionDesign.java`: new DTO for one business action draft.
- `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/front/dto/BusinessActionEffectDesign.java`: new DTO for normalized effects such as update/insert/status change.
- `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/front/dto/DatabaseDesignResponse.java`: add `List<BusinessActionDesign> businessActions`.
- `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/front/FrontProject.java`: add `businessBlueprint`.
- `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenProject.java`: add transient `businessActions`.
- `ruoyi-generator/src/main/java/com/ruoyi/generator/service/front/AiBusinessBlueprintValidator.java`: new validator for action codes, table references, request fields, rule/effect limits, and safe strings.
- `ruoyi-generator/src/main/java/com/ruoyi/generator/service/front/AiGenerateServiceImpl.java`: prompt for business actions, parse aliases, normalize actions, validate blueprint, and persist JSON.
- `ruoyi-generator/src/main/java/com/ruoyi/generator/service/front/FrontProjectServiceImpl.java`: save/load blueprint JSON with database draft.
- `ruoyi-generator/src/main/java/com/ruoyi/generator/service/front/FrontProjectConverter.java`: copy business actions into `GenProject`.
- `ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java`: expose `businessActions` to project-level templates.
- `ruoyi-generator/src/main/resources/mapper/front/FrontProjectMapper.xml`: map `business_blueprint`.
- `sql/front_workbench.sql` and `sql/db.sql`: add `business_blueprint longtext`.
- `easycode-web/src/views/GenerateView.vue`: show business blueprint draft returned by AI and saved with the database design.
- Backend tests in `AiGenerateServiceImplTest`, `FrontProjectServiceImplTest`, `FrontProjectPreviewServiceImplTest`.

---

### Task 1: Backend Blueprint DTO And Validation

**Files:**
- Create: `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/front/dto/BusinessActionDesign.java`
- Create: `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/front/dto/BusinessActionEffectDesign.java`
- Modify: `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/front/dto/DatabaseDesignResponse.java`
- Create: `ruoyi-generator/src/main/java/com/ruoyi/generator/service/front/AiBusinessBlueprintValidator.java`
- Test: `ruoyi-generator/src/test/java/com/ruoyi/generator/service/front/AiGenerateServiceImplTest.java`

- [ ] **Step 1: Write failing tests**

Add tests asserting AI returns `businessActions`, unsafe action codes are normalized, and actions referencing missing tables are rejected.

- [ ] **Step 2: Run tests and verify RED**

Run: `mvn -pl ruoyi-generator -Dtest=AiGenerateServiceImplTest test`
Expected: compile/test failure because `getBusinessActions()` and validator classes do not exist.

- [ ] **Step 3: Add DTOs and validator**

Add action fields: `code`, `name`, `ownerTable`, `method`, `path`, `transaction`, `requestFields`, `rules`, `effects`.
Add effect fields: `type`, `targetTable`, `targetField`, `value`, `description`.
Validator rules: max 12 actions, code/path safe, owner table exists, request fields exist in any table, effect tables exist, rule/effect text length bounded.

- [ ] **Step 4: Run tests and verify GREEN**

Run: `mvn -pl ruoyi-generator -Dtest=AiGenerateServiceImplTest test`
Expected: PASS.

### Task 2: Persist And Reload Blueprint Draft

**Files:**
- Modify: `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/front/FrontProject.java`
- Modify: `ruoyi-generator/src/main/resources/mapper/front/FrontProjectMapper.xml`
- Modify: `ruoyi-generator/src/main/java/com/ruoyi/generator/service/front/FrontProjectServiceImpl.java`
- Modify: `sql/front_workbench.sql`
- Modify: `sql/db.sql`
- Test: `ruoyi-generator/src/test/java/com/ruoyi/generator/service/front/FrontProjectServiceImplTest.java`

- [ ] **Step 1: Write failing tests**

Add tests asserting `saveDatabase()` writes `businessBlueprint` JSON and `getDatabase()` returns `businessActions`.

- [ ] **Step 2: Run tests and verify RED**

Run: `mvn -pl ruoyi-generator -Dtest=FrontProjectServiceImplTest test`
Expected: failure because `businessBlueprint` is not mapped or returned.

- [ ] **Step 3: Implement persistence**

Add `business_blueprint longtext` to front project SQL, map it in MyBatis, add `FrontProject.businessBlueprint`, serialize `request.getBusinessActions()` on save, and deserialize on get.

- [ ] **Step 4: Run tests and verify GREEN**

Run: `mvn -pl ruoyi-generator -Dtest=FrontProjectServiceImplTest test`
Expected: PASS.

### Task 3: Expose Blueprint To Preview Generation

**Files:**
- Modify: `ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenProject.java`
- Modify: `ruoyi-generator/src/main/java/com/ruoyi/generator/service/front/FrontProjectConverter.java`
- Modify: `ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java`
- Test: `ruoyi-generator/src/test/java/com/ruoyi/generator/service/front/FrontProjectPreviewServiceImplTest.java`

- [ ] **Step 1: Write failing test**

Add a test that converts a front draft with one business action and asserts `GenProject.businessActions` is passed into the generation service and project Velocity context can read it.

- [ ] **Step 2: Run test and verify RED**

Run: `mvn -pl ruoyi-generator -Dtest=FrontProjectPreviewServiceImplTest test`
Expected: compile/test failure because `GenProject.getBusinessActions()` does not exist.

- [ ] **Step 3: Implement conversion and Velocity context**

Add transient `businessActions` to `GenProject`, copy it in `FrontProjectConverter`, and put `businessActions` plus `hasBusinessActions` into `VelocityUtils.prepareContextProject`.

- [ ] **Step 4: Run test and verify GREEN**

Run: `mvn -pl ruoyi-generator -Dtest=FrontProjectPreviewServiceImplTest test`
Expected: PASS.

### Task 4: Show Blueprint Draft In Workbench

**Files:**
- Modify: `easycode-web/src/views/GenerateView.vue`

- [ ] **Step 1: Add frontend normalization**

Extend `normalizeDatabase()` with `businessActions: Array.isArray(data.businessActions) ? data.businessActions : []`.

- [ ] **Step 2: Add compact UI**

Add a full-width panel under database design that lists action name, method, path, owner table, rules, and effects. Keep it read-only for this MVP.

- [ ] **Step 3: Build frontend**

Run: use the existing Node build command for `easycode-web` if dependencies are available.
Expected: build passes, or dependency/network blocker is reported.

### Task 5: Full Verification

**Files:**
- No new files.

- [ ] **Step 1: Run focused backend tests**

Run: `mvn -pl ruoyi-generator -Dtest=AiGenerateServiceImplTest,FrontProjectServiceImplTest,FrontProjectPreviewServiceImplTest test`
Expected: PASS.

- [ ] **Step 2: Run final status check**

Run: `git status --short`
Expected: only intended files modified.

