admin
2022-08-25 146dbd5ed24c1fe63766aa45e0ce602caa39748e
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
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.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.dto.suning.SuningGoodsInfo;
import com.yeshi.fanli.dto.vip.goods.VIPGoodsInfo;
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.jd.JDGoods;
import com.yeshi.goods.facade.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 addVIPScanHistory(Long uid, String device, VIPGoodsInfo goods)
            throws CommonGoodsException, ScanHistoryException {
 
        if (uid == null && StringUtil.isNullOrEmpty(device))
            throw new ScanHistoryException(1, "设备或用户信息缺失");
 
        CommonGoods commonGoods = CommonGoodsFactory.create(goods);
        addScanHistory(commonGoods, device, uid);
    }
 
    @Override
    public void addSuningScanHistory(Long uid, String device, SuningGoodsInfo suningGoods)
            throws CommonGoodsException, ScanHistoryException {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            throw new ScanHistoryException(1, "设备或用户信息缺失");
 
        CommonGoods commonGoods = CommonGoodsFactory.create(suningGoods);
        addScanHistory(commonGoods, device, uid);
    }
 
    private void addScanHistory(CommonGoods commonGoods, String device, Long uid)
            throws CommonGoodsException, ScanHistoryException {
        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(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 addJDScanHistory(Long uid, String device, JDGoods goods)
            throws CommonGoodsException, ScanHistoryException {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            throw new ScanHistoryException(1, "设备或用户信息缺失");
 
        CommonGoods commonGoods = CommonGoodsFactory.create(goods);
        addScanHistory(commonGoods, device, uid);
    }
 
    @Override
    public void addPDDScanHistory(Long uid, String device, PDDGoodsDetail pddGoods)
            throws CommonGoodsException, ScanHistoryException {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            throw new ScanHistoryException(1, "设备或用户信息缺失");
 
        CommonGoods commonGoods = CommonGoodsFactory.create(pddGoods);
        addScanHistory(commonGoods, device, uid);
    }
 
    @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,
            Integer source, boolean notBackSuVip) {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            return null;
        return scanHistoryV2Mapper.selectByDeviceOrUid(uid, device, (page - 1) * pageSize, pageSize, source,
                notBackSuVip);
    }
 
    @Override
    public long getCountByDeviceOrUid(Long uid, String device, Integer source, boolean notBackSuVip) {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            return 0;
        Long count = scanHistoryV2Mapper.selectCountByDeviceOrUid(uid, device, source, notBackSuVip);
        return count == null ? 0 : count;
    }
 
    @Override
    public void deleteByDeviceOrUid(Long uid, String device) {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            return;
        List<ScanHistoryV2> list = scanHistoryV2Mapper.listDeviceOrUid(uid, device);
        if (list != null)
            for (ScanHistoryV2 sv : list)
                scanHistoryV2Mapper.deleteByPrimaryKey(sv.getId());
    }
 
    @Override
    public void deleteByAuctionIdAndDeviceOrUid(Long uid, String device, String auctionId) {
        if (uid == null && StringUtil.isNullOrEmpty(device))
            return;
        List<ScanHistoryV2> list = scanHistoryV2Mapper.selectByDeviceOrUidAndGoodsIdAndGoodsType(uid, device,
                auctionId);
        if (list != null)
            for (ScanHistoryV2 sv : list)
                scanHistoryV2Mapper.deleteByPrimaryKey(sv.getId());
    }
 
    @Override
    public void deleteByCommonIdAndDeviceOrUid(Long uid, String device, Long commonId) {
        if (uid == null && StringUtil.isNullOrEmpty(device)) {
            return;
        }
 
        List<ScanHistoryV2> list = scanHistoryV2Mapper.getByCommonGoodsId(uid, device, commonId);
        if (list != null && list.size() > 0) {
            for (ScanHistoryV2 sv : list) {
                scanHistoryV2Mapper.deleteByPrimaryKey(sv.getId());
            }
        }
    }
 
}