diff --git a/pom.xml b/pom.xml index 0a2dbbbf346c2823479080a4464cd7c41123c931..fb2565b3da61637b9500284003fcf92b83411343 100644 --- a/pom.xml +++ b/pom.xml @@ -160,6 +160,30 @@ ${slf4j.version} + + + commons-fileupload + commons-fileupload + 1.3.1 + + + commons-io + commons-io + 2.5 + + + org.apache.poi + poi + 3.7 + + + org.apache.poi + poi-ooxml + 3.7 + + + + ch.qos.logback diff --git a/src/main/java/com/hxtec/polaris/commons/constant/GlobalVar.java b/src/main/java/com/hxtec/polaris/commons/constant/GlobalVar.java index 9ac404ed4526cf88e3b34c4c53b695300e6b1cce..18a54c7fa9fb83e3aedeeb7c3533399e0f390993 100644 --- a/src/main/java/com/hxtec/polaris/commons/constant/GlobalVar.java +++ b/src/main/java/com/hxtec/polaris/commons/constant/GlobalVar.java @@ -1,5 +1,6 @@ package com.hxtec.polaris.commons.constant; +import java.io.File; import java.util.Arrays; import java.util.List; @@ -55,4 +56,7 @@ public final class GlobalVar { //延时收货时间 默认为3天 public static final int DELAY_TIME = 3; + + //上传文件存储路径 + public static final String CATEGORY_IMG_PATH = File.separator +"upload" + File.separator + "category"; } diff --git a/src/main/java/com/hxtec/polaris/controller/CategoryController.java b/src/main/java/com/hxtec/polaris/controller/CategoryController.java index 22e7b42c45447376231639e080c420cba0492991..2ad649ea1285dfc1dd0630b2117c0e4e526e289e 100644 --- a/src/main/java/com/hxtec/polaris/controller/CategoryController.java +++ b/src/main/java/com/hxtec/polaris/controller/CategoryController.java @@ -74,8 +74,8 @@ public class CategoryController { @ApiImplicitParam(name = "name", value = "当前节点编辑后的节点名", required = false, dataType = "String"), }) @PutMapping("update") - public Object updateCategory(HttpServletRequest request,String id, String pid, String name){ - return categoryService.updateCategory(request,pid,id,name); + public Object updateCategory(HttpServletRequest request,String id, String pid, String name,String img){ + return categoryService.updateCategory(request,pid,id,name,img); } /** @@ -84,7 +84,17 @@ public class CategoryController { * @return */ @GetMapping("buildTree") - public Object buildTree(Integer id){ + public Object buildTree(Integer id,String condition){ return categoryService.buildTree(id); } + + /** + * 搜索tree + * @param condition + * @return + */ + @GetMapping("selectTree") + public Object selectTree(String condition){ + return categoryService.selectTree(condition); + } } diff --git a/src/main/java/com/hxtec/polaris/controller/FileController.java b/src/main/java/com/hxtec/polaris/controller/FileController.java new file mode 100644 index 0000000000000000000000000000000000000000..cb71916b4e63d74e088abefeed11943440208550 --- /dev/null +++ b/src/main/java/com/hxtec/polaris/controller/FileController.java @@ -0,0 +1,34 @@ +package com.hxtec.polaris.controller; + +import com.hxtec.polaris.service.FileService; +import io.swagger.annotations.Api; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; + +/** + * @Describtion 文件服务 + * @Author yonyong + * @Date 2019/12/18 14:23 + * @Version 1.0.0 + **/ +@RestController +@RequestMapping(value = "file",produces = "application/json;charset=utf-8") +@CrossOrigin +@Api(value = "/file", tags = "文件服务") +public class FileController { + + @Autowired + private FileService fileService; + + /** + * 上传分类图片 + * @return + */ + @PostMapping("/upload/category/image") + public Object uploadCategoryImage(MultipartFile file, HttpServletRequest request){ + return fileService.uploadCategoryImage(file,request); + } +} diff --git a/src/main/java/com/hxtec/polaris/mapper/ShopCategoryMapper.java b/src/main/java/com/hxtec/polaris/mapper/ShopCategoryMapper.java index e6c8d806827cec7c3c56a2f62b0247a02d2ab2a1..ce2ae8e9c644c0bf42cb8bba097be9287c129f77 100644 --- a/src/main/java/com/hxtec/polaris/mapper/ShopCategoryMapper.java +++ b/src/main/java/com/hxtec/polaris/mapper/ShopCategoryMapper.java @@ -50,9 +50,10 @@ public interface ShopCategoryMapper extends MyMapper { * 更新父id * @param id * @param pid + * @param img * @return */ - Integer updateCategoryParentId(@Param("id") String id,@Param("pid") String pid,@Param("name") String name); + Integer updateCategoryParentId(@Param("id") String id,@Param("pid") String pid,@Param("name") String name,@Param("img") String img); /** * 更新父id @@ -89,4 +90,11 @@ public interface ShopCategoryMapper extends MyMapper { * @return */ List> getChildTree(Integer id); + + /** + * 搜索分类 + * @param condition + * @return + */ + List> selectTree(@Param("condition") String condition); } \ No newline at end of file diff --git a/src/main/java/com/hxtec/polaris/service/CategoryService.java b/src/main/java/com/hxtec/polaris/service/CategoryService.java index 42200fdfc453a31117999b4d514e7aaee6c2e1f6..d766e5fbb2bb4100c47e9b0765d7caf4f1332c6d 100644 --- a/src/main/java/com/hxtec/polaris/service/CategoryService.java +++ b/src/main/java/com/hxtec/polaris/service/CategoryService.java @@ -32,9 +32,10 @@ public interface CategoryService { * @param pid * @param id * @param name + * @param img * @return */ - Object updateCategory(HttpServletRequest request, String pid, String id, String name); + Object updateCategory(HttpServletRequest request, String pid, String id, String name,String img); /** * 构建分类tree @@ -42,4 +43,11 @@ public interface CategoryService { * @return */ Object buildTree(Integer id); + + /** + * 搜索tree + * @param condition + * @return + */ + Object selectTree(String condition); } diff --git a/src/main/java/com/hxtec/polaris/service/FileService.java b/src/main/java/com/hxtec/polaris/service/FileService.java new file mode 100644 index 0000000000000000000000000000000000000000..cc2549577266dff776fa9b16ef92ebf587bb2ea8 --- /dev/null +++ b/src/main/java/com/hxtec/polaris/service/FileService.java @@ -0,0 +1,23 @@ +package com.hxtec.polaris.service; + +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; + +/** + * @Author yonyong + * @Description //文件服务 + * @Date 2019/12/18 14:37 + * @Param + * @return + **/ +public interface FileService { + + /** + * 上传分类图片 + * @param img + * @param request + * @return + */ + Object uploadCategoryImage(MultipartFile img, HttpServletRequest request); +} diff --git a/src/main/java/com/hxtec/polaris/service/impl/CategoryServiceImpl.java b/src/main/java/com/hxtec/polaris/service/impl/CategoryServiceImpl.java index 53d7de27d0428115a07664bdcf9750b78c405e79..de437a871da9fa2101ba83b33f66f3319fed2f50 100644 --- a/src/main/java/com/hxtec/polaris/service/impl/CategoryServiceImpl.java +++ b/src/main/java/com/hxtec/polaris/service/impl/CategoryServiceImpl.java @@ -113,10 +113,10 @@ public class CategoryServiceImpl implements CategoryService { } @Override - public Object updateCategory(HttpServletRequest request,String pid, String id, String name) { + public Object updateCategory(HttpServletRequest request,String pid, String id, String name,String img) { String params = "pid="+pid+",id="+id+",name="+name; String LOG_MSG = MessageFormat.format(Log.PATTERN_LOG,Log.UPDATE,LOG_CLASS_NAME,LOG_METHOD_UPDATE_CATORY,params); - if (!StringUtils.isNumeric(pid) || !StringUtils.isNumeric(id) || StringUtils.isBlank(name)){ + if (!StringUtils.isNumeric(id) || StringUtils.isBlank(name)){ throw new MyException(Result.error(Code.FAIL_4101,Msg.PARAM_INVALID),LOG_MSG); } @@ -127,7 +127,7 @@ public class CategoryServiceImpl implements CategoryService { } //执行更新操作 try { - doUpdateShopCategory(request,pid,id,name); + doUpdateShopCategory(request,pid,id,name,img); }catch (Exception e){ throw new MyException(Result.error(Code.FAIL_4101,Msg.CATEGORY_UPDATE_FAIL),LOG_MSG); } @@ -148,6 +148,12 @@ public class CategoryServiceImpl implements CategoryService { } } + @Override + public Object selectTree(String condition) { + List> mapList = shopCategoryMapper.selectTree(condition); + return Result.ok(mapList); + } + /** * ShopCategory实例赋值 * @param pid @@ -174,13 +180,13 @@ public class CategoryServiceImpl implements CategoryService { * @param name * @return */ - private void doUpdateShopCategory(HttpServletRequest request,String pid, String id, String name){ + private void doUpdateShopCategory(HttpServletRequest request,String pid, String id, String name,String img){ //编辑的节点原来的父节点 List shopCategorys = shopCategoryMapper.getCategoryById(id); String oldParentId = shopCategorys.get(0).getParentId(); //step1 将当前节点的父节点和名字更新为修改后的值 - shopCategoryMapper.updateCategoryParentId(id,pid,name); + shopCategoryMapper.updateCategoryParentId(id,pid,name,img); //如果上级节点没有变动,则不需要变更子节点,业务结束 if (equalsIgnoreCase(pid,oldParentId)){ diff --git a/src/main/java/com/hxtec/polaris/service/impl/FileServiceImpl.java b/src/main/java/com/hxtec/polaris/service/impl/FileServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..61a12624c0646794c7759688ae4e43de81edc3b3 --- /dev/null +++ b/src/main/java/com/hxtec/polaris/service/impl/FileServiceImpl.java @@ -0,0 +1,66 @@ +package com.hxtec.polaris.service.impl; + +import com.hxtec.polaris.commons.api.vo.Result; +import com.hxtec.polaris.commons.constant.Code; +import com.hxtec.polaris.commons.constant.GlobalVar; +import com.hxtec.polaris.commons.constant.Log; +import com.hxtec.polaris.commons.constant.Msg; +import com.hxtec.polaris.commons.exception.MyException; +import com.hxtec.polaris.service.FileService; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import org.apache.commons.io.FileUtils; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.text.MessageFormat; +import java.util.Map; + +/** + * @Describtion 文件服务 + * @Author yonyong + * @Date 2019/12/18 14:35 + * @Version 1.0.0 + **/ +@Service +@Transactional +public class FileServiceImpl implements FileService{ + + private static final String LOG_CLASS_NAME = "FileServiceImpl"; + private static final String LOG_METHOD_UPLOAD_CATEGORY= "uploadCategoryImage"; + + @Override + public Object uploadCategoryImage(MultipartFile img, HttpServletRequest request) { + try { + return Result.ok(saveImg(img,request,GlobalVar.CATEGORY_IMG_PATH)); + }catch (Exception e){ + String params = "img="+img; + String LOG_MSG = MessageFormat.format(Log.PATTERN_LOG,Log.INSERT,LOG_CLASS_NAME,LOG_METHOD_UPLOAD_CATEGORY,params); + throw new MyException(Result.error(Code.FAIL_4501, Msg.COMMON_FAIL),LOG_MSG); + } + } + + /** + * 保存文件到指定目录,并返回路径 + * @param img + * @param request + * @param categoryImgPath + * @return + */ + private String saveImg(MultipartFile img, HttpServletRequest request, String categoryImgPath) throws IOException { + String path = "error"; + if(!img.isEmpty()) { + //原本的文件名 + //现在的文件名是时间戳加原文件名,出现图片相同时,读取不出来的bug + String nowName = System.currentTimeMillis() + img.getOriginalFilename(); + //将文件保存在当前工程下的一个upload文件 + String realPath = request.getSession().getServletContext().getRealPath(categoryImgPath); + //将文件的输入流输出到一个新的文件 + FileUtils.copyInputStreamToFile(img.getInputStream(), new File(realPath, nowName)); + path=categoryImgPath+nowName; + } + return path; + } +} diff --git a/src/main/resources/mapper/ShopCategoryMapper.xml b/src/main/resources/mapper/ShopCategoryMapper.xml index 130cc06f4d85791d8137459fd19ca8ef9fe7843e..ba600779b68b04caaee205ea14a1d1a1a3e4f07c 100644 --- a/src/main/resources/mapper/ShopCategoryMapper.xml +++ b/src/main/resources/mapper/ShopCategoryMapper.xml @@ -25,6 +25,10 @@ id, parent_id, is_parent, name,image, weight,is_delete + + * + + + + insert into shop_category (parent_id, is_parent, name, decriptsion, image, @@ -122,7 +134,7 @@ update shop_category - set parent_id = #{pid},name = #{name},is_parent = 'N' + set parent_id = #{pid},name = #{name},image=#{img},is_parent = 'N' where id = #{id} and is_delete = 'N'