✅P192_商城业务-检索服务-条件筛选联动
页面效果
在未进行条件筛选时,商品属性值位于侧边栏一侧,进行部分条件筛选后,属性值位移到面包屑上
面包屑vo设置一个默认值
cfmall-search/com.gyz.cfmall.search.vo.SearchResult
调用product服务查询品牌名称
编写Feign接口
@FeignClient("cfmall-product")
public interface ProductFeignService {
//省略代码...
@GetMapping("/product/brand/info/ids")
R brandsInfo(@RequestParam("brandIds") List<Long> brandIds);
}
Controller
cfmall-search/com.gyz.cfmall.product.controller.BrandController
/**
* 通过品牌id查询
*/
@RequestMapping("/info/ids")
public R brandinfoByIds(@RequestParam("brandIds") List<Long> brandIds) {
List<BrandEntity> brandInfoList = brandService.queryBrandInfoByids(brandIds);
return R.ok().put("brand", brandInfoList);
}
Service
cfmall-search/com.gyz.cfmall.product.service.BrandService
public interface BrandService extends IService<BrandEntity> {
//省略代码...
List<BrandEntity> queryBrandInfoByids(List<Long> brandIds);
}
cfmall-search/com.gyz.cfmall.product.service.impl.BrandServiceImpl#queryBrandInfoByids
@Override
public List<BrandEntity> queryBrandInfoByids(List<Long> brandIds) {
return this.baseMapper.selectList(new QueryWrapper<BrandEntity>().in("brand_id",brandIds));
}
远程服务调用,查询很费时,可以将查询的结果保存进缓存中 ,在方法上加@Cacheable注解实现,
cfmall-search/com.gyz.cfmall.product.service.impl.AttrServiceImpl#getAttrInfo
//value:分区名,key:用于标识第几号属性
@Cacheable(value = "attr", key = "'attrinfo:'+#root.args[0]")
@Override
public AttrRespVo getAttrInfo(Long attrId) {...}
抽取替换url方法
选中要抽取的逻辑,IDE快捷键:Ctrl+Alt+M
,抽取为:replaceQueryString()
方法
cfmall-search/com.gyz.cfmall.search.service.impl.MallSearchServiceImpl#replaceQueryString
/**
* url替换
*
* @param param
* @param value
* @return
*/
private String replaceQueryString(SearchParam param, String value, String key) {
//封装链接
String encode = null;
try {
encode = URLEncoder.encode(value, "UTF-8");
//浏览器对空格的编码和Java不一样,差异化处理
encode = encode.replace("%28","(").replace("%29",")").replace("+","%20");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String replace = param.get_queryString().replace("&" + key + "=" + encode, "");
return replace;
}
编写面包屑导航栏功能
cfmall-search/com.gyz.cfmall.search.service.impl.MallSearchServiceImpl#buildSearchResult
private SearchResult buildSearchResult(SearchResponse response, SearchParam param) {
//省略其它代码...
//7、品牌面包屑导航
if (param.getBrandId() != null && param.getBrandId().size() > 0) {
List<SearchResult.NavVo> navs = result.getNavs();
SearchResult.NavVo navVo = new SearchResult.NavVo();
//封装属性名
navVo.setNavName("品牌");
R r = productFeignService.brandsInfo(param.getBrandId());
if (r.getCode() == 0) {
List<BrandEntity> brandList = r.getData("brand", new TypeReference<List<BrandEntity>>() {
});
StringBuffer stringBuffer = new StringBuffer();
String replace = "";
for (BrandEntity brand : brandList) {
stringBuffer.append(brand.getName() + ";");
replace = replaceQueryString(param, brand.getBrandId() + "", "brandId");
}
navVo.setNavValue(stringBuffer.toString());
navVo.setLink("http://search.cfmall.com/list.html?" + replace);
}
navs.add(navVo);
result.setNavs(navs);
}
}
品牌面包屑导航栏,品牌筛选剔除
cfmall-search/src/main/resources/templates/list.html
th:with="brandId=${param.brandId}"
th:if="${#strings.isEmpty(brandId)}"
封装已经筛选的属性id
创建一个list用于封装已经筛选的属性id
**cfmall-search/src/main/java/com/gyz/cfmall/search/vo/SearchResult.java**
**cfmall-search/src/main/java/com/gyz/cfmall/search/service/impl/MallSearchServiceImpl.java**
cfmall-search/src/main/resources/templates/list.html