yujian
2019-11-11 3ce9e928e082ff8abf697dba560cbfbb412e89f8
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.yeshi.fanli.service.impl.shop;
 
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.shop.BanLiShopGoodsMapper;
import com.yeshi.fanli.entity.shop.BanLiShopGoods;
import com.yeshi.fanli.entity.shop.BanLiShopGoodsImg;
import com.yeshi.fanli.entity.shop.BanLiShopGoodsSets;
import com.yeshi.fanli.exception.shop.BanLiShopGoodsException;
import com.yeshi.fanli.exception.shop.BanLiShopGoodsSetException;
import com.yeshi.fanli.exception.shop.BanLiShopGoodsSetPayException;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsImgService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class BanLiShopGoodsServiceImpl implements BanLiShopGoodsService {
 
    @Resource
    private BanLiShopGoodsSetService banLiShopGoodsSetService;
 
    @Resource
    private BanLiShopGoodsImgService banLiShopGoodsImgService;
 
    @Resource
    private BanLiShopGoodsMapper banLiShopGoodsMapper;
 
    @Override
    public List<BanLiShopGoods> listGoods(String key, Integer state, int page, int pageSize) {
        return banLiShopGoodsMapper.listGoods(key, state, (page - 1) * pageSize, pageSize);
    }
 
    @Override
    public long countGoods(String key, Integer state) {
        return banLiShopGoodsMapper.countGoods(key, state);
    }
 
    @Override
    public BanLiShopGoods getGoodsDetail(Long goodsId) {
        return banLiShopGoodsMapper.selectDetailByPrimaryKey(goodsId);
    }
 
    @Override
    public BanLiShopGoods selectByPrimaryKey(Long id) {
        return banLiShopGoodsMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public void addGoods(BanLiShopGoods goods)
            throws BanLiShopGoodsException, BanLiShopGoodsSetException, BanLiShopGoodsSetPayException {
        if (goods.getId() == null) {// 新增
            if (goods.getGoodsClass() == null || goods.getGoodsClass().getId() == null) {
                throw new BanLiShopGoodsSetException(1, "请指定商品分类");
            }
 
            if (StringUtil.isNullOrEmpty(goods.getTitle()))
                throw new BanLiShopGoodsSetException(1, "缺少标题");
 
            if (StringUtil.isNullOrEmpty(goods.getPicture()))
                throw new BanLiShopGoodsSetException(1, "缺少封面图");
 
            // 默认上线
            if (goods.getState() == null)
                goods.setState(BanLiShopGoods.STATE_ONLINE);
 
            if (goods.getSalesCount() == null)
                goods.setSalesCount(0L);
 
            if (goods.getCreateTime() == null)
                goods.setCreateTime(new Date());
 
            banLiShopGoodsMapper.insertSelective(goods);
            if (goods.getWeight() == null) {// 权重更新
                BanLiShopGoods update = new BanLiShopGoods();
                update.setId(goods.getId());
                update.setWeight((int) goods.getId().longValue());
                banLiShopGoodsMapper.updateByPrimaryKeySelective(update);
            }
 
            if (goods.getSetsList() != null && goods.getSetsList().size() > 0) {
                for (BanLiShopGoodsSets set : goods.getSetsList())// 更新支付方式
                {
                    set.setGoods(goods);
                    banLiShopGoodsSetService.addSet(set);
                }
            }
 
            // TODO 更改图片
            if (goods.getImgList() != null)
                for (BanLiShopGoodsImg img : goods.getImgList()) {
                    banLiShopGoodsImgService.addImg(img.getUrl(), goods.getId());
                }
 
        } else {// 修改
            updateSelectiveByPrimaryKey(goods);
            // 更新支付方式
            if (goods.getSetsList() != null && goods.getSetsList().size() > 0) {
                // 删除原有套餐
                List<BanLiShopGoodsSets> setList = banLiShopGoodsSetService.listByGoodsId(goods.getId());
                for (BanLiShopGoodsSets set : setList)
                    banLiShopGoodsSetService.deleteByPrimaryKey(set.getId());
 
                // 添加新的套餐
                for (BanLiShopGoodsSets set : goods.getSetsList())// 更新支付方式
                    banLiShopGoodsSetService.addSet(set);
            }
 
            // 更新图片
            if (goods.getImgList() != null && goods.getImgList().size() > 0) {
                // 删除原有图片
                List<BanLiShopGoodsImg> imgList = banLiShopGoodsImgService.listByGoodsId(goods.getId());
                for (BanLiShopGoodsImg img : imgList)
                    banLiShopGoodsImgService.deleteByPrimaryKey(img.getId());
                // 添加新的图片
                for (BanLiShopGoodsImg img : goods.getImgList())// 更新支付方式
                    banLiShopGoodsImgService.addImg(img.getUrl(), goods.getId());
            }
 
        }
 
    }
 
    
    @Override
    public void saveObject(MultipartFile file, MultipartFile file2, BanLiShopGoods record) throws BanLiShopGoodsException, Exception{
        if (record.getGoodsClass() == null || record.getGoodsClass().getId() == null)
            throw new BanLiShopGoodsException(1, "请指定商品分类");
 
        if (StringUtil.isNullOrEmpty(record.getTitle()))
            throw new BanLiShopGoodsException(1, "缺少标题");
 
        if (file != null)
            record.setPicture(uploadPicture(file));
        
        if (file2 != null)
            record.setSquarePicture(uploadPicture(file2));
        
        if (record.getState() == null)
            record.setState(BanLiShopGoods.STATE_ONLINE);
        
        if (record.getSalesCount() == null)
            record.setSalesCount(0L);
        
        record.setUpdateTime(new Date());
        
        if (record.getId() == null) {
            if (StringUtil.isNullOrEmpty(record.getPicture()))
                throw new BanLiShopGoodsException(1, "缺少封面图");
            
            record.setCreateTime(new Date());
            banLiShopGoodsMapper.insertSelective(record);
        } else {
            BanLiShopGoods resultObj = banLiShopGoodsMapper.selectDetailByPrimaryKey(record.getId());
            if (resultObj == null)
                throw new BanLiShopGoodsException(1, "修改内容已不存在");
            
            if (StringUtil.isNullOrEmpty(record.getPicture())) {
                record.setPicture(resultObj.getPicture());
            } else {
                removePicture(resultObj.getPicture());
            }
            
            if (StringUtil.isNullOrEmpty(record.getSquarePicture())) {
                record.setSquarePicture(resultObj.getSquarePicture());
            } else {
                removePicture(resultObj.getSquarePicture());
            }
            
            if (StringUtil.isNullOrEmpty(record.getPicture()))
                throw new BanLiShopGoodsException(1, "缺少封面图");
            
            record.setCreateTime(resultObj.getCreateTime());
            banLiShopGoodsMapper.updateByPrimaryKey(record);
        }
    }
    
 
    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadPicture(MultipartFile file) throws Exception {
        // 文件解析 
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
        // 文件路径
        String filePath="/img/BanLiShopGoods/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
        // 执行上传
        String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
        return fileLink;
    }
 
    /**
     * 删除图片-不更新数据库
     * @param record
     * @throws Exception
     */
    public void removePicture(String picture) throws Exception {
        if (picture != null && picture.trim().length() > 0) {
            COSManager.getInstance().deleteFile(picture);
        }
    }
    
    
    
    @Override
    public void updateSelectiveByPrimaryKey(BanLiShopGoods goods) {
        if (goods == null || goods.getId() == null)
            return;
        if (goods.getUpdateTime() == null)
            goods.setUpdateTime(new Date());
 
        banLiShopGoodsMapper.updateByPrimaryKeySelective(goods);
    }
 
    @Transactional
    @Override
    public void delete(List<Long> idsList) {
        if (idsList != null)
            for (Long id : idsList)
                deleteByPrimaryKey(id);
 
    }
 
    @Transactional
    @Override
    public void deleteByPrimaryKey(Long id) {
        banLiShopGoodsMapper.deleteByPrimaryKey(id);
        List<BanLiShopGoodsSets> setList = banLiShopGoodsSetService.listByGoodsId(id);
        if (setList != null)
            for (BanLiShopGoodsSets set : setList)
                banLiShopGoodsSetService.deleteByPrimaryKey(set.getId());
    }
 
    @Override
    public void addSalesCount(Long id, int count) {
        BanLiShopGoods goods = selectByPrimaryKey(id);
        if (goods != null) {
            BanLiShopGoods update = new BanLiShopGoods(id);
            update.setSalesCount(goods.getSalesCount() + count);
            update.setUpdateTime(new Date());
            updateSelectiveByPrimaryKey(goods);
 
        }
    }
 
}