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;
}
}
@@ -0,0 +1,90 @@
package xuan.entity;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@Table(name = "deduct_record")
public class DeductRecord {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String userId;
@Column(nullable = false)
private String type;
@Column(nullable = false)
private String name;
private String occupy;
private Integer sex;
@Column(nullable = false)
private String inputDate;
@Column(columnDefinition = "TEXT")
private String inputData;
@Column(columnDefinition = "TEXT")
private String resultData;
@Column(columnDefinition = "TEXT")
private String aiAnalysis;
@Column(nullable = false, updatable = false)
private LocalDateTime createTime;
private LocalDateTime updateTime;
@PrePersist
protected void onCreate() {
createTime = LocalDateTime.now();
updateTime = createTime;
}
@PreUpdate
protected void onUpdate() {
updateTime = LocalDateTime.now();
}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getUserId() { return userId; }
public void setUserId(String userId) { this.userId = userId; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getOccupy() { return occupy; }
public void setOccupy(String occupy) { this.occupy = occupy; }
public Integer getSex() { return sex; }
public void setSex(Integer sex) { this.sex = sex; }
public String getInputDate() { return inputDate; }
public void setInputDate(String inputDate) { this.inputDate = inputDate; }
public String getInputData() { return inputData; }
public void setInputData(String inputData) { this.inputData = inputData; }
public String getResultData() { return resultData; }
public void setResultData(String resultData) { this.resultData = resultData; }
public String getAiAnalysis() { return aiAnalysis; }
public void setAiAnalysis(String aiAnalysis) { this.aiAnalysis = aiAnalysis; }
public LocalDateTime getCreateTime() { return createTime; }
public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; }
public LocalDateTime getUpdateTime() { return updateTime; }
public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; }
}
@@ -0,0 +1,20 @@
package xuan.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import xuan.entity.DeductRecord;
import java.util.List;
@Repository
public interface DeductRecordRepository extends JpaRepository<DeductRecord, Long> {
List<DeductRecord> findByUserIdOrderByCreateTimeDesc(String userId);
List<DeductRecord> findByUserIdAndTypeOrderByCreateTimeDesc(String userId, String type);
@Query("SELECT d FROM DeductRecord d WHERE d.userId = :userId ORDER BY d.createTime DESC LIMIT 10")
List<DeductRecord> findTop10ByUserIdOrderByCreateTimeDesc(@Param("userId") String userId);
}
@@ -0,0 +1,49 @@
package xuan.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xuan.entity.DeductRecord;
import xuan.repository.DeductRecordRepository;
import java.util.List;
import java.util.Optional;
@Service
public class DeductRecordService {
@Autowired
private DeductRecordRepository recordRepository;
@Transactional
public DeductRecord saveRecord(DeductRecord record) {
return recordRepository.save(record);
}
public Optional<DeductRecord> findById(Long id) {
return recordRepository.findById(id);
}
public List<DeductRecord> findByUserId(String userId) {
return recordRepository.findByUserIdOrderByCreateTimeDesc(userId);
}
public List<DeductRecord> findByUserIdAndType(String userId, String type) {
return recordRepository.findByUserIdAndTypeOrderByCreateTimeDesc(userId, type);
}
public List<DeductRecord> findRecentByUserId(String userId) {
return recordRepository.findTop10ByUserIdOrderByCreateTimeDesc(userId);
}
@Transactional
public void deleteById(Long id) {
recordRepository.deleteById(id);
}
@Transactional
public void deleteByUserId(String userId) {
List<DeductRecord> records = recordRepository.findByUserIdOrderByCreateTimeDesc(userId);
recordRepository.deleteAll(records);
}
}