|
|
package com.yohoufo.product.service.impl;
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.config.ConfigReader;
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yoho.core.rest.client.ServiceCaller;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.service.model.response.ProfileInfoRsp;
|
|
|
import com.yohoufo.common.cache.CacheClient;
|
|
|
import com.yohoufo.common.exception.GatewayException;
|
|
|
import com.yohoufo.common.utils.DateUtil;
|
|
|
import com.yohoufo.common.utils.MobileHelper;
|
|
|
import com.yohoufo.dal.order.BuyerOrderGoodsMapper;
|
|
|
import com.yohoufo.dal.order.BuyerOrderMapper;
|
|
|
import com.yohoufo.dal.order.QiniuLiveRecordMapper;
|
|
|
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
|
|
|
import com.yohoufo.dal.order.model.BuyerOrder;
|
|
|
import com.yohoufo.dal.order.model.BuyerOrderGoods;
|
|
|
import com.yohoufo.dal.order.model.QiniuLiveRecord;
|
|
|
import com.yohoufo.dal.order.model.SellerOrderGoods;
|
|
|
import com.yohoufo.dal.product.IdentifyRecordsMapper;
|
|
|
import com.yohoufo.dal.product.model.IdentifyRecord;
|
|
|
import com.yohoufo.product.response.IdentifyShareInfoResp;
|
|
|
import com.yohoufo.product.response.IdentifyTrackResp;
|
|
|
import com.yohoufo.product.response.ProductIdentifyResp;
|
|
|
import com.yohoufo.product.service.ProductIdentifyService;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class ProductIdentifyServiceImpl implements ProductIdentifyService{
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(ProductIdentifyServiceImpl.class);
|
|
|
|
|
|
public static final String LIVE_VEDIO_DOMAIN = "http://yhb-img01.qiniudn.com/";
|
|
|
|
|
|
@Autowired
|
|
|
private IdentifyRecordsMapper identifyRecordsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private BuyerOrderGoodsMapper buyerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private QiniuLiveRecordMapper qiniuLiveRecordMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private BuyerOrderMapper buyerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private ServiceCaller serviceCaller;
|
|
|
|
|
|
@Autowired
|
|
|
private CacheClient clientCache;
|
|
|
|
|
|
@Autowired
|
|
|
private ConfigReader configReader;
|
|
|
|
|
|
/**
|
|
|
* 鉴定结果查询接口
|
|
|
* @param tagId
|
|
|
* @param nfcUid
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
public ProductIdentifyResp queryIdentifyInfo(String tagId, String nfcUid) throws GatewayException {
|
|
|
logger.info("enter queryIdentifyInfo, tagId = {}, nfcUid={}", tagId, nfcUid);
|
|
|
//返回结果可能在缓存中
|
|
|
ProductIdentifyResp result = getIdentifyFromCache(tagId, nfcUid);
|
|
|
if(result != null ){
|
|
|
logger.info("queryIdentifyInfo get result from cache success! tagId = {}, nfcUid={},result={} ", tagId, nfcUid, result );
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
result = new ProductIdentifyResp();
|
|
|
//鉴定 记录--先从缓存去取
|
|
|
IdentifyRecord identifyRecord = queryIdentifyRecord(tagId, nfcUid);
|
|
|
if(identifyRecord == null || identifyRecord.getOrderCode() == null){
|
|
|
throw new GatewayException(402, "鉴定信息不存在");
|
|
|
}
|
|
|
//根据鉴定记录 获取订单号
|
|
|
Long orderCode = identifyRecord.getOrderCode();
|
|
|
//订单号 获取订单详细信息
|
|
|
BuyerOrder buyerOrder = buyerOrderMapper.selectOnlyByOrderCode(orderCode);
|
|
|
if(buyerOrder == null){
|
|
|
throw new GatewayException(403, "订单不存在");
|
|
|
}
|
|
|
Integer buyerUid = buyerOrder.getUid();//买家
|
|
|
Integer sellerUid = buyerOrder.getSellerUid();//卖家
|
|
|
Integer orderStatus = buyerOrder.getStatus();//5--订单已经完成
|
|
|
|
|
|
//订单号获取 skup
|
|
|
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectSkupByOrderCode(orderCode);
|
|
|
Integer skup = buyerOrderGoods.getSkup();
|
|
|
//skup获取 productInfo
|
|
|
SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(skup);
|
|
|
result.setProductImageUrl(sellerOrderGoods.getImageUrl());
|
|
|
result.setNfcUid(identifyRecord.getNfcUid());
|
|
|
result.setProductId(sellerOrderGoods.getProductId());
|
|
|
result.setProductName(sellerOrderGoods.getProductName());
|
|
|
result.setProductSize(sellerOrderGoods.getSizeName());
|
|
|
|
|
|
//vedioFileUrl视频链接
|
|
|
QiniuLiveRecord qiniuLiveRecords = qiniuLiveRecordMapper.selectByOrderCodes(orderCode);
|
|
|
if (qiniuLiveRecords != null && StringUtils.isNotBlank(qiniuLiveRecords.getVedioFileUrl())) {
|
|
|
result.setVedioFileUrl(LIVE_VEDIO_DOMAIN + qiniuLiveRecords.getVedioFileUrl());
|
|
|
}
|
|
|
|
|
|
//鉴定时间
|
|
|
int authTime = identifyRecord.getAuthTime();
|
|
|
String timeStr = formatDate(authTime, "yyyy.MM.dd HH:mm:ss");
|
|
|
result.setIdentifyTime(timeStr);
|
|
|
String authName = "UFO鉴定中心";
|
|
|
result.setIdentifyPlat(authName);
|
|
|
|
|
|
//交易轨迹---最多三条
|
|
|
List<IdentifyTrackResp> trackRespList = new ArrayList<>();
|
|
|
result.setTrackList(trackRespList);
|
|
|
//1)鉴定结果
|
|
|
IdentifyTrackResp identifyTrack = new IdentifyTrackResp();
|
|
|
identifyTrack.setTime(authTime);
|
|
|
identifyTrack.setTimeStr(timeStr);
|
|
|
identifyTrack.setContent( authName + "鉴定结果为\"真\"");
|
|
|
String auth_inco = configReader.getString("ufo.product.authIcon", "XXXXXX");
|
|
|
identifyTrack.setHeadIcon(auth_inco);
|
|
|
trackRespList.add(identifyTrack);
|
|
|
|
|
|
|
|
|
List<Integer> uidsList = Arrays.asList(buyerUid, sellerUid);
|
|
|
Map<Integer, ProfileInfoRsp> profileMap = getUserProfileInfo(uidsList);
|
|
|
ProfileInfoRsp buyerInfo = profileMap.get(buyerUid);
|
|
|
|
|
|
//订单轨迹
|
|
|
String mobile = MobileHelper.coverMobile(buyerInfo.getMobile());//隐位的手机号码
|
|
|
String headIcon = buyerInfo.getHead_ico();//头像
|
|
|
//订单更新时间
|
|
|
if(orderStatus != null && orderStatus.intValue() == 5){//订单已完成
|
|
|
IdentifyTrackResp updateOrderTrack = new IdentifyTrackResp();
|
|
|
timeStr = formatDate(buyerOrder.getUpdateTime(), "yyyy.MM.dd HH:mm:ss");
|
|
|
updateOrderTrack.setTime(buyerOrder.getUpdateTime());
|
|
|
updateOrderTrack.setTimeStr(timeStr);
|
|
|
updateOrderTrack.setContent("用户" + mobile + "成为商品新主人");
|
|
|
updateOrderTrack.setHeadIcon(headIcon);
|
|
|
updateOrderTrack.setMobile(mobile);
|
|
|
|
|
|
trackRespList.add(updateOrderTrack);
|
|
|
}
|
|
|
//创建订单的时间
|
|
|
IdentifyTrackResp createOrderTrack = new IdentifyTrackResp();
|
|
|
createOrderTrack.setUid(buyerUid);
|
|
|
createOrderTrack.setHeadIcon(headIcon);
|
|
|
createOrderTrack.setContent("用户"+ mobile + "在UFO平台下单购买");
|
|
|
createOrderTrack.setTime(buyerOrder.getCreateTime());
|
|
|
timeStr = formatDate(buyerOrder.getCreateTime(),"yyyy.MM.dd HH:mm:ss");
|
|
|
createOrderTrack.setTimeStr(timeStr);
|
|
|
createOrderTrack.setMobile(mobile);
|
|
|
trackRespList.add(createOrderTrack);
|
|
|
|
|
|
Collections.sort(trackRespList, new Comparator<IdentifyTrackResp>(){//时间倒序排列
|
|
|
@Override
|
|
|
public int compare(IdentifyTrackResp o1, IdentifyTrackResp o2) {
|
|
|
Integer time1 = o1.getTime();
|
|
|
Integer time2 = o2.getTime();
|
|
|
return time2 -time1;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
//设置缓存--可能会有延时,不影响的
|
|
|
setIdentifyCache(tagId, nfcUid, result);
|
|
|
logger.info("queryIdentifyInfo success!, tagId = {}, nfcUid={}, result ={}", tagId, nfcUid, result );
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
private IdentifyRecord queryIdentifyRecord(String tagId, String nfcUid) {
|
|
|
|
|
|
RedisKeyBuilder kb = new RedisKeyBuilder().appendFixed("ufo:product:identifyRecord:").
|
|
|
appendVar(tagId).appendVar(":").appendVar(nfcUid);
|
|
|
IdentifyRecord result = clientCache.get(kb, IdentifyRecord.class);
|
|
|
if( result != null){
|
|
|
logger.info("queryIdentifyRecord from cache success!");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
result = identifyRecordsMapper.selectByTagAndNfcId(tagId, nfcUid);
|
|
|
|
|
|
if(result != null){
|
|
|
clientCache.set(kb.getKey(), 5 * 60, result );
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param tagId
|
|
|
* @param nfcUid
|
|
|
* @param result
|
|
|
*/
|
|
|
private void setIdentifyCache(String tagId, String nfcUid, ProductIdentifyResp result) {
|
|
|
|
|
|
RedisKeyBuilder kb = new RedisKeyBuilder().appendFixed("ufo:product:identifyInfo:").
|
|
|
appendVar(tagId).appendVar(":").appendVar(nfcUid);
|
|
|
clientCache.set(kb.getKey(), 5 * 60, result );
|
|
|
}
|
|
|
|
|
|
private ProductIdentifyResp getIdentifyFromCache(String tagId, String nfcUid) {
|
|
|
RedisKeyBuilder kb = new RedisKeyBuilder().appendFixed("ufo:product:identifyInfo:").
|
|
|
appendVar(tagId).appendVar(":").appendVar(nfcUid);
|
|
|
ProductIdentifyResp identifyResp = clientCache.get(kb.getKey(), ProductIdentifyResp.class);
|
|
|
return identifyResp;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 鉴定结果分享
|
|
|
* @param uid
|
|
|
* @param tagId
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
public IdentifyShareInfoResp getShareIdentifyInfo( String tagId, String nfcUid) {
|
|
|
logger.info("enter getShareIdentifyInfo , tagId={}, nfcUid={}", tagId, nfcUid);
|
|
|
IdentifyShareInfoResp result = new IdentifyShareInfoResp();
|
|
|
|
|
|
IdentifyRecord identifyRecord = identifyRecordsMapper.selectByTagAndNfcId(tagId, nfcUid);
|
|
|
if(identifyRecord == null){
|
|
|
throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
}
|
|
|
|
|
|
Long orderCode = identifyRecord.getOrderCode();//订单号
|
|
|
|
|
|
//订单号获取 skup
|
|
|
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectSkupByOrderCode(orderCode);
|
|
|
Integer skup = buyerOrderGoods.getSkup();
|
|
|
//skup获取 productInfo
|
|
|
SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(skup);
|
|
|
result.setProductImageUrl(sellerOrderGoods.getImageUrl());
|
|
|
result.setProductId(sellerOrderGoods.getProductId());
|
|
|
result.setProductName(sellerOrderGoods.getProductName());
|
|
|
result.setAllowTransfer(identifyRecord.getAllowTransfer() == 0 ? "Y" : "N");
|
|
|
|
|
|
//买家的头像
|
|
|
Integer buyerUid = buyerOrderGoods.getUid();
|
|
|
List<Integer> uidsList = Arrays.asList(buyerUid);
|
|
|
Map<Integer, ProfileInfoRsp> profileMap = getUserProfileInfo(uidsList);
|
|
|
ProfileInfoRsp profile = profileMap.get(buyerUid);
|
|
|
result.setHeadIcon(profile.getHead_ico());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private Map<Integer,ProfileInfoRsp> getUserProfileInfo(List<Integer> uidsList) {
|
|
|
Map<Integer,ProfileInfoRsp > result = new HashMap<>();
|
|
|
if(uidsList == null || uidsList.size() == 0){
|
|
|
return result;
|
|
|
}
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
for(Integer uid : uidsList ){
|
|
|
if(uid != null && uid > 0){
|
|
|
sb.append(uid).append(",");
|
|
|
}
|
|
|
}
|
|
|
String s = sb.toString();
|
|
|
if(s.length() == 0){
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
String uids = s.substring(0, s.length() - 1);
|
|
|
List<JSONObject> rsp = serviceCaller.call("uic.getUserProfilesByUids", uids, List.class);
|
|
|
//必须要强转下
|
|
|
List<ProfileInfoRsp> resultList = rsp.stream().map(profileInfoRsp -> JSONObject.parseObject(JSONObject.toJSONString(profileInfoRsp),ProfileInfoRsp.class))
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
result = resultList.stream().collect(Collectors.toMap(ProfileInfoRsp :: getUid, (ProfileInfoRsp obj) -> obj));
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
private String formatDate(Integer time, String pattern) {
|
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
|
|
|
return dateFormat.format(new Date((long) time * 1000));
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|