feat: 添加数据库支持,保存测算记录

- 添加Spring Data JPA和H2/MySQL数据库依赖
- 创建DeductRecord实体类,保存测算记录
- 创建DeductRecordRepository和DeductRecordService
- 创建RecordController,提供记录查询和删除API
- 修改BaZiController,保存测算结果到数据库
- 配置H2数据库用于开发,MySQL用于生产环境
This commit is contained in:
H1d3rOne committed 2026-03-07 22:54:02 +08:00
1 parent 251a49d810
commit 683f55e4ca
7 files changed
+338

No files matched your search

@@ -1,12 +1,16 @@
package xuan.controller;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuan.core.bazi.BaZi;
import xuan.core.bazi.settings.BaZiJiChuSetting;
import xuan.core.bazi.settings.BaZiShenShaSetting;
import xuan.core.bazi.settings.BaZiGanZhiLiuYiSetting;
import xuan.entity.DeductRecord;
import xuan.service.HunYuanAiService;
import xuan.service.PromptTemplates;
import xuan.service.DeductRecordService;
import java.util.HashMap;
import java.util.Map;
@@ -16,6 +20,9 @@ import java.util.Map;
public class BaZiController {
private final HunYuanAiService aiService = new HunYuanAiService();
@Autowired
private DeductRecordService recordService;
@PostMapping("/paipan")
public Map<String, Object> paiPan(@RequestBody BaZiRequest request) {
@@ -49,6 +56,20 @@ public class BaZiController {
);
data.put("aiAnalysis", aiAnalysis);
DeductRecord record = new DeductRecord();
record.setUserId(request.getUserId() != null ? request.getUserId() : "default_user");
record.setType("bazi");
record.setName(request.getName() != null ? request.getName() : "某人");
record.setOccupy(request.getOccupy());
record.setSex(request.getSex());
record.setInputDate(request.getDate());
record.setInputData(JSON.toJSONString(request));
record.setResultData(JSON.toJSONString(data));
record.setAiAnalysis(aiAnalysis);
recordService.saveRecord(record);
data.put("recordId", record.getId());
result.put("success", true);
result.put("data", data);
} catch (Exception e) {
@@ -94,6 +115,7 @@ public class BaZiController {
}
public static class BaZiRequest {
private String userId;
private String name;
private String occupy;
private Integer sex;
@@ -109,6 +131,8 @@ public class BaZiController {
private Integer monthGanZhiType;
private Integer dayGanZhiType;
public String getUserId() { return userId; }
public void setUserId(String userId) { this.userId = userId; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getOccupy() { return occupy; }
@@ -0,0 +1,110 @@
package xuan.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuan.entity.DeductRecord;
import xuan.service.DeductRecordService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@RestController
@RequestMapping("/api/records")
public class RecordController {
@Autowired
private DeductRecordService recordService;
@GetMapping("/list")
public Map<String, Object> getRecords(@RequestParam String userId) {
Map<String, Object> result = new HashMap<>();
try {
List<DeductRecord> records = recordService.findByUserId(userId);
result.put("success", true);
result.put("data", records);
} catch (Exception e) {
result.put("success", false);
result.put("error", e.getMessage());
}
return result;
}
@GetMapping("/recent")
public Map<String, Object> getRecentRecords(@RequestParam String userId) {
Map<String, Object> result = new HashMap<>();
try {
List<DeductRecord> records = recordService.findRecentByUserId(userId);
result.put("success", true);
result.put("data", records);
} catch (Exception e) {
result.put("success", false);
result.put("error", e.getMessage());
}
return result;
}
@GetMapping("/type")
public Map<String, Object> getRecordsByType(
@RequestParam String userId,
@RequestParam String type) {
Map<String, Object> result = new HashMap<>();
try {
List<DeductRecord> records = recordService.findByUserIdAndType(userId, type);
result.put("success", true);
result.put("data", records);
} catch (Exception e) {
result.put("success", false);
result.put("error", e.getMessage());
}
return result;
}
@GetMapping("/detail")
public Map<String, Object> getRecordDetail(@RequestParam Long id) {
Map<String, Object> result = new HashMap<>();
try {
Optional<DeductRecord> record = recordService.findById(id);
if (record.isPresent()) {
result.put("success", true);
result.put("data", record.get());
} else {
result.put("success", false);
result.put("error", "记录不存在");
}
} catch (Exception e) {
result.put("success", false);
result.put("error", e.getMessage());
}
return result;
}
@DeleteMapping("/delete")
public Map<String, Object> deleteRecord(@RequestParam Long id) {
Map<String, Object> result = new HashMap<>();
try {
recordService.deleteById(id);
result.put("success", true);
result.put("message", "删除成功");
} catch (Exception e) {
result.put("success", false);
result.put("error", e.getMessage());
}
return result;
}
@DeleteMapping("/clear")
public Map<String, Object> clearRecords(@RequestParam String userId) {
Map<String, Object> result = new HashMap<>();
try {
recordService.deleteByUserId(userId);
result.put("success", true);
result.put("message", "清空成功");
} catch (Exception e) {
result.put("success", false);
result.put("error", e.getMessage());
}
return result;
}
}