|
|
1
|
+package com.yohoufo.product.service.impl;
|
|
|
2
|
+
|
|
|
3
|
+
|
|
|
4
|
+import com.alibaba.fastjson.JSON;
|
|
|
5
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
6
|
+import com.yoho.core.config.ConfigReader;
|
|
|
7
|
+import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
8
|
+import com.yoho.core.rest.client.ServiceCaller;
|
|
|
9
|
+import com.yoho.error.ServiceError;
|
|
|
10
|
+import com.yoho.error.exception.ServiceException;
|
|
|
11
|
+import com.yoho.service.model.response.ProfileInfoRsp;
|
|
|
12
|
+import com.yohoufo.common.cache.CacheClient;
|
|
|
13
|
+import com.yohoufo.common.exception.GatewayException;
|
|
|
14
|
+import com.yohoufo.common.utils.DateUtil;
|
|
|
15
|
+import com.yohoufo.common.utils.MobileHelper;
|
|
|
16
|
+import com.yohoufo.dal.order.BuyerOrderGoodsMapper;
|
|
|
17
|
+import com.yohoufo.dal.order.BuyerOrderMapper;
|
|
|
18
|
+import com.yohoufo.dal.order.QiniuLiveRecordMapper;
|
|
|
19
|
+import com.yohoufo.dal.order.SellerOrderGoodsMapper;
|
|
|
20
|
+import com.yohoufo.dal.order.model.BuyerOrder;
|
|
|
21
|
+import com.yohoufo.dal.order.model.BuyerOrderGoods;
|
|
|
22
|
+import com.yohoufo.dal.order.model.QiniuLiveRecord;
|
|
|
23
|
+import com.yohoufo.dal.order.model.SellerOrderGoods;
|
|
|
24
|
+import com.yohoufo.dal.product.IdentifyRecordsMapper;
|
|
|
25
|
+import com.yohoufo.dal.product.model.IdentifyRecord;
|
|
|
26
|
+import com.yohoufo.product.response.IdentifyShareInfoResp;
|
|
|
27
|
+import com.yohoufo.product.response.IdentifyTrackResp;
|
|
|
28
|
+import com.yohoufo.product.response.ProductIdentifyResp;
|
|
|
29
|
+import com.yohoufo.product.service.ProductIdentifyService;
|
|
|
30
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
31
|
+import org.slf4j.Logger;
|
|
|
32
|
+import org.slf4j.LoggerFactory;
|
|
|
33
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
34
|
+import org.springframework.stereotype.Service;
|
|
|
35
|
+
|
|
|
36
|
+import java.text.SimpleDateFormat;
|
|
|
37
|
+import java.util.*;
|
|
|
38
|
+import java.util.stream.Collectors;
|
|
|
39
|
+
|
|
|
40
|
+@Service
|
|
|
41
|
+public class ProductIdentifyServiceImpl implements ProductIdentifyService{
|
|
|
42
|
+
|
|
|
43
|
+ private final Logger logger = LoggerFactory.getLogger(ProductIdentifyServiceImpl.class);
|
|
|
44
|
+
|
|
|
45
|
+ public static final String LIVE_VEDIO_DOMAIN = "http://yhb-img01.qiniudn.com/";
|
|
|
46
|
+
|
|
|
47
|
+ @Autowired
|
|
|
48
|
+ private IdentifyRecordsMapper identifyRecordsMapper;
|
|
|
49
|
+
|
|
|
50
|
+ @Autowired
|
|
|
51
|
+ private BuyerOrderGoodsMapper buyerOrderGoodsMapper;
|
|
|
52
|
+
|
|
|
53
|
+ @Autowired
|
|
|
54
|
+ private SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
55
|
+
|
|
|
56
|
+ @Autowired
|
|
|
57
|
+ private QiniuLiveRecordMapper qiniuLiveRecordMapper;
|
|
|
58
|
+
|
|
|
59
|
+ @Autowired
|
|
|
60
|
+ private BuyerOrderMapper buyerOrderMapper;
|
|
|
61
|
+
|
|
|
62
|
+ @Autowired
|
|
|
63
|
+ private ServiceCaller serviceCaller;
|
|
|
64
|
+
|
|
|
65
|
+ @Autowired
|
|
|
66
|
+ private CacheClient clientCache;
|
|
|
67
|
+
|
|
|
68
|
+ @Autowired
|
|
|
69
|
+ private ConfigReader configReader;
|
|
|
70
|
+
|
|
|
71
|
+ /**
|
|
|
72
|
+ * 鉴定结果查询接口
|
|
|
73
|
+ * @param tagId
|
|
|
74
|
+ * @param nfcUid
|
|
|
75
|
+ * @return
|
|
|
76
|
+ */
|
|
|
77
|
+ @Override
|
|
|
78
|
+ public ProductIdentifyResp queryIdentifyInfo(String tagId, String nfcUid) throws GatewayException {
|
|
|
79
|
+ logger.info("enter queryIdentifyInfo, tagId = {}, nfcUid={}", tagId, nfcUid);
|
|
|
80
|
+ //返回结果可能在缓存中
|
|
|
81
|
+ ProductIdentifyResp result = getIdentifyFromCache(tagId, nfcUid);
|
|
|
82
|
+ if(result != null ){
|
|
|
83
|
+ logger.info("queryIdentifyInfo get result from cache success! tagId = {}, nfcUid={},result={} ", tagId, nfcUid, result );
|
|
|
84
|
+ return result;
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ result = new ProductIdentifyResp();
|
|
|
88
|
+ //鉴定 记录--先从缓存去取
|
|
|
89
|
+ IdentifyRecord identifyRecord = queryIdentifyRecord(tagId, nfcUid);
|
|
|
90
|
+ if(identifyRecord == null || identifyRecord.getOrderCode() == null){
|
|
|
91
|
+ throw new GatewayException(402, "鉴定信息不存在");
|
|
|
92
|
+ }
|
|
|
93
|
+ //根据鉴定记录 获取订单号
|
|
|
94
|
+ Long orderCode = identifyRecord.getOrderCode();
|
|
|
95
|
+ //订单号 获取订单详细信息
|
|
|
96
|
+ BuyerOrder buyerOrder = buyerOrderMapper.selectOnlyByOrderCode(orderCode);
|
|
|
97
|
+ if(buyerOrder == null){
|
|
|
98
|
+ throw new GatewayException(403, "订单不存在");
|
|
|
99
|
+ }
|
|
|
100
|
+ Integer buyerUid = buyerOrder.getUid();//买家
|
|
|
101
|
+ Integer sellerUid = buyerOrder.getSellerUid();//卖家
|
|
|
102
|
+ Integer orderStatus = buyerOrder.getStatus();//5--订单已经完成
|
|
|
103
|
+
|
|
|
104
|
+ //订单号获取 skup
|
|
|
105
|
+ BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectSkupByOrderCode(orderCode);
|
|
|
106
|
+ Integer skup = buyerOrderGoods.getSkup();
|
|
|
107
|
+ //skup获取 productInfo
|
|
|
108
|
+ SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(skup);
|
|
|
109
|
+ result.setProductImageUrl(sellerOrderGoods.getImageUrl());
|
|
|
110
|
+ result.setNfcUid(identifyRecord.getNfcUid());
|
|
|
111
|
+ result.setProductId(sellerOrderGoods.getProductId());
|
|
|
112
|
+ result.setProductName(sellerOrderGoods.getProductName());
|
|
|
113
|
+ result.setProductSize(sellerOrderGoods.getSizeName());
|
|
|
114
|
+
|
|
|
115
|
+ //vedioFileUrl视频链接
|
|
|
116
|
+ QiniuLiveRecord qiniuLiveRecords = qiniuLiveRecordMapper.selectByOrderCodes(orderCode);
|
|
|
117
|
+ if (qiniuLiveRecords != null && StringUtils.isNotBlank(qiniuLiveRecords.getVedioFileUrl())) {
|
|
|
118
|
+ result.setVedioFileUrl(LIVE_VEDIO_DOMAIN + qiniuLiveRecords.getVedioFileUrl());
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ //鉴定时间
|
|
|
122
|
+ int authTime = identifyRecord.getAuthTime();
|
|
|
123
|
+ String timeStr = formatDate(authTime, "yyyy.MM.dd HH:mm:ss");
|
|
|
124
|
+ result.setIdentifyTime(timeStr);
|
|
|
125
|
+ String authName = "UFO鉴定中心";
|
|
|
126
|
+ result.setIdentifyPlat(authName);
|
|
|
127
|
+
|
|
|
128
|
+ //交易轨迹---最多三条
|
|
|
129
|
+ List<IdentifyTrackResp> trackRespList = new ArrayList<>();
|
|
|
130
|
+ result.setTrackList(trackRespList);
|
|
|
131
|
+ //1)鉴定结果
|
|
|
132
|
+ IdentifyTrackResp identifyTrack = new IdentifyTrackResp();
|
|
|
133
|
+ identifyTrack.setTime(authTime);
|
|
|
134
|
+ identifyTrack.setTimeStr(timeStr);
|
|
|
135
|
+ identifyTrack.setContent( authName + "鉴定结果为\"真\"");
|
|
|
136
|
+ String auth_inco = configReader.getString("ufo.product.authIcon", "XXXXXX");
|
|
|
137
|
+ identifyTrack.setHeadIcon(auth_inco);
|
|
|
138
|
+ trackRespList.add(identifyTrack);
|
|
|
139
|
+
|
|
|
140
|
+
|
|
|
141
|
+ List<Integer> uidsList = Arrays.asList(buyerUid, sellerUid);
|
|
|
142
|
+ Map<Integer, ProfileInfoRsp> profileMap = getUserProfileInfo(uidsList);
|
|
|
143
|
+ ProfileInfoRsp buyerInfo = profileMap.get(buyerUid);
|
|
|
144
|
+
|
|
|
145
|
+ //订单轨迹
|
|
|
146
|
+ String mobile = MobileHelper.coverMobile(buyerInfo.getMobile());//隐位的手机号码
|
|
|
147
|
+ String headIcon = buyerInfo.getHead_ico();//头像
|
|
|
148
|
+ //订单更新时间
|
|
|
149
|
+ if(orderStatus != null && orderStatus.intValue() == 5){//订单已完成
|
|
|
150
|
+ IdentifyTrackResp updateOrderTrack = new IdentifyTrackResp();
|
|
|
151
|
+ timeStr = formatDate(buyerOrder.getUpdateTime(), "yyyy.MM.dd HH:mm:ss");
|
|
|
152
|
+ updateOrderTrack.setTime(buyerOrder.getUpdateTime());
|
|
|
153
|
+ updateOrderTrack.setTimeStr(timeStr);
|
|
|
154
|
+ updateOrderTrack.setContent("用户" + mobile + "成为商品新主人");
|
|
|
155
|
+ updateOrderTrack.setHeadIcon(headIcon);
|
|
|
156
|
+ updateOrderTrack.setMobile(mobile);
|
|
|
157
|
+
|
|
|
158
|
+ trackRespList.add(updateOrderTrack);
|
|
|
159
|
+ }
|
|
|
160
|
+ //创建订单的时间
|
|
|
161
|
+ IdentifyTrackResp createOrderTrack = new IdentifyTrackResp();
|
|
|
162
|
+ createOrderTrack.setUid(buyerUid);
|
|
|
163
|
+ createOrderTrack.setHeadIcon(headIcon);
|
|
|
164
|
+ createOrderTrack.setContent("用户"+ mobile + "在UFO平台下单购买");
|
|
|
165
|
+ createOrderTrack.setTime(buyerOrder.getCreateTime());
|
|
|
166
|
+ timeStr = formatDate(buyerOrder.getCreateTime(),"yyyy.MM.dd HH:mm:ss");
|
|
|
167
|
+ createOrderTrack.setTimeStr(timeStr);
|
|
|
168
|
+ createOrderTrack.setMobile(mobile);
|
|
|
169
|
+ trackRespList.add(createOrderTrack);
|
|
|
170
|
+
|
|
|
171
|
+ Collections.sort(trackRespList, new Comparator<IdentifyTrackResp>(){//时间倒序排列
|
|
|
172
|
+ @Override
|
|
|
173
|
+ public int compare(IdentifyTrackResp o1, IdentifyTrackResp o2) {
|
|
|
174
|
+ Integer time1 = o1.getTime();
|
|
|
175
|
+ Integer time2 = o2.getTime();
|
|
|
176
|
+ return time2 -time1;
|
|
|
177
|
+ }
|
|
|
178
|
+ });
|
|
|
179
|
+
|
|
|
180
|
+ //设置缓存--可能会有延时,不影响的
|
|
|
181
|
+ setIdentifyCache(tagId, nfcUid, result);
|
|
|
182
|
+ logger.info("queryIdentifyInfo success!, tagId = {}, nfcUid={}, result ={}", tagId, nfcUid, result );
|
|
|
183
|
+ return result;
|
|
|
184
|
+ }
|
|
|
185
|
+
|
|
|
186
|
+
|
|
|
187
|
+ private IdentifyRecord queryIdentifyRecord(String tagId, String nfcUid) {
|
|
|
188
|
+
|
|
|
189
|
+ RedisKeyBuilder kb = new RedisKeyBuilder().appendFixed("ufo:product:identifyRecord:").
|
|
|
190
|
+ appendVar(tagId).appendVar(":").appendVar(nfcUid);
|
|
|
191
|
+ IdentifyRecord result = clientCache.get(kb, IdentifyRecord.class);
|
|
|
192
|
+ if( result != null){
|
|
|
193
|
+ logger.info("queryIdentifyRecord from cache success!");
|
|
|
194
|
+ return result;
|
|
|
195
|
+ }
|
|
|
196
|
+
|
|
|
197
|
+ result = identifyRecordsMapper.selectByTagAndNfcId(tagId, nfcUid);
|
|
|
198
|
+
|
|
|
199
|
+ if(result != null){
|
|
|
200
|
+ clientCache.set(kb.getKey(), 5 * 60, result );
|
|
|
201
|
+ }
|
|
|
202
|
+ return result;
|
|
|
203
|
+ }
|
|
|
204
|
+
|
|
|
205
|
+ /**
|
|
|
206
|
+ *
|
|
|
207
|
+ * @param tagId
|
|
|
208
|
+ * @param nfcUid
|
|
|
209
|
+ * @param result
|
|
|
210
|
+ */
|
|
|
211
|
+ private void setIdentifyCache(String tagId, String nfcUid, ProductIdentifyResp result) {
|
|
|
212
|
+
|
|
|
213
|
+ RedisKeyBuilder kb = new RedisKeyBuilder().appendFixed("ufo:product:identifyInfo:").
|
|
|
214
|
+ appendVar(tagId).appendVar(":").appendVar(nfcUid);
|
|
|
215
|
+ clientCache.set(kb.getKey(), 5 * 60, result );
|
|
|
216
|
+ }
|
|
|
217
|
+
|
|
|
218
|
+ private ProductIdentifyResp getIdentifyFromCache(String tagId, String nfcUid) {
|
|
|
219
|
+ RedisKeyBuilder kb = new RedisKeyBuilder().appendFixed("ufo:product:identifyInfo:").
|
|
|
220
|
+ appendVar(tagId).appendVar(":").appendVar(nfcUid);
|
|
|
221
|
+ ProductIdentifyResp identifyResp = clientCache.get(kb.getKey(), ProductIdentifyResp.class);
|
|
|
222
|
+ return identifyResp;
|
|
|
223
|
+ }
|
|
|
224
|
+
|
|
|
225
|
+ /**
|
|
|
226
|
+ * 鉴定结果分享
|
|
|
227
|
+ * @param uid
|
|
|
228
|
+ * @param tagId
|
|
|
229
|
+ * @return
|
|
|
230
|
+ */
|
|
|
231
|
+ @Override
|
|
|
232
|
+ public IdentifyShareInfoResp getShareIdentifyInfo( String tagId, String nfcUid) {
|
|
|
233
|
+ logger.info("enter getShareIdentifyInfo , tagId={}, nfcUid={}", tagId, nfcUid);
|
|
|
234
|
+ IdentifyShareInfoResp result = new IdentifyShareInfoResp();
|
|
|
235
|
+
|
|
|
236
|
+ IdentifyRecord identifyRecord = identifyRecordsMapper.selectByTagAndNfcId(tagId, nfcUid);
|
|
|
237
|
+ if(identifyRecord == null){
|
|
|
238
|
+ throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
239
|
+ }
|
|
|
240
|
+
|
|
|
241
|
+ Long orderCode = identifyRecord.getOrderCode();//订单号
|
|
|
242
|
+
|
|
|
243
|
+ //订单号获取 skup
|
|
|
244
|
+ BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectSkupByOrderCode(orderCode);
|
|
|
245
|
+ Integer skup = buyerOrderGoods.getSkup();
|
|
|
246
|
+ //skup获取 productInfo
|
|
|
247
|
+ SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(skup);
|
|
|
248
|
+ result.setProductImageUrl(sellerOrderGoods.getImageUrl());
|
|
|
249
|
+ result.setProductId(sellerOrderGoods.getProductId());
|
|
|
250
|
+ result.setProductName(sellerOrderGoods.getProductName());
|
|
|
251
|
+ result.setAllowTransfer(identifyRecord.getAllowTransfer() == 0 ? "Y" : "N");
|
|
|
252
|
+
|
|
|
253
|
+ //买家的头像
|
|
|
254
|
+ Integer buyerUid = buyerOrderGoods.getUid();
|
|
|
255
|
+ List<Integer> uidsList = Arrays.asList(buyerUid);
|
|
|
256
|
+ Map<Integer, ProfileInfoRsp> profileMap = getUserProfileInfo(uidsList);
|
|
|
257
|
+ ProfileInfoRsp profile = profileMap.get(buyerUid);
|
|
|
258
|
+ result.setHeadIcon(profile.getHead_ico());
|
|
|
259
|
+ return result;
|
|
|
260
|
+ }
|
|
|
261
|
+
|
|
|
262
|
+ private Map<Integer,ProfileInfoRsp> getUserProfileInfo(List<Integer> uidsList) {
|
|
|
263
|
+ Map<Integer,ProfileInfoRsp > result = new HashMap<>();
|
|
|
264
|
+ if(uidsList == null || uidsList.size() == 0){
|
|
|
265
|
+ return result;
|
|
|
266
|
+ }
|
|
|
267
|
+ StringBuilder sb = new StringBuilder();
|
|
|
268
|
+ for(Integer uid : uidsList ){
|
|
|
269
|
+ if(uid != null && uid > 0){
|
|
|
270
|
+ sb.append(uid).append(",");
|
|
|
271
|
+ }
|
|
|
272
|
+ }
|
|
|
273
|
+ String s = sb.toString();
|
|
|
274
|
+ if(s.length() == 0){
|
|
|
275
|
+ return result;
|
|
|
276
|
+ }
|
|
|
277
|
+
|
|
|
278
|
+ String uids = s.substring(0, s.length() - 1);
|
|
|
279
|
+ List<JSONObject> rsp = serviceCaller.call("uic.getUserProfilesByUids", uids, List.class);
|
|
|
280
|
+ //必须要强转下
|
|
|
281
|
+ List<ProfileInfoRsp> resultList = rsp.stream().map(profileInfoRsp -> JSONObject.parseObject(JSONObject.toJSONString(profileInfoRsp),ProfileInfoRsp.class))
|
|
|
282
|
+ .collect(Collectors.toList());
|
|
|
283
|
+
|
|
|
284
|
+ result = resultList.stream().collect(Collectors.toMap(ProfileInfoRsp :: getUid, (ProfileInfoRsp obj) -> obj));
|
|
|
285
|
+ return result;
|
|
|
286
|
+
|
|
|
287
|
+ }
|
|
|
288
|
+
|
|
|
289
|
+ private String formatDate(Integer time, String pattern) {
|
|
|
290
|
+
|
|
|
291
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
|
|
|
292
|
+ return dateFormat.format(new Date((long) time * 1000));
|
|
|
293
|
+ }
|
|
|
294
|
+
|
|
|
295
|
+
|
|
|
296
|
+} |