|
|
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
|
+} |