master
袁野 7 months ago
parent c94ad1df82
commit 8b947075c0
  1. 65
      bweb/src/main/java/com/bweb/controller/FileUploadController.java
  2. 27
      bweb/src/main/java/com/bweb/controller/goods/GoodsTypeController.java
  3. 15
      service/src/main/java/com/hfkj/model/GoodsTypeModel.java
  4. 11
      service/src/main/java/com/hfkj/service/goods/GoodsTypeService.java
  5. 34
      service/src/main/java/com/hfkj/service/goods/impl/GoodsTypeServiceImpl.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<String> fileNames = new ArrayList<String>();
if (multipartResolver.isMultipart(request)) {
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> 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);
}
}
}

@ -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<String , Object> 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<GoodsType> list = goodsTypeService.getList(map);
return ResponseMsgUtil.success(goodsTypeService.getGoodsTypeModelList(list));
} catch (Exception e) {
log.error("error!",e);

@ -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<GoodsType> children;
}

@ -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<com.hfkj.entity.GoodsType>
*/
List<GoodsType> getList(Map<String , Object> map);
/**
* @Author Sum1Dream
* @Name getGoodsTypeModelList
* @Description // 获取列表
* @Date 15:13 2024/4/19
* @Param map
* @return java.util.List<com.hfkj.entity.GoodsType>
*/
List<GoodsTypeModel> getGoodsTypeModelList(List<GoodsType> list);
}

@ -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<GoodsTypeModel> getGoodsTypeModelList(List<GoodsType> list) {
Map<String , Object> map = new HashMap<>();
List<GoodsTypeModel> goodsTypeModels = new ArrayList<>();
for (GoodsType goodsType : list) {
GoodsTypeModel goodsTypeModel = new GoodsTypeModel();
BeanUtils.copyProperties(goodsType, goodsTypeModel);
map.clear();
map.put("parentId" , goodsType.getId());
List<GoodsType> goodsTypes = getList(map);
if (goodsTypes != null) {
goodsTypeModel.setChildren(goodsTypes);
getGoodsTypeModelList(goodsTypes);
}
goodsTypeModels.add(goodsTypeModel);
}
return goodsTypeModels;
}
}

Loading…
Cancel
Save