add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.iotechn.unimall.app.api.favorite;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AppServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.component.CacheComponent;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.constant.CacheConst;
import com.iotechn.unimall.data.domain.FavoriteDO;
import com.iotechn.unimall.data.dto.FavoriteDTO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.FavoriteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
 
/*
@author kbq
@date  2019/7/5 - 10:48
*/
@Service
public class FavoriteServiceImpl implements FavoriteService {
 
    @Autowired
    private FavoriteMapper favoriteMapper;
 
    @Autowired
    private CacheComponent cacheComponent;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(Long spuId, Long userId) throws ServiceException {
        //校验SPU是否存在
        int count = favoriteMapper.selectCount(new QueryWrapper<FavoriteDO>()
                .eq("user_id", userId)
                .eq("spu_id", spuId));
        if (count > 0) {
            // 若收藏已存在,这可能是缓存与数据库不一致导致的,在正常的系统操作下不可能发生
            cacheComponent.putHashRaw(CacheConst.PRT_USER_FAVORITE_HASH_BUCKET + spuId, "U" + userId, "1");
            return "ok";
        }
        Date now = new Date();
        FavoriteDO favoriteDO = new FavoriteDO();
        favoriteDO.setSpuId(spuId);
        favoriteDO.setUserId(userId);
        favoriteDO.setGmtCreate(now);
        favoriteDO.setGmtUpdate(now);
        cacheComponent.putHashRaw(CacheConst.PRT_USER_FAVORITE_HASH_BUCKET + spuId, "U" + userId, "1");
        if (favoriteMapper.insert(favoriteDO) > 0) {
            return "ok";
        }
        throw new AppServiceException(ExceptionDefinition.APP_UNKNOWN_EXCEPTION);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long spuId, Long userId) throws ServiceException {
        Integer num = favoriteMapper.delete(
                new QueryWrapper<FavoriteDO>()
                        .eq("spu_id", spuId)
                        .eq("user_id", userId));
        if (num > 0) {
            cacheComponent.delHashKey(CacheConst.PRT_USER_FAVORITE_HASH_BUCKET + spuId, "U" + userId);
            return "ok";
        }
        throw new AppServiceException(ExceptionDefinition.PARAM_CHECK_FAILED);
    }
 
    @Override
    public Page<FavoriteDTO> list(Long userId, Integer pageNo, Integer pageSize) throws ServiceException {
        return favoriteMapper.getFavoritePage(Page.div(pageNo, pageSize, FavoriteDTO.class), userId);
    }
 
    @Override
    public FavoriteDTO getFavoriteById(Long userId, Long collectId, Long spuId) throws ServiceException {
        return favoriteMapper.getFavoriteById(userId, collectId, spuId);
    }
 
    @Override
    public Boolean getFavoriteBySpuId(Long spuId, Long userId) throws ServiceException {
        String hashRaw = cacheComponent.getHashRaw(CacheConst.PRT_USER_FAVORITE_HASH_BUCKET + spuId, "U" + userId);
        return hashRaw != null && "1".equals(hashRaw);
    }
}