Commit f8a64fa54e35b06007e3750c8e8c889150d688c8

Authored by zhangxing
2 parents e17741d6 ecf58492
src/main/java/com/diligrp/xtrade/product/common/utils/ImageUtil.java 0 → 100644
  1 +package com.diligrp.xtrade.product.common.utils;
  2 +
  3 +import java.awt.image.BufferedImage;
  4 +import java.io.IOException;
  5 +import java.io.InputStream;
  6 +import java.security.MessageDigest;
  7 +import java.security.NoSuchAlgorithmException;
  8 +
  9 +import javax.imageio.ImageIO;
  10 +
  11 +import com.diligrp.xtrade.product.exception.ExceptionEnum;
  12 +import com.diligrp.xtrade.product.exception.ProductException;
  13 +
  14 +
  15 +/**
  16 + *
  17 + * <B>Description</B> 图片工具类 <br />
  18 + * <B>Copyright</B> Copyright (c) 2015 www.diligrp.com All rights reserved. <br />
  19 + * 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br />
  20 + * <B>Company</B> 地利集团
  21 + * @createTime 2015年4月24日 上午11:30:36
  22 + * @author celine
  23 + */
  24 +public class ImageUtil {
  25 +
  26 + /**
  27 + * 检测文件是否是图片
  28 + * @param inputStream
  29 + * @return
  30 + * @createTime 2015年4月24日 上午11:35:29
  31 + * @author celine
  32 + */
  33 + public static Boolean isImage(InputStream inputStream) {
  34 + try {
  35 + BufferedImage bi = ImageIO.read(inputStream);
  36 + return null == bi ? false : true;
  37 + } catch (IOException e) {
  38 + throw new ProductException(ExceptionEnum.FILE_UP_ERROR);
  39 + }
  40 + }
  41 +
  42 + public static Boolean checkImageSize(InputStream inputStream, int maxWidth, int maxHeight, int minWidth,
  43 + int minHeight) {
  44 + try {
  45 + BufferedImage bi = ImageIO.read(inputStream);
  46 + if (bi != null) {
  47 + if (bi.getWidth() <= maxWidth && bi.getHeight() <= maxHeight && bi.getWidth() >= minWidth
  48 + && bi.getHeight() >= minHeight) {
  49 + return true;
  50 + }
  51 + }
  52 + return false;
  53 + } catch (IOException e) {
  54 + throw new ProductException(ExceptionEnum.FILE_UP_ERROR);
  55 + }
  56 + }
  57 +
  58 + public static String getMd5(byte[] imgBytes) {
  59 + if (imgBytes != null && imgBytes.length > 0) {
  60 + StringBuilder sb = new StringBuilder();
  61 + MessageDigest md5;
  62 + try {
  63 + md5 = MessageDigest.getInstance("MD5");
  64 + } catch (NoSuchAlgorithmException e) {
  65 + throw new ProductException(ExceptionEnum.FILE_UP_ERROR);
  66 + }
  67 + md5.update(imgBytes);
  68 + for (byte by : md5.digest()) {
  69 + sb.append(String.format("%02X", by));
  70 + }
  71 + return sb.toString();
  72 + }
  73 + return "";
  74 + }
  75 +
  76 +}
... ...
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/builder/MerchantBuilder.java
... ... @@ -19,7 +19,7 @@ public class MerchantBuilder {
19 19 public static MerchantDo buildDoFromRequest(MerchantRequestDto requestDto) {
20 20 Integer type = Optional.ofNullable(requestDto.getAccountType())
21 21 .map(AccountType::getCode)
22   - .orElse(0);
  22 + .orElse(null);
23 23 MerchantDo merchantDo = new MerchantDo();
24 24 merchantDo.setMerName(requestDto.getMerchantName());
25 25 merchantDo.setAccountId(requestDto.getAccountId());
... ...
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
... ... @@ -8,4 +8,7 @@ public class ProductException extends BaseBusinessException {
8 8 public ProductException(IEnumType type) {
9 9 super(type);
10 10 }
  11 + public ProductException(int code, String message) {
  12 + super(code,message);
  13 + }
11 14 }
... ...
src/main/java/com/diligrp/xtrade/product/repository/MerchantRepository.java
... ... @@ -42,12 +42,11 @@ public class MerchantRepository implements IMerchantRepository {
42 42 public void add(MerchantDo newMerchantDo) {
43 43 IKeyGenerator keyGenerator = keyGeneratorManager.getKeyGenerator(IKeyGeneratorKeys.MERCHANT_SEQUENCE);
44 44 newMerchantDo.setMerId(keyGenerator.nextId());
45   - newMerchantDo.setMarketId("SY001");
46 45 merchantDao.insert(newMerchantDo);
47 46 }
48 47  
49 48 @Override
50 49 public void update(MerchantDo merchantDo) {
51   -
  50 + merchantDao.update(merchantDo);
52 51 }
53 52 }
... ...
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 }
... ...
src/main/java/com/diligrp/xtrade/product/service/impl/MerchantServiceImpl.java
... ... @@ -18,6 +18,8 @@ import org.springframework.beans.factory.annotation.Autowired;
18 18 import org.springframework.stereotype.Service;
19 19 import org.springframework.transaction.annotation.Transactional;
20 20  
  21 +import java.util.Optional;
  22 +
21 23 @Service
22 24 public class MerchantServiceImpl implements MerchantService {
23 25 @Autowired
... ... @@ -42,11 +44,14 @@ public class MerchantServiceImpl implements MerchantService {
42 44 MerchantDo merchantDo = merchantRepository.getByAccountId(requestDto.getAccountId());
43 45 if (merchantDo == null){
44 46 merchantDo = MerchantBuilder.buildDoFromRequest(requestDto);
  47 + merchantDo.setMarketId("SYSGDL");
45 48 merchantRepository.add(merchantDo);
46 49 } else {
47 50 if (AccountType.PERSONAL.getCode() != merchantDo.getType()){
48 51 throw new ProductException(ExceptionEnum.CARD_MERCHANT_CREATED);
49 52 }
  53 + this.updateMerchant(merchantDo, requestDto);
  54 + merchantRepository.update(merchantDo);
50 55 shopService.updateShopTypeWithMerchant(merchantDo);
51 56 }
52 57 //创建市场管理员账户
... ... @@ -54,4 +59,12 @@ public class MerchantServiceImpl implements MerchantService {
54 59 return MerchantBuilder.buildResponseFromDo(merchantDo);
55 60 }
56 61  
  62 + private void updateMerchant(MerchantDo merchantDo, MerchantRequestDto requestDto) {
  63 + Integer accountType = Optional.ofNullable(requestDto.getAccountType())
  64 + .map(AccountType::getCode).orElse(null);
  65 + merchantDo.setMerName(requestDto.getMerchantName());
  66 + merchantDo.setAccountId(requestDto.getAccountId());
  67 + merchantDo.setType(accountType);
  68 + merchantDo.setDescription(requestDto.getAddress());
  69 + }
57 70 }
... ...