Skip to content

Commit

Permalink
商品服务初步搭建
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxianzi-99 committed Feb 12, 2025
1 parent 8e8582c commit 2b02d98
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</select>
<select id="findBrandByCategoryId" resultType="com.model.entity.product.CategoryBrand">
select
b.*
cb.brand_id as brandId,b.name as brandName,b.logo,cb.category_id as categoryId
from category_brand cb
inner join brand b on b.id = cb.brand_id
where cb.category_id = #{categoryId} and cb.is_deleted = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.manager.mapper.ProductMapper">
<insert id="save" keyProperty="id">
<insert id="save" keyProperty="id" useGeneratedKeys="true">
insert into product ( name, brand_id, category1_id, category2_id, category3_id, unit_name, slider_urls, spec_value, status, audit_status, audit_message)
value (
#{name},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
#{marketPrice},
#{costPrice},
#{stockNum},
#{saleNum},
#{skuSpec},
#{weight},
#{volume},
#{status},
#{saleNum})
#{status}
)
</insert>
<update id="update">
update product_sku set
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.model.vo.product;

import com.alibaba.fastjson.JSONArray;
import com.model.entity.product.Product;
import com.model.entity.product.ProductSku;
import lombok.Data;

import java.util.List;
import java.util.Map;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/12
**/
@Data
public class ProductItemVo {
private ProductSku productSku;
private Product product;
private List<String> sliderUrlList;
private List<String> detailsImageUrlList;
private JSONArray specValueList;
private Map<String,Object> skuSpecValueMap;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.model.vo.common.Result;
import com.model.vo.common.ResultCodeEnum;
import com.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -17,7 +18,7 @@
@RestController
@RequestMapping("/api/product/brand")
public class BrandController {
@Override
@Autowired
BrandService brandService;
@GetMapping("findAll")
public Result<List<Brand>> findAll() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package com.product.controller;

import com.github.pagehelper.PageInfo;
import com.model.dto.product.ProductSkuDto;
import com.model.entity.product.ProductSku;
import com.model.vo.common.Result;
import com.model.vo.common.ResultCodeEnum;
import com.model.vo.product.ProductItemVo;
import com.product.service.ProductService;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -10,4 +21,24 @@
@RestController
@RequestMapping("/api/product")
public class ProductController {
@Autowired
ProductService productService;
@GetMapping(value = "/{page}/{limit}")
public Result<PageInfo<ProductSku>> findByPage(@Parameter(name = "page", description = "当前页码", required = true) @PathVariable Integer page,
@Parameter(name = "limit", description = "每页记录数", required = true) @PathVariable Integer limit,
@Parameter(name = "productSkuDto", description = "搜索条件对象", required = false) ProductSkuDto productSkuDto) {
PageInfo<ProductSku> pageInfo = productService.findByPage(page, limit, productSkuDto);
return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
}

/**
* 获取商品详情
* @param skuId
* @return
*/
@GetMapping("/item/{skuId}")
public Result<ProductItemVo> item(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable Long skuId) {
ProductItemVo productItemVo = productService.item(skuId);
return Result.build(productItemVo , ResultCodeEnum.SUCCESS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.product.mapper;

import com.model.entity.product.ProductDetails;
import org.apache.ibatis.annotations.Mapper;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/12
**/
@Mapper
public interface ProductDetailsMapper {
ProductDetails getByProductId(Long productId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.product.mapper;

import com.model.entity.product.Product;
import org.apache.ibatis.annotations.Mapper;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/12
**/
@Mapper
public interface ProductMapper {
Product getById(Long productId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.product.mapper;

import com.model.dto.product.ProductSkuDto;
import com.model.entity.product.ProductSku;
import org.apache.ibatis.annotations.Mapper;

Expand All @@ -12,4 +13,10 @@
@Mapper
public interface ProductSkuMapper {
List<ProductSku> findProductSkuBySale();

List<ProductSku> findByPage(ProductSkuDto productSkuDto);

ProductSku getById(Long skuId);

List<ProductSku> findByProductId(Long productId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.product.service;

import com.github.pagehelper.PageInfo;
import com.model.dto.product.ProductSkuDto;
import com.model.entity.product.ProductSku;
import com.model.vo.product.ProductItemVo;

import java.util.List;

Expand All @@ -14,4 +17,8 @@ public interface ProductService {
* @return
*/
List<ProductSku> findProductSkuBySale();

PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto);

ProductItemVo item(Long skuId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public List<Category> findOneCategory() {
}

@Override
@Cacheable(value = "category" , key = "'all'")
@Cacheable(value = "category", key = "'all'")
public List<Category> findCategoryTree() {
List<Category> categoryList = categoryMapper.findAll();
//全部一级分类
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package com.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.dto.product.ProductSkuDto;
import com.model.entity.product.Product;
import com.model.entity.product.ProductDetails;
import com.model.entity.product.ProductSku;
import com.model.vo.product.ProductItemVo;
import com.product.mapper.ProductDetailsMapper;
import com.product.mapper.ProductMapper;
import com.product.mapper.ProductSkuMapper;
import com.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 帕斯卡的芦苇
Expand All @@ -15,9 +27,60 @@
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
ProductSkuMapper productMapper;
ProductMapper productMapper;
@Autowired
ProductDetailsMapper productDetailsMapper;
@Autowired
ProductSkuMapper productSkuMapper;

@Override
public List<ProductSku> findProductSkuBySale() {
return productMapper.findProductSkuBySale();
return productSkuMapper.findProductSkuBySale();
}

/**
* @param page
* @param limit
* @param productSkuDto
* @return
*/
@Override
public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
PageHelper.startPage(page, limit);
List<ProductSku> productSkuList = productSkuMapper.findByPage(productSkuDto);
return new PageInfo<>(productSkuList);
}

/**
* @param skuId
* @return
*/
@Override
public ProductItemVo item(Long skuId) {
//当前sku信息
ProductSku productSku = productSkuMapper.getById(skuId);

//当前商品信息
Product product = productMapper.getById(productSku.getProductId());

//同一个商品下面的sku信息列表
List<ProductSku> productSkuList = productSkuMapper.findByProductId(productSku.getProductId());
//建立sku规格与skuId对应关系
Map<String, Object> skuSpecValueMap = new HashMap<>();
productSkuList.forEach(item -> {
skuSpecValueMap.put(item.getSkuSpec(), item.getId());
});

//商品详情信息
ProductDetails productDetails = productDetailsMapper.getByProductId(productSku.getProductId());

ProductItemVo productItemVo = new ProductItemVo();
productItemVo.setProductSku(productSku);
productItemVo.setProduct(product);
productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
productItemVo.setSkuSpecValueMap(skuSpecValueMap);
return productItemVo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.product.mapper.ProductMapper">
<sql id="columns">
id,name,brand_id,category1_id,category2_id,category3_id,unit_name,slider_urls,spec_value,status,audit_status,audit_message,create_time,update_time,is_deleted
</sql>
<select id="getById" resultType="com.model.entity.product.Product">
select
<include refid="columns"/>
from product
where
id = #{id}
</select>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
volume,
status,
create_time,
update_time,
update_time
</sql>
<select id="findProductSkuBySale" resultType="com.model.entity.product.ProductSku">
select sku.id,
Expand Down Expand Up @@ -47,4 +47,50 @@
order by sku.sale_num desc
limit 20
</select>
<select id="findByPage" resultType="com.model.entity.product.ProductSku">
select
sku.id,sku.sku_code,sku.sku_name,sku.product_id,sku.thumb_img,sku.sale_price,sku.market_price,sku.cost_price,sku.stock_num,sku.sale_num,sku.sku_spec,sku.weight,sku.volume,sku.status,sku.create_time,sku.update_time,sku.is_deleted
from product_sku sku
left join product p on p.id = sku.product_id
<where>
<if test="keyword != null and keyword != ''">
and sku.sku_name like CONCAT('%',#{keyword},'%')
</if>
<if test="brandId != null">
and p.brand_id = #{brandId}
</if>
<if test="category1Id != null">
and p.category1_id = #{category1Id}
</if>
<if test="category2Id != null">
and p.category2_id = #{category2Id}
</if>
<if test="category3Id != null">
and p.category3_id = #{category3Id}
</if>
and p.status = 1
and p.audit_status = 1
and sku.is_deleted = 0
and p.is_deleted = 0
</where>
<if test="order == 1">
order by sku.sale_num desc
</if>
<if test="order == 2">
order by sku.sale_price asc
</if>
<if test="order == 3">
order by sku.sale_price desc
</if>
</select>
<select id="getById" resultType="com.model.entity.product.ProductSku">
select <include refid="colum"/>
from product_sku
where id = #{id}
</select>
<select id="findByProductId" resultType="com.model.entity.product.ProductSku">
select <include refid="colum"/>
from product_sku
where product_id = #{productId}
</select>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.product.mapper.ProductDetailsMapper">
<sql id="columns">
id,product_id,image_urls,create_time,update_time,is_deleted
</sql>
<select id="getByProductId" resultType="com.model.entity.product.ProductDetails">
select
<include refid="columns"/>
from product_details
where product_id = #{productId}
</select>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author 帕斯卡的芦苇
* @date 2025/2/12
**/
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}

0 comments on commit 2b02d98

Please sign in to comment.