yujian
2019-10-29 9b55262bb45c81c997ab04e55e8de13e14b8ca7d
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package com.yeshi.fanli.service.impl.brand;
 
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.brand.BrandShopCaheDao;
import com.yeshi.fanli.dao.mybatis.brand.BrandInfoMapper;
import com.yeshi.fanli.dto.ConfigParamsDTO;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.entity.brand.BrandGoodsCahe;
import com.yeshi.fanli.entity.brand.BrandInfo;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoShop;
import com.yeshi.fanli.exception.brand.BrandInfoException;
import com.yeshi.fanli.service.inter.brand.BrandClassShopService;
import com.yeshi.fanli.service.inter.brand.BrandGoodsCaheService;
import com.yeshi.fanli.service.inter.brand.BrandInfoService;
import com.yeshi.fanli.service.inter.brand.BrandShopCaheService;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.service.inter.lable.QualityGoodsService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
import com.yeshi.fanli.vo.brand.BrandInfoVO;
import com.yeshi.fanli.vo.brand.TaoBaoShopVO;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
 
@Service
public class BrandInfoServiceImpl implements BrandInfoService {
 
    @Resource
    private BrandInfoMapper brandInfoMapper;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource
    private QualityGoodsService qualityGoodsService;
 
    @Resource
    private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
 
    @Resource
    private TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
 
    @Resource
    private BrandClassShopService brandClassShopService;
 
    @Resource
    private BrandShopCaheDao brandShopCaheDao;
 
    @Resource
    private BrandGoodsCaheService brandGoodsCaheService;
 
    @Resource
    private BrandShopCaheService brandShopCaheService;
 
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
    
    
    @Override
    public void saveObject(MultipartFile file, BrandInfo record) throws BrandInfoException {
        String name = record.getName();
        if (StringUtil.isNullOrEmpty(name))
            throw new BrandInfoException(1, "名称不能为空");
 
        String searchKey = record.getSearchKey();
        if (StringUtil.isNullOrEmpty(searchKey))
            record.setSearchKey(name);
        
        Integer state = record.getState();
        if (state == null)
            record.setState(0);
        
        // 图片上传
        String picture = null;
        if (file != null) {
            try {
                picture = uploadPicture(file);
            } catch (Exception e) {
                throw new BrandInfoException(1, "图片上传失败");
            }
        }
 
        Long id = record.getId();
        if (id == null) {
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            brandInfoMapper.insert(record);
        } else {
            BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
            if (resultObj == null)
                throw new BrandInfoException(1, "修改内容已不存在");
 
            if (picture != null && picture.trim().length() > 0) {
                // 删除老图
                if (picture != null && picture.trim().length() > 0 && !Constant.IS_TEST) {
                    COSManager.getInstance().deleteFile(picture);
                };
                // 存储新图
                record.setIcon(picture);
            } else {
                record.setIcon(resultObj.getIcon());
            }
            
            record.setGoodsTotal(resultObj.getGoodsTotal());
            record.setCreateTime(resultObj.getCreateTime());
            record.setUpdateTime(new Date());
            brandInfoMapper.updateByPrimaryKey(record);
            
            
            if (state == 0){
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        brandGoodsCaheService.removeByBrandId(id);
                        brandShopCaheService.removeByBrandId(id);
                    }
                });
            }
        }
 
        if (state == 1) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    int goodsTotal = brandGoodsCaheService.addBrandGoods(record);
                    record.setGoodsTotal(goodsTotal);
                    brandInfoMapper.updateByPrimaryKeySelective(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/brand/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
        // 执行上传
        String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
        
        return fileLink;
    }
    
    @Override
    public int deleteBatchByPrimaryKey(List<Long> list) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                for (Long id: list) {
                    brandGoodsCaheService.removeByBrandId(id);
                    brandShopCaheService.removeByBrandId(id);
                }
            }
        });
        
        return brandInfoMapper.deleteBatchByPrimaryKey(list);
    }
 
    @Override
    public List<BrandInfo> listQuery(long start, int count, String key, Long cid, Integer state) {
        return brandInfoMapper.listQuery(start, count, key, cid, state);
    }
 
    @Override
    public long countQuery(String key, Long cid, Integer state) {
        return brandInfoMapper.countQuery(key, cid, state);
    }
 
    @Override
    @Cacheable(value = "brandCache", key = "'listValidBrandInfoCache-'+#cid")
    public List<BrandInfo> listValidBrandInfoCache(Long cid) {
        List<BrandInfo> listInfo = new ArrayList<BrandInfo>();
 
        List<TaoBaoShop> listShop = brandClassShopService.listEffectiveClassShop(cid);
        if (listShop == null || listShop.size() == 0)
            return listInfo;
        for (TaoBaoShop taoBaoShop : listShop) {
            BrandInfo brandInfo = new BrandInfo();
            brandInfo.setId(taoBaoShop.getId());
            brandInfo.setName(taoBaoShop.getShopName());
            brandInfo.setIcon(taoBaoShop.getShopIcon());
            listInfo.add(brandInfo);
        }
        return listInfo;
    }
 
    @Override
    @Cacheable(value = "brandCache", key = "'listBrandInfoCache-'+#start+'-'+#start +'-'+#cid")
    public List<BrandInfoVO> listBrandInfoCache(long start, int count, Long cid) {
        List<BrandInfoVO> listInfo = new ArrayList<BrandInfoVO>();
 
        List<TaoBaoShopVO> listShop = brandClassShopService.listEffectiveShop(start, count, cid);
        if (listShop == null || listShop.size() == 0)
            return listInfo;
 
        for (TaoBaoShopVO taoBaoShopVO : listShop) {
            String shopNameCustom = taoBaoShopVO.getShopNameCustom();
            if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
                taoBaoShopVO.setShopName(shopNameCustom);
            }
            String shopIconCustom = taoBaoShopVO.getShopIconCustom();
            if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
                taoBaoShopVO.setShopIcon(shopIconCustom);
            }
 
            BrandInfoVO brandInfoVO = new BrandInfoVO();
            brandInfoVO.setId(taoBaoShopVO.getId());
            brandInfoVO.setName(taoBaoShopVO.getShopName());
            brandInfoVO.setIcon(taoBaoShopVO.getShopIcon());
            brandInfoVO.setListGoods(taoBaoShopVO.getListGoodsVO());
            listInfo.add(brandInfoVO);
        }
        return listInfo;
    }
 
    @Override
    public long countBrandInfo(Long cid) {
        return brandClassShopService.countBrandShopinfo(cid);
    }
 
    
    @Override
    public void removeAgoByDate(Date date) {
        // 删除商品
        brandGoodsCaheService.removeAgoByDate(date);
        
        // 删除店铺
        brandShopCaheService.removeAgoByDate(date);
    }
 
    
    
    @Override
    public void addShopAndGoods(long start, int count) {
        List<BrandInfo> list = brandInfoMapper.listValidAll(start, count);
        if (list == null || list.size() == 0)
            return;
 
        for (BrandInfo brandInfo : list) {
            String name = brandInfo.getName();
            String searchKey = brandInfo.getSearchKey();
            if (StringUtil.isNullOrEmpty(name) && StringUtil.isNullOrEmpty(searchKey))
                continue;
 
            // 添加商品
            int goodsTotal = brandGoodsCaheService.addBrandGoods(brandInfo);
 
            brandInfo.setGoodsTotal(goodsTotal);
            brandInfo.setUpdateTime(new Date());
            brandInfoMapper.updateByPrimaryKeySelective(brandInfo);
        }
    }
 
    @Override
    public long countValidByCid(Long cid) {
        return brandInfoMapper.countValidByCid(cid);
    }
 
    @Override
    public long countValidByCidToApp(Long cid) {
        return brandInfoMapper.countValidByCidToApp(cid);
    }
 
    @Override
    @Cacheable(value = "brandCache", key = "'listValidByCidToApp-'+#cid")
    public List<BrandInfo> listValidByCidToApp(Long cid) {
        if (cid == null)
            return null;
        return brandInfoMapper.listValidByCidToApp(cid);
    }
 
    @Cacheable(value = "brandCache", key = "'listValidToApp-'+#start+'-'+#start +'-'+#cid")
    @Override
    public List<BrandInfoVO> listValidToApp(long start, int count, Long cid) {
        List<BrandInfoVO> list = brandInfoMapper.listBrandInfoVO(start, count, cid);
        if (list == null || list.size() == 0)
            return null;
 
        BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
        BigDecimal shareRate = hongBaoManageService.getShareRate();
        ConfigParamsDTO configParamsDTO = new ConfigParamsDTO(fanLiRate, shareRate, Constant.MAX_REWARD_RATE);
        List<BrandInfoVO> listInfo = new ArrayList<BrandInfoVO>();
        for (int i = 0; i < list.size(); i++) {
            BrandInfoVO brand = list.get(i);
            if (brand == null)
                continue;
            
            List<BrandGoodsCahe> listGoods = brandGoodsCaheService.getByBrandId(1, 3, brand.getId());
            if (listGoods == null || listGoods.size() < 3)
                continue;
 
            List<GoodsDetailVO> listGoodsVO = new ArrayList<GoodsDetailVO>();
            for (BrandGoodsCahe brandGoods : listGoods) {
                JDGoods goodsJD = brandGoods.getGoodsJD();
                if (goodsJD != null) {
                    listGoodsVO.add(GoodsDetailVOFactory.convertJDGoods(goodsJD, configParamsDTO));
                    continue;
                }
 
                TaoBaoGoodsBrief goodsTB = brandGoods.getGoodsTB();
                if (goodsTB != null) {
                    listGoodsVO.add(GoodsDetailVOFactory.convertTaoBao(goodsTB, configParamsDTO));
                    continue;
                }
 
                PDDGoodsDetail goodsPDD = brandGoods.getGoodsPDD();
                if (goodsPDD != null) {
                    listGoodsVO.add(GoodsDetailVOFactory.convertPDDGoods(goodsPDD, configParamsDTO));
                    continue;
                }
            }
 
            if (listGoodsVO.size() >= 3) {
                brand.setListGoods(listGoodsVO);
                listInfo.add(brand);
            }
        }
        return listInfo;
    }
}