Commit 5f41a9726387c1e78e2561c0e925910fbab0027d
1 parent
40e23572
category 图片上传
Showing
7 changed files
with
130 additions
and
9 deletions
src/main/java/com/diligrp/xtrade/product/controllor/CategoryController.java
1 | 1 | package com.diligrp.xtrade.product.controllor; |
2 | 2 | |
3 | +import java.io.IOException; | |
3 | 4 | import java.util.List; |
4 | 5 | |
5 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
7 | +import org.springframework.beans.factory.annotation.Value; | |
6 | 8 | import org.springframework.web.bind.annotation.PathVariable; |
9 | +import org.springframework.web.bind.annotation.RequestBody; | |
7 | 10 | import org.springframework.web.bind.annotation.RequestMapping; |
11 | +import org.springframework.web.bind.annotation.RequestMethod; | |
12 | +import org.springframework.web.bind.annotation.RequestParam; | |
8 | 13 | import org.springframework.web.bind.annotation.RestController; |
14 | +import org.springframework.web.multipart.MultipartFile; | |
9 | 15 | |
16 | +import com.diligrp.xtrade.product.common.utils.ImageUtil; | |
10 | 17 | import com.diligrp.xtrade.product.domain.dto.CategoryDto; |
11 | 18 | import com.diligrp.xtrade.product.domain.entity.CategoryDo; |
19 | +import com.diligrp.xtrade.product.exception.ExceptionEnum; | |
20 | +import com.diligrp.xtrade.product.exception.ProductException; | |
12 | 21 | import com.diligrp.xtrade.product.service.CategoryService; |
13 | 22 | import com.diligrp.xtrade.shared.domain.Message; |
14 | 23 | |
... | ... | @@ -21,7 +30,15 @@ import com.diligrp.xtrade.shared.domain.Message; |
21 | 30 | @RestController |
22 | 31 | @RequestMapping("sapi/category/") |
23 | 32 | public class CategoryController { |
33 | + | |
34 | + @Value("${max.file.size}") | |
35 | + private int MAX_FILE_SIZE; | |
24 | 36 | |
37 | + private final Integer MAX_WIDTH = 300; | |
38 | + private final Integer MAX_HEIGHT = 300; | |
39 | + private final Integer MIN_WIDTH = 200; | |
40 | + private final Integer MIN_HEIGHT = 200; | |
41 | + | |
25 | 42 | @Autowired |
26 | 43 | private CategoryService categoryService; |
27 | 44 | |
... | ... | @@ -33,7 +50,14 @@ public class CategoryController { |
33 | 50 | * @return |
34 | 51 | * @throws |
35 | 52 | */ |
36 | - public Message<?> save(CategoryDto categoryDTO) { | |
53 | + @RequestMapping("save") | |
54 | + public Message<?> save(@RequestBody CategoryDto categoryDTO,@RequestParam(value = "file",required = false) MultipartFile file) { | |
55 | + checkImageFile(file); | |
56 | + try { | |
57 | + categoryDTO.setImage(file.getBytes()); | |
58 | + } catch (IOException e) { | |
59 | + throw new ProductException(ExceptionEnum.FILE_UP_ERROR); | |
60 | + } | |
37 | 61 | categoryService.insert(categoryDTO); |
38 | 62 | return Message.success(); |
39 | 63 | } |
... | ... | @@ -93,4 +117,29 @@ public class CategoryController { |
93 | 117 | List<CategoryDo> categoryDos = categoryService.selectCateChild(cateCode); |
94 | 118 | return Message.success(categoryDos); |
95 | 119 | } |
120 | + | |
121 | + /** | |
122 | + * | |
123 | + * @Title checkImageFile | |
124 | + * @Description 文件检查 | |
125 | + * @param file | |
126 | + * @throws | |
127 | + */ | |
128 | + private void checkImageFile(MultipartFile file) { | |
129 | + try { | |
130 | + if (!ImageUtil.isImage(file.getInputStream())) { | |
131 | + throw new ProductException(ExceptionEnum.NOT_IMAGE); | |
132 | + } | |
133 | + if (!ImageUtil.checkImageSize(file.getInputStream(), MAX_WIDTH, MAX_HEIGHT, MIN_WIDTH, MIN_HEIGHT)) { | |
134 | + | |
135 | + throw new ProductException(200005,"上传图片长宽超过限制,请保证图片大小范围为" + MIN_WIDTH + "*" + MIN_HEIGHT + "到" + MAX_WIDTH + "*" | |
136 | + + MAX_HEIGHT); | |
137 | + } | |
138 | + if (file.getBytes().length > MAX_FILE_SIZE) { | |
139 | + new ProductException(200005,"上传图片大小超过限制,请保证图片不超过256K"); | |
140 | + } | |
141 | + } catch (IOException e) { | |
142 | + throw new ProductException(ExceptionEnum.FILE_UP_ERROR); | |
143 | + } | |
144 | + } | |
96 | 145 | } | ... | ... |
src/main/java/com/diligrp/xtrade/product/domain/dto/CategoryDto.java
... | ... | @@ -37,7 +37,7 @@ public class CategoryDto{ |
37 | 37 | /** |
38 | 38 | * 图片 |
39 | 39 | */ |
40 | - private String image; | |
40 | + private byte[] image; | |
41 | 41 | /** |
42 | 42 | * 品类有效期 |
43 | 43 | */ |
... | ... | @@ -50,7 +50,10 @@ public class CategoryDto{ |
50 | 50 | * 品类等级 |
51 | 51 | */ |
52 | 52 | private Integer level; |
53 | - | |
53 | + /** | |
54 | + * 市场id | |
55 | + */ | |
56 | + private Integer marketId; | |
54 | 57 | public String getParentId() { |
55 | 58 | return parentId; |
56 | 59 | } |
... | ... | @@ -93,10 +96,11 @@ public class CategoryDto{ |
93 | 96 | public void setIcon(String icon) { |
94 | 97 | this.icon = icon; |
95 | 98 | } |
96 | - public String getImage() { | |
99 | + | |
100 | + public byte[] getImage() { | |
97 | 101 | return image; |
98 | 102 | } |
99 | - public void setImage(String image) { | |
103 | + public void setImage(byte[] image) { | |
100 | 104 | this.image = image; |
101 | 105 | } |
102 | 106 | public Integer getValidDay() { |
... | ... | @@ -117,5 +121,11 @@ public class CategoryDto{ |
117 | 121 | public void setLevel(Integer level) { |
118 | 122 | this.level = level; |
119 | 123 | } |
124 | + public Integer getMarketId() { | |
125 | + return marketId; | |
126 | + } | |
127 | + public void setMarketId(Integer marketId) { | |
128 | + this.marketId = marketId; | |
129 | + } | |
120 | 130 | |
121 | 131 | } | ... | ... |
src/main/java/com/diligrp/xtrade/product/domain/dto/CategoryQueryDto.java
... | ... | @@ -34,6 +34,10 @@ public class CategoryQueryDto{ |
34 | 34 | * 品类等级 |
35 | 35 | */ |
36 | 36 | private Integer level; |
37 | + /** | |
38 | + * 市场id | |
39 | + */ | |
40 | + private Integer marketId; | |
37 | 41 | |
38 | 42 | private Integer currentPage; |
39 | 43 | |
... | ... | @@ -79,5 +83,11 @@ public class CategoryQueryDto{ |
79 | 83 | public void setCurrentPage(Integer currentPage) { |
80 | 84 | this.currentPage = currentPage; |
81 | 85 | } |
86 | + public Integer getMarketId() { | |
87 | + return marketId; | |
88 | + } | |
89 | + public void setMarketId(Integer marketId) { | |
90 | + this.marketId = marketId; | |
91 | + } | |
82 | 92 | |
83 | 93 | } | ... | ... |
src/main/java/com/diligrp/xtrade/product/domain/entity/CategoryDo.java
... | ... | @@ -36,7 +36,7 @@ public class CategoryDo extends BaseDo{ |
36 | 36 | /** |
37 | 37 | * 图片 |
38 | 38 | */ |
39 | - private String image; | |
39 | + private byte[] image; | |
40 | 40 | /** |
41 | 41 | * 品类有效期 |
42 | 42 | */ |
... | ... | @@ -49,6 +49,10 @@ public class CategoryDo extends BaseDo{ |
49 | 49 | * 品类等级 |
50 | 50 | */ |
51 | 51 | private Integer level; |
52 | + /** | |
53 | + * 市场id | |
54 | + */ | |
55 | + private Integer marketId; | |
52 | 56 | |
53 | 57 | public String getCateCode() { |
54 | 58 | return cateCode; |
... | ... | @@ -86,10 +90,11 @@ public class CategoryDo extends BaseDo{ |
86 | 90 | public void setIcon(String icon) { |
87 | 91 | this.icon = icon; |
88 | 92 | } |
89 | - public String getImage() { | |
93 | + | |
94 | + public byte[] getImage() { | |
90 | 95 | return image; |
91 | 96 | } |
92 | - public void setImage(String image) { | |
97 | + public void setImage(byte[] image) { | |
93 | 98 | this.image = image; |
94 | 99 | } |
95 | 100 | public Integer getValidDay() { |
... | ... | @@ -110,5 +115,11 @@ public class CategoryDo extends BaseDo{ |
110 | 115 | public void setLevel(Integer level) { |
111 | 116 | this.level = level; |
112 | 117 | } |
118 | + public Integer getMarketId() { | |
119 | + return marketId; | |
120 | + } | |
121 | + public void setMarketId(Integer marketId) { | |
122 | + this.marketId = marketId; | |
123 | + } | |
113 | 124 | |
114 | 125 | } | ... | ... |
src/main/java/com/diligrp/xtrade/product/exception/ExceptionEnum.java
... | ... | @@ -2,6 +2,8 @@ package com.diligrp.xtrade.product.exception; |
2 | 2 | |
3 | 3 | import java.util.Arrays; |
4 | 4 | |
5 | +import javax.validation.constraints.NotBlank; | |
6 | + | |
5 | 7 | import com.diligrp.xtrade.shared.type.IEnumType; |
6 | 8 | |
7 | 9 | /** |
... | ... | @@ -14,6 +16,9 @@ public enum ExceptionEnum implements IEnumType { |
14 | 16 | SHOP_NOT_EXISTENCE(200001, "SHOP_NOT_EXISTENCE"), |
15 | 17 | MERCHANT_ACCOUNT_EXIST(200002,"商户账号已存在"), |
16 | 18 | CARD_MERCHANT_CREATED(200003,"该卡已创建商户,请使用用户名密码登录"), |
19 | + CATE_EXISTENCE(200004,"品类已存在"), | |
20 | + FILE_UP_ERROR(200005,"文件上传失败"), | |
21 | + NOT_IMAGE(200006,"只能上传图片") | |
17 | 22 | ; |
18 | 23 | |
19 | 24 | private int code; | ... | ... |
src/main/java/com/diligrp/xtrade/product/exception/ProductException.java
src/main/java/com/diligrp/xtrade/product/service/impl/CategoryServiceImpl.java
... | ... | @@ -7,11 +7,14 @@ import org.springframework.beans.BeanUtils; |
7 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
8 | 8 | import org.springframework.stereotype.Service; |
9 | 9 | |
10 | +import com.diligrp.xtrade.product.common.utils.ImageUtil; | |
10 | 11 | import com.diligrp.xtrade.product.dao.CategoryDao; |
11 | 12 | import com.diligrp.xtrade.product.domain.dto.CategoryDto; |
12 | 13 | import com.diligrp.xtrade.product.domain.dto.CategoryQueryDto; |
13 | 14 | import com.diligrp.xtrade.product.domain.emuns.IKeyGeneratorKeys; |
14 | 15 | import com.diligrp.xtrade.product.domain.entity.CategoryDo; |
16 | +import com.diligrp.xtrade.product.exception.ExceptionEnum; | |
17 | +import com.diligrp.xtrade.product.exception.ProductException; | |
15 | 18 | import com.diligrp.xtrade.product.service.CategoryService; |
16 | 19 | import com.diligrp.xtrade.shared.sequence.KeyGeneratorManager; |
17 | 20 | |
... | ... | @@ -29,9 +32,15 @@ public class CategoryServiceImpl implements CategoryService { |
29 | 32 | @Autowired |
30 | 33 | private CategoryDao categoryDao; |
31 | 34 | |
35 | + private static String ICON_PREFIX = "/mobile/category/getImageByIcon/"; | |
36 | + | |
32 | 37 | @Override |
33 | 38 | public void insert(CategoryDto category) { |
34 | 39 | CategoryDo categoryDo = new CategoryDo(); |
40 | + // 查询品类名是否存在 | |
41 | + if(categoryIsExit(category)) { | |
42 | + throw new ProductException(ExceptionEnum.CATE_EXISTENCE); | |
43 | + } | |
35 | 44 | BeanUtils.copyProperties(category, categoryDo); |
36 | 45 | long id = keyGeneratorManager.getKeyGenerator(IKeyGeneratorKeys.CATEGORY_SEQUENCE).nextId(); |
37 | 46 | String cateCode = ""+id; |
... | ... | @@ -41,12 +50,17 @@ public class CategoryServiceImpl implements CategoryService { |
41 | 50 | cateCode = String.format("%s_%d", pCategoryDo.getCateCode(),id); |
42 | 51 | } |
43 | 52 | categoryDo.setCateCode(cateCode); |
53 | + category.setIcon(ICON_PREFIX + ImageUtil.getMd5(categoryDo.getImage()) + id); | |
44 | 54 | categoryDao.insert(categoryDo); |
45 | 55 | } |
46 | 56 | |
47 | 57 | @Override |
48 | 58 | public void update(CategoryDto category) { |
49 | 59 | CategoryDo categoryDo = new CategoryDo(); |
60 | + // 查询品类名是否存在 | |
61 | + if(categoryIsExit(category)) { | |
62 | + throw new ProductException(ExceptionEnum.CATE_EXISTENCE); | |
63 | + } | |
50 | 64 | BeanUtils.copyProperties(category, categoryDo); |
51 | 65 | categoryDao.update(categoryDo); |
52 | 66 | } |
... | ... | @@ -70,5 +84,24 @@ public class CategoryServiceImpl implements CategoryService { |
70 | 84 | public List<CategoryDo> selectCateChild(String cateCode) { |
71 | 85 | return categoryDao.selectCateChild(cateCode); |
72 | 86 | } |
73 | - | |
87 | + | |
88 | + /** | |
89 | + * 根据品类名称,等级,市场查询品类名是否重复 | |
90 | + * @Title categoryIsExit | |
91 | + * @Description 根据品类名称,等级,市场查询品类名是否重复 | |
92 | + * @param category | |
93 | + * @return true/false | |
94 | + * @throws | |
95 | + */ | |
96 | + private boolean categoryIsExit(CategoryDto category) { | |
97 | + CategoryQueryDto categoryQueryDto = new CategoryQueryDto(); | |
98 | + categoryQueryDto.setCname(category.getCname()); | |
99 | + categoryQueryDto.setLevel(category.getLevel()); | |
100 | + categoryQueryDto.setMarketId(category.getMarketId()); | |
101 | + List<CategoryDo> categorys = categoryDao.selectList(categoryQueryDto); | |
102 | + if(categorys != null && categorys.size() > 0) { | |
103 | + return true; | |
104 | + } | |
105 | + return false; | |
106 | + } | |
74 | 107 | } | ... | ... |