Authored by csgyoho

种草-潮物管理

  1 +package com.yohobuy.platform.grass.restapi;
  2 +
  3 +import com.yohobuy.platform.grass.service.IGrassArticleCommentService;
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.web.bind.annotation.RequestMapping;
  8 +import org.springframework.web.bind.annotation.RestController;
  9 +
  10 +/**
  11 + * Created by shengguo.cai on 2019/1/21.
  12 + */
  13 +@RestController
  14 +@RequestMapping("/grassArticleComment")
  15 +public class GrassArticleCommentController {
  16 + private static final Logger logger = LoggerFactory.getLogger(GrassArticleCommentController.class);
  17 + @Autowired
  18 + private IGrassArticleCommentService grassArticleCommentService;
  19 +}
  1 +package com.yohobuy.platform.grass.restapi;
  2 +
  3 +import com.yohobuy.platform.grass.service.IGrassGoodsCommentService;
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.web.bind.annotation.RequestMapping;
  8 +import org.springframework.web.bind.annotation.RestController;
  9 +
  10 +/**
  11 + * Created by shengguo.cai on 2019/1/21.
  12 + */
  13 +@RestController
  14 +@RequestMapping("/grassGoodsComment")
  15 +public class GrassGoodsCommentController {
  16 + private static final Logger logger = LoggerFactory.getLogger(GrassGoodsCommentController.class);
  17 + @Autowired
  18 + private IGrassGoodsCommentService grassGoodsCommentService;
  19 +}
  1 +package com.yohobuy.platform.grass.restapi;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.yohobuy.platform.dal.grass.model.GrassGoods;
  5 +import com.yohobuy.platform.grass.service.IGrassGoodsService;
  6 +import com.yohobuy.platform.model.common.ApiResponse;
  7 +import com.yohobuy.platform.model.common.PageResponseVO;
  8 +import com.yohobuy.platform.model.grass.request.BatchAddGoodsReq;
  9 +import com.yohobuy.platform.model.grass.request.GoodsModifyReq;
  10 +import com.yohobuy.platform.model.grass.request.GoodsQueryReq;
  11 +import com.yohobuy.platform.model.grass.request.ProductSearchReq;
  12 +import com.yohobuy.platform.model.grass.response.GoodsQueryRsp;
  13 +import org.slf4j.Logger;
  14 +import org.slf4j.LoggerFactory;
  15 +import org.springframework.beans.factory.annotation.Autowired;
  16 +import org.springframework.web.bind.annotation.*;
  17 +
  18 +import java.util.List;
  19 +
  20 +/**
  21 + * Created by shengguo.cai on 2019/1/21.
  22 + */
  23 +@RestController
  24 +@RequestMapping("/grassGoods")
  25 +public class GrassGoodsController {
  26 + private static final Logger logger = LoggerFactory.getLogger(GrassGoodsController.class);
  27 + @Autowired
  28 + private IGrassGoodsService grassGoodsService;
  29 +
  30 + @RequestMapping("/queryGoods")
  31 + @ResponseBody
  32 + public ApiResponse queryGoods(GoodsQueryReq req){
  33 + logger.info("enter queryGoods.param is {}",req);
  34 + PageResponseVO<GoodsQueryRsp> result = grassGoodsService.queryGoods(req);
  35 + return new ApiResponse.ApiResponseBuilder().data(result).build();
  36 + }
  37 +
  38 + @RequestMapping("/searchProducts")
  39 + @ResponseBody
  40 + public ApiResponse searchProducts(ProductSearchReq req){
  41 + logger.info("enter searchProducts.param is {}",req);
  42 + PageResponseVO<JSONObject> result = grassGoodsService.searchProducts(req);
  43 + return new ApiResponse.ApiResponseBuilder().data(result).build();
  44 + }
  45 +
  46 + @RequestMapping("/batchAddGoods")
  47 + @ResponseBody
  48 + public ApiResponse batchAddGoods(@RequestBody List<BatchAddGoodsReq> reqList){
  49 + logger.info("enter batchAddGoods.param is {}",reqList);
  50 + grassGoodsService.batchAddGoods(reqList);
  51 + return new ApiResponse.ApiResponseBuilder().message("批量新增成功").build();
  52 + }
  53 +
  54 + @RequestMapping("/getGoods")
  55 + @ResponseBody
  56 + public ApiResponse getGoods(@RequestParam("id") Integer id){
  57 + logger.info("enter getGoods.id is {}",id);
  58 + GrassGoods result = grassGoodsService.getGoods(id);
  59 + return new ApiResponse.ApiResponseBuilder().data(result).build();
  60 + }
  61 +
  62 + @RequestMapping("/modifyGoods")
  63 + @ResponseBody
  64 + public ApiResponse modifyGoods(GoodsModifyReq req){
  65 + logger.info("enter modifyGoods.param is {}",req);
  66 + grassGoodsService.modifyGoods(req);
  67 + return new ApiResponse.ApiResponseBuilder().message("修改成功").build();
  68 + }
  69 +}
  1 +package com.yohobuy.platform.grass.service;
  2 +
  3 +/**
  4 + * Created by shengguo.cai on 2019/1/21.
  5 + */
  6 +public interface IGrassArticleCommentService {
  7 +}
  1 +package com.yohobuy.platform.grass.service;
  2 +
  3 +/**
  4 + * Created by shengguo.cai on 2019/1/21.
  5 + */
  6 +public interface IGrassGoodsCommentService {
  7 +}
  1 +package com.yohobuy.platform.grass.service;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.yohobuy.platform.dal.grass.model.GrassGoods;
  5 +import com.yohobuy.platform.model.common.PageResponseVO;
  6 +import com.yohobuy.platform.model.grass.request.BatchAddGoodsReq;
  7 +import com.yohobuy.platform.model.grass.request.GoodsModifyReq;
  8 +import com.yohobuy.platform.model.grass.request.GoodsQueryReq;
  9 +import com.yohobuy.platform.model.grass.request.ProductSearchReq;
  10 +import com.yohobuy.platform.model.grass.response.GoodsQueryRsp;
  11 +
  12 +import java.util.List;
  13 +
  14 +/**
  15 + * Created by shengguo.cai on 2019/1/21.
  16 + */
  17 +public interface IGrassGoodsService {
  18 + PageResponseVO<GoodsQueryRsp> queryGoods(GoodsQueryReq req);
  19 +
  20 + PageResponseVO<JSONObject> searchProducts(ProductSearchReq req);
  21 +
  22 +
  23 + void batchAddGoods(List<BatchAddGoodsReq> reqList);
  24 +
  25 + GrassGoods getGoods(Integer id);
  26 +
  27 + void modifyGoods(GoodsModifyReq req);
  28 +}
  1 +package com.yohobuy.platform.grass.service.impl;
  2 +
  3 +import com.yohobuy.platform.grass.service.IGrassArticleCommentService;
  4 +import org.springframework.stereotype.Service;
  5 +
  6 +/**
  7 + * Created by shengguo.cai on 2019/1/21.
  8 + */
  9 +@Service
  10 +public class GrassArticleCommentServiceImpl implements IGrassArticleCommentService {
  11 +}
  1 +package com.yohobuy.platform.grass.service.impl;
  2 +
  3 +import com.yohobuy.platform.grass.service.IGrassGoodsCommentService;
  4 +import org.springframework.stereotype.Service;
  5 +
  6 +/**
  7 + * Created by shengguo.cai on 2019/1/21.
  8 + */
  9 +@Service
  10 +public class GrassGoodsCommentServiceImpl implements IGrassGoodsCommentService {
  11 +}
  1 +package com.yohobuy.platform.grass.service.impl;
  2 +
  3 +import com.alibaba.fastjson.JSONArray;
  4 +import com.alibaba.fastjson.JSONObject;
  5 +import com.yoho.core.common.utils.DateUtil;
  6 +import com.yoho.core.rest.client.ServiceCaller;
  7 +import com.yoho.error.exception.ServiceException;
  8 +import com.yohobuy.platform.dal.grass.IGrassGoodsCommentDao;
  9 +import com.yohobuy.platform.dal.grass.IGrassGoodsDao;
  10 +import com.yohobuy.platform.dal.grass.model.GrassGoods;
  11 +import com.yohobuy.platform.dal.product.ProductMapper;
  12 +import com.yohobuy.platform.dal.product.model.Product;
  13 +import com.yohobuy.platform.grass.service.IGrassGoodsService;
  14 +import com.yohobuy.platform.model.common.PageResponseVO;
  15 +import com.yohobuy.platform.model.grass.GoodsCommentTotalBo;
  16 +import com.yohobuy.platform.model.grass.request.BatchAddGoodsReq;
  17 +import com.yohobuy.platform.model.grass.request.GoodsModifyReq;
  18 +import com.yohobuy.platform.model.grass.request.GoodsQueryReq;
  19 +import com.yohobuy.platform.model.grass.request.ProductSearchReq;
  20 +import com.yohobuy.platform.model.grass.response.GoodsQueryRsp;
  21 +import org.apache.commons.collections.CollectionUtils;
  22 +import org.apache.commons.lang3.StringUtils;
  23 +import org.slf4j.Logger;
  24 +import org.slf4j.LoggerFactory;
  25 +import org.springframework.beans.factory.annotation.Autowired;
  26 +import org.springframework.beans.factory.annotation.Value;
  27 +import org.springframework.stereotype.Service;
  28 +
  29 +import java.util.HashMap;
  30 +import java.util.List;
  31 +import java.util.Map;
  32 +import java.util.stream.Collectors;
  33 +
  34 +/**
  35 + * Created by shengguo.cai on 2019/1/21.
  36 + */
  37 +@Service
  38 +public class GrassGoodsServiceImpl implements IGrassGoodsService{
  39 + private static final Logger logger = LoggerFactory.getLogger(GrassGoodsServiceImpl.class);
  40 + @Value("${search.server.address}")
  41 + private String searchServerAddress;
  42 + @Autowired
  43 + private ServiceCaller serviceCaller;
  44 + @Autowired
  45 + private IGrassGoodsDao grassGoodsDao;
  46 + @Autowired
  47 + private IGrassGoodsCommentDao grassGoodsCommentDao;
  48 + @Autowired
  49 + private ProductMapper productMapper;
  50 +
  51 + @Override
  52 + public PageResponseVO<GoodsQueryRsp> queryGoods(GoodsQueryReq req) {
  53 + if(req == null){
  54 + throw new ServiceException(400,"param can not be null!");
  55 + }
  56 + PageResponseVO<GoodsQueryRsp> result = new PageResponseVO<>();
  57 + result.setPage(req.getPage());
  58 + result.setSize(req.getSize());
  59 + int total = grassGoodsDao.selectTotalByGoodsQueryReq(req);
  60 + result.setTotal(total);
  61 + if(total == 0){
  62 + logger.info("queryGoods#selectTotalByGoodsQueryReq total is 0.param is {}",req);
  63 + return result;
  64 + }
  65 + List<GoodsQueryRsp> goodsBoList = grassGoodsDao.selectByGoodsQueryReq(req);
  66 + if(CollectionUtils.isEmpty(goodsBoList)){
  67 + logger.info("queryGoods#selectByGoodsQueryReq goodsBoList is empty.param is {}",req);
  68 + return result;
  69 + }
  70 + // 查询评论总数
  71 + List<Integer> goodsIds = goodsBoList.stream().map(goods->goods.getId()).collect(Collectors.toList());
  72 + Map<Integer,GoodsCommentTotalBo> commentMap = grassGoodsCommentDao.selectTotalMapByGoodsIds(goodsIds);
  73 + // 查询商品信息
  74 + String skns = goodsBoList.stream().filter(goods-> StringUtils.isNotBlank(goods.getProductSkn())).map(goods->goods.getProductSkn()).collect(Collectors.joining(","));
  75 + Map<String,JSONObject> productMap = buildProductMap(skns,1);
  76 + // 组装返回
  77 + buildGoodsQueryRspBo(goodsBoList,commentMap,productMap);
  78 + result.setList(goodsBoList);
  79 + return result;
  80 + }
  81 +
  82 + @Override
  83 + public PageResponseVO<JSONObject> searchProducts(ProductSearchReq req) {
  84 + try {
  85 + JSONObject responseObj = searchProduct(req.getSkns(),req.getPage());
  86 + if(responseObj == null || !responseObj.get("code").equals(200)){
  87 + throw new ServiceException(500,"search product failed!");
  88 + }
  89 + PageResponseVO<JSONObject> responseVO = new PageResponseVO<>();
  90 + responseVO.setPage(responseObj.getJSONObject("data").getInteger("page"));
  91 + responseVO.setSize(responseObj.getJSONObject("data").getInteger("page_size"));
  92 + responseVO.setTotal(responseObj.getJSONObject("data").getInteger("total"));
  93 + responseVO.setList((List) responseObj.getJSONObject("data").get("product_list"));
  94 + return responseVO;
  95 + } catch (Exception e) {
  96 + logger.warn("searchProducts exp is {}",e);
  97 + throw new ServiceException(500,"search product result deal failed!");
  98 + }
  99 + }
  100 +
  101 + @Override
  102 + public void batchAddGoods(List<BatchAddGoodsReq> reqList) {
  103 + if(CollectionUtils.isEmpty(reqList)){
  104 + throw new ServiceException(400,"param is empty!");
  105 + }
  106 + initAddGoodsContent(reqList);
  107 + int createTime = DateUtil.getCurrentTimeSecond();
  108 + grassGoodsDao.insertByBatchAddGoodsReq(reqList,createTime);
  109 + }
  110 +
  111 + @Override
  112 + public GrassGoods getGoods(Integer id) {
  113 + return grassGoodsDao.selectById(id);
  114 + }
  115 +
  116 + @Override
  117 + public void modifyGoods(GoodsModifyReq req) {
  118 + if(req==null||StringUtils.isBlank(req.getTitle())|| StringUtils.isBlank(req.getContent())
  119 + ||StringUtils.isBlank(req.getIcon())||req.getWeight()==null||req.getId() == null){
  120 + logger.warn("modifyGoods#some params are null!param is {}",req);
  121 + throw new ServiceException(400,"some params are null!");
  122 + }
  123 + req.setUpdateTime(DateUtil.getCurrentTimeSecond());
  124 + grassGoodsDao.updateByGoodsModifyReq(req);
  125 + }
  126 +
  127 + private void initAddGoodsContent(List<BatchAddGoodsReq> reqList) {
  128 + List<Integer> skns = reqList.stream().map(req->req.getSkn()).collect(Collectors.toList());
  129 + logger.info("initAddGoodsContent#before selectProductBySkns,skns is {}",skns);
  130 + List<Product> productList = productMapper.selectProductBySkns(skns);
  131 + if(CollectionUtils.isEmpty(productList)){
  132 + logger.info("initAddGoodsContent#selectProductBySkns result is empty.skns is {}",skns);
  133 + return;
  134 + }
  135 + Map<Integer,String> productContentMap = new HashMap<>();
  136 + productList.stream().forEach(product -> {productContentMap.put(product.getErpProductId(),product.getPhrase());});
  137 + reqList.stream().forEach(req->{
  138 + req.setContent(productContentMap.get(req.getSkn()));
  139 + });
  140 + }
  141 +
  142 + private void buildGoodsQueryRspBo(List<GoodsQueryRsp> goodsBoList, Map<Integer, GoodsCommentTotalBo> commentMap, Map<String, JSONObject> productMap) {
  143 + for(GoodsQueryRsp goods : goodsBoList){
  144 + GoodsCommentTotalBo comment = commentMap.get(goods.getId());
  145 + JSONObject productJSON = productMap==null?null:productMap.get(goods.getProductSkn());
  146 + goods.setCommentTotal(comment==null?0:comment.getTotal());
  147 + if(null != productJSON){
  148 + goods.setAvailTotal(productJSON.getInteger("storage_num"));
  149 + goods.setShelfStatus(productJSON.getInteger("status"));
  150 + }
  151 + }
  152 + }
  153 +
  154 + private Map<String,JSONObject> buildProductMap(String skns,int page){
  155 + logger.info("enter buildProductMap,skns is {},page is {}",skns,page);
  156 + if(StringUtils.isBlank(skns)){
  157 + return null;
  158 + }
  159 + try {
  160 + JSONObject responseObj = searchProduct(skns,page);
  161 + if(!responseObj.get("code").equals(200)){
  162 + return null;
  163 + }
  164 + JSONArray productList = responseObj.getJSONObject("data").getJSONArray("product_list");
  165 + if(CollectionUtils.isEmpty(productList)){
  166 + return null;
  167 + }
  168 + Map<String,JSONObject> productMap = new HashMap<>();
  169 + for(int i=0;i<productList.size();i++){
  170 + JSONObject jsonObject = productList.getJSONObject(i);
  171 + productMap.put(jsonObject.getString("product_skn"),jsonObject);
  172 + }
  173 + return productMap;
  174 + } catch (Exception e) {
  175 + logger.warn("buildProductMap failed!exp is {}",e);
  176 + }
  177 + return null;
  178 + }
  179 +
  180 + private JSONObject searchProduct(String skns,int page){
  181 + logger.info("enter searchProduct,skns is {},page is {}",skns,page);
  182 + String url = searchServerAddress + "search" ;
  183 + Map<String,Object> reqMap = new HashMap<>();
  184 + if(StringUtils.isNotBlank(skns)){
  185 + reqMap.put("query",skns);
  186 + }
  187 + reqMap.put("page",page);
  188 + try {
  189 + String responseStr=serviceCaller.get("yohosearch.search", url, reqMap, String.class, null).get();
  190 + logger.info("searchProduct#yohosearch.search result is {},param is {},url is {}",responseStr,reqMap,url);
  191 + JSONObject responseObj = JSONObject.parseObject(responseStr);
  192 + return responseObj;
  193 + } catch (Exception e) {
  194 + logger.warn("searchProduct failed!exp is {}",e);
  195 + }
  196 + return null;
  197 + }
  198 +}
