yujian
2019-03-27 cdcbed9af813b2a02cdc01eefa24db8bec6b51a9
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
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 com.yeshi.fanli.dao.mybatis.goods.ScanHistoryV2Mapper;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.goods.ScanHistoryV2;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.goods.CommonGoodsException;
import com.yeshi.fanli.exception.goods.ScanHistoryException;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.goods.ScanHistoryV2Service;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
 
@Service
public class ScanHistoryV2ServiceImpl implements ScanHistoryV2Service {
 
    @Resource
    private CommonGoodsService commonGoodsService;
 
    @Resource
    private ScanHistoryV2Mapper scanHistoryV2Mapper;
 
    @Override
    public void addScanHistory(Long uid, String device, TaoBaoGoodsBrief goods)
            throws CommonGoodsException, ScanHistoryException {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            throw new ScanHistoryException(1, "设备或用户信息缺失");
        CommonGoods commonGoods = CommonGoodsFactory.create(goods);
        commonGoods = commonGoodsService.addOrUpdateCommonGoods(commonGoods);
        if (commonGoods == null)
            throw new CommonGoodsException(2, "商品信息不完整");
        // 添加浏览记录
        ScanHistoryV2 scanHistoryV2 = new ScanHistoryV2();
        scanHistoryV2.setCommonGoods(commonGoods);
        scanHistoryV2.setCreateTime(new Date());
        scanHistoryV2.setDevice(device);
        if (uid != null)
            scanHistoryV2.setUserInfo(new UserInfo(uid));
        scanHistoryV2.setUpdateTime(new Date());
        scanHistoryV2Mapper.insertSelective(scanHistoryV2);
    }
 
    @Override
    public void addScanHistory(ScanHistoryV2 history) throws CommonGoodsException, ScanHistoryException {
        if (history == null)
            throw new ScanHistoryException(1, "浏览信息不能为空");
        if (history.getUserInfo() == null && StringUtil.isNullOrEmpty(history.getDevice()))
            throw new ScanHistoryException(1, "设备或用户信息缺失");
 
        CommonGoods commonGoods = commonGoodsService.addCommonGoods(history.getCommonGoods());
        if (commonGoods == null)
            throw new CommonGoodsException(2, "商品信息不完整");
        history.setCommonGoods(commonGoods);
        scanHistoryV2Mapper.insertSelective(history);
    }
 
    @Override
    public List<ScanHistoryV2> getScanHistoryByDeviceOrUid(Long uid, String device, int page, int pageSize) {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            return null;
        return scanHistoryV2Mapper.selectByDeviceOrUid(uid, device, (page - 1) * pageSize, pageSize);
    }
 
    @Override
    public long getCountByDeviceOrUid(Long uid, String device) {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            return 0;
        Long count = scanHistoryV2Mapper.selectCountByDeviceOrUid(uid, device);
        return count == null ? 0 : count;
    }
 
    @Override
    public void deleteByDeviceOrUid(Long uid, String device) {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            return;
        scanHistoryV2Mapper.deleteByDeviceOrUid(uid, device);
    }
 
    @Override
    public void deleteByAuctionIdAndDeviceOrUid(Long uid, String device, Long auctionId) {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            return;
        List<ScanHistoryV2> list = scanHistoryV2Mapper.selectByDeviceOrUidAndGoodsIdAndGoodsType(uid, device, auctionId,
                CommonGoods.GOODS_TYPE_TB);
        if (list != null)
            for (ScanHistoryV2 sv : list)
                scanHistoryV2Mapper.deleteByPrimaryKey(sv.getId());
    }
 
}