解决跨域;修改MySQL语法错误;获取分类树形数据

This commit is contained in:
Bunny 2023-12-25 16:47:44 +08:00
parent 35c44ac961
commit 76a18899ce
13 changed files with 3465 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
#n:db_spzx
!<md> [1703518861000, 0, null, null, -2147483648, -2147483648]

View File

@ -0,0 +1,2 @@
#n:information_schema
!<md> [null, 0, null, null, -2147483648, -2147483648]

View File

@ -0,0 +1,2 @@
#n:mysql
!<md> [null, 0, null, null, -2147483648, -2147483648]

View File

@ -0,0 +1,2 @@
#n:performance_schema
!<md> [null, 0, null, null, -2147483648, -2147483648]

View File

@ -0,0 +1,2 @@
#n:sys
!<md> [null, 0, null, null, -2147483648, -2147483648]

View File

@ -0,0 +1,11 @@
package cn.bunny.web.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
}

View File

@ -0,0 +1,44 @@
package cn.bunny.web.product.controller;
import cn.bunny.common.spzx.model.entity.product.Category;
import cn.bunny.common.spzx.model.entity.product.ProductSku;
import cn.bunny.common.spzx.model.vo.common.Result;
import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
import cn.bunny.common.spzx.model.vo.h5.IndexVo;
import cn.bunny.web.product.service.CategoryService;
import cn.bunny.web.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Tag(name = "首页接口管理")
@RestController
@RequestMapping(value = "/api/product/index")
@CrossOrigin
public class IndexController {
@Resource
private CategoryService categoryService;
@Resource
private ProductService productService;
@Operation(summary = "获取首页数据")
@GetMapping("")
public Result index() {
// 所有一级分类
List<Category> categoryList = categoryService. selectOneCategory();
// 根据销量排序获取前十条激励
List<ProductSku> productSkuList = productService.selectProductSkuBySal();
// 封装数据到VO
IndexVo indexVo = new IndexVo();
indexVo.setCategoryList(categoryList);
indexVo.setProductSkuList(productSkuList);
return Result.build(indexVo, ResultCodeEnum.SUCCESS);
}
}

View File

@ -0,0 +1,15 @@
package cn.bunny.web.product.mapper;
import cn.bunny.common.spzx.model.entity.product.Category;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface CategoryMapper {
// 查询所有一级分类
List<Category> selectOneCategory();
// 1. 查询所有分类返回list集合
List<Category> findAll();
}

View File

@ -0,0 +1,12 @@
package cn.bunny.web.product.service;
import cn.bunny.common.spzx.model.entity.product.Category;
import java.util.List;
public interface CategoryService {
// 所有一级分类
List<Category> selectOneCategory();
// 获取分类树形数据
List<Category> findCategoryTree();
}

View File

@ -0,0 +1,10 @@
package cn.bunny.web.product.service;
import cn.bunny.common.spzx.model.entity.product.ProductSku;
import java.util.List;
public interface ProductService {
// 根据销量排序获取前十条激励
List<ProductSku> selectProductSkuBySal();
}

View File

@ -0,0 +1,48 @@
package cn.bunny.web.product.service.imp;
import cn.bunny.common.spzx.model.entity.product.Category;
import cn.bunny.web.product.mapper.CategoryMapper;
import cn.bunny.web.product.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
public class CategoryServiceImpl implements CategoryService {
@Resource
private CategoryMapper categoryMapper;
// 商品一级分类接口
@Override
public List<Category> selectOneCategory() {
return categoryMapper.selectOneCategory();
}
// 获取分类树形数据
@Override
public List<Category> findCategoryTree() {
// 1. 查询所有分类返回list集合
List<Category> allCategoryList = categoryMapper.findAll();
// 2. 遍历所有分类list集合通过parentId=0得到所有一级分类
List<Category> categoryList = allCategoryList.stream().filter(item -> item.getParentId() == 0)
.toList();
// 3. 编辑所有一级分类list几个条件判断id=parentId得到一级下面二级分类
categoryList.forEach(category -> {
List<Category> categories = allCategoryList.stream().filter(item -> Objects.equals(item.getParentId(), category.getId())).collect(Collectors.toList());
category.setChildren(categories);
categories.forEach(twoCategory -> {
List<Category> threeCategorieList = allCategoryList.stream().filter(item -> Objects.equals(item.getParentId(), twoCategory.getId())).toList();
twoCategory.setChildren(threeCategorieList);
});
});
// 4. 遍历所有二级分类条件判断id=parentId得到二级下面三级分类
return categoryList;
}
}

View File

@ -0,0 +1,20 @@
package cn.bunny.web.product.service.imp;
import cn.bunny.common.spzx.model.entity.product.ProductSku;
import cn.bunny.web.product.mapper.ProductSkuMapper;
import cn.bunny.web.product.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
@Resource
private ProductSkuMapper productSkuMapper;
@Override
public List<ProductSku> selectProductSkuBySal() {
return productSkuMapper.selectProductSkuBySale();
}
}