cloudroam
3 天以前 3a819e4f668c15e8b77b188b322470da12bb7a43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.mzl.flower.service.customer;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.customer.CreateCollectDTO;
import com.mzl.flower.dto.request.flower.FlowerShowQueryDTO;
import com.mzl.flower.dto.response.flower.FlowerShowListDTO;
import com.mzl.flower.dto.response.flower.FlowerSupplierListDTO;
import com.mzl.flower.entity.customer.Collect;
import com.mzl.flower.mapper.customer.CollectMapper;
import com.mzl.flower.mapper.flower.FlowerMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
@Service
@Transactional
public class CollectService {
 
    private final CollectMapper collectMapper;
 
    private final FlowerMapper flowerMapper;
 
    public CollectService(CollectMapper collectMapper, FlowerMapper flowerMapper) {
        this.collectMapper = collectMapper;
        this.flowerMapper = flowerMapper;
    }
 
    public void add(CreateCollectDTO dto) {
        String userId = SecurityUtils.getUserId();
        Collect collect = collectMapper.selectOne(new LambdaQueryWrapper<Collect>()
                .eq(Collect::getUserId, userId)
                .eq(Collect::getFlowerId, dto.getFlowerId()));
        if(collect==null){
            collect = new Collect();
            collect.setUserId(userId);
            collect.setFlowerId(dto.getFlowerId());
            collect.create(userId);
            collectMapper.insert(collect);
        }
    }
 
    public void delete(Long flowerId) {
        Collect collect = collectMapper.selectOne(new LambdaQueryWrapper<Collect>()
                .eq(Collect::getFlowerId, flowerId)
                .eq(Collect::getUserId, SecurityUtils.getUserId()));
        if(collect!=null && collect.getUserId().equals(SecurityUtils.getUserId())){
            collectMapper.deleteById(collect.getId());
        }else {
            throw new ValidationException("取消收藏失败或者权限不足");
        }
    }
}