Authored by wangnan

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

package com.yoho.search.consumer.index.fullbuild;
import com.yoho.search.consumer.index.common.IIndexBuilder;
import com.yoho.search.consumer.service.base.TblBrandService;
import com.yoho.search.consumer.service.bo.ShopsBO;
import com.yoho.search.consumer.service.logic.tbl.TblShopsLogicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Created by wangnan on 2017/1/4.
*/
@Component
public class TblShopsIndexBuilder extends IIndexBuilder {
@Autowired
private TblBrandService tblBrandService;
@Autowired
private TblShopsLogicService tblShopsLogicService;
@Override
public int getTotalCount() throws Exception {
return tblBrandService.selectCount();
}
@Override
public List<?> getPageLists(int offset, int limit) throws Exception {
return tblShopsLogicService.getShopsBOs(offset, limit);
}
@Override
public String getId(Object object) {
return ((ShopsBO) object).getShopsId().toString();
}
}
package com.yoho.search.consumer.index.increment;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.index.common.IYohoIndexService;
import com.yoho.search.consumer.service.base.ShopsBrandsService;
import com.yoho.search.consumer.service.bo.ShopsBO;
import com.yoho.search.consumer.service.logic.ShopsLogicService;
import com.yoho.search.core.es.model.ESBluk;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import com.yoho.search.dal.model.ShopsBrands;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by wangnan on 2016/12/1.
*/
@Component
public class ShopsBrandsMqListener extends AbstractMqListener implements ChannelAwareMessageListener {
private static final Logger logger = LoggerFactory.getLogger(ShopsBrandsMqListener.class);
@Autowired
private IYohoIndexService indexService;
@Autowired
private ShopsBrandsService shopsBrandsService;
@Autowired
private ShopsLogicService shopsLogicService;
@Override
public void onMessage(Message message, Channel channel) throws Exception {
try {
String messageStr = new String(message.getBody(), "UTF-8");
logger.info("[model=ShopsBrandsMqListener][message={}]", messageStr);
// 如果在重建索引等待
this.waitingRebuildingIndex();
JSONObject json = JSONObject.parseObject(messageStr);
String indexName = ISearchConstants.INDEX_NAME_SHOPS;
String idField = ISearchConstants.getKeyField(indexName);
if (ISearchConstants.ACTION_DELETE.equals(json.getString("action"))) {
deleteData(json.getString("data"), indexName, idField);
} else if (ISearchConstants.ACTION_UPDATE.equals(json.getString("action"))) {
updateData(json.getObject("data", Map.class), indexName, idField);
} else {
updateData(json.getObject("data", Map.class), indexName, idField);
}
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SHOPSBRADNSMQLISTENER_ONMESSAGE.getEventName(),
EventReportEnum.SHOPSBRADNSMQLISTENER_ONMESSAGE.getFunctionName(),
EventReportEnum.SHOPSBRADNSMQLISTENER_ONMESSAGE.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
Thread.sleep(1000);
throw e;
}
}
@SuppressWarnings({"rawtypes", "unchecked"})
public void updateData(final Map data, final String indexName, final String idField) throws Exception {
long begin = System.currentTimeMillis();
ShopsBrands shopsBrands = new ShopsBrands();
shopsBrands = (ShopsBrands) ConvertUtils.toJavaBean(shopsBrands, data);
if (shopsBrands == null || shopsBrands.getShopsId() == null) {
return;
}
String idValue = shopsBrands.getId().toString();
shopsBrandsService.saveOrUpdate(shopsBrands);
logger.info("[func=updateData][step=saveToDb][indexName={}] [id={}][cost={}ms]", indexName, idValue, (System.currentTimeMillis() - begin));
List<Integer> ids = new ArrayList<>();
ids.add(shopsBrands.getShopsId());
List<ShopsBO> shopBOs = shopsLogicService.getShopsBOs(ids);
if (!CollectionUtils.isEmpty(shopBOs)) {
ShopsBO shopsBO = shopBOs.get(0);
List<ESBluk> results = new ArrayList<ESBluk>();
results.add(new ESBluk(JSONObject.toJSONString(this.beanToMap(shopsBO)), shopsBO.getShopsId().toString(), indexName, indexName, false));
indexService.bulk(results);
}
logger.info("[func=updateData][step=success][indexName={}] [id={}][cost={}ms]", indexName, idValue, (System.currentTimeMillis() - begin));
}
public void deleteData(final String id, final String indexName, final String idField) throws Exception {
if (StringUtils.isBlank(id)) {
return;
}
long begin = System.currentTimeMillis();
ShopsBrands shopsBrands = shopsBrandsService.getById(Integer.valueOf(id));
shopsBrandsService.delete(Integer.valueOf(id));
logger.info("[func=deleteData][step=deleteFromDb][indexName={}] [id={}][cost={}ms]", indexName, id, (System.currentTimeMillis() - begin));
List<Integer> ids = new ArrayList<>();
ids.add(shopsBrands.getShopsId());
List<ShopsBO> shopBOs = shopsLogicService.getShopsBOs(ids);
if (!CollectionUtils.isEmpty(shopBOs)) {
ShopsBO shopsBO = shopBOs.get(0);
List<ESBluk> results = new ArrayList<ESBluk>();
results.add(new ESBluk(JSONObject.toJSONString(this.beanToMap(shopsBO)), shopsBO.getShopsId().toString(), indexName, indexName, false));
indexService.bulk(results);
}
logger.info("[func=deleteData][step=success][indexName={}][id={}][cost={}ms]", indexName, id, (System.currentTimeMillis() - begin));
}
private Map<String, Object> beanToMap(ShopsBO shopsBO) {
JSONObject josnoJsonObject = (JSONObject) JSONObject.toJSON(shopsBO);
return josnoJsonObject;
}
}
package com.yoho.search.consumer.service.bo;
import com.yoho.search.dal.model.Shops;
/**
* Created by wangnan on 2016/12/.
*/
public class ShopsBO extends Shops {
private String brandName;
private String brandDomain;
private String brandNameCn;
private String brandNameEn;
private String isGlobal = "N";
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName == null ? null : brandName.trim();
}
public String getBrandDomain() {
return brandDomain;
}
public void setBrandDomain(String brandDomain) {
this.brandDomain = brandDomain == null ? null : brandDomain.trim();
}
public String getBrandNameCn() {
return brandNameCn;
}
public void setBrandNameCn(String brandNameCn) {
this.brandNameCn = brandNameCn;
}
public String getBrandNameEn() {
return brandNameEn;
}
public void setBrandNameEn(String brandNameEn) {
this.brandNameEn = brandNameEn;
}
public String getIsGlobal() {
return isGlobal;
}
public void setIsGlobal(String isGlobal) {
this.isGlobal = isGlobal;
}
}
package com.yoho.search.consumer.service.logic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yoho.search.consumer.service.bo.ShopsBO;
import com.yoho.search.dal.BrandMapper;
import com.yoho.search.dal.ShopMapper;
import com.yoho.search.dal.ShopsBrandsMapper;
import com.yoho.search.dal.model.Brand;
import com.yoho.search.dal.model.Shops;
import com.yoho.search.dal.model.ShopsBrands;
/**
* Created by wangnan on 2016/12/1. 有货店铺索引
*/
@Component
public class ShopsLogicService {
private final Logger logger = LoggerFactory.getLogger(ShopsLogicService.class);
@Autowired
private ShopMapper shopMapper;
@Autowired
private ShopsBrandsMapper shopsBrandsMapper;
@Autowired
private BrandMapper brandMapper;
/**
* 增量
*/
public List<ShopsBO> getShopsBOs(List<Integer> ids) {
long begin = System.currentTimeMillis();
List<Shops> shops = shopMapper.selectByIds(ids);
List<ShopsBO> shopsBOs = buildShopsBOs(shops);
logger.info("[class=ShopsLogicService][function=getShopsBOs],[size:{}],[cost: {}]", shopsBOs.size(), System.currentTimeMillis() - begin);
return shopsBOs;
}
/**
* 全量
*/
public List<ShopsBO> getShopsBOs(int start, int limit) {
long begin = System.currentTimeMillis();
List<Shops> shops = shopMapper.getPageLists(start, limit);
List<ShopsBO> shopsBOs = buildShopsBOs(shops);
logger.info("[class=ShopsLogicService][function=getShopsBOs],[size:{}],[cost: {}]", shopsBOs.size(), System.currentTimeMillis() - begin);
return shopsBOs;
}
/**
* 生成BO
*/
private List<ShopsBO> buildShopsBOs(List<Shops> shops) {
List<ShopsBO> shopsBOs = new ArrayList<>();
if (shops == null || shops.isEmpty()) {
return new ArrayList<ShopsBO>();
}
List<Brand> brands = brandMapper.getAll();
Map<Integer, Brand> brandMap = brands.stream().collect(Collectors.toMap(Brand::getId, (p) -> p));
Map<Integer, List<ShopsBrands>> shopsBrandsMap = getShopsBrandsMap(shops);
for (Shops shop : shops) {
List<ShopsBrands> sbs = shopsBrandsMap.get(shop.getShopsId());
ShopsBO shopsBO = new ShopsBO();
buildBrandName(sbs, brandMap, shopsBO);
shopsBO.setShopsId(shop.getShopsId());
shopsBO.setShopName(shop.getShopName());
shopsBO.setShopDomain(shop.getShopDomain());
shopsBO.setShopNature(shop.getShopNature());
shopsBO.setShopLogo(shop.getShopLogo());
shopsBO.setWebsiteUrl(shop.getWebsiteUrl());
shopsBO.setOtherUrl(shop.getOtherUrl());
shopsBO.setShopAddress(shop.getShopAddress());
shopsBO.setCreatePid(shop.getCreatePid());
shopsBO.setShopsType(shop.getShopsType());
shopsBO.setOperationStatus(shop.getOperationStatus());
shopsBO.setExamineStatus(shop.getExamineStatus());
shopsBO.setStatus(shop.getStatus());
shopsBO.setCheckStatus(shop.getCheckStatus());
shopsBO.setCreateTime(shop.getCreateTime());
shopsBO.setUpdateTime(shop.getUpdateTime());
shopsBO.setBlkStatus(shop.getBlkStatus());
shopsBO.setDecoratorTemplateType(shop.getDecoratorTemplateType());
shopsBOs.add(shopsBO);
}
return shopsBOs;
}
/**
* 处理品牌名
*/
private void buildBrandName(List<ShopsBrands> sbs, Map<Integer, Brand> brandMap, ShopsBO shopsBO) {
if (!CollectionUtils.isEmpty(sbs)) {
StringBuilder brandName = new StringBuilder();
StringBuilder brandNameCn = new StringBuilder();
StringBuilder brandNameEn = new StringBuilder();
StringBuilder brandDomain = new StringBuilder();
for (ShopsBrands shopsBrand : sbs) {
Brand brand = brandMap.get(shopsBrand.getBrandId());
if (brand != null) {
brandName.append(brand.getBrandName() + ",");
brandNameCn.append(brand.getBrandNameCn() + ",");
brandNameEn.append(brand.getBrandNameEn() + ",");
brandDomain.append(brand.getBrandDomain() + ",");
}
}
shopsBO.setBrandName(brandName.toString());
shopsBO.setBrandNameCn(brandNameCn.toString());
shopsBO.setBrandNameEn(brandNameEn.toString());
shopsBO.setBrandDomain(brandDomain.toString());
}
}
/**
* 构建ShopsBrandsMap
*/
private Map<Integer, List<ShopsBrands>> getShopsBrandsMap(List<Shops> shops) {
List<Integer> shopsIds = shops.stream().parallel().map(Shops::getShopsId).collect(Collectors.toList());
List<ShopsBrands> shopsBrandsList = shopsBrandsMapper.selectByShopIds(shopsIds);
Map<Integer, List<ShopsBrands>> shopsBrandsMap = new HashMap<>();
for (ShopsBrands shopsBrands : shopsBrandsList) {
if (shopsBrandsMap.containsKey(shopsBrands.getShopsId())) {
List<ShopsBrands> sbs = shopsBrandsMap.get(shopsBrands.getShopsId());
sbs.add(shopsBrands);
} else {
List<ShopsBrands> sbs = new ArrayList<>();
sbs.add(shopsBrands);
shopsBrandsMap.put(shopsBrands.getShopsId(), sbs);
}
}
return shopsBrandsMap;
}
}
package com.yoho.search.consumer.service.logic.tbl;
import com.yoho.search.consumer.service.base.TblBrandService;
import com.yoho.search.consumer.service.bo.ShopsBO;
import com.yoho.search.dal.model.TblBrand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* Created by wangnan on 2017/1/4.
* 将全球购的店铺加入到shops索引中,使其可以在有货中被搜索出来
*/
@Component
public class TblShopsLogicService {
@Autowired
private TblImgUrlLogicService tblImgUrlLogicService;
@Autowired
private TblBrandService tblBrandService;
/**
* 增量
*/
public List<ShopsBO> getShopsBOs(List<Integer> ids) {
List<TblBrand> tblBrands = tblBrandService.selectByIds(ids);
return generateShopsBOs(tblBrands);
}
/**
* 全量
*/
public List<ShopsBO> getShopsBOs(int start, int limit) {
List<TblBrand> tblBrands = tblBrandService.selectBrandPageList(start, limit);
return generateShopsBOs(tblBrands);
}
/**
* 生成BO
*/
private List<ShopsBO> generateShopsBOs(List<TblBrand> tblBrands) {
List<ShopsBO> shopsBOs = new ArrayList<>();
for (TblBrand tblBrand : tblBrands) {
ShopsBO shopsBO = new ShopsBO();
shopsBO.setBrandName(tblBrand.getBrandNameEn());
shopsBO.setBrandNameCn(tblBrand.getBrandNameCn());
shopsBO.setBrandNameEn(tblBrand.getBrandNameEn());
shopsBO.setBrandDomain("");
shopsBO.setShopsId(0);
shopsBO.setShopName(tblBrand.getBrandNameEn());
shopsBO.setShopDomain("");
shopsBO.setShopNature(0);
if (tblBrand.getLogo() != null) {
shopsBO.setShopLogo(tblImgUrlLogicService.getTblImageUrl(tblBrand.getLogo(), true));
}
shopsBO.setWebsiteUrl("");
shopsBO.setOtherUrl("");
shopsBO.setShopAddress("");
shopsBO.setCreatePid(0);
shopsBO.setShopsType(1);
shopsBO.setOperationStatus(2);
shopsBO.setExamineStatus(1);
if (tblBrand.getStatus().equals("1")) {
shopsBO.setStatus(0);
}
if (tblBrand.getStatus().equals("2")) {
shopsBO.setStatus(1);
}
shopsBO.setCheckStatus(0);
shopsBO.setCreateTime(tblBrand.getCreateTime());
shopsBO.setUpdateTime(tblBrand.getUpdateTime());
shopsBO.setBlkStatus(0);
shopsBO.setIsGlobal("Y");
shopsBOs.add(shopsBO);
}
return shopsBOs;
}
}
... ... @@ -128,7 +128,6 @@
<rabbit:listener queue-names="data_update_channel_productpriceplan" ref="productPricePlanMqListener" />
<rabbit:listener queue-names="data_update_channel_producttiming" ref="productTimingMqListener" />
<rabbit:listener queue-names="data_update_channel_shops" ref="shopsMqListener" />
<rabbit:listener queue-names="data_update_channel_shopsbrands" ref="shopsBrandsMqListener" />
<rabbit:listener queue-names="data_update_channel_salescategory" ref="salesCategoryMqListener" />
<rabbit:listener queue-names="data_update_channel_product" ref="productMqListener" />
... ...