feat(商城): 代码优化

master
wayn 1 year ago
parent d374feb44e
commit 8bfc592d07

@ -2564,7 +2564,7 @@ CREATE TABLE `shop_goods_product` (
`del_flag` tinyint(1) NULL DEFAULT 0 COMMENT '删除标志0代表存在 1代表删除', `del_flag` tinyint(1) NULL DEFAULT 0 COMMENT '删除标志0代表存在 1代表删除',
PRIMARY KEY (`id`) USING BTREE, PRIMARY KEY (`id`) USING BTREE,
INDEX `goods_id`(`goods_id`) USING BTREE INDEX `goods_id`(`goods_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 257 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '商品货品' ROW_FORMAT = DYNAMIC; ) ENGINE = InnoDB AUTO_INCREMENT = 257 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '商品 SKU ' ROW_FORMAT = DYNAMIC;
-- ---------------------------- -- ----------------------------
-- Records of shop_goods_product -- Records of shop_goods_product

@ -0,0 +1,25 @@
package com.wayn.common.core.domain.shop.vo;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.domain.shop.GoodsAttribute;
import com.wayn.common.core.domain.shop.GoodsProduct;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
/**
* @author: waynaqua
* @date: 2023/11/7 22:40
*/
@Data
public class GoodsDetailResponseVO implements Serializable {
@Serial
private static final long serialVersionUID = -4222874009558306929L;
private Goods info;
private List<SpecificationVO> specificationList;
private List<GoodsProduct> productList;
private List<GoodsAttribute> attributes;
}

@ -0,0 +1,17 @@
package com.wayn.common.core.domain.shop.vo;
import com.wayn.common.core.domain.shop.GoodsSpecification;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
@Data
public class SpecificationVO implements Serializable {
@Serial
private static final long serialVersionUID = 950869220112646783L;
private String name;
private List<GoodsSpecification> valueList;
}

@ -2,6 +2,9 @@ package com.wayn.common.core.service.shop;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.wayn.common.core.domain.shop.GoodsSpecification; import com.wayn.common.core.domain.shop.GoodsSpecification;
import com.wayn.common.core.domain.shop.vo.SpecificationVO;
import java.util.List;
/** /**
* *
@ -12,5 +15,5 @@ import com.wayn.common.core.domain.shop.GoodsSpecification;
public interface IGoodsSpecificationService extends IService<GoodsSpecification> { public interface IGoodsSpecificationService extends IService<GoodsSpecification> {
Object getSpecificationVOList(Long goodsId); List<SpecificationVO> getSpecificationVOList(Long goodsId);
} }

@ -3,6 +3,7 @@ package com.wayn.common.core.service.shop.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wayn.common.core.domain.shop.GoodsSpecification; import com.wayn.common.core.domain.shop.GoodsSpecification;
import com.wayn.common.core.domain.shop.vo.SpecificationVO;
import com.wayn.common.core.mapper.shop.GoodsSpecificationMapper; import com.wayn.common.core.mapper.shop.GoodsSpecificationMapper;
import com.wayn.common.core.service.shop.IGoodsSpecificationService; import com.wayn.common.core.service.shop.IGoodsSpecificationService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -22,16 +23,15 @@ import java.util.Map;
public class GoodsSpecificationServiceImpl extends ServiceImpl<GoodsSpecificationMapper, GoodsSpecification> implements IGoodsSpecificationService { public class GoodsSpecificationServiceImpl extends ServiceImpl<GoodsSpecificationMapper, GoodsSpecification> implements IGoodsSpecificationService {
@Override @Override
public List<VO> getSpecificationVOList(Long goodsId) { public List<SpecificationVO> getSpecificationVOList(Long goodsId) {
List<GoodsSpecification> specificationList = list(new QueryWrapper<GoodsSpecification>().eq("goods_id", goodsId)); List<GoodsSpecification> specificationList = list(new QueryWrapper<GoodsSpecification>().eq("goods_id", goodsId));
Map<String, SpecificationVO> map = new HashMap<>();
Map<String, VO> map = new HashMap<>(); List<SpecificationVO> specificationVoList = new ArrayList<>();
List<VO> specificationVoList = new ArrayList<>();
for (GoodsSpecification goodsSpecification : specificationList) { for (GoodsSpecification goodsSpecification : specificationList) {
String specification = goodsSpecification.getSpecification(); String specification = goodsSpecification.getSpecification();
VO goodsSpecificationVo = map.get(specification); SpecificationVO goodsSpecificationVo = map.get(specification);
if (goodsSpecificationVo == null) { if (goodsSpecificationVo == null) {
goodsSpecificationVo = new VO(); goodsSpecificationVo = new SpecificationVO();
goodsSpecificationVo.setName(specification); goodsSpecificationVo.setName(specification);
List<GoodsSpecification> valueList = new ArrayList<>(); List<GoodsSpecification> valueList = new ArrayList<>();
valueList.add(goodsSpecification); valueList.add(goodsSpecification);
@ -46,25 +46,4 @@ public class GoodsSpecificationServiceImpl extends ServiceImpl<GoodsSpecificatio
return specificationVoList; return specificationVoList;
} }
private static class VO {
private String name;
private List<GoodsSpecification> valueList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<GoodsSpecification> getValueList() {
return valueList;
}
public void setValueList(List<GoodsSpecification> valueList) {
this.valueList = valueList;
}
}
} }

@ -17,6 +17,6 @@ public class GoodsController {
@GetMapping("detail/{goodsId}") @GetMapping("detail/{goodsId}")
public R detail(@PathVariable Long goodsId) { public R detail(@PathVariable Long goodsId) {
return iGoodsDetailService.getGoodsDetailData(goodsId); return R.success(iGoodsDetailService.getGoodsDetailData(goodsId));
} }
} }

@ -1,6 +1,6 @@
package com.wayn.mobile.api.service; package com.wayn.mobile.api.service;
import com.wayn.common.util.R; import com.wayn.common.core.domain.shop.vo.GoodsDetailResponseVO;
public interface IGoodsDetailService { public interface IGoodsDetailService {
@ -10,5 +10,5 @@ public interface IGoodsDetailService {
* @param goodsId ID * @param goodsId ID
* @return * @return
*/ */
R getGoodsDetailData(Long goodsId); GoodsDetailResponseVO getGoodsDetailData(Long goodsId);
} }

@ -3,14 +3,16 @@ package com.wayn.mobile.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wayn.common.core.domain.shop.GoodsAttribute; import com.wayn.common.core.domain.shop.GoodsAttribute;
import com.wayn.common.core.domain.shop.GoodsProduct; import com.wayn.common.core.domain.shop.GoodsProduct;
import com.wayn.common.core.domain.shop.vo.GoodsDetailResponseVO;
import com.wayn.common.core.domain.shop.vo.SpecificationVO;
import com.wayn.common.core.service.shop.IGoodsAttributeService; import com.wayn.common.core.service.shop.IGoodsAttributeService;
import com.wayn.common.core.service.shop.IGoodsProductService; import com.wayn.common.core.service.shop.IGoodsProductService;
import com.wayn.common.core.service.shop.IGoodsService; import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.core.service.shop.IGoodsSpecificationService; import com.wayn.common.core.service.shop.IGoodsSpecificationService;
import com.wayn.common.util.R;
import com.wayn.mobile.api.service.IGoodsDetailService; import com.wayn.mobile.api.service.IGoodsDetailService;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -31,27 +33,29 @@ public class GoodsDetailServiceImpl implements IGoodsDetailService {
private IGoodsAttributeService iGoodsAttributeService; private IGoodsAttributeService iGoodsAttributeService;
private ThreadPoolTaskExecutor commonThreadPoolTaskExecutor; private ThreadPoolTaskExecutor commonThreadPoolTaskExecutor;
@Cacheable(value = "goods_detail_cache_#600", unless = "#result == null")
@Override @Override
public R getGoodsDetailData(Long goodsId) { public GoodsDetailResponseVO getGoodsDetailData(Long goodsId) {
R success = R.success(); GoodsDetailResponseVO responseVO = new GoodsDetailResponseVO();
Callable<Object> specificationCall = () -> iGoodsSpecificationService.getSpecificationVOList(goodsId); Callable<List<SpecificationVO>> specificationCall = () -> iGoodsSpecificationService.getSpecificationVOList(goodsId);
Callable<List<GoodsProduct>> productCall = () -> iGoodsProductService.list(new QueryWrapper<GoodsProduct>().eq("goods_id", goodsId)); Callable<List<GoodsProduct>> productCall = () -> iGoodsProductService.list(new QueryWrapper<GoodsProduct>().eq("goods_id", goodsId));
Callable<List<GoodsAttribute>> attrCall = () -> iGoodsAttributeService.list(new QueryWrapper<GoodsAttribute>().eq("goods_id", goodsId)); Callable<List<GoodsAttribute>> attrCall = () -> iGoodsAttributeService.list(new QueryWrapper<GoodsAttribute>().eq("goods_id", goodsId));
FutureTask<Object> specificationTask = new FutureTask<>(specificationCall); FutureTask<List<SpecificationVO>> specificationTask = new FutureTask<>(specificationCall);
FutureTask<List<GoodsProduct>> productTask = new FutureTask<>(productCall); FutureTask<List<GoodsProduct>> productTask = new FutureTask<>(productCall);
FutureTask<List<GoodsAttribute>> attrTask = new FutureTask<>(attrCall); FutureTask<List<GoodsAttribute>> attrTask = new FutureTask<>(attrCall);
commonThreadPoolTaskExecutor.submit(specificationTask); commonThreadPoolTaskExecutor.submit(specificationTask);
commonThreadPoolTaskExecutor.submit(productTask); commonThreadPoolTaskExecutor.submit(productTask);
commonThreadPoolTaskExecutor.submit(attrTask); commonThreadPoolTaskExecutor.submit(attrTask);
try { try {
success.add("info", iGoodsService.getById(goodsId)); responseVO.setInfo(iGoodsService.getById(goodsId));
success.add("specificationList", specificationTask.get()); responseVO.setProductList(productTask.get());
success.add("productList", productTask.get()); responseVO.setSpecificationList(specificationTask.get());
success.add("attributes", attrTask.get()); responseVO.setAttributes(attrTask.get());
return responseVO;
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
return success; return null;
} }
} }

Loading…
Cancel
Save