feat: 添加数据库支持,保存测算记录
- 添加Spring Data JPA和H2/MySQL数据库依赖 - 创建DeductRecord实体类,保存测算记录 - 创建DeductRecordRepository和DeductRecordService - 创建RecordController,提供记录查询和删除API - 修改BaZiController,保存测算结果到数据库 - 配置H2数据库用于开发,MySQL用于生产环境
This commit is contained in:
1 parent
251a49d810
commit
683f55e4ca
7 files changed
+338
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user