Browse Source

添加文件上传通用接口

5 years ago
parent
commit
d3f515c1d4

+ 54 - 12
pcbms/src/main/java/com/hssx/pcbms/controller/GoodsController.java

@@ -6,10 +6,13 @@ import com.hssx.pcbms.entity.GoodsNo;
 import com.hssx.pcbms.entity.User;
 import com.hssx.pcbms.entity.vo.GoodsVO;
 import com.hssx.pcbms.service.GoodsService;
+import com.hssx.pcbms.util.FileUtil;
 import com.hssx.pcbms.util.HttpRespMsg;
 import com.hssx.pcbms.util.PageUtil;
+import com.hssx.pcbms.util.UploadFileToFileNameUtil;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 
@@ -31,22 +34,48 @@ public class GoodsController {
 
     @Autowired
     private GoodsService goodsService;
+    @Value("${upload.path}")
+    private String path;
 
     /**
      * 资产录入
      * 参数:
      * userId :当前登录人的id
      * name :名称,tagId:标签id,modelNumber:模具型号,company:公司
-     * ,address:地址,factory:生产厂家,file:产品图片文件,serviceLife:服务期限(天),
+     * ,address:地址,factory:生产厂家,pic:产品图片文件路径,serviceLife:服务期限(天),
      * nextIndate:下次维护日期,number:数量,deptStr:部门编号前缀,goodStr:产品编号前缀
      * 注:产品编号=部门编号前缀+产品编号前缀+随机数
+     *
      * @return
      */
     @ApiOperation(value = "资产录入", notes = "资产录入方法")
     @RequestMapping("/add")
     @ResponseBody
-    public HttpRespMsg add(GoodsVO goodsVO, @RequestParam(required = false) MultipartFile file) {
-        HttpRespMsg msg = goodsService.add(goodsVO,file);
+    public HttpRespMsg add(GoodsVO goodsVO) {
+        HttpRespMsg msg = goodsService.add(goodsVO);
+        return msg;
+    }
+
+    /**
+     * 图片上传通用接口
+     * 参数:
+     * file:产品图片文件
+     *
+     * @return
+     */
+    @ApiOperation(value = "图片上传通用接口", notes = "图片上传通用接口方法")
+    @RequestMapping("/uploadFile")
+    @ResponseBody
+    public HttpRespMsg uploadFile(MultipartFile file) {
+        HttpRespMsg msg = new HttpRespMsg();
+        String filePath = "";
+        if (file != null) {
+            filePath = UploadFileToFileNameUtil.uploadFile(file, path);
+        }
+        if(filePath.equals("")){
+            msg.setError("上传图片失败了");
+        }
+        msg.data = filePath;
         return msg;
     }
 
@@ -54,15 +83,16 @@ public class GoodsController {
      * 资产基本信息修改
      * 参数:id:资产信息id
      * name :名称,tagId:标签id,modelNumber:模具型号,company:公司
-     * ,address:地址,factory:生产厂家,file:产品图片文件,serviceLife:服务期限(天),
-     * nextIndate:下次维护日期
+     * ,address:地址,factory:生产厂家,pic:产品图片文件路径,serviceLife:服务期限(天),
+     * nextIndate:下次维护日期(以上信息除了id外未修改的均不传)
+     *
      * @return
      */
     @ApiOperation(value = "资产基本信息修改", notes = "资产基本信息修改方法")
     @RequestMapping("/update")
     @ResponseBody
     public HttpRespMsg updateInfo(Goods goods, @RequestParam(required = false) MultipartFile file) {
-        HttpRespMsg msg = goodsService.updateInfo(goods,file);
+        HttpRespMsg msg = goodsService.updateInfo(goods, file);
         return msg;
     }
 
@@ -71,18 +101,21 @@ public class GoodsController {
      * 资产列表
      * 参数:
      * name :名称,tagId:标签id,keyName:关键字,pageNum:当前页,pageSize:每页多少条
+     *
      * @return
      */
     @ApiOperation(value = "资产列表", notes = "资产列表方法")
     @RequestMapping("/list")
     @ResponseBody
-    public HttpRespMsg list(PageUtil page,@RequestParam(required = false)String keyName,Integer tagId) {
-        HttpRespMsg msg = goodsService.getList(page,keyName,tagId);
+    public HttpRespMsg list(PageUtil page, @RequestParam(required = false) String keyName, Integer tagId) {
+        HttpRespMsg msg = goodsService.getList(page, keyName, tagId);
         return msg;
     }
+
     /**
      * 资产详情
      * 参数:id:资产id
+     *
      * @return
      */
     @ApiOperation(value = "资产详情", notes = "资产详情方法")
@@ -92,9 +125,11 @@ public class GoodsController {
         HttpRespMsg msg = goodsService.getDetail(id);
         return msg;
     }
+
     /**
      * 分配资产
      * 参数:goodsId:资产id ,divisionCode 部门编号,suffixCode 字母前缀
+     *
      * @return
      */
     @ApiOperation(value = "分配资产", notes = "分配资产方法")
@@ -108,6 +143,7 @@ public class GoodsController {
     /**
      * 资产基本信息的删除
      * 参数:id:资产基本信息id
+     *
      * @return
      */
     @ApiOperation(value = "资产基本信息的删除", notes = "资产的删除方法")
@@ -121,6 +157,7 @@ public class GoodsController {
     /**
      * 资产编号的删除
      * 参数:id:资产编号id
+     *
      * @return
      */
     @ApiOperation(value = "资产的删除", notes = "资产的删除方法")
@@ -136,13 +173,14 @@ public class GoodsController {
      * 参数:
      * id:对应编号的id,state:资产状态0-空闲 1-在用 3-报废
      * userId :对应人员的id(不选传0)
+     *
      * @return
      */
     @ApiOperation(value = "处置资产", notes = "处置资产方法")
     @RequestMapping("/handle")
     @ResponseBody
-    public HttpRespMsg handle(GoodsNo goodsNo,Integer currentUserId) {
-        HttpRespMsg msg = goodsService.handle(goodsNo,currentUserId);
+    public HttpRespMsg handle(GoodsNo goodsNo, Integer currentUserId) {
+        HttpRespMsg msg = goodsService.handle(goodsNo, currentUserId);
         return msg;
     }
 
@@ -150,6 +188,7 @@ public class GoodsController {
      * 资产编号详细
      * 参数:
      * id:对应编号的id
+     *
      * @return
      */
     @ApiOperation(value = "资产编号详细", notes = "资产编号详细方法")
@@ -164,6 +203,7 @@ public class GoodsController {
      * 资产编号删除
      * 参数:
      * id:对应编号的id
+     *
      * @return
      */
     @ApiOperation(value = "资产编号删除", notes = "资产编号删除方法")
@@ -178,13 +218,14 @@ public class GoodsController {
      * 资产编号添加
      * 参数:
      * id:资产的goodId
+     *
      * @return
      */
     @ApiOperation(value = "资产编号添加", notes = "资产编号添加方法")
     @RequestMapping("/goodsNoAdd")
     @ResponseBody
-    public HttpRespMsg goodsNoAdd(GoodsNo goodsNo,Integer count) {
-        HttpRespMsg msg = goodsService.goodsNoAdd(goodsNo,count);
+    public HttpRespMsg goodsNoAdd(GoodsNo goodsNo, Integer count) {
+        HttpRespMsg msg = goodsService.goodsNoAdd(goodsNo, count);
         return msg;
     }
 
@@ -192,6 +233,7 @@ public class GoodsController {
      * 处置资产操作记录
      * 参数:
      * id:资产id,
+     *
      * @return
      */
     @ApiOperation(value = "处置资产操作记录", notes = "处置资产操作记录方法")

+ 1 - 1
pcbms/src/main/java/com/hssx/pcbms/service/GoodsService.java

@@ -18,7 +18,7 @@ import org.springframework.web.multipart.MultipartFile;
  */
 public interface GoodsService extends IService<Goods> {
 
-    HttpRespMsg add(GoodsVO goodsVO,MultipartFile file);
+    HttpRespMsg add(GoodsVO goodsVO);
 
     HttpRespMsg updateInfo(Goods goods, MultipartFile file);
 

+ 1 - 7
pcbms/src/main/java/com/hssx/pcbms/service/impl/GoodsServiceImpl.java

@@ -54,11 +54,8 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
     private UserMapper userMapper;
 
     @Override
-    public HttpRespMsg add(GoodsVO goodsVO, MultipartFile file) {
+    public HttpRespMsg add(GoodsVO goodsVO) {
         HttpRespMsg msg = new HttpRespMsg();
-        if (null != file) {
-            goodsVO.setPic(UploadFileToFileNameUtil.uploadFile(file, path));
-        }
         Goods goods = new Goods();
         BeanUtils.copyProperties(goodsVO, goods);
         List<GoodsNo> list = new ArrayList<>();
@@ -89,9 +86,6 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
     @Override
     public HttpRespMsg updateInfo(Goods goods, MultipartFile file) {
         HttpRespMsg msg = new HttpRespMsg();
-        if (null != file) {
-            goods.setPic(UploadFileToFileNameUtil.uploadFile(file, path));
-        }
         goodsMapper.updateById(goods);
         return msg;
     }