@@ -342,7 +342,8 @@ datasources: @@ -342,7 +342,8 @@ datasources:
342 - com.yohobuy.platform.dal.grass.IGrassVirtualUserDao 342 - com.yohobuy.platform.dal.grass.IGrassVirtualUserDao
343 - com.yohobuy.platform.dal.grass.IGrassLabelDAO 343 - com.yohobuy.platform.dal.grass.IGrassLabelDAO
344 - com.yohobuy.platform.dal.grass.IGrassTopicDAO 344 - com.yohobuy.platform.dal.grass.IGrassTopicDAO
345 - 345 + - com.yohobuy.platform.dal.grass.IGrassGoodsDao
  346 + - com.yohobuy.platform.dal.grass.IGrassGoodsCommentDao
346 347
347 348
348 349
@@ -340,6 +340,8 @@ datasources: @@ -340,6 +340,8 @@ datasources:
340 - com.yohobuy.platform.dal.grass.IGrassArticleProductDao 340 - com.yohobuy.platform.dal.grass.IGrassArticleProductDao
341 - com.yohobuy.platform.dal.grass.IGrassVirtualUserDao 341 - com.yohobuy.platform.dal.grass.IGrassVirtualUserDao
342 - com.yohobuy.platform.dal.grass.IGrassTopicDAO 342 - com.yohobuy.platform.dal.grass.IGrassTopicDAO
  343 + - com.yohobuy.platform.dal.grass.IGrassGoodsDao
  344 + - com.yohobuy.platform.dal.grass.IGrassGoodsCommentDao
343 345
344 yhb_promotion: 346 yhb_promotion:
345 servers: 347 servers: