Commit 926db6dd5d07985f5d79f76a99d2dee1dd1158a7
1 parent
7375407d
feat(shop): 添加商品关联类别导入功能
- 新增 EbCategorysRelation 模型和相关 mapper、service - 实现 ProductController 中的 importRelatedCategory 方法 - 添加 ImportRelatedCategoryVo 和 ImportRelatedCategoryListenter 用于处理导入逻辑 - 在 pom.xml 中添加 easyexcel 依赖
Showing
13 changed files
with
326 additions
and
2 deletions
shop/pom.xml
... | ... | @@ -37,6 +37,11 @@ |
37 | 37 | <optional>true</optional> |
38 | 38 | </dependency> |
39 | 39 | <dependency> |
40 | + <groupId>com.alibaba</groupId> | |
41 | + <artifactId>easyexcel</artifactId> | |
42 | + <version>3.3.4</version> | |
43 | + </dependency> | |
44 | + <dependency> | |
40 | 45 | <groupId>org.springframework.boot</groupId> |
41 | 46 | <artifactId>spring-boot-starter-freemarker</artifactId> |
42 | 47 | <version>3.3.5</version> | ... | ... |
shop/src/main/java/com/canrd/shop/common/excel/listenter/ImportRelatedCategoryListenter.java
0 → 100644
1 | +package com.canrd.shop.common.excel.listenter; | |
2 | + | |
3 | +import cn.hutool.core.bean.BeanUtil; | |
4 | +import com.alibaba.excel.context.AnalysisContext; | |
5 | +import com.alibaba.excel.event.AnalysisEventListener; | |
6 | +import com.canrd.shop.module.dto.EbCategorysRelation; | |
7 | +import com.canrd.shop.module.dto.ProductCategoryDO; | |
8 | +import com.canrd.shop.module.vo.ImportRelatedCategoryVo; | |
9 | +import com.canrd.shop.service.IEbCategorysRelationService; | |
10 | +import com.canrd.shop.service.ProductCategoryService; | |
11 | + | |
12 | + | |
13 | +import java.util.Optional; | |
14 | + | |
15 | +/** | |
16 | + * @author zgt | |
17 | + * @project mobile.canrd.com-service | |
18 | + * @description | |
19 | + * @date 2024/8/26 | |
20 | + */ | |
21 | +public class ImportRelatedCategoryListenter extends AnalysisEventListener<ImportRelatedCategoryVo> { | |
22 | + | |
23 | + private IEbCategorysRelationService ebCategorysRelationService; | |
24 | + private ProductCategoryService categoryService; | |
25 | + | |
26 | + public ImportRelatedCategoryListenter(IEbCategorysRelationService ebCategorysRelationService, ProductCategoryService categoryService) { | |
27 | + this.ebCategorysRelationService = ebCategorysRelationService; | |
28 | + this.categoryService = categoryService; | |
29 | + } | |
30 | + | |
31 | + @Override | |
32 | + public void invoke(ImportRelatedCategoryVo importRelatedCategoryVo, AnalysisContext analysisContext) { | |
33 | + Optional<ProductCategoryDO> categoryOptional = categoryService.lambdaQuery() | |
34 | + .eq(ProductCategoryDO::getName, importRelatedCategoryVo.getCategoryName()) | |
35 | + .select(ProductCategoryDO::getId) | |
36 | + .oneOpt(); | |
37 | + Optional<ProductCategoryDO> relatedCategoryOptional = categoryService.lambdaQuery() | |
38 | + .eq(ProductCategoryDO::getName, importRelatedCategoryVo.getRelatedCategoryName()) | |
39 | + .select(ProductCategoryDO::getId) | |
40 | + .oneOpt(); | |
41 | + if (!categoryOptional.isPresent()||!relatedCategoryOptional.isPresent()){ | |
42 | + return; | |
43 | + } | |
44 | + EbCategorysRelation bean = BeanUtil.toBean(importRelatedCategoryVo, EbCategorysRelation.class); | |
45 | + bean.setCategoryId(categoryOptional.get().getId()); | |
46 | + bean.setRelatedCategoryId(relatedCategoryOptional.get().getId()); | |
47 | + ebCategorysRelationService.save(bean); | |
48 | + } | |
49 | + | |
50 | + @Override | |
51 | + public void doAfterAllAnalysed(AnalysisContext analysisContext) { | |
52 | + | |
53 | + } | |
54 | + | |
55 | + @Override | |
56 | + public void onException(Exception exception, AnalysisContext context) throws Exception { | |
57 | + super.onException(exception, context); | |
58 | + } | |
59 | +} | ... | ... |
shop/src/main/java/com/canrd/shop/controller/ProductController.java
1 | 1 | package com.canrd.shop.controller; |
2 | 2 | |
3 | 3 | import cn.hutool.core.collection.CollUtil; |
4 | +import com.alibaba.excel.EasyExcel; | |
4 | 5 | import com.canrd.shop.common.constant.ServerResult; |
6 | +import com.canrd.shop.common.excel.listenter.ImportRelatedCategoryListenter; | |
5 | 7 | import com.canrd.shop.common.exception.BusinessException; |
6 | 8 | import com.canrd.shop.common.jsr303.OperateGroup; |
7 | 9 | import com.canrd.shop.config.StaticHtmlConfig; |
8 | 10 | import com.canrd.shop.module.dto.ProductCategoryDO; |
9 | 11 | import com.canrd.shop.module.dto.ProductDO; |
10 | 12 | import com.canrd.shop.module.dto.ProductFunctionDO; |
13 | +import com.canrd.shop.module.vo.ImportRelatedCategoryVo; | |
11 | 14 | import com.canrd.shop.module.vo.ProductCategoryQueryVO; |
12 | 15 | import com.canrd.shop.module.vo.ProductQueryVO; |
13 | 16 | import com.canrd.shop.module.vo.ProductVO; |
17 | +import com.canrd.shop.service.IEbCategorysRelationService; | |
14 | 18 | import com.canrd.shop.service.ProductCategoryService; |
15 | 19 | import com.canrd.shop.service.ProductFunctionService; |
16 | 20 | import com.canrd.shop.service.ProductService; |
17 | 21 | import freemarker.template.Configuration; |
18 | 22 | import freemarker.template.Template; |
19 | 23 | import freemarker.template.TemplateException; |
24 | +import org.apache.catalina.Server; | |
20 | 25 | import org.apache.commons.lang3.StringUtils; |
21 | 26 | import org.springframework.beans.factory.annotation.Autowired; |
22 | 27 | import org.springframework.beans.factory.annotation.Value; |
23 | 28 | import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; |
24 | 29 | import org.springframework.validation.annotation.Validated; |
25 | 30 | import org.springframework.web.bind.annotation.*; |
31 | +import org.springframework.web.multipart.MultipartFile; | |
26 | 32 | |
27 | 33 | import javax.annotation.Resource; |
28 | 34 | import java.io.*; |
... | ... | @@ -59,6 +65,9 @@ public class ProductController { |
59 | 65 | @Autowired |
60 | 66 | private ProductFunctionService productFunctionService; |
61 | 67 | |
68 | + @Autowired | |
69 | + private IEbCategorysRelationService ebCategorysRelationService; | |
70 | + | |
62 | 71 | /** |
63 | 72 | * 产品分类 |
64 | 73 | * |
... | ... | @@ -87,7 +96,7 @@ public class ProductController { |
87 | 96 | } |
88 | 97 | |
89 | 98 | /** |
90 | - * 分页查询 | |
99 | + * 生成静态页面 | |
91 | 100 | * |
92 | 101 | * @param productQueryVO 查询条件 |
93 | 102 | * @return 查询结果 |
... | ... | @@ -158,7 +167,7 @@ public class ProductController { |
158 | 167 | } |
159 | 168 | |
160 | 169 | System.out.println("文件已成功保存到 " + outputFile.getAbsolutePath()); |
161 | - return null; | |
170 | + return ServerResult.success(fileName); | |
162 | 171 | } |
163 | 172 | |
164 | 173 | /** |
... | ... | @@ -210,5 +219,23 @@ public class ProductController { |
210 | 219 | return productService.chatgpt(productQueryVO); |
211 | 220 | } |
212 | 221 | |
222 | + /** | |
223 | + * 导入商品关联文章 | |
224 | + */ | |
225 | + @RequestMapping(value = "/importRelatedCategory", method = RequestMethod.POST) | |
226 | + public ServerResult importRelatedCategory(@RequestParam MultipartFile file) throws IOException { | |
227 | + //校验表格后缀是否为以.xlsx或者xls结尾的 | |
228 | + String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, | |
229 | + file.getOriginalFilename().length() - 1); | |
230 | + if (!"xlsx".equals(suffixName) && !"xls".equals(suffixName)) { | |
231 | + throw new RuntimeException("文件格式错误"); | |
232 | + } | |
233 | + EasyExcel.read(file.getInputStream(), ImportRelatedCategoryVo.class, | |
234 | + new ImportRelatedCategoryListenter(ebCategorysRelationService,productCategoryService)) | |
235 | + .sheet(0) | |
236 | + .doRead(); | |
237 | + return ServerResult.success(); | |
238 | + } | |
239 | + | |
213 | 240 | } |
214 | 241 | ... | ... |
shop/src/main/java/com/canrd/shop/mapper/EbCategorysRelationMapper.java
0 → 100644
1 | +package com.canrd.shop.mapper; | |
2 | + | |
3 | +import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |
4 | +import com.canrd.shop.module.dto.EbCategorysRelation; | |
5 | + | |
6 | +/** | |
7 | + * <p> | |
8 | + * Mapper 接口 | |
9 | + * </p> | |
10 | + * | |
11 | + * @author author | |
12 | + * @since 2024-11-14 | |
13 | + */ | |
14 | +public interface EbCategorysRelationMapper extends BaseMapper<EbCategorysRelation> { | |
15 | + | |
16 | +} | ... | ... |
shop/src/main/java/com/canrd/shop/module/dto/EbCategorysRelation.java
0 → 100644
1 | +package com.canrd.shop.module.dto; | |
2 | + | |
3 | +import com.baomidou.mybatisplus.annotation.TableName; | |
4 | +import com.baomidou.mybatisplus.annotation.IdType; | |
5 | +import com.baomidou.mybatisplus.annotation.TableId; | |
6 | +import java.io.Serializable; | |
7 | +import lombok.Data; | |
8 | +import lombok.EqualsAndHashCode; | |
9 | +import lombok.experimental.Accessors; | |
10 | + | |
11 | +/** | |
12 | + * <p> | |
13 | + * | |
14 | + * </p> | |
15 | + * | |
16 | + * @author author | |
17 | + * @since 2024-11-14 | |
18 | + */ | |
19 | +@Data | |
20 | +@EqualsAndHashCode(callSuper = false) | |
21 | +@Accessors(chain = true) | |
22 | +@TableName("eb_categorys_relation") | |
23 | +public class EbCategorysRelation implements Serializable { | |
24 | + | |
25 | + private static final long serialVersionUID = 1L; | |
26 | + | |
27 | + @TableId(value = "id", type = IdType.AUTO) | |
28 | + private Long id; | |
29 | + | |
30 | + private String categoryName; | |
31 | + | |
32 | + private String relatedCategoryName; | |
33 | + | |
34 | + private String categoryId; | |
35 | + | |
36 | + private String relatedCategoryId; | |
37 | + | |
38 | + private Integer relevance; | |
39 | + | |
40 | + | |
41 | +} | ... | ... |
shop/src/main/java/com/canrd/shop/module/vo/ImportRelatedCategoryVo.java
0 → 100644
1 | +package com.canrd.shop.module.vo; | |
2 | + | |
3 | +import com.alibaba.excel.annotation.ExcelProperty; | |
4 | +import lombok.Data; | |
5 | +import lombok.EqualsAndHashCode; | |
6 | + | |
7 | +/** | |
8 | + * @author zgt | |
9 | + * @project mobile.canrd.com-service | |
10 | + * @description | |
11 | + * @date 2024/8/27 | |
12 | + */ | |
13 | +@Data | |
14 | +@EqualsAndHashCode(callSuper = false) | |
15 | +public class ImportRelatedCategoryVo { | |
16 | + @ExcelProperty(value = "分类") | |
17 | + private String categoryName; | |
18 | + @ExcelProperty(value = "关联分类") | |
19 | + private String relatedCategoryName; | |
20 | + | |
21 | + @ExcelProperty(value = "排序") | |
22 | + private Integer relevance; | |
23 | +} | ... | ... |
shop/src/main/java/com/canrd/shop/service/IEbCategorysRelationService.java
0 → 100644
1 | +package com.canrd.shop.service; | |
2 | + | |
3 | +import com.baomidou.mybatisplus.extension.service.IService; | |
4 | +import com.canrd.shop.module.dto.EbCategorysRelation; | |
5 | + | |
6 | +/** | |
7 | + * <p> | |
8 | + * 服务类 | |
9 | + * </p> | |
10 | + * | |
11 | + * @author author | |
12 | + * @since 2024-11-14 | |
13 | + */ | |
14 | +public interface IEbCategorysRelationService extends IService<EbCategorysRelation> { | |
15 | + | |
16 | +} | ... | ... |
shop/src/main/java/com/canrd/shop/service/impl/EbCategorysRelationServiceImpl.java
0 → 100644
1 | +package com.canrd.shop.service.impl; | |
2 | + | |
3 | + | |
4 | +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |
5 | +import com.canrd.shop.mapper.EbCategorysRelationMapper; | |
6 | +import com.canrd.shop.module.dto.EbCategorysRelation; | |
7 | +import com.canrd.shop.service.IEbCategorysRelationService; | |
8 | +import org.springframework.stereotype.Service; | |
9 | + | |
10 | +/** | |
11 | + * <p> | |
12 | + * 服务实现类 | |
13 | + * </p> | |
14 | + * | |
15 | + * @author author | |
16 | + * @since 2024-11-14 | |
17 | + */ | |
18 | +@Service | |
19 | +public class EbCategorysRelationServiceImpl extends ServiceImpl<EbCategorysRelationMapper, EbCategorysRelation> implements IEbCategorysRelationService { | |
20 | + | |
21 | +} | ... | ... |
src/main/java/com/order/erp/domain/model/EbCategorysRelation.java
0 → 100644
1 | +package com.order.erp.domain.model; | |
2 | + | |
3 | +import com.baomidou.mybatisplus.annotation.TableName; | |
4 | +import com.baomidou.mybatisplus.annotation.IdType; | |
5 | +import com.baomidou.mybatisplus.annotation.TableId; | |
6 | +import java.io.Serializable; | |
7 | +import io.swagger.annotations.ApiModel; | |
8 | +import io.swagger.annotations.ApiModelProperty; | |
9 | +import lombok.Data; | |
10 | +import lombok.EqualsAndHashCode; | |
11 | +import lombok.experimental.Accessors; | |
12 | + | |
13 | +/** | |
14 | + * <p> | |
15 | + * | |
16 | + * </p> | |
17 | + * | |
18 | + * @author author | |
19 | + * @since 2024-11-14 | |
20 | + */ | |
21 | +@Data | |
22 | +@EqualsAndHashCode(callSuper = false) | |
23 | +@Accessors(chain = true) | |
24 | +@TableName("eb_categorys_relation") | |
25 | +@ApiModel(value="EbCategorysRelation对象", description="") | |
26 | +public class EbCategorysRelation implements Serializable { | |
27 | + | |
28 | + private static final long serialVersionUID = 1L; | |
29 | + | |
30 | + @TableId(value = "id", type = IdType.AUTO) | |
31 | + private Long id; | |
32 | + | |
33 | + private String categoryName; | |
34 | + | |
35 | + private String relatedCategoryName; | |
36 | + | |
37 | + private Integer categoryId; | |
38 | + | |
39 | + private Integer relatedCategoryId; | |
40 | + | |
41 | + private Integer relevance; | |
42 | + | |
43 | + | |
44 | +} | ... | ... |
src/main/java/com/order/erp/mapper/EbCategorysRelationMapper.java
0 → 100644
1 | +package com.order.erp.mapper; | |
2 | + | |
3 | +import com.order.erp.domain.model.EbCategorysRelation; | |
4 | +import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |
5 | + | |
6 | +/** | |
7 | + * <p> | |
8 | + * Mapper 接口 | |
9 | + * </p> | |
10 | + * | |
11 | + * @author author | |
12 | + * @since 2024-11-14 | |
13 | + */ | |
14 | +public interface EbCategorysRelationMapper extends BaseMapper<EbCategorysRelation> { | |
15 | + | |
16 | +} | ... | ... |
src/main/java/com/order/erp/service/IEbCategorysRelationService.java
0 → 100644
1 | +package com.order.erp.service; | |
2 | + | |
3 | +import com.order.erp.domain.model.EbCategorysRelation; | |
4 | +import com.baomidou.mybatisplus.extension.service.IService; | |
5 | + | |
6 | +/** | |
7 | + * <p> | |
8 | + * 服务类 | |
9 | + * </p> | |
10 | + * | |
11 | + * @author author | |
12 | + * @since 2024-11-14 | |
13 | + */ | |
14 | +public interface IEbCategorysRelationService extends IService<EbCategorysRelation> { | |
15 | + | |
16 | +} | ... | ... |
src/main/java/com/order/erp/service/impl/EbCategorysRelationServiceImpl.java
0 → 100644
1 | +package com.order.erp.service.impl; | |
2 | + | |
3 | +import com.order.erp.domain.model.EbCategorysRelation; | |
4 | +import com.order.erp.mapper.EbCategorysRelationMapper; | |
5 | +import com.order.erp.service.IEbCategorysRelationService; | |
6 | +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |
7 | +import org.springframework.stereotype.Service; | |
8 | + | |
9 | +/** | |
10 | + * <p> | |
11 | + * 服务实现类 | |
12 | + * </p> | |
13 | + * | |
14 | + * @author author | |
15 | + * @since 2024-11-14 | |
16 | + */ | |
17 | +@Service | |
18 | +public class EbCategorysRelationServiceImpl extends ServiceImpl<EbCategorysRelationMapper, EbCategorysRelation> implements IEbCategorysRelationService { | |
19 | + | |
20 | +} | ... | ... |
src/main/resources/mapper/EbCategorysRelationMapper.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
3 | +<mapper namespace="com.order.erp.mapper.EbCategorysRelationMapper"> | |
4 | + | |
5 | + <!-- 通用查询映射结果 --> | |
6 | + <resultMap id="BaseResultMap" type="com.order.erp.domain.model.EbCategorysRelation"> | |
7 | + <id column="id" property="id" /> | |
8 | + <result column="category_name" property="categoryName" /> | |
9 | + <result column="related_category_name" property="relatedCategoryName" /> | |
10 | + <result column="category_id" property="categoryId" /> | |
11 | + <result column="related_category_id" property="relatedCategoryId" /> | |
12 | + <result column="relevance" property="relevance" /> | |
13 | + </resultMap> | |
14 | + | |
15 | + <!-- 通用查询结果列 --> | |
16 | + <sql id="Base_Column_List"> | |
17 | + id, category_name, related_category_name, category_id, related_category_id, relevance | |
18 | + </sql> | |
19 | + | |
20 | +</mapper> | ... | ... |