新增消费详情功能,记录金币使用明细
后端实现: - 新增CoinConsumeRecord实体类,记录消费明细 - 新增CoinConsumeRecordRepository和CoinConsumeRecordService - 新增CoinConsumeController,提供消费记录API - 支持分页查询和按类型查询 前端实现: - 新增消费详情页面consume-details.vue - 显示累计消费和消费次数统计 - 展示消费记录列表,包含类型、描述、时间、金额 - 支持分页加载 功能集成: - 在“我的”页面“金币任务”下方添加“消费详情”入口 - 修改coinUtils.js,consumeCoin方法增加参数 - 金币消耗时自动记录消费明细 - 记录消费类型、描述、关联ID等信息 API接口: - POST /api/coin-consume/save - 保存消费记录 - GET /api/coin-consume/list - 分页查询消费记录 - GET /api/coin-consume/all - 查询所有消费记录 - GET /api/coin-consume/type - 按类型查询消费记录
This commit is contained in:
1 parent
1d76df3ead
commit
14b209bf85
8 files changed
+630
-12
No files matched your search
@@ -0,0 +1,88 @@
|
||||
package xuan.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xuan.entity.CoinConsumeRecord;
|
||||
import xuan.service.CoinConsumeRecordService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/coin-consume")
|
||||
public class CoinConsumeController {
|
||||
|
||||
@Autowired
|
||||
private CoinConsumeRecordService recordService;
|
||||
|
||||
@PostMapping("/save")
|
||||
public Map<String, Object> saveRecord(@RequestBody CoinConsumeRecord record) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
CoinConsumeRecord saved = recordService.saveRecord(record);
|
||||
result.put("success", true);
|
||||
result.put("data", saved);
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("error", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public Map<String, Object> getRecords(
|
||||
@RequestParam String userId,
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@RequestParam(defaultValue = "10") int size) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
Page<CoinConsumeRecord> pageResult = recordService.findByUserId(userId, page, size);
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("records", pageResult.getContent());
|
||||
data.put("total", pageResult.getTotalElements());
|
||||
data.put("page", page);
|
||||
data.put("size", size);
|
||||
data.put("totalPages", pageResult.getTotalPages());
|
||||
|
||||
result.put("success", true);
|
||||
result.put("data", data);
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("error", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public Map<String, Object> getAllRecords(@RequestParam String userId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
List<CoinConsumeRecord> 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("/type")
|
||||
public Map<String, Object> getRecordsByType(
|
||||
@RequestParam String userId,
|
||||
@RequestParam String type) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
List<CoinConsumeRecord> 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package xuan.entity;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Table(name = "coin_consume_record")
|
||||
public class CoinConsumeRecord {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String userId;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer amount;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String type;
|
||||
|
||||
private String description;
|
||||
|
||||
private String relatedId;
|
||||
|
||||
@Column(nullable = false, updatable = false)
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
createTime = 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 Integer getAmount() { return amount; }
|
||||
public void setAmount(Integer amount) { this.amount = amount; }
|
||||
|
||||
public String getType() { return type; }
|
||||
public void setType(String type) { this.type = type; }
|
||||
|
||||
public String getDescription() { return description; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
|
||||
public String getRelatedId() { return relatedId; }
|
||||
public void setRelatedId(String relatedId) { this.relatedId = relatedId; }
|
||||
|
||||
public LocalDateTime getCreateTime() { return createTime; }
|
||||
public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package xuan.repository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import xuan.entity.CoinConsumeRecord;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface CoinConsumeRecordRepository extends JpaRepository<CoinConsumeRecord, Long> {
|
||||
|
||||
List<CoinConsumeRecord> findByUserIdOrderByCreateTimeDesc(String userId);
|
||||
|
||||
Page<CoinConsumeRecord> findByUserIdOrderByCreateTimeDesc(String userId, Pageable pageable);
|
||||
|
||||
List<CoinConsumeRecord> findByUserIdAndTypeOrderByCreateTimeDesc(String userId, String type);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package xuan.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import xuan.entity.CoinConsumeRecord;
|
||||
import xuan.repository.CoinConsumeRecordRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class CoinConsumeRecordService {
|
||||
|
||||
@Autowired
|
||||
private CoinConsumeRecordRepository recordRepository;
|
||||
|
||||
@Transactional
|
||||
public CoinConsumeRecord saveRecord(CoinConsumeRecord record) {
|
||||
return recordRepository.save(record);
|
||||
}
|
||||
|
||||
public Optional<CoinConsumeRecord> findById(Long id) {
|
||||
return recordRepository.findById(id);
|
||||
}
|
||||
|
||||
public List<CoinConsumeRecord> findByUserId(String userId) {
|
||||
return recordRepository.findByUserIdOrderByCreateTimeDesc(userId);
|
||||
}
|
||||
|
||||
public Page<CoinConsumeRecord> findByUserId(String userId, int page, int size) {
|
||||
Pageable pageable = PageRequest.of(page - 1, size);
|
||||
return recordRepository.findByUserIdOrderByCreateTimeDesc(userId, pageable);
|
||||
}
|
||||
|
||||
public List<CoinConsumeRecord> findByUserIdAndType(String userId, String type) {
|
||||
return recordRepository.findByUserIdAndTypeOrderByCreateTimeDesc(userId, type);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteById(Long id) {
|
||||
recordRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteByUserId(String userId) {
|
||||
List<CoinConsumeRecord> records = recordRepository.findByUserIdOrderByCreateTimeDesc(userId);
|
||||
recordRepository.deleteAll(records);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user