admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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 org.yeshi.utils.StringUtil;
 
import com.yeshi.fanli.dao.mybatis.goods.CommonGoodsMapper;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.exception.goods.CommonGoodsException;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
 
@Service
public class CommonGoodsServiceImpl implements CommonGoodsService {
 
    @Resource
    private CommonGoodsMapper commonGoodsMapper;
 
    
    @Override
    public CommonGoods selectByPrimaryKey(Long id) {
        return commonGoodsMapper.selectByPrimaryKey(id);
    }
    
    
    
    /**
     * 验证数据正确性
     * 
     * @param commonGoods
     * @throws CommonGoodsException
     */
    private void filterCommonGoods(CommonGoods commonGoods) throws CommonGoodsException {
        // 判断信息是否完成
        if (commonGoods == null)
            throw new CommonGoodsException(1, "商品信息为空");
        if (commonGoods.getGoodsId() == null)
            throw new CommonGoodsException(1, "商品信息不完整");
        if (commonGoods.getGoodsType() == null)
            throw new CommonGoodsException(1, "商品信息不完整");
        if (StringUtil.isNullOrEmpty(commonGoods.getPicture()))
            throw new CommonGoodsException(1, "商品信息不完整");
        if (commonGoods.getPrice() == null)
            throw new CommonGoodsException(1, "商品信息不完整");
        if (commonGoods.getRate() == null)
            throw new CommonGoodsException(1, "商品信息不完整");
        if (commonGoods.getSales() == null)
            throw new CommonGoodsException(1, "商品信息不完整");
        if (commonGoods.getSellerId() == null)
            throw new CommonGoodsException(1, "商品信息不完整");
        if (commonGoods.getSellerName() == null)
            throw new CommonGoodsException(1, "商品信息不完整");
        if (commonGoods.getShopType() == null)
            throw new CommonGoodsException(1, "商品信息不完整");
        if (StringUtil.isNullOrEmpty(commonGoods.getTitle()))
            throw new CommonGoodsException(1, "商品信息不完整");
    }
 
    @Override
    public CommonGoods addOrUpdateCommonGoods(CommonGoods commonGoods) throws CommonGoodsException {
        // 验证数据完整性
        filterCommonGoods(commonGoods);
        
        CommonGoods goods = commonGoodsMapper.selectByGoodsIdAndGoodsType(commonGoods.getGoodsId(),
                commonGoods.getGoodsType());
        if (goods != null) {
            updateCommonGoods(commonGoods);
            commonGoods.setId(goods.getId());
            return commonGoods;
        }
        commonGoods.setCreateTime(new Date());
        commonGoods.setUpdateTime(new Date());
        commonGoodsMapper.insertSelective(commonGoods);
        return commonGoods;
    }
    
    
    @Override
    public CommonGoods addCommonGoods(CommonGoods commonGoods) throws CommonGoodsException {
        // 验证数据完整性
        filterCommonGoods(commonGoods);
        
        CommonGoods goods = commonGoodsMapper.selectByGoodsIdAndGoodsType(commonGoods.getGoodsId(),
                commonGoods.getGoodsType());
        if (goods != null) {
            commonGoods.setId(goods.getId());
            return commonGoods;
        }
        commonGoods.setCreateTime(new Date());
        commonGoods.setUpdateTime(new Date());
        commonGoodsMapper.insertSelective(commonGoods);
        return commonGoods;
    }
 
 
    @Override
    public void updateCommonGoods(CommonGoods commonGoods) throws CommonGoodsException {
        // 判断信息是否完整
        filterCommonGoods(commonGoods);
        
        CommonGoods goods = commonGoodsMapper.selectByGoodsIdAndGoodsType(commonGoods.getGoodsId(),
                commonGoods.getGoodsType());
        if (goods == null) {
            throw new CommonGoodsException(3, "商品不存在");
        }
        // 更新商品信息
        commonGoods.setId(goods.getId());
        commonGoods.setCreateTime(null);
        commonGoods.setUpdateTime(new Date());
        commonGoodsMapper.updateByPrimaryKeySelective(commonGoods);
    }
 
    @Override
    public void offlineCommonGoods(Long goodsId, Integer goodsType) {
        CommonGoods goods = commonGoodsMapper.selectByGoodsIdAndGoodsType(goodsId, goodsType);
        if (goods == null)
            return;
        CommonGoods update = new CommonGoods();
        update.setId(goods.getId());
        update.setUpdateTime(new Date());
        update.setState(CommonGoods.STATE_OFFLINE);
        commonGoodsMapper.updateByPrimaryKeySelective(update);
    }
 
    @Override
    public CommonGoods getCommonGoodsByGoodsIdAndGoodsType(Long goodsId, Integer goodsType) {
        return commonGoodsMapper.selectByGoodsIdAndGoodsType(goodsId, goodsType);
    }
 
    @Override
    @Transactional
    public void updateBatchCommonGoods(List<CommonGoods> listCommonGoods) {
        
        if (listCommonGoods == null || listCommonGoods.size() == 0) {
            return;
        }
        
        for (CommonGoods commonGoods: listCommonGoods) {
            try {
                updateCommonGoods(commonGoods);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    @Override
    @Transactional
    public void addBatchCommonGoods(List<CommonGoods> listCommonGoods) {
        
        if (listCommonGoods == null || listCommonGoods.size() == 0) {
            return;
        }
        
        for (CommonGoods commonGoods: listCommonGoods) {
            try {
                addCommonGoods(commonGoods);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
    @Override
    public List<CommonGoods> listBySellerId(Long sellerId, Integer goodsType) {
        return commonGoodsMapper.listBySellerId(sellerId, goodsType);
    } 
    
    @Override
    public long countBySellerIdAndHasCoupon(Long sellerId, Integer goodsType) {
        return commonGoodsMapper.countBySellerIdAndHasCoupon(sellerId, goodsType);
    }
    
    @Override
    public List<CommonGoods> getByListGoodsId(List<Long> list, Integer goodsType) {
        return commonGoodsMapper.getByListGoodsId(list, goodsType);
    }
    
}