yujian
2020-04-24 f276a5a1fc38e735499aa95089ff8105bd2ba196
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package com.yeshi.fanli.service.impl.goods;
 
import java.io.InputStream;
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.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.GoodsSubClassMapper;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.bus.clazz.GoodsSubClass;
import com.yeshi.fanli.entity.bus.clazz.GoodsSubClassLabel;
import com.yeshi.fanli.entity.bus.clazz.GoodsSubClassLabelMap;
import com.yeshi.fanli.exception.goods.GoodsSubClassException;
import com.yeshi.fanli.service.inter.clazz.GoodsSubClassLabelService;
import com.yeshi.fanli.service.inter.goods.GoodsSubClassService;
import com.yeshi.fanli.service.inter.goods.TaoBaoClassService;
import com.yeshi.fanli.service.inter.lable.LabelClassService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class GoodsSubClassServiceImpl implements GoodsSubClassService {
 
    @Resource
    private GoodsSubClassMapper goodsSubClassMapper;
    @Resource
    private GoodsSubClassService goodsSubClassService;
    @Resource
    private LabelClassService labelClassService;
 
    @Resource
    private TaoBaoClassService taoBaoClassService;
 
    @Resource
    private GoodsSubClassLabelService goodsSubClassLabelService;
 
    @Override
    public int deleteByPrimaryKey(Long id) {
        return goodsSubClassMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(GoodsSubClass record) {
        return goodsSubClassMapper.insert(record);
    }
 
    @Override
    public int insertSelective(GoodsSubClass record) {
        return goodsSubClassMapper.insertSelective(record);
    }
 
    @Override
    public GoodsSubClass selectByPrimaryKey(Long id) {
        return goodsSubClassMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(GoodsSubClass record) {
        return goodsSubClassMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteByRootId(Long id) throws Exception {
 
        List<GoodsSubClass> subClassList = goodsSubClassMapper.queryByRootId(id, null, null);
        if (subClassList != null && subClassList.size() > 0) {
            for (GoodsSubClass goodsSubClass : subClassList) {
                deleteSub(goodsSubClass.getId());
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteByPrimaryKeyBatch(List<String> recordIds) throws Exception {
        if (recordIds != null && recordIds.size() > 0) {
            for (String recordId : recordIds) {
                deleteSub(Long.parseLong(recordId));
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteSub(Long recordId) throws Exception {
        GoodsSubClass goodsSubClass = goodsSubClassMapper.selectByPrimaryKey(recordId);
 
        if (goodsSubClass == null)
            return;
 
        /* 删除网络图片 */
        String picture = goodsSubClass.getPicture();
        if (!StringUtil.isNullOrEmpty(picture)) {
            COSManager.getInstance().deleteFile(picture);
        }
 
        /* 删除网络图片 */
        String pictureSecond = goodsSubClass.getPictureSecond();
        if (!StringUtil.isNullOrEmpty(pictureSecond)) {
            COSManager.getInstance().deleteFile(pictureSecond);
        }
 
        /* 删除所有关联子类 */
        List<GoodsSubClass> subList = goodsSubClassMapper.queryByPid(recordId, null);
        if (subList != null && subList.size() > 0) {
            for (GoodsSubClass subClass : subList) {
                Long id = subClass.getId();
                // 继续删除下级
                deleteSub(id);
            }
        }
 
        /* 删除关联标签 */
        labelClassService.deleteBySubClassId(recordId);
 
        /* 删除数据 */
        goodsSubClassMapper.deleteByPrimaryKey(recordId);
    }
 
    @Override
    public void saveObject(MultipartFile file, MultipartFile file2, GoodsSubClass record, Integer type, Long pid,
            Long labelId) throws GoodsSubClassException, Exception {
 
        String name = record.getName();
        if (name == null || name.trim().length() == 0) {
            throw new GoodsSubClassException(1, "分类名称不能为空");
        }
 
        if (StringUtil.isNullOrEmpty(record.getMonth()))
            record.setMonth(null);
 
        // 图片上传
        String picture = null;
        if (file != null) {
            picture = uploadPicture(file);
        }
 
        // 图片上传
        String pictureSecond = null;
        if (file2 != null) {
            pictureSecond = uploadPicture(file2);
        }
 
        String params = record.getSearchParam();
        if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) {
            record.setSearchJson(null);
        } else if (!StringUtil.isJson(params)) {
            throw new GoodsSubClassException(1, "筛选条件非JSON格式");
        } else {
            record.setSearchJson(params);
        }
 
        Long id = record.getId();
        if (id == null) {
            if (type == null) {
                throw new GoodsSubClassException(1, "等级不能为空");
            }
 
            if (type > 5) {
                throw new GoodsSubClassException(1, "等级不能超过五级");
            }
 
            if (pid == null) {
                throw new GoodsSubClassException(1, "上级id为空");
            }
 
            if (type == 2) {
                record.setRootClass(new GoodsClass(pid));
                int weight = goodsSubClassMapper.getMaxWeightByRootId(pid);
                record.setWeight(weight + 1);
            } else {
                record.setParent(new GoodsSubClass(pid));
                int weight = goodsSubClassMapper.getMaxWeightByPid(pid);
                record.setWeight(weight + 1);
            }
 
            String key = record.getKey();
            if (StringUtil.isNullOrEmpty(key)) {
                record.setKey(name.trim());
            }
 
            record.setLevel(type);
            record.setPicture(picture);
            record.setPictureSecond(pictureSecond);
            record.setState(0);
            record.setAndroidClick(0L);
            record.setIosClick(0L);
            record.setCreatetime(new Date());
            record.setUpdatetime(new Date());
            if (labelId != null)
                record.setClassLabel(new GoodsSubClassLabel(labelId));
 
            if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) {
                // 搜索条件:有券、在售价20-200、牛皮癣轻微
                record.setSearchJson("{\"quan\":1,\"endPrice\":220,\"includeGoodRate\":true}");
            }
 
            goodsSubClassMapper.insert(record);
 
            if (labelId != null) {
                GoodsSubClassLabelMap map = new GoodsSubClassLabelMap();
                map.setGoodsSubClass(record);
                map.setLabel(new GoodsSubClassLabel(labelId));
                goodsSubClassLabelService.addSubClassLabelMap(map);
            }
 
        } else {
            // 修改
            GoodsSubClass resultObj = goodsSubClassMapper.selectByPrimaryKey(id);
            if (resultObj == null) {
                throw new GoodsSubClassException(1, "修改内容已不存在");
            }
 
            if (picture != null && picture.trim().length() > 0) {
                // 删除老图
                if (!Constant.IS_TEST)
                    removePicture(resultObj.getPicture());
                // 存储新图
                record.setPicture(picture);
            } else {
                record.setPicture(resultObj.getPicture());
            }
 
            if (pictureSecond != null && pictureSecond.trim().length() > 0) {
                // 删除老图
                if (!Constant.IS_TEST)
                    removePicture(resultObj.getPictureSecond());
                // 存储新图
                record.setPictureSecond(pictureSecond);
            } else {
                record.setPictureSecond(resultObj.getPictureSecond());
            }
 
            record.setLevel(resultObj.getLevel());
            record.setRootClass(resultObj.getRootClass());
            record.setWeight(resultObj.getWeight());
            record.setIosClick(resultObj.getIosClick());
            record.setAndroidClick(resultObj.getAndroidClick());
            record.setCreatetime(resultObj.getCreatetime());
            record.setUpdatetime(new Date());
            if (labelId != null) {
                GoodsSubClassLabelMap map = new GoodsSubClassLabelMap();
                map.setGoodsSubClass(record);
                map.setLabel(new GoodsSubClassLabel(labelId));
                try {
                    goodsSubClassLabelService.addSubClassLabelMap(map);
                } catch (Exception e) {
                }
            }
 
            goodsSubClassMapper.updateByPrimaryKey(record);
        }
 
        // 保存淘宝商品分类id
        String taobaoCids = record.getTaobaoCids();
        List<Long> tbCidList = new ArrayList<>();
        if (!StringUtil.isNullOrEmpty(taobaoCids) && !"null".equalsIgnoreCase(taobaoCids)) {
 
            String[] sts = taobaoCids.split(",");
            for (String st : sts) {
                String cid = st.split("-")[st.split("-").length - 1];
                tbCidList.add(Long.parseLong(cid));
            }
        }
        taoBaoClassService.saveSub(record.getId(), tbCidList);
    }
 
    /**
     * 上传图片
     * 
     * @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 =FilePathEnum.goodsSubClass.getPath() + 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 int removePicture(GoodsSubClass record) throws Exception {
        int result = -2;
        String fileUrl = record.getPicture();
        if (StringUtil.isNullOrEmpty(fileUrl)) {
            return result;
        }
 
        boolean deleteFile = COSManager.getInstance().deleteFile(fileUrl);;
        if (deleteFile) {
            record.setPicture(null);
            // 更新数据库
            result = goodsSubClassMapper.updateByPrimaryKey(record);
        }
        return result;
    }
 
    
    @Override
    public void switchState(Long id) throws GoodsSubClassException {
        if (id == null) {
            throw new GoodsSubClassException(1, "请传递正确参数");
        }
        GoodsSubClass resultObj = goodsSubClassMapper.selectByPrimaryKey(id);
        if (resultObj == null) {
            throw new GoodsSubClassException(1, "此内容已不存在");
        }
        
        Integer state = resultObj.getState();
        if (state == null || state == 0) {
            state = 1;
        } else {
            state = 0;
        }
        
        GoodsSubClass updateObj = new GoodsSubClass();
        updateObj.setId(id);
        updateObj.setState(state);
        goodsSubClassMapper.updateByPrimaryKeySelective(updateObj);
    }
    
    
    @Override
    public List<GoodsSubClass> queryByRootId(Long rootId, Integer state) throws Exception {
        return goodsSubClassMapper.queryByRootId(rootId, state, null);
    }
 
    @Override
    public List<GoodsSubClass> queryByPid(Long pid, Integer state) throws Exception {
        return goodsSubClassMapper.queryByPid(pid, state);
    }
 
    @Override
    public List<GoodsSubClass> queryByRootIdAndWeight(Long rootId, int type, int weight) throws Exception {
        return goodsSubClassMapper.queryByRootIdAndWeight(rootId, type, weight);
    }
 
    @Override
    public List<GoodsSubClass> queryByPidAndWeight(Long pid, int type, int weight) throws Exception {
        return goodsSubClassMapper.queryByPidAndWeight(pid, type, weight);
    }
 
    @Override
    public List<GoodsSubClass> getGoodsSecondClass(Long rootId, Integer state, Integer month) throws Exception {
        return goodsSubClassMapper.queryByRootId(rootId, state, month);
    }
 
    @Override
    @Cacheable(value = "classCache", key = "'getSubClassCache-'+#rootId +'-'+#state")
    public List<GoodsSubClass> getSubClassCache(Long rootId, Integer state, Integer month) throws Exception {
        return getGoodsSecondClass(rootId, state, month);
    }
 
    @Override
    @Cacheable(value = "classCache", key = "'getSubClassByPrimaryKeyCache-'+#id")
    public GoodsSubClass getSubClassByPrimaryKeyCache(Long id) throws Exception {
        return selectByPrimaryKey(id);
    }
 
    /**
     * 统计一级之下的所有二级分类
     * 
     * @param rootId
     *            一级id
     * @returnL
     */
    @Override
    public int countByRootId(Long rootId) {
        return goodsSubClassMapper.countByRootId(rootId);
    }
 
    /**
     * 统计二级分类之下其他分类
     * 
     * @param rootId
     *            一级id
     * @return
     */
    @Override
    public int countByPid(Long pid) {
        return goodsSubClassMapper.countByPid(pid);
    }
 
    @Override
    public void countClick(AcceptData acceptData, GoodsSubClass record) {
        if ("android".equalsIgnoreCase(acceptData.getPlatform())) {
            Long androidClick = record.getAndroidClick();
            if (androidClick != null) {
                record.setAndroidClick(androidClick + 1);
            } else {
                record.setAndroidClick(1L);
            }
        } else if ("ios".equalsIgnoreCase(acceptData.getPlatform())) {
            Long iosClick = record.getIosClick();
            if (iosClick != null) {
                record.setIosClick(iosClick + 1);
            } else {
                record.setIosClick(1L);
            }
        }
        goodsSubClassService.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public List<GoodsSubClass> queryByListCid(List<Long> list) {
        return goodsSubClassMapper.queryByListCid(list);
    }
 
}