崔向南 | 03d21b9 | 2025-06-05 17:42:23 +0800 | [diff] [blame] | 1 | package bounty.service; |
| 2 | |
| 3 | import bounty.domain.Bounty; |
| 4 | import bounty.mapper.BountyMapper; |
| 5 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 6 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 7 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 8 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| 9 | import com.ruoyi.common.utils.SecurityUtils; // 导入 Ruoyi 安全工具类 |
| 10 | import org.springframework.stereotype.Service; |
| 11 | import java.util.List; |
| 12 | |
| 13 | @Service |
| 14 | public class BountyServiceImpl extends ServiceImpl<BountyMapper, Bounty> implements BountyService { |
| 15 | @Override |
| 16 | public boolean saveBounty(Bounty bounty) { |
| 17 | return this.save(bounty); |
| 18 | } |
| 19 | |
| 20 | @Override |
| 21 | public IPage<Bounty> getBountiesByPage(Page<Bounty> page) { |
| 22 | QueryWrapper<Bounty> wrapper = new QueryWrapper<>(); |
| 23 | return this.page(page, wrapper); |
| 24 | } |
| 25 | |
| 26 | @Override |
| 27 | public Bounty getBountyById(Long id) { |
| 28 | return this.getById(id); |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public boolean publishBounty(Bounty bounty) { |
| 33 | // 校验标题不能为空 |
| 34 | if (bounty.getTitle() == null || bounty.getTitle().trim().isEmpty()) { |
| 35 | throw new IllegalArgumentException("悬赏标题不能为空"); |
| 36 | } |
| 37 | |
| 38 | // 获取当前用户 ID 并设置到 Bounty 对象 |
| 39 | Long currentUserId = SecurityUtils.getUserId(); // 使用 Ruoyi 工具类获取用户 ID |
| 40 | bounty.setCreator_id(currentUserId); // 设置 creator_id(需与实体类字段名一致) |
| 41 | // 设置默认状态(假设 1 为已发布) |
| 42 | bounty.setStatus(1); |
| 43 | return this.save(bounty); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | @Override |
| 48 | public List<Bounty> getBounties() { |
| 49 | // 直接调用 MyBatis-Plus 的 list 方法查询所有数据(可根据需求添加查询条件) |
| 50 | return baseMapper.selectList(); // null 表示无查询条件,查询所有 |
| 51 | } |
| 52 | |
| 53 | } |