From 8b947075c0a194d2fa810159a1929eeaa67e38f1 Mon Sep 17 00:00:00 2001 From: yuanye <418471657@qq.com> Date: Mon, 6 May 2024 16:21:45 +0800 Subject: [PATCH] Changes --- .../bweb/controller/FileUploadController.java | 65 +++++++++++++++++++ .../controller/goods/GoodsTypeController.java | 27 ++++---- .../java/com/hfkj/model/GoodsTypeModel.java | 15 +++++ .../hfkj/service/goods/GoodsTypeService.java | 11 ++++ .../goods/impl/GoodsTypeServiceImpl.java | 34 +++++++++- 5 files changed, 137 insertions(+), 15 deletions(-) create mode 100644 service/src/main/java/com/hfkj/model/GoodsTypeModel.java diff --git a/bweb/src/main/java/com/bweb/controller/FileUploadController.java b/bweb/src/main/java/com/bweb/controller/FileUploadController.java index c595467..7343d82 100644 --- a/bweb/src/main/java/com/bweb/controller/FileUploadController.java +++ b/bweb/src/main/java/com/bweb/controller/FileUploadController.java @@ -7,6 +7,7 @@ import com.hfkj.model.ResponseData; import com.hfkj.service.FileUploadService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import net.coobird.thumbnailator.Thumbnails; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; @@ -18,7 +19,9 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.InputStream; import java.util.*; @RestController @@ -31,6 +34,9 @@ public class FileUploadController { @Resource private SysConfig sysConfig; + @Resource + private FileUploadService fileUploadService; + @RequestMapping(value="/uploadfile",method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "文件上传") @@ -85,5 +91,64 @@ public class FileUploadController { } + @RequestMapping(value = "/fileUpload", method = RequestMethod.POST) + @ApiOperation(value = "上传文件(超过500KB压缩)") + @ResponseBody + public ResponseData fileUpload(@RequestParam(value = "files" , required = false) MultipartFile files, + HttpServletRequest request, + HttpServletResponse response + ) { + try { + response.setHeader("Access-Control-Allow-Origin", "*"); + CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( + request.getSession().getServletContext()); + // 判断 request 是否有文件上传,即多部分请求 + List fileNames = new ArrayList(); + if (multipartResolver.isMultipart(request)) { + // 转换成多部分request + MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; + Iterator iterator = multiRequest.getFileNames(); + + while (iterator.hasNext()) { + MultipartFile file = multiRequest.getFile(iterator.next()); + if (file != null) { + FileOutputStream out = null; + try { + String fileType = file.getOriginalFilename() + .substring(file.getOriginalFilename().lastIndexOf(".") + 1); + String fileName = System.currentTimeMillis() + "." + fileType; + String childPath = DateUtil.date2String(new Date(), "yyyyMM"); + String destDirName = sysConfig.getFileUrl() + File.separator + childPath; + File dir = new File(destDirName); + if (!dir.exists()) { + dir.mkdirs(); + } + out = new FileOutputStream(destDirName + File.separator + fileName); + out.write(file.getBytes()); + out.flush(); + fileNames.add(childPath + "/" + fileName); + // 图片压缩 + InputStream fis = new FileInputStream(destDirName + File.separator + fileName); + if (fis.available() > 500000) { + Thumbnails.of(new FileInputStream(destDirName + File.separator + fileName)).scale(0.5).toFile(new File(destDirName + File.separator + fileName)); + } + } catch (Exception e) { + log.error(e.getMessage(), e); + } finally { + if (out != null) { + out.close(); + } + } + } + } + } + return ResponseMsgUtil.success(fileNames); + + } catch (Exception e) { + log.error(e.getMessage(), e); + return ResponseMsgUtil.exception(e); + } + } + } diff --git a/bweb/src/main/java/com/bweb/controller/goods/GoodsTypeController.java b/bweb/src/main/java/com/bweb/controller/goods/GoodsTypeController.java index b3708fa..6e94287 100644 --- a/bweb/src/main/java/com/bweb/controller/goods/GoodsTypeController.java +++ b/bweb/src/main/java/com/bweb/controller/goods/GoodsTypeController.java @@ -12,18 +12,24 @@ import com.hfkj.entity.SecMenu; import com.hfkj.model.ResponseData; import com.hfkj.model.SecUserSessionObject; import com.hfkj.service.goods.GoodsTypeService; +import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; +@Controller +@RequestMapping(value="/goodsType") +@Api(value="商品类型") public class GoodsTypeController { private static final Logger log = LoggerFactory.getLogger(FileUploadController.class); @@ -33,10 +39,10 @@ public class GoodsTypeController { @Resource private UserCenter userCenter; - @RequestMapping(value="/editMenu",method = RequestMethod.POST) + @RequestMapping(value="/editGoodsType",method = RequestMethod.POST) @ResponseBody - @ApiOperation(value = "编辑菜单") - public ResponseData editMenu(@RequestBody GoodsType body, HttpServletRequest request) { + @ApiOperation(value = "编辑商品类型") + public ResponseData editGoodsType(@RequestBody GoodsType body, HttpServletRequest request) { SessionObject sessionObject = userCenter.getSessionObject(request); SecUserSessionObject userModel = (SecUserSessionObject) sessionObject.getObject(); @@ -62,6 +68,7 @@ public class GoodsTypeController { goodsType.setUpdateTime(new Date()); goodsType.setStatus(1); + goodsType.setTitle(body.getTitle()); goodsType.setParentId(body.getParentId()); goodsType.setBusinessType(body.getBusinessType()); goodsType.setImgUrl(body.getImgUrl()); @@ -80,19 +87,15 @@ public class GoodsTypeController { @RequestMapping(value="/getList",method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "查询列表") - public ResponseData getList( - @RequestParam(value = "parentId" , required = false) Long parentId, - @RequestParam(value = "businessType" , required = false) Integer businessType, - @RequestParam(value = "title" , required = false) String title - ) { + public ResponseData getList() { try { Map map = new HashMap<>(); - map.put("parentId" , parentId); - map.put("businessType" , businessType); - map.put("title" , title); + map.put("parentId" , null); - return ResponseMsgUtil.success(goodsTypeService.getList(map)); + List list = goodsTypeService.getList(map); + + return ResponseMsgUtil.success(goodsTypeService.getGoodsTypeModelList(list)); } catch (Exception e) { log.error("error!",e); diff --git a/service/src/main/java/com/hfkj/model/GoodsTypeModel.java b/service/src/main/java/com/hfkj/model/GoodsTypeModel.java new file mode 100644 index 0000000..fcb4b0e --- /dev/null +++ b/service/src/main/java/com/hfkj/model/GoodsTypeModel.java @@ -0,0 +1,15 @@ +package com.hfkj.model; + +import com.hfkj.entity.GoodsType; +import lombok.Data; + +import java.util.List; + +/** + * 数据类型 + */ +@Data +public class GoodsTypeModel extends GoodsType { + + List children; +} diff --git a/service/src/main/java/com/hfkj/service/goods/GoodsTypeService.java b/service/src/main/java/com/hfkj/service/goods/GoodsTypeService.java index b90f219..bfe14fb 100644 --- a/service/src/main/java/com/hfkj/service/goods/GoodsTypeService.java +++ b/service/src/main/java/com/hfkj/service/goods/GoodsTypeService.java @@ -1,6 +1,7 @@ package com.hfkj.service.goods; import com.hfkj.entity.GoodsType; +import com.hfkj.model.GoodsTypeModel; import java.util.List; import java.util.Map; @@ -73,4 +74,14 @@ public interface GoodsTypeService { * @return java.util.List */ List getList(Map map); + + /** + * @Author Sum1Dream + * @Name getGoodsTypeModelList + * @Description // 获取列表 + * @Date 15:13 2024/4/19 + * @Param map + * @return java.util.List + */ + List getGoodsTypeModelList(List list); } diff --git a/service/src/main/java/com/hfkj/service/goods/impl/GoodsTypeServiceImpl.java b/service/src/main/java/com/hfkj/service/goods/impl/GoodsTypeServiceImpl.java index 49e44aa..5004e2a 100644 --- a/service/src/main/java/com/hfkj/service/goods/impl/GoodsTypeServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/goods/impl/GoodsTypeServiceImpl.java @@ -3,14 +3,14 @@ package com.hfkj.service.goods.impl; import com.hfkj.dao.GoodsTypeMapper; import com.hfkj.entity.GoodsType; import com.hfkj.entity.GoodsTypeExample; +import com.hfkj.model.GoodsTypeModel; import com.hfkj.service.goods.GoodsTypeService; import org.apache.commons.collections4.MapUtils; +import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.Date; -import java.util.List; -import java.util.Map; +import java.util.*; @Service("goodsTypeService") public class GoodsTypeServiceImpl implements GoodsTypeService { @@ -78,6 +78,8 @@ public class GoodsTypeServiceImpl implements GoodsTypeService { if (MapUtils.getLong(map, "parentId") != null) { criteria.andParentIdEqualTo(MapUtils.getLong(map, "parentId")); + } else { + criteria.andParentIdIsNull(); } if (MapUtils.getString(map, "title") != null) { criteria.andTitleEqualTo(MapUtils.getString(map, "title")); @@ -88,4 +90,30 @@ public class GoodsTypeServiceImpl implements GoodsTypeService { return goodsTypeMapper.selectByExample(example); } + + @Override + public List getGoodsTypeModelList(List list) { + + Map map = new HashMap<>(); + + List goodsTypeModels = new ArrayList<>(); + + for (GoodsType goodsType : list) { + GoodsTypeModel goodsTypeModel = new GoodsTypeModel(); + BeanUtils.copyProperties(goodsType, goodsTypeModel); + map.clear(); + map.put("parentId" , goodsType.getId()); + List goodsTypes = getList(map); + + if (goodsTypes != null) { + goodsTypeModel.setChildren(goodsTypes); + getGoodsTypeModelList(goodsTypes); + } + + goodsTypeModels.add(goodsTypeModel); + + } + + return goodsTypeModels; + } }