我是人,我提交了悬赏功能哦!悬赏功能是:1.人可以发布悬赏 2.人可以回复悬赏 3.只有回复人和悬赏发布者可以下载回复的附件
Change-Id: I269fb69c6ee4dd695a38fa0c91fa8fbe72fc5322
diff --git a/ruoyi-system/src/main/java/bounty/service/BountyService.java b/ruoyi-system/src/main/java/bounty/service/BountyService.java
new file mode 100644
index 0000000..a65e487
--- /dev/null
+++ b/ruoyi-system/src/main/java/bounty/service/BountyService.java
@@ -0,0 +1,26 @@
+package bounty.service;
+
+import bounty.domain.Bounty;
+import bounty.domain.BountySubmission;
+import bounty.mapper.BountyMapper;
+import bounty.mapper.BountySubmissionMapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+
+import java.util.List;
+
+// 悬赏 Service 接口
+public interface BountyService {
+ boolean saveBounty(Bounty bounty);
+ IPage<Bounty> getBountiesByPage(Page<Bounty> page);
+ Bounty getBountyById(Long id);
+ // 新增:发布悬赏(带业务校验)
+ boolean publishBounty(Bounty bounty);
+ List<Bounty> getBounties(); //新增直接获得所有悬赏
+}
+
+
diff --git a/ruoyi-system/src/main/java/bounty/service/BountyServiceImpl.java b/ruoyi-system/src/main/java/bounty/service/BountyServiceImpl.java
new file mode 100644
index 0000000..fd6d635
--- /dev/null
+++ b/ruoyi-system/src/main/java/bounty/service/BountyServiceImpl.java
@@ -0,0 +1,53 @@
+package bounty.service;
+
+import bounty.domain.Bounty;
+import bounty.mapper.BountyMapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.utils.SecurityUtils; // 导入 Ruoyi 安全工具类
+import org.springframework.stereotype.Service;
+import java.util.List;
+
+@Service
+public class BountyServiceImpl extends ServiceImpl<BountyMapper, Bounty> implements BountyService {
+ @Override
+ public boolean saveBounty(Bounty bounty) {
+ return this.save(bounty);
+ }
+
+ @Override
+ public IPage<Bounty> getBountiesByPage(Page<Bounty> page) {
+ QueryWrapper<Bounty> wrapper = new QueryWrapper<>();
+ return this.page(page, wrapper);
+ }
+
+ @Override
+ public Bounty getBountyById(Long id) {
+ return this.getById(id);
+ }
+
+ @Override
+ public boolean publishBounty(Bounty bounty) {
+ // 校验标题不能为空
+ if (bounty.getTitle() == null || bounty.getTitle().trim().isEmpty()) {
+ throw new IllegalArgumentException("悬赏标题不能为空");
+ }
+
+ // 获取当前用户 ID 并设置到 Bounty 对象
+ Long currentUserId = SecurityUtils.getUserId(); // 使用 Ruoyi 工具类获取用户 ID
+ bounty.setCreator_id(currentUserId); // 设置 creator_id(需与实体类字段名一致)
+ // 设置默认状态(假设 1 为已发布)
+ bounty.setStatus(1);
+ return this.save(bounty);
+ }
+
+
+ @Override
+ public List<Bounty> getBounties() {
+ // 直接调用 MyBatis-Plus 的 list 方法查询所有数据(可根据需求添加查询条件)
+ return baseMapper.selectList(); // null 表示无查询条件,查询所有
+ }
+
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/bounty/service/BountySubmissionService.java b/ruoyi-system/src/main/java/bounty/service/BountySubmissionService.java
new file mode 100644
index 0000000..707f54c
--- /dev/null
+++ b/ruoyi-system/src/main/java/bounty/service/BountySubmissionService.java
@@ -0,0 +1,14 @@
+package bounty.service;
+
+import bounty.domain.BountySubmission;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+
+// 悬赏提交 Service 接口
+public interface BountySubmissionService {
+ boolean saveBountySubmission(BountySubmission bountySubmission);
+ IPage<BountySubmission> getBountySubmissionsByPage(Page<BountySubmission> page, Long bountyId);
+ boolean adoptSubmission(Long submissionId);
+}
+
+
diff --git a/ruoyi-system/src/main/java/bounty/service/BountySubmissionServiceImpl.java b/ruoyi-system/src/main/java/bounty/service/BountySubmissionServiceImpl.java
new file mode 100644
index 0000000..4300911
--- /dev/null
+++ b/ruoyi-system/src/main/java/bounty/service/BountySubmissionServiceImpl.java
@@ -0,0 +1,154 @@
+package bounty.service;
+
+import bounty.domain.Bounty;
+import bounty.domain.BountySubmission;
+import bounty.mapper.BountyMapper;
+import bounty.mapper.BountySubmissionMapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.utils.SecurityUtils;
+import org.mybatis.spring.MyBatisSystemException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Date;
+
+@Service
+public class BountySubmissionServiceImpl extends ServiceImpl<BountySubmissionMapper, BountySubmission> implements BountySubmissionService {
+
+ @Autowired
+ private BountyMapper bountyMapper; // 注入 BountyMapper 校验悬赏存在性
+
+ @Override
+ public boolean saveBountySubmission(BountySubmission bountySubmission) {
+ // 校验 1:悬赏 ID 不能为空
+ Long bountyId = bountySubmission.getBountyId();
+ if (bountyId == null) {
+ throw new IllegalArgumentException("悬赏 ID 不能为空");
+ }
+
+ // 校验 2:目标悬赏是否存在
+ Bounty targetBounty = bountyMapper.selectById(bountyId);
+ if (targetBounty == null) {
+ throw new IllegalArgumentException("无效的悬赏 ID,未找到对应悬赏");
+ }
+
+ // 校验 3:提交内容不能为空
+ String content = bountySubmission.getContent();
+ if (!StringUtils.hasText(content)) {
+ throw new IllegalArgumentException("提交内容不能为空");
+ }
+
+ // 自动填充当前用户 ID(需用户已登录)
+ Long currentUserId = SecurityUtils.getUserId();
+ bountySubmission.setUserId(currentUserId);
+
+
+ // 调用 MyBatis-Plus 保存方法
+ boolean saveResult = this.save(bountySubmission);
+
+ // 新增:保存成功后,更新对应悬赏的状态为1(已回复)
+ if (saveResult) {
+ targetBounty.setStatus(1); // 将悬赏状态改为1
+ bountyMapper.updateById(targetBounty); // 执行更新
+ }
+
+ return saveResult;
+ }
+
+ @Override
+ public IPage<BountySubmission> getBountySubmissionsByPage(Page<BountySubmission> page, Long bountyId) {
+ QueryWrapper<BountySubmission> wrapper = new QueryWrapper<>();
+ wrapper.eq("bounty_id", bountyId);
+ return this.page(page, wrapper);
+ }
+
+ private static final Logger log = LoggerFactory.getLogger(BountySubmissionServiceImpl.class);
+
+ @Override
+ public boolean adoptSubmission(Long submissionId) {
+ log.info("【采纳流程开始】submissionId={}, 当前时间={}", submissionId, new Date());
+
+ try {
+ // 参数校验
+ if (submissionId == null || submissionId <= 0) {
+ log.warn("【参数校验失败】submissionId为空或无效");
+ throw new IllegalArgumentException("无效的回复ID");
+ }
+
+ // 获取回复详情
+ BountySubmission submission = this.getById(submissionId);
+ if (submission == null) {
+ log.warn("【回复不存在】submissionId={}", submissionId);
+ throw new IllegalArgumentException("无效的回复ID");
+ }
+
+ if (submission.getId() == null || submission.getBountyId() == null) {
+ log.warn("【无效提交数据】submissionId={}", submissionId);
+ throw new IllegalArgumentException("提交数据不完整");
+ }
+
+
+ log.debug("【原始回复数据】{}", submission);
+
+ // 获取关联悬赏
+ Bounty bounty = bountyMapper.selectById(submission.getBountyId());
+
+ if (bounty == null) {
+ log.warn("【关联悬赏不存在】bountyId={}", submission.getBountyId());
+ throw new IllegalArgumentException("关联悬赏不存在");
+ }
+
+ // 用户权限校验
+ Long currentUserId = SecurityUtils.getUserId();
+ if (currentUserId == null) {
+ log.warn("【用户未登录】");
+ throw new IllegalArgumentException("用户未登录");
+ }
+
+
+ if (!bounty.getCreator_id().equals(currentUserId)) {
+ log.warn("【权限不足】用户ID={} 试图访问悬赏ID={}", currentUserId, bounty.getId());
+ throw new IllegalArgumentException("无权操作");
+ }
+
+ // 状态校验
+ if (submission.getStatus() == 1) {
+ log.warn("【重复采纳】submissionId={}", submissionId);
+ throw new IllegalArgumentException("该回复已被采纳");
+ }
+
+ log.info("【更新前对象】{}", submission);
+
+ // 执行更新
+ submission.setStatus(1);
+ boolean submissionResult = this.updateById(submission);
+ log.info("【更新后对象】{}", this.getById(submissionId));
+
+
+ log.info("【操作结果】submissionResult={}, 新状态={}", submissionResult, submission.getStatus());
+
+ return submissionResult;
+ } catch (MyBatisSystemException e) {
+ // 显式捕获 MyBatis 异常
+ log.error("【MyBatis 异常】submissionId={}, rootCause={}", submissionId, e.getRootCause());
+ log.error("【异常详情】", e); // 打印完整堆栈
+ throw new RuntimeException("MyBatis 错误: " + e.getRootCause(), e);
+
+ } catch (IllegalArgumentException e) {
+ log.warn("【参数异常】submissionId={}, message={}", submissionId, e.getMessage());
+ throw e;
+ } catch (Exception e) {
+ log.error("【采纳异常】submissionId={}, 错误={}", submissionId, e.getMessage(), e);
+ log.error("【未分类异常】submissionId={}, exceptionClass={}", submissionId, e.getClass().getName());
+ log.error("【异常详情】", e); // 打印完整堆栈
+ throw e;
+ }
+ }
+}
diff --git a/ruoyi-system/src/main/java/bounty/service/FileStorageService.java b/ruoyi-system/src/main/java/bounty/service/FileStorageService.java
new file mode 100644
index 0000000..9f12122
--- /dev/null
+++ b/ruoyi-system/src/main/java/bounty/service/FileStorageService.java
@@ -0,0 +1,55 @@
+package bounty.service;
+
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.UUID;
+
+@Service
+public class FileStorageService {
+ public String saveFile(MultipartFile file) {
+ try {
+ // 1. 校验文件名非空
+ String originalFilename = file.getOriginalFilename();
+ if (originalFilename == null || originalFilename.isEmpty()) {
+ throw new IllegalArgumentException("上传文件名不能为空");
+ }
+
+ // 2. 生成唯一文件名
+ String fileExtension = "";
+ int dotIndex = originalFilename.lastIndexOf(".");
+ if (dotIndex > 0) {
+ fileExtension = originalFilename.substring(dotIndex);
+ }
+ String newFilename = UUID.randomUUID() + fileExtension;
+
+ // 👇 新增:打印文件名生成结果(用于调试)
+ System.out.println("【生成文件名】" + newFilename);
+
+ // 3. 指定存储路径
+ Path uploadDir = Paths.get("uploads/");
+ if (!Files.exists(uploadDir)) {
+ Files.createDirectories(uploadDir);
+ }
+
+ // 👇 新增:打印完整路径(用于调试)
+ Path filePath = uploadDir.resolve(newFilename);
+ System.out.println("【文件保存路径】" + filePath.toAbsolutePath());
+
+ // 4. 写入文件
+ Files.write(filePath, file.getBytes());
+
+ // 5. 返回访问路径
+ return "/uploads/" + newFilename;
+ } catch (IOException e) {
+ // 👇 新增:记录异常信息
+ System.err.println("【文件上传失败】" + e.getMessage());
+ throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
+ }
+ }
+}
+