admin
2018-12-04 57b80e02ce56e3cacff75ba789fc633ab46b911a
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.yeshi.fanli.service.impl.goods;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.goods.CollectionGoodsV2Mapper;
import com.yeshi.fanli.dao.mybatis.goods.CommonGoodsMapper;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.goods.CollectionGoodsException;
import com.yeshi.fanli.exception.goods.CommonGoodsException;
import com.yeshi.fanli.goods.CollectionGoodsV2;
import com.yeshi.fanli.goods.CommonGoods;
import com.yeshi.fanli.service.inter.goods.CollectionGoodsV2Service;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
 
@Service
public class CollectionGoodsV2ServiceImpl implements CollectionGoodsV2Service {
 
    @Resource
    private CollectionGoodsV2Mapper collectionGoodsV2Mapper;
 
    @Resource
    private CommonGoodsMapper commonGoodsMapper;
 
    @Resource
    private CommonGoodsService commonGoodsService;
 
    @Transactional
    @Override
    public void addCollection(Long uid, TaoBaoGoodsBrief goods) throws CollectionGoodsException {
        if (uid == null || uid == 0)
            throw new CollectionGoodsException(1, "无用户ID");
        CommonGoods commonGoods = CommonGoodsFactory.create(goods);
        if (commonGoods == null)
            throw new CollectionGoodsException(2, "商品信息获取失败");
        // 判断商品是否存在
        try {
            commonGoods = commonGoodsService.addOrUpdateCommonGoods(commonGoods);
        } catch (CommonGoodsException e) {
            commonGoods = null;
        }
        if (commonGoods == null || commonGoods.getId() == null)
            throw new CollectionGoodsException(3, "商品信息添加失败");
 
        CollectionGoodsV2 collectionGoodsV2 = collectionGoodsV2Mapper.selectByUidAndCommonGoodsId(uid,
                commonGoods.getId());
        if (collectionGoodsV2 != null)
            throw new CollectionGoodsException(4, "商品已被收藏");
 
        collectionGoodsV2 = new CollectionGoodsV2();
        collectionGoodsV2.setCommonGoods(commonGoods);
        collectionGoodsV2.setCreateTime(new Date());
        collectionGoodsV2.setUpdateTime(new Date());
        collectionGoodsV2.setUserInfo(new UserInfo(uid));
        collectionGoodsV2Mapper.insertSelective(collectionGoodsV2);
    }
 
    @Override
    public void addCollection(CollectionGoodsV2 goods) throws CollectionGoodsException {
        if (goods.getUserInfo() == null || goods.getCommonGoods() == null)
            throw new CollectionGoodsException(1, "数据不完整");
        try {
            CommonGoods commonGoods = commonGoodsService.addOrUpdateCommonGoods(goods.getCommonGoods());
            goods.setCommonGoods(commonGoods);
        } catch (CommonGoodsException e) {
            goods.setCommonGoods(null);
        }
 
        if (goods.getCommonGoods() == null)
            throw new CollectionGoodsException(3, "商品信息添加失败");
 
        CollectionGoodsV2 collectionGoodsV2 = collectionGoodsV2Mapper
                .selectByUidAndCommonGoodsId(goods.getUserInfo().getId(), goods.getCommonGoods().getId());
        if (collectionGoodsV2 != null)
            throw new CollectionGoodsException(4, "商品已被收藏");
        collectionGoodsV2Mapper.insertSelective(goods);
    }
 
    @Transactional
    @Override
    public void cancelCollection(Long uid, Long id) throws CollectionGoodsException {
        if (uid == null || uid == 0)
            throw new CollectionGoodsException(1, "无用户ID");
 
        if (id == null || id == 0)
            throw new CollectionGoodsException(1, "无收藏");
 
        CollectionGoodsV2 collectionGoodsV2 = collectionGoodsV2Mapper.selectByPrimaryKey(id);
        if (collectionGoodsV2 == null)
            return;
        if (collectionGoodsV2.getUserInfo().getId() != uid.longValue())
            throw new CollectionGoodsException(2, "无权限");
        collectionGoodsV2Mapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public List<CollectionGoodsV2> getCollectionGoodsList(Long uid, int page, int pageSize) {
        return collectionGoodsV2Mapper.selectByUidOrderByCreateTimeDesc(uid, (page - 1) * pageSize, pageSize);
    }
 
    @Override
    public long getCollectionGoodsCount(Long uid) {
        return collectionGoodsV2Mapper.selectCountByUid(uid);
    }
 
    @Override
    public void cancelCollectionByAuctionId(Long uid, Long auctionId) throws CollectionGoodsException {
        CollectionGoodsV2 goodsV2 = collectionGoodsV2Mapper.selectByUidAndGoodsTypeAndGoodsId(uid,
                CommonGoods.GOODS_TYPE_TB, auctionId);
        if (goodsV2 == null)
            throw new CollectionGoodsException(1, "无收藏");
        collectionGoodsV2Mapper.deleteByPrimaryKey(goodsV2.getId());
    }
 
    @Override
    public void cancelCollectionByUid(Long uid) throws CollectionGoodsException {
        collectionGoodsV2Mapper.deleteByUid(uid);
    }
 
    @Override
    public CollectionGoodsV2 findByUidAndAuctionId(Long uid, Long actionId) {
        CollectionGoodsV2 v2 = collectionGoodsV2Mapper.selectByUidAndGoodsTypeAndGoodsId(uid, CommonGoods.GOODS_TYPE_TB,
                actionId);
        return v2;
    }
 
}