|
|
package com.yohobuy.platform.grass.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.common.utils.DateUtil;
|
|
|
import com.yoho.core.rest.client.ServiceCaller;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yohobuy.platform.dal.grass.IGrassGoodsCommentDao;
|
|
|
import com.yohobuy.platform.dal.grass.IGrassGoodsDao;
|
|
|
import com.yohobuy.platform.dal.grass.model.GrassGoods;
|
|
|
import com.yohobuy.platform.dal.product.ProductMapper;
|
|
|
import com.yohobuy.platform.dal.product.model.Product;
|
|
|
import com.yohobuy.platform.grass.service.IGrassGoodsService;
|
|
|
import com.yohobuy.platform.model.common.PageResponseVO;
|
|
|
import com.yohobuy.platform.model.grass.GoodsCommentTotalBo;
|
|
|
import com.yohobuy.platform.model.grass.request.BatchAddGoodsReq;
|
|
|
import com.yohobuy.platform.model.grass.request.GoodsModifyReq;
|
|
|
import com.yohobuy.platform.model.grass.request.GoodsQueryReq;
|
|
|
import com.yohobuy.platform.model.grass.request.ProductSearchReq;
|
|
|
import com.yohobuy.platform.model.grass.response.GoodsQueryRsp;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by shengguo.cai on 2019/1/21.
|
|
|
*/
|
|
|
@Service
|
|
|
public class GrassGoodsServiceImpl implements IGrassGoodsService{
|
|
|
private static final Logger logger = LoggerFactory.getLogger(GrassGoodsServiceImpl.class);
|
|
|
@Value("${search.server.address}")
|
|
|
private String searchServerAddress;
|
|
|
@Autowired
|
|
|
private ServiceCaller serviceCaller;
|
|
|
@Autowired
|
|
|
private IGrassGoodsDao grassGoodsDao;
|
|
|
@Autowired
|
|
|
private IGrassGoodsCommentDao grassGoodsCommentDao;
|
|
|
@Autowired
|
|
|
private ProductMapper productMapper;
|
|
|
|
|
|
@Override
|
|
|
public PageResponseVO<GoodsQueryRsp> queryGoods(GoodsQueryReq req) {
|
|
|
if(req == null){
|
|
|
throw new ServiceException(400,"param can not be null!");
|
|
|
}
|
|
|
PageResponseVO<GoodsQueryRsp> result = new PageResponseVO<>();
|
|
|
result.setPage(req.getPage());
|
|
|
result.setSize(req.getSize());
|
|
|
int total = grassGoodsDao.selectTotalByGoodsQueryReq(req);
|
|
|
result.setTotal(total);
|
|
|
if(total == 0){
|
|
|
logger.info("queryGoods#selectTotalByGoodsQueryReq total is 0.param is {}",req);
|
|
|
return result;
|
|
|
}
|
|
|
List<GoodsQueryRsp> goodsBoList = grassGoodsDao.selectByGoodsQueryReq(req);
|
|
|
if(CollectionUtils.isEmpty(goodsBoList)){
|
|
|
logger.info("queryGoods#selectByGoodsQueryReq goodsBoList is empty.param is {}",req);
|
|
|
return result;
|
|
|
}
|
|
|
// 查询评论总数
|
|
|
List<Integer> goodsIds = goodsBoList.stream().map(goods->goods.getId()).collect(Collectors.toList());
|
|
|
Map<Integer,GoodsCommentTotalBo> commentMap = grassGoodsCommentDao.selectTotalMapByGoodsIds(goodsIds);
|
|
|
// 查询商品信息
|
|
|
String skns = goodsBoList.stream().filter(goods-> StringUtils.isNotBlank(goods.getProductSkn())).map(goods->goods.getProductSkn()).collect(Collectors.joining(","));
|
|
|
Map<String,JSONObject> productMap = buildProductMap(skns,1);
|
|
|
// 组装返回
|
|
|
buildGoodsQueryRspBo(goodsBoList,commentMap,productMap);
|
|
|
result.setList(goodsBoList);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public PageResponseVO<JSONObject> searchProducts(ProductSearchReq req) {
|
|
|
try {
|
|
|
JSONObject responseObj = searchProduct(req.getSkns(),req.getPage());
|
|
|
if(responseObj == null || !responseObj.get("code").equals(200)){
|
|
|
throw new ServiceException(500,"search product failed!");
|
|
|
}
|
|
|
PageResponseVO<JSONObject> responseVO = new PageResponseVO<>();
|
|
|
responseVO.setPage(responseObj.getJSONObject("data").getInteger("page"));
|
|
|
responseVO.setSize(responseObj.getJSONObject("data").getInteger("page_size"));
|
|
|
responseVO.setTotal(responseObj.getJSONObject("data").getInteger("total"));
|
|
|
responseVO.setList((List) responseObj.getJSONObject("data").get("product_list"));
|
|
|
return responseVO;
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("searchProducts exp is {}",e);
|
|
|
throw new ServiceException(500,"search product result deal failed!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void batchAddGoods(List<BatchAddGoodsReq> reqList) {
|
|
|
if(CollectionUtils.isEmpty(reqList)){
|
|
|
throw new ServiceException(400,"param is empty!");
|
|
|
}
|
|
|
initAddGoodsContent(reqList);
|
|
|
int createTime = DateUtil.getCurrentTimeSecond();
|
|
|
grassGoodsDao.insertByBatchAddGoodsReq(reqList,createTime);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public GrassGoods getGoods(Integer id) {
|
|
|
return grassGoodsDao.selectById(id);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void modifyGoods(GoodsModifyReq req) {
|
|
|
if(req==null||StringUtils.isBlank(req.getTitle())|| StringUtils.isBlank(req.getContent())
|
|
|
||StringUtils.isBlank(req.getIcon())||req.getWeight()==null||req.getId() == null){
|
|
|
logger.warn("modifyGoods#some params are null!param is {}",req);
|
|
|
throw new ServiceException(400,"some params are null!");
|
|
|
}
|
|
|
req.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
grassGoodsDao.updateByGoodsModifyReq(req);
|
|
|
}
|
|
|
|
|
|
private void initAddGoodsContent(List<BatchAddGoodsReq> reqList) {
|
|
|
List<Integer> skns = reqList.stream().map(req->req.getSkn()).collect(Collectors.toList());
|
|
|
logger.info("initAddGoodsContent#before selectProductBySkns,skns is {}",skns);
|
|
|
List<Product> productList = productMapper.selectProductBySkns(skns);
|
|
|
if(CollectionUtils.isEmpty(productList)){
|
|
|
logger.info("initAddGoodsContent#selectProductBySkns result is empty.skns is {}",skns);
|
|
|
return;
|
|
|
}
|
|
|
Map<Integer,String> productContentMap = new HashMap<>();
|
|
|
productList.stream().forEach(product -> {productContentMap.put(product.getErpProductId(),product.getPhrase());});
|
|
|
reqList.stream().forEach(req->{
|
|
|
req.setContent(productContentMap.get(req.getSkn()));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private void buildGoodsQueryRspBo(List<GoodsQueryRsp> goodsBoList, Map<Integer, GoodsCommentTotalBo> commentMap, Map<String, JSONObject> productMap) {
|
|
|
for(GoodsQueryRsp goods : goodsBoList){
|
|
|
GoodsCommentTotalBo comment = commentMap.get(goods.getId());
|
|
|
JSONObject productJSON = productMap==null?null:productMap.get(goods.getProductSkn());
|
|
|
goods.setCommentTotal(comment==null?0:comment.getTotal());
|
|
|
if(null != productJSON){
|
|
|
goods.setAvailTotal(productJSON.getInteger("storage_num"));
|
|
|
goods.setShelfStatus(productJSON.getInteger("status"));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private Map<String,JSONObject> buildProductMap(String skns,int page){
|
|
|
logger.info("enter buildProductMap,skns is {},page is {}",skns,page);
|
|
|
if(StringUtils.isBlank(skns)){
|
|
|
return null;
|
|
|
}
|
|
|
try {
|
|
|
JSONObject responseObj = searchProduct(skns,page);
|
|
|
if(!responseObj.get("code").equals(200)){
|
|
|
return null;
|
|
|
}
|
|
|
JSONArray productList = responseObj.getJSONObject("data").getJSONArray("product_list");
|
|
|
if(CollectionUtils.isEmpty(productList)){
|
|
|
return null;
|
|
|
}
|
|
|
Map<String,JSONObject> productMap = new HashMap<>();
|
|
|
for(int i=0;i<productList.size();i++){
|
|
|
JSONObject jsonObject = productList.getJSONObject(i);
|
|
|
productMap.put(jsonObject.getString("product_skn"),jsonObject);
|
|
|
}
|
|
|
return productMap;
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("buildProductMap failed!exp is {}",e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
private JSONObject searchProduct(String skns,int page){
|
|
|
logger.info("enter searchProduct,skns is {},page is {}",skns,page);
|
|
|
String url = searchServerAddress + "search" ;
|
|
|
Map<String,Object> reqMap = new HashMap<>();
|
|
|
if(StringUtils.isNotBlank(skns)){
|
|
|
reqMap.put("query",skns);
|
|
|
}
|
|
|
reqMap.put("page",page);
|
|
|
try {
|
|
|
String responseStr=serviceCaller.get("yohosearch.search", url, reqMap, String.class, null).get();
|
|
|
logger.info("searchProduct#yohosearch.search result is {},param is {},url is {}",responseStr,reqMap,url);
|
|
|
JSONObject responseObj = JSONObject.parseObject(responseStr);
|
|
|
return responseObj;
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("searchProduct failed!exp is {}",e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
} |
...
|
...
|
|