yujian
2020-06-01 d48079c9eeec9c4f19f550a44d461275b4a31fd4
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.yeshi.fanli.service.impl.user.cloud;
 
import java.util.Date;
import java.util.List;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.user.cloud.UserCloudGoodsMapper;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.entity.bus.user.cloud.UserCloudGoods;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.goods.CommonGoodsException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.exception.user.cloud.UserCloudGoodsException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.user.cloud.UserCloudGoodsService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.cache.JDGoodsCacheUtil;
import com.yeshi.fanli.util.cache.PinDuoDuoCacheUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
 
 
@Service
public class UserCloudGoodsServiceImpl implements UserCloudGoodsService {
 
    @Resource
    private UserCloudGoodsMapper userCloudGoodsMapper;
    
    @Resource
    private RedisManager redisManager;
    
    @Resource
    private JDGoodsCacheUtil jdGoodsCacheUtil;
    
    @Resource
    private PinDuoDuoCacheUtil pinDuoDuoCacheUtil;
    
    @Resource
    private CommonGoodsService commonGoodsService;
    
    
    @Override
    public void deleteByPrimaryKeyAndUid(Long id, Long uid) {
        userCloudGoodsMapper.deleteByPrimaryKeyAndUid(id, uid);
    }
    
    @Override
    public UserCloudGoods selectByPrimaryKey(Long id) {
        return userCloudGoodsMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public void updateByPrimaryKeySelective(UserCloudGoods record) {
        userCloudGoodsMapper.updateByPrimaryKeySelective(record);
    }
    
    
    @Override
    public List<UserCloudGoods> listByUid(long start, int count, Long uid) {
        return userCloudGoodsMapper.listByUid(start, count, uid);
    }
 
    @Override
    public List<UserCloudGoods> listByNotShare(Long uid) {
        return userCloudGoodsMapper.listByNotShare(uid);
    }
 
 
    @Override
    public long countByUid(Long uid) {
        return userCloudGoodsMapper.countByUid(uid);
    }
 
    
    @Override
    public UserCloudGoods getByUidAndGoods(Long uid, Long goodsId, Integer goodsType) {
        return userCloudGoodsMapper.getByUidAndGoods(uid, goodsId, goodsType);
    }
 
    @Override
    public void addGoods(Long uid, Set<Long> set, Integer goodsType) throws UserCloudGoodsException {
        if (uid == null) {
            throw new UserCloudGoodsException(1, "用户尚未登录");
        }
 
        if (set == null || set.size() == 0 || goodsType == null) {
            throw new UserCloudGoodsException(1, "系统参数不正确");
        }
 
        for (Long goodsId : set) {
            CommonGoods commonGoods = null;
            if (goodsType == Constant.SOURCE_TYPE_TAOBAO)  { // 淘宝
                try {
                    TaoBaoGoodsBrief goodsBrief = redisManager.getTaoBaoGoodsBrief(goodsId);
                    commonGoods = CommonGoodsFactory.create(goodsBrief);
                } catch (TaobaoGoodsDownException e) {
                    throw new UserCloudGoodsException(1, goodsId + "商品已下架");
                }
            } else if (goodsType == Constant.SOURCE_TYPE_JD) { // 京东
                JDGoods goods = jdGoodsCacheUtil.getGoodsInfo(goodsId);
                if (goods == null)
                    throw new UserCloudGoodsException(1, goodsId + "未找到商品信息");
                commonGoods = CommonGoodsFactory.create(goods);
            } else if (goodsType == Constant.SOURCE_TYPE_PDD) { // 拼多多
                PDDGoodsDetail goods = pinDuoDuoCacheUtil.getGoodsInfo(goodsId);
                if (goods == null)
                    throw new UserCloudGoodsException(1, goodsId + "未找到商品信息");
                commonGoods = CommonGoodsFactory.create(goods);
            }  
 
            if (commonGoods == null) {
                LogHelper.test("云发单未找到商品详情,id=" + goodsId + "type=" + goodsType);
                continue;
            }
                
            try {
                commonGoodsService.addOrUpdateCommonGoods(commonGoods);
            } catch (CommonGoodsException e) {
                LogHelper.errorDetailInfo(e);
                throw new UserCloudGoodsException(1, "商品信息更新失败");
            }
 
            UserCloudGoods cloudGoods = userCloudGoodsMapper.getByUidAndCommonGoodsId(uid, commonGoods.getId());
            if (cloudGoods != null) {
                UserCloudGoods update = new UserCloudGoods();
                update.setId(cloudGoods.getId());
                update.setState(UserCloudGoods.STATE_NORMAL);
                update.setUpdateTime(new Date());
                userCloudGoodsMapper.updateByPrimaryKeySelective(update);
            } else {
                cloudGoods = new UserCloudGoods();
                cloudGoods.setUid(uid);
                cloudGoods.setState(UserCloudGoods.STATE_NORMAL);
                cloudGoods.setCommonGoods(commonGoods);
                cloudGoods.setCreateTime(new Date());
                cloudGoods.setUpdateTime(new Date());
                userCloudGoodsMapper.insertSelective(cloudGoods);
            }
        }
    }
    
     
}