|
@@ -1,13 +1,77 @@
|
|
|
-package edu.travel.commodity.serviceImpl;
|
|
|
+package edu.travel.commodity.service.impl;
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import edu.travel.commodity.constant.BaseConstant;
|
|
|
+import edu.travel.commodity.constant.RedisKey;
|
|
|
+import edu.travel.commodity.dto.ShopTypeDto;
|
|
|
import edu.travel.commodity.entity.ShopCategory;
|
|
|
import edu.travel.commodity.mapper.ShopCategoryMapper;
|
|
|
import edu.travel.commodity.service.ShopCategoryService;
|
|
|
+import edu.travel.commodity.utils.RedisUtil;
|
|
|
+import edu.travel.commodity.vo.ShopTypeVo;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
@Service
|
|
|
public class ShopCategoryServiceImpl extends ServiceImpl<ShopCategoryMapper, ShopCategory> implements ShopCategoryService {
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ShopTypeVo> getShopType(ShopTypeDto shopTypeDto) {
|
|
|
+ String redisKey="";
|
|
|
+ if(shopTypeDto.getIsHeat().equals(BaseConstant.BASIC_STATUS_NO_STR)){
|
|
|
+ redisKey=RedisKey.PRODUCT_HOT_TYPE;
|
|
|
+ }else {
|
|
|
+ redisKey=RedisKey.PRODUCT_TYPE;
|
|
|
+ }
|
|
|
+ String string = redisUtil.getString(redisKey);
|
|
|
+ if(!ObjectUtil.isEmpty(string)){
|
|
|
+ JSONArray objects = JSONUtil.parseArray(string);
|
|
|
+ return JSONUtil.toList(objects, ShopTypeVo.class);
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<ShopCategory> query = Wrappers.lambdaQuery();
|
|
|
+ //条件构建
|
|
|
+ query.eq(ShopCategory::getCountry, shopTypeDto.getCountryId());
|
|
|
|
|
|
+ if(shopTypeDto.getIsHeat().equals(BaseConstant.BASIC_STATUS_NO_STR)){
|
|
|
+ query.orderByDesc(ShopCategory::getHeatValue);
|
|
|
+ }else {
|
|
|
+ query.orderByDesc(ShopCategory::getSortOrder);
|
|
|
+ }
|
|
|
+ List<ShopCategory> list = list(query);
|
|
|
+ if(list.isEmpty()){
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ //父级
|
|
|
+ List<ShopCategory> collect = list.stream().filter(item -> item.getParentId().equals(0L)).collect(Collectors.toList());
|
|
|
+ if(collect.isEmpty()){
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ List<ShopTypeVo> parent = BeanUtil.copyToList(collect, ShopTypeVo.class);
|
|
|
+ for (ShopTypeVo shopCategory : parent) {
|
|
|
+ List<ShopCategory> children = list.stream().filter(item -> item.getParentId().toString().equals(shopCategory.getId())).collect(Collectors.toList());
|
|
|
+ List<ShopTypeVo> shopTypeVos = BeanUtil.copyToList(children, ShopTypeVo.class);
|
|
|
+ shopCategory.setChildren(shopTypeVos);
|
|
|
+ }
|
|
|
+ if(shopTypeDto.getIsHeat().equals(BaseConstant.BASIC_STATUS_NO_STR)){
|
|
|
+ //24小时热门过期
|
|
|
+ redisUtil.setString(RedisKey.PRODUCT_TYPE,JSONUtil.toJsonStr(parent),24*60*60*60, TimeUnit.SECONDS);
|
|
|
+ }else {
|
|
|
+ redisUtil.setString(RedisKey.PRODUCT_TYPE,JSONUtil.toJsonStr(parent));
|
|
|
+ }
|
|
|
+ return parent;
|
|
|
+ }
|
|
|
}
|