Authored by wangnan

Merge branch '5.6-global' of http://git.yoho.cn/yoho-search/yoho-search-consumer into 5.6-global

1 -package com.yoho.search.consumer.index.fullbuild;  
2 -  
3 -import com.yoho.search.consumer.index.common.IIndexBuilder;  
4 -import com.yoho.search.consumer.service.base.TblBrandService;  
5 -import com.yoho.search.consumer.service.bo.ShopsBO;  
6 -import com.yoho.search.consumer.service.logic.tbl.TblShopsLogicService;  
7 -import org.springframework.beans.factory.annotation.Autowired;  
8 -import org.springframework.stereotype.Component;  
9 -  
10 -import java.util.List;  
11 -  
12 -/**  
13 - * Created by wangnan on 2017/1/4.  
14 - */  
15 -@Component  
16 -public class TblShopsIndexBuilder extends IIndexBuilder {  
17 - @Autowired  
18 - private TblBrandService tblBrandService;  
19 - @Autowired  
20 - private TblShopsLogicService tblShopsLogicService;  
21 -  
22 - @Override  
23 - public int getTotalCount() throws Exception {  
24 - return tblBrandService.selectCount();  
25 - }  
26 -  
27 - @Override  
28 - public List<?> getPageLists(int offset, int limit) throws Exception {  
29 - return tblShopsLogicService.getShopsBOs(offset, limit);  
30 - }  
31 -  
32 - @Override  
33 - public String getId(Object object) {  
34 - return ((ShopsBO) object).getShopsId().toString();  
35 - }  
36 -}  
1 -package com.yoho.search.consumer.index.increment;  
2 -  
3 -import com.alibaba.fastjson.JSONObject;  
4 -import com.rabbitmq.client.Channel;  
5 -import com.yoho.error.event.SearchEvent;  
6 -import com.yoho.search.base.utils.ConvertUtils;  
7 -import com.yoho.search.base.utils.EventReportEnum;  
8 -import com.yoho.search.base.utils.ISearchConstants;  
9 -import com.yoho.search.consumer.index.common.IYohoIndexService;  
10 -import com.yoho.search.consumer.service.base.ShopsBrandsService;  
11 -import com.yoho.search.consumer.service.bo.ShopsBO;  
12 -import com.yoho.search.consumer.service.logic.ShopsLogicService;  
13 -import com.yoho.search.core.es.model.ESBluk;  
14 -import com.yoho.search.core.es.utils.IgnoreSomeException;  
15 -import com.yoho.search.dal.model.ShopsBrands;  
16 -import org.apache.commons.lang.StringUtils;  
17 -import org.slf4j.Logger;  
18 -import org.slf4j.LoggerFactory;  
19 -import org.springframework.amqp.core.Message;  
20 -import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;  
21 -import org.springframework.beans.factory.annotation.Autowired;  
22 -import org.springframework.stereotype.Component;  
23 -import org.springframework.util.CollectionUtils;  
24 -  
25 -import java.util.ArrayList;  
26 -import java.util.List;  
27 -import java.util.Map;  
28 -  
29 -/**  
30 - * Created by wangnan on 2016/12/1.  
31 - */  
32 -@Component  
33 -public class ShopsBrandsMqListener extends AbstractMqListener implements ChannelAwareMessageListener {  
34 - private static final Logger logger = LoggerFactory.getLogger(ShopsBrandsMqListener.class);  
35 - @Autowired  
36 - private IYohoIndexService indexService;  
37 - @Autowired  
38 - private ShopsBrandsService shopsBrandsService;  
39 - @Autowired  
40 - private ShopsLogicService shopsLogicService;  
41 -  
42 - @Override  
43 - public void onMessage(Message message, Channel channel) throws Exception {  
44 - try {  
45 - String messageStr = new String(message.getBody(), "UTF-8");  
46 - logger.info("[model=ShopsBrandsMqListener][message={}]", messageStr);  
47 - // 如果在重建索引等待  
48 - this.waitingRebuildingIndex();  
49 - JSONObject json = JSONObject.parseObject(messageStr);  
50 - String indexName = ISearchConstants.INDEX_NAME_SHOPS;  
51 - String idField = ISearchConstants.getKeyField(indexName);  
52 - if (ISearchConstants.ACTION_DELETE.equals(json.getString("action"))) {  
53 - deleteData(json.getString("data"), indexName, idField);  
54 - } else if (ISearchConstants.ACTION_UPDATE.equals(json.getString("action"))) {  
55 - updateData(json.getObject("data", Map.class), indexName, idField);  
56 - } else {  
57 - updateData(json.getObject("data", Map.class), indexName, idField);  
58 - }  
59 - } catch (Exception e) {  
60 - publisher.publishEvent(new SearchEvent(EventReportEnum.SHOPSBRADNSMQLISTENER_ONMESSAGE.getEventName(),  
61 - EventReportEnum.SHOPSBRADNSMQLISTENER_ONMESSAGE.getFunctionName(),  
62 - EventReportEnum.SHOPSBRADNSMQLISTENER_ONMESSAGE.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));  
63 - Thread.sleep(1000);  
64 - throw e;  
65 - }  
66 - }  
67 -  
68 - @SuppressWarnings({"rawtypes", "unchecked"})  
69 - public void updateData(final Map data, final String indexName, final String idField) throws Exception {  
70 - long begin = System.currentTimeMillis();  
71 - ShopsBrands shopsBrands = new ShopsBrands();  
72 - shopsBrands = (ShopsBrands) ConvertUtils.toJavaBean(shopsBrands, data);  
73 - if (shopsBrands == null || shopsBrands.getShopsId() == null) {  
74 - return;  
75 - }  
76 - String idValue = shopsBrands.getId().toString();  
77 - shopsBrandsService.saveOrUpdate(shopsBrands);  
78 - logger.info("[func=updateData][step=saveToDb][indexName={}] [id={}][cost={}ms]", indexName, idValue, (System.currentTimeMillis() - begin));  
79 - List<Integer> ids = new ArrayList<>();  
80 - ids.add(shopsBrands.getShopsId());  
81 - List<ShopsBO> shopBOs = shopsLogicService.getShopsBOs(ids);  
82 - if (!CollectionUtils.isEmpty(shopBOs)) {  
83 - ShopsBO shopsBO = shopBOs.get(0);  
84 - List<ESBluk> results = new ArrayList<ESBluk>();  
85 - results.add(new ESBluk(JSONObject.toJSONString(this.beanToMap(shopsBO)), shopsBO.getShopsId().toString(), indexName, indexName, false));  
86 - indexService.bulk(results);  
87 - }  
88 - logger.info("[func=updateData][step=success][indexName={}] [id={}][cost={}ms]", indexName, idValue, (System.currentTimeMillis() - begin));  
89 - }  
90 -  
91 - public void deleteData(final String id, final String indexName, final String idField) throws Exception {  
92 - if (StringUtils.isBlank(id)) {  
93 - return;  
94 - }  
95 - long begin = System.currentTimeMillis();  
96 - ShopsBrands shopsBrands = shopsBrandsService.getById(Integer.valueOf(id));  
97 - shopsBrandsService.delete(Integer.valueOf(id));  
98 - logger.info("[func=deleteData][step=deleteFromDb][indexName={}] [id={}][cost={}ms]", indexName, id, (System.currentTimeMillis() - begin));  
99 - List<Integer> ids = new ArrayList<>();  
100 - ids.add(shopsBrands.getShopsId());  
101 - List<ShopsBO> shopBOs = shopsLogicService.getShopsBOs(ids);  
102 - if (!CollectionUtils.isEmpty(shopBOs)) {  
103 - ShopsBO shopsBO = shopBOs.get(0);  
104 - List<ESBluk> results = new ArrayList<ESBluk>();  
105 - results.add(new ESBluk(JSONObject.toJSONString(this.beanToMap(shopsBO)), shopsBO.getShopsId().toString(), indexName, indexName, false));  
106 - indexService.bulk(results);  
107 - }  
108 - logger.info("[func=deleteData][step=success][indexName={}][id={}][cost={}ms]", indexName, id, (System.currentTimeMillis() - begin));  
109 - }  
110 -  
111 - private Map<String, Object> beanToMap(ShopsBO shopsBO) {  
112 - JSONObject josnoJsonObject = (JSONObject) JSONObject.toJSON(shopsBO);  
113 - return josnoJsonObject;  
114 - }  
115 -}  
1 -package com.yoho.search.consumer.service.bo;  
2 -  
3 -import com.yoho.search.dal.model.Shops;  
4 -  
5 -/**  
6 - * Created by wangnan on 2016/12/.  
7 - */  
8 -public class ShopsBO extends Shops {  
9 -  
10 - private String brandName;  
11 -  
12 - private String brandDomain;  
13 -  
14 - private String brandNameCn;  
15 -  
16 - private String brandNameEn;  
17 -  
18 - private String isGlobal = "N";  
19 -  
20 - public String getBrandName() {  
21 - return brandName;  
22 - }  
23 -  
24 - public void setBrandName(String brandName) {  
25 - this.brandName = brandName == null ? null : brandName.trim();  
26 - }  
27 -  
28 - public String getBrandDomain() {  
29 - return brandDomain;  
30 - }  
31 -  
32 - public void setBrandDomain(String brandDomain) {  
33 - this.brandDomain = brandDomain == null ? null : brandDomain.trim();  
34 - }  
35 -  
36 - public String getBrandNameCn() {  
37 - return brandNameCn;  
38 - }  
39 -  
40 - public void setBrandNameCn(String brandNameCn) {  
41 - this.brandNameCn = brandNameCn;  
42 - }  
43 -  
44 - public String getBrandNameEn() {  
45 - return brandNameEn;  
46 - }  
47 -  
48 - public void setBrandNameEn(String brandNameEn) {  
49 - this.brandNameEn = brandNameEn;  
50 - }  
51 -  
52 - public String getIsGlobal() {  
53 - return isGlobal;  
54 - }  
55 -  
56 - public void setIsGlobal(String isGlobal) {  
57 - this.isGlobal = isGlobal;  
58 - }  
59 -}  
1 -package com.yoho.search.consumer.service.logic;  
2 -  
3 -import java.util.ArrayList;  
4 -import java.util.HashMap;  
5 -import java.util.List;  
6 -import java.util.Map;  
7 -import java.util.stream.Collectors;  
8 -  
9 -import org.apache.commons.collections.CollectionUtils;  
10 -import org.slf4j.Logger;  
11 -import org.slf4j.LoggerFactory;  
12 -import org.springframework.beans.factory.annotation.Autowired;  
13 -import org.springframework.stereotype.Component;  
14 -  
15 -import com.yoho.search.consumer.service.bo.ShopsBO;  
16 -import com.yoho.search.dal.BrandMapper;  
17 -import com.yoho.search.dal.ShopMapper;  
18 -import com.yoho.search.dal.ShopsBrandsMapper;  
19 -import com.yoho.search.dal.model.Brand;  
20 -import com.yoho.search.dal.model.Shops;  
21 -import com.yoho.search.dal.model.ShopsBrands;  
22 -  
23 -/**  
24 - * Created by wangnan on 2016/12/1. 有货店铺索引  
25 - */  
26 -@Component  
27 -public class ShopsLogicService {  
28 - private final Logger logger = LoggerFactory.getLogger(ShopsLogicService.class);  
29 -  
30 - @Autowired  
31 - private ShopMapper shopMapper;  
32 - @Autowired  
33 - private ShopsBrandsMapper shopsBrandsMapper;  
34 - @Autowired  
35 - private BrandMapper brandMapper;  
36 -  
37 - /**  
38 - * 增量  
39 - */  
40 - public List<ShopsBO> getShopsBOs(List<Integer> ids) {  
41 - long begin = System.currentTimeMillis();  
42 - List<Shops> shops = shopMapper.selectByIds(ids);  
43 - List<ShopsBO> shopsBOs = buildShopsBOs(shops);  
44 - logger.info("[class=ShopsLogicService][function=getShopsBOs],[size:{}],[cost: {}]", shopsBOs.size(), System.currentTimeMillis() - begin);  
45 - return shopsBOs;  
46 - }  
47 -  
48 - /**  
49 - * 全量  
50 - */  
51 - public List<ShopsBO> getShopsBOs(int start, int limit) {  
52 - long begin = System.currentTimeMillis();  
53 - List<Shops> shops = shopMapper.getPageLists(start, limit);  
54 - List<ShopsBO> shopsBOs = buildShopsBOs(shops);  
55 - logger.info("[class=ShopsLogicService][function=getShopsBOs],[size:{}],[cost: {}]", shopsBOs.size(), System.currentTimeMillis() - begin);  
56 - return shopsBOs;  
57 - }  
58 -  
59 - /**  
60 - * 生成BO  
61 - */  
62 - private List<ShopsBO> buildShopsBOs(List<Shops> shops) {  
63 - List<ShopsBO> shopsBOs = new ArrayList<>();  
64 - if (shops == null || shops.isEmpty()) {  
65 - return new ArrayList<ShopsBO>();  
66 - }  
67 - List<Brand> brands = brandMapper.getAll();  
68 - Map<Integer, Brand> brandMap = brands.stream().collect(Collectors.toMap(Brand::getId, (p) -> p));  
69 - Map<Integer, List<ShopsBrands>> shopsBrandsMap = getShopsBrandsMap(shops);  
70 - for (Shops shop : shops) {  
71 - List<ShopsBrands> sbs = shopsBrandsMap.get(shop.getShopsId());  
72 - ShopsBO shopsBO = new ShopsBO();  
73 - buildBrandName(sbs, brandMap, shopsBO);  
74 - shopsBO.setShopsId(shop.getShopsId());  
75 - shopsBO.setShopName(shop.getShopName());  
76 - shopsBO.setShopDomain(shop.getShopDomain());  
77 - shopsBO.setShopNature(shop.getShopNature());  
78 - shopsBO.setShopLogo(shop.getShopLogo());  
79 - shopsBO.setWebsiteUrl(shop.getWebsiteUrl());  
80 - shopsBO.setOtherUrl(shop.getOtherUrl());  
81 - shopsBO.setShopAddress(shop.getShopAddress());  
82 - shopsBO.setCreatePid(shop.getCreatePid());  
83 - shopsBO.setShopsType(shop.getShopsType());  
84 - shopsBO.setOperationStatus(shop.getOperationStatus());  
85 - shopsBO.setExamineStatus(shop.getExamineStatus());  
86 - shopsBO.setStatus(shop.getStatus());  
87 - shopsBO.setCheckStatus(shop.getCheckStatus());  
88 - shopsBO.setCreateTime(shop.getCreateTime());  
89 - shopsBO.setUpdateTime(shop.getUpdateTime());  
90 - shopsBO.setBlkStatus(shop.getBlkStatus());  
91 - shopsBO.setDecoratorTemplateType(shop.getDecoratorTemplateType());  
92 - shopsBOs.add(shopsBO);  
93 - }  
94 - return shopsBOs;  
95 - }  
96 -  
97 - /**  
98 - * 处理品牌名  
99 - */  
100 - private void buildBrandName(List<ShopsBrands> sbs, Map<Integer, Brand> brandMap, ShopsBO shopsBO) {  
101 - if (!CollectionUtils.isEmpty(sbs)) {  
102 - StringBuilder brandName = new StringBuilder();  
103 - StringBuilder brandNameCn = new StringBuilder();  
104 - StringBuilder brandNameEn = new StringBuilder();  
105 - StringBuilder brandDomain = new StringBuilder();  
106 - for (ShopsBrands shopsBrand : sbs) {  
107 - Brand brand = brandMap.get(shopsBrand.getBrandId());  
108 - if (brand != null) {  
109 - brandName.append(brand.getBrandName() + ",");  
110 - brandNameCn.append(brand.getBrandNameCn() + ",");  
111 - brandNameEn.append(brand.getBrandNameEn() + ",");  
112 - brandDomain.append(brand.getBrandDomain() + ",");  
113 - }  
114 - }  
115 - shopsBO.setBrandName(brandName.toString());  
116 - shopsBO.setBrandNameCn(brandNameCn.toString());  
117 - shopsBO.setBrandNameEn(brandNameEn.toString());  
118 - shopsBO.setBrandDomain(brandDomain.toString());  
119 - }  
120 - }  
121 -  
122 - /**  
123 - * 构建ShopsBrandsMap  
124 - */  
125 - private Map<Integer, List<ShopsBrands>> getShopsBrandsMap(List<Shops> shops) {  
126 - List<Integer> shopsIds = shops.stream().parallel().map(Shops::getShopsId).collect(Collectors.toList());  
127 - List<ShopsBrands> shopsBrandsList = shopsBrandsMapper.selectByShopIds(shopsIds);  
128 - Map<Integer, List<ShopsBrands>> shopsBrandsMap = new HashMap<>();  
129 - for (ShopsBrands shopsBrands : shopsBrandsList) {  
130 - if (shopsBrandsMap.containsKey(shopsBrands.getShopsId())) {  
131 - List<ShopsBrands> sbs = shopsBrandsMap.get(shopsBrands.getShopsId());  
132 - sbs.add(shopsBrands);  
133 - } else {  
134 - List<ShopsBrands> sbs = new ArrayList<>();  
135 - sbs.add(shopsBrands);  
136 - shopsBrandsMap.put(shopsBrands.getShopsId(), sbs);  
137 - }  
138 - }  
139 - return shopsBrandsMap;  
140 - }  
141 -}  
1 -package com.yoho.search.consumer.service.logic.tbl;  
2 -  
3 -import com.yoho.search.consumer.service.base.TblBrandService;  
4 -import com.yoho.search.consumer.service.bo.ShopsBO;  
5 -import com.yoho.search.dal.model.TblBrand;  
6 -import org.springframework.beans.factory.annotation.Autowired;  
7 -import org.springframework.stereotype.Component;  
8 -  
9 -import java.util.ArrayList;  
10 -import java.util.List;  
11 -  
12 -/**  
13 - * Created by wangnan on 2017/1/4.  
14 - * 将全球购的店铺加入到shops索引中,使其可以在有货中被搜索出来  
15 - */  
16 -@Component  
17 -public class TblShopsLogicService {  
18 - @Autowired  
19 - private TblImgUrlLogicService tblImgUrlLogicService;  
20 - @Autowired  
21 - private TblBrandService tblBrandService;  
22 -  
23 - /**  
24 - * 增量  
25 - */  
26 - public List<ShopsBO> getShopsBOs(List<Integer> ids) {  
27 - List<TblBrand> tblBrands = tblBrandService.selectByIds(ids);  
28 - return generateShopsBOs(tblBrands);  
29 - }  
30 -  
31 - /**  
32 - * 全量  
33 - */  
34 - public List<ShopsBO> getShopsBOs(int start, int limit) {  
35 - List<TblBrand> tblBrands = tblBrandService.selectBrandPageList(start, limit);  
36 - return generateShopsBOs(tblBrands);  
37 - }  
38 -  
39 -  
40 - /**  
41 - * 生成BO  
42 - */  
43 - private List<ShopsBO> generateShopsBOs(List<TblBrand> tblBrands) {  
44 - List<ShopsBO> shopsBOs = new ArrayList<>();  
45 - for (TblBrand tblBrand : tblBrands) {  
46 - ShopsBO shopsBO = new ShopsBO();  
47 - shopsBO.setBrandName(tblBrand.getBrandNameEn());  
48 - shopsBO.setBrandNameCn(tblBrand.getBrandNameCn());  
49 - shopsBO.setBrandNameEn(tblBrand.getBrandNameEn());  
50 - shopsBO.setBrandDomain("");  
51 - shopsBO.setShopsId(0);  
52 - shopsBO.setShopName(tblBrand.getBrandNameEn());  
53 - shopsBO.setShopDomain("");  
54 - shopsBO.setShopNature(0);  
55 - if (tblBrand.getLogo() != null) {  
56 - shopsBO.setShopLogo(tblImgUrlLogicService.getTblImageUrl(tblBrand.getLogo(), true));  
57 - }  
58 - shopsBO.setWebsiteUrl("");  
59 - shopsBO.setOtherUrl("");  
60 - shopsBO.setShopAddress("");  
61 - shopsBO.setCreatePid(0);  
62 - shopsBO.setShopsType(1);  
63 - shopsBO.setOperationStatus(2);  
64 - shopsBO.setExamineStatus(1);  
65 - if (tblBrand.getStatus().equals("1")) {  
66 - shopsBO.setStatus(0);  
67 - }  
68 - if (tblBrand.getStatus().equals("2")) {  
69 - shopsBO.setStatus(1);  
70 - }  
71 - shopsBO.setCheckStatus(0);  
72 - shopsBO.setCreateTime(tblBrand.getCreateTime());  
73 - shopsBO.setUpdateTime(tblBrand.getUpdateTime());  
74 - shopsBO.setBlkStatus(0);  
75 - shopsBO.setIsGlobal("Y");  
76 - shopsBOs.add(shopsBO);  
77 - }  
78 - return shopsBOs;  
79 - }  
80 -}  
@@ -128,7 +128,6 @@ @@ -128,7 +128,6 @@
128 <rabbit:listener queue-names="data_update_channel_productpriceplan" ref="productPricePlanMqListener" /> 128 <rabbit:listener queue-names="data_update_channel_productpriceplan" ref="productPricePlanMqListener" />
129 <rabbit:listener queue-names="data_update_channel_producttiming" ref="productTimingMqListener" /> 129 <rabbit:listener queue-names="data_update_channel_producttiming" ref="productTimingMqListener" />
130 <rabbit:listener queue-names="data_update_channel_shops" ref="shopsMqListener" /> 130 <rabbit:listener queue-names="data_update_channel_shops" ref="shopsMqListener" />
131 - <rabbit:listener queue-names="data_update_channel_shopsbrands" ref="shopsBrandsMqListener" />  
132 <rabbit:listener queue-names="data_update_channel_salescategory" ref="salesCategoryMqListener" /> 131 <rabbit:listener queue-names="data_update_channel_salescategory" ref="salesCategoryMqListener" />
133 132
134 <rabbit:listener queue-names="data_update_channel_product" ref="productMqListener" /> 133 <rabbit:listener queue-names="data_update_channel_product" ref="productMqListener" />