Authored by csgyoho

种草-潮物管理

package com.yohobuy.platform.grass.restapi;
import com.yohobuy.platform.grass.service.IGrassArticleCommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by shengguo.cai on 2019/1/21.
*/
@RestController
@RequestMapping("/grassArticleComment")
public class GrassArticleCommentController {
private static final Logger logger = LoggerFactory.getLogger(GrassArticleCommentController.class);
@Autowired
private IGrassArticleCommentService grassArticleCommentService;
}
... ...
package com.yohobuy.platform.grass.restapi;
import com.yohobuy.platform.grass.service.IGrassGoodsCommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by shengguo.cai on 2019/1/21.
*/
@RestController
@RequestMapping("/grassGoodsComment")
public class GrassGoodsCommentController {
private static final Logger logger = LoggerFactory.getLogger(GrassGoodsCommentController.class);
@Autowired
private IGrassGoodsCommentService grassGoodsCommentService;
}
... ...
package com.yohobuy.platform.grass.restapi;
import com.alibaba.fastjson.JSONObject;
import com.yohobuy.platform.dal.grass.model.GrassGoods;
import com.yohobuy.platform.grass.service.IGrassGoodsService;
import com.yohobuy.platform.model.common.ApiResponse;
import com.yohobuy.platform.model.common.PageResponseVO;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Created by shengguo.cai on 2019/1/21.
*/
@RestController
@RequestMapping("/grassGoods")
public class GrassGoodsController {
private static final Logger logger = LoggerFactory.getLogger(GrassGoodsController.class);
@Autowired
private IGrassGoodsService grassGoodsService;
@RequestMapping("/queryGoods")
@ResponseBody
public ApiResponse queryGoods(GoodsQueryReq req){
logger.info("enter queryGoods.param is {}",req);
PageResponseVO<GoodsQueryRsp> result = grassGoodsService.queryGoods(req);
return new ApiResponse.ApiResponseBuilder().data(result).build();
}
@RequestMapping("/searchProducts")
@ResponseBody
public ApiResponse searchProducts(ProductSearchReq req){
logger.info("enter searchProducts.param is {}",req);
PageResponseVO<JSONObject> result = grassGoodsService.searchProducts(req);
return new ApiResponse.ApiResponseBuilder().data(result).build();
}
@RequestMapping("/batchAddGoods")
@ResponseBody
public ApiResponse batchAddGoods(@RequestBody List<BatchAddGoodsReq> reqList){
logger.info("enter batchAddGoods.param is {}",reqList);
grassGoodsService.batchAddGoods(reqList);
return new ApiResponse.ApiResponseBuilder().message("批量新增成功").build();
}
@RequestMapping("/getGoods")
@ResponseBody
public ApiResponse getGoods(@RequestParam("id") Integer id){
logger.info("enter getGoods.id is {}",id);
GrassGoods result = grassGoodsService.getGoods(id);
return new ApiResponse.ApiResponseBuilder().data(result).build();
}
@RequestMapping("/modifyGoods")
@ResponseBody
public ApiResponse modifyGoods(GoodsModifyReq req){
logger.info("enter modifyGoods.param is {}",req);
grassGoodsService.modifyGoods(req);
return new ApiResponse.ApiResponseBuilder().message("修改成功").build();
}
}
... ...
package com.yohobuy.platform.grass.service;
/**
* Created by shengguo.cai on 2019/1/21.
*/
public interface IGrassArticleCommentService {
}
... ...
package com.yohobuy.platform.grass.service;
/**
* Created by shengguo.cai on 2019/1/21.
*/
public interface IGrassGoodsCommentService {
}
... ...
package com.yohobuy.platform.grass.service;
import com.alibaba.fastjson.JSONObject;
import com.yohobuy.platform.dal.grass.model.GrassGoods;
import com.yohobuy.platform.model.common.PageResponseVO;
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 java.util.List;
/**
* Created by shengguo.cai on 2019/1/21.
*/
public interface IGrassGoodsService {
PageResponseVO<GoodsQueryRsp> queryGoods(GoodsQueryReq req);
PageResponseVO<JSONObject> searchProducts(ProductSearchReq req);
void batchAddGoods(List<BatchAddGoodsReq> reqList);
GrassGoods getGoods(Integer id);
void modifyGoods(GoodsModifyReq req);
}
... ...
package com.yohobuy.platform.grass.service.impl;
import com.yohobuy.platform.grass.service.IGrassArticleCommentService;
import org.springframework.stereotype.Service;
/**
* Created by shengguo.cai on 2019/1/21.
*/
@Service
public class GrassArticleCommentServiceImpl implements IGrassArticleCommentService {
}
... ...
package com.yohobuy.platform.grass.service.impl;
import com.yohobuy.platform.grass.service.IGrassGoodsCommentService;
import org.springframework.stereotype.Service;
/**
* Created by shengguo.cai on 2019/1/21.
*/
@Service
public class GrassGoodsCommentServiceImpl implements IGrassGoodsCommentService {
}
... ...
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;
}
}
... ...
... ... @@ -342,7 +342,8 @@ datasources:
- com.yohobuy.platform.dal.grass.IGrassVirtualUserDao
- com.yohobuy.platform.dal.grass.IGrassLabelDAO
- com.yohobuy.platform.dal.grass.IGrassTopicDAO
- com.yohobuy.platform.dal.grass.IGrassGoodsDao
- com.yohobuy.platform.dal.grass.IGrassGoodsCommentDao
... ...
... ... @@ -340,6 +340,8 @@ datasources:
- com.yohobuy.platform.dal.grass.IGrassArticleProductDao
- com.yohobuy.platform.dal.grass.IGrassVirtualUserDao
- com.yohobuy.platform.dal.grass.IGrassTopicDAO
- com.yohobuy.platform.dal.grass.IGrassGoodsDao
- com.yohobuy.platform.dal.grass.IGrassGoodsCommentDao
yhb_promotion:
servers:
... ...