Authored by 胡古飞

revert

package com.yoho.search.consumer.service.logic;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.consumer.service.bo.ShopsBO;
import com.yoho.search.dal.*;
import com.yoho.search.dal.model.*;
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.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
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.
* 有货店铺索引
* 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;
@Autowired
private ShopsDecoratorMapper shopsDecoratorMapper;
@Autowired
private ShopsDecoratorTemplateMapper shopsDecoratorTemplateMapper;
/**
* 增量
*/
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);
//获取店铺装修信息
List<ShopDecoratorBO> shopDecoratorBOList = shopsDecoratorMapper.selectForDecoratorId();
HashMap<Integer, Integer> shopsIdShopsDecoratorIdMap = new HashMap<>();
List<Integer> decoratorIds = new ArrayList<>();
shopDecoratorBOList.stream().collect(Collectors.toList()).forEach(ShopDecoratorBO -> shopsIdShopsDecoratorIdMap.put(ShopDecoratorBO.getShopsId(), ShopDecoratorBO.getShopsDecoratorId()));
shopDecoratorBOList.stream().collect(Collectors.toList()).forEach(ShopDecoratorBO -> decoratorIds.add(ShopDecoratorBO.getShopsDecoratorId()));
List<ShopsDecoratorTemplate> shopsDecoratorTemplateList = shopsDecoratorTemplateMapper.getByShopsDecoratorIds(decoratorIds);
HashMap<Integer, List<ShopsDecoratorTemplate>> shopDecoratorListHashMap = new HashMap<>();
for (ShopsDecoratorTemplate shopsDecoratorTemplate : shopsDecoratorTemplateList) {
if (shopDecoratorListHashMap.containsKey(shopsDecoratorTemplate.getShopsDecoratorId())) {
List<ShopsDecoratorTemplate> shopsDecoratorTemplates = shopDecoratorListHashMap.get(shopsDecoratorTemplate.getShopsDecoratorId());
shopsDecoratorTemplates.add(shopsDecoratorTemplate);
}
List<ShopsDecoratorTemplate> shopsDecoratorTemplates = new ArrayList<>();
shopsDecoratorTemplates.add(shopsDecoratorTemplate);
shopDecoratorListHashMap.put(shopsDecoratorTemplate.getShopsDecoratorId(), shopsDecoratorTemplates);
}
private final Logger logger = LoggerFactory.getLogger(ShopsLogicService.class);
@Autowired
private ShopMapper shopMapper;
@Autowired
private ShopsBrandsMapper shopsBrandsMapper;
@Autowired
private BrandMapper brandMapper;
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());
//构建店铺装修信息
buildDecoratorInfo(shopsIdShopsDecoratorIdMap, shopDecoratorListHashMap, shopsBO);
shopsBOs.add(shopsBO);
}
return shopsBOs;
}
/**
* 增量
*/
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;
}
/**
* 构建装修信息
*/
private void buildDecoratorInfo(HashMap<Integer, Integer> shopsIdShopsDecoratorIdMap, HashMap<Integer, List<ShopsDecoratorTemplate>> shopDecoratorListHashMap, ShopsBO shopsBO) {
Integer decoratorId = shopsIdShopsDecoratorIdMap.get(shopsBO.getShopsId());
List<ShopsDecoratorTemplate> shopsDecoratorTemplateList = shopDecoratorListHashMap.get(decoratorId);
if(CollectionUtils.isEmpty(shopsDecoratorTemplateList)){
return;
}
JSONArray jsonArray = new JSONArray();
for (ShopsDecoratorTemplate shopsDecoratorTemplate : shopsDecoratorTemplateList) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", shopsDecoratorTemplate.getId() == null ? "" : shopsDecoratorTemplate.getId());
jsonObject.put("shopsDecoratorId", shopsDecoratorTemplate.getShopsDecoratorId() == null ? "" : shopsDecoratorTemplate.getShopsDecoratorId());
jsonObject.put("platform", StringUtils.isBlank(shopsDecoratorTemplate.getPlatform()) ? "" : shopsDecoratorTemplate.getPlatform());
jsonObject.put("templateType", StringUtils.isBlank(shopsDecoratorTemplate.getTemplateType()) ? "" : shopsDecoratorTemplate.getTemplateType());
jsonObject.put("isEnable", StringUtils.isBlank(shopsDecoratorTemplate.getIsEnable()) ? "" : shopsDecoratorTemplate.getIsEnable());
jsonObject.put("appType", shopsDecoratorTemplate.getAppType() == null ? "" : shopsDecoratorTemplate.getAppType());
jsonArray.add(jsonObject);
}
shopsBO.setShopsDecorators(jsonArray);
/**
* 全量
*/
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());
}
}
/**
* 处理品牌名
*/
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;
}
/**
* 构建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;
}
}
... ...