|
|
@@ -0,0 +1,110 @@
|
|
|
+package com.ylx.collect.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.ylx.base.BaseServiceImpl;
|
|
|
+import com.ylx.collect.domain.Collect;
|
|
|
+import com.ylx.collect.domain.dto.CollectOperateDTO;
|
|
|
+import com.ylx.collect.domain.vo.CollectPageVO;
|
|
|
+import com.ylx.collect.mapper.CollectMapper;
|
|
|
+import com.ylx.collect.service.CollectService;
|
|
|
+import com.ylx.common.utils.DateUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class CollectServiceImpl extends BaseServiceImpl<CollectMapper, Collect> implements CollectService {
|
|
|
+
|
|
|
+ private static final int NOT_DELETE = 0;
|
|
|
+ private static final int DELETE = 1;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean addCollect(CollectOperateDTO dto) {
|
|
|
+ // 1. 直接调用基类方法获取用户ID,无需再手动校验登录状态
|
|
|
+ Long userId = getCurrentUserId();
|
|
|
+
|
|
|
+ // 2. 幂等性检查:防止重复收藏
|
|
|
+ LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(Collect::getUserId, userId)
|
|
|
+ .eq(Collect::getIsDelete, NOT_DELETE)
|
|
|
+ .eq(Collect::getMerchantId, dto.getMerchantId());
|
|
|
+ if (this.count(wrapper) > 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 执行新增
|
|
|
+ Collect collect = new Collect();
|
|
|
+ collect.setUserId(userId);
|
|
|
+ collect.setMerchantId(dto.getMerchantId());
|
|
|
+ collect.setCreateTime(DateUtils.getNowDate());
|
|
|
+ collect.setCreateBy(getCurrentUserNickName());
|
|
|
+ collect.setIsDelete(NOT_DELETE);
|
|
|
+ return this.save(collect);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean cancelCollect(CollectOperateDTO dto) {
|
|
|
+ // 1. 获取当前用户
|
|
|
+ Long userId = getCurrentUserId();
|
|
|
+
|
|
|
+ Collect updateEntity = new Collect();
|
|
|
+ updateEntity.setIsDelete(DELETE);
|
|
|
+ updateEntity.setUpdateTime(DateUtils.getNowDate());
|
|
|
+ updateEntity.setUpdateBy(getCurrentUserNickName());
|
|
|
+
|
|
|
+ // 2. 构造删除条件
|
|
|
+ LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(Collect::getUserId, userId)
|
|
|
+ .eq(Collect::getIsDelete, NOT_DELETE)
|
|
|
+ .eq(Collect::getMerchantId, dto.getMerchantId());
|
|
|
+
|
|
|
+ // 3. 执行逻辑删除
|
|
|
+ return this.update(updateEntity, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<CollectPageVO> page(CollectOperateDTO dto) {
|
|
|
+ // 1. 获取当前用户,确保只能查自己的收藏
|
|
|
+ Long userId = getCurrentUserId();
|
|
|
+
|
|
|
+ // 2. 构建分页对象
|
|
|
+ Page<Collect> pageParam = new Page<>(dto.getCurrent(), dto.getSize());
|
|
|
+
|
|
|
+ // 3. 构建查询条件
|
|
|
+ LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(Collect::getUserId, userId)
|
|
|
+ .eq(Collect::getIsDelete, NOT_DELETE)
|
|
|
+ .orderByDesc(Collect::getCreateTime); // 按时间倒序
|
|
|
+
|
|
|
+ // 4. 执行查询
|
|
|
+ Page<Collect> resultPage = this.page(pageParam, wrapper);
|
|
|
+
|
|
|
+ Page<CollectPageVO> voPage = new Page<>();
|
|
|
+ // 1. 复制分页元数据(总条数、当前页码等)
|
|
|
+ voPage.setCurrent(resultPage.getCurrent());
|
|
|
+ voPage.setSize(resultPage.getSize());
|
|
|
+ voPage.setTotal(resultPage.getTotal());
|
|
|
+ voPage.setPages(resultPage.getPages());
|
|
|
+
|
|
|
+ // 2. 转换列表数据
|
|
|
+ List<CollectPageVO> voList = resultPage.getRecords().stream().map(collect -> {
|
|
|
+ CollectPageVO vo = new CollectPageVO();
|
|
|
+ // 属性拷贝
|
|
|
+ BeanUtil.copyProperties(collect, vo);
|
|
|
+ return vo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ voPage.setRecords(voList);
|
|
|
+ return voPage;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|