Browse Source

修改StoreService

Menethil 7 years ago
parent
commit
6f8bedd4e5

+ 4 - 4
litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminGoodsController.java

@@ -97,8 +97,8 @@ public class AdminGoodsController {
         try {
 
             //将生成的分享图片地址写入数据库
-            qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
-            goods.setShareUrl(qCodeService.getShareImageUrl(goods.getId().toString()));
+            String url = qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
+            goods.setShareUrl(url);
 
             // 商品基本信息表litemall_goods
             goodsService.updateById(goods);
@@ -194,8 +194,8 @@ public class AdminGoodsController {
             goodsService.add(goods);
 
             //将生成的分享图片地址写入数据库
-            qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
-            goods.setShareUrl(qCodeService.getShareImageUrl(goods.getId().toString()));
+            String url = qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
+            goods.setShareUrl(url);
             goodsService.updateById(goods);
 
             // 商品规格表litemall_goods_specification

+ 8 - 50
litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminStorageController.java

@@ -2,25 +2,18 @@ package org.linlinjava.litemall.admin.web;
 
 import org.linlinjava.litemall.admin.annotation.LoginAdmin;
 import org.linlinjava.litemall.core.storage.StorageService;
-import org.linlinjava.litemall.core.util.CharUtil;
 import org.linlinjava.litemall.core.util.ResponseUtil;
 import org.linlinjava.litemall.core.validator.Order;
 import org.linlinjava.litemall.core.validator.Sort;
 import org.linlinjava.litemall.db.domain.LitemallStorage;
 import org.linlinjava.litemall.db.service.LitemallStorageService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.io.Resource;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.validation.constraints.NotNull;
 import java.io.IOException;
-import java.io.InputStream;
-import java.time.LocalDateTime;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -35,29 +28,13 @@ public class AdminStorageController {
     @Autowired
     private LitemallStorageService litemallStorageService;
 
-    private String generateKey(String originalFilename){
-        int index = originalFilename.lastIndexOf('.');
-        String suffix = originalFilename.substring(index);
-
-        String key = null;
-        LitemallStorage storageInfo = null;
-
-        do{
-            key = CharUtil.getRandomString(20) + suffix;
-            storageInfo = litemallStorageService.findByKey(key);
-        }
-        while(storageInfo != null);
-
-        return key;
-    }
-
     @GetMapping("/list")
     public Object list(@LoginAdmin Integer adminId,
                        String key, String name,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer limit,
                        @Sort @RequestParam(defaultValue = "add_time") String sort,
-                       @Order @RequestParam(defaultValue = "desc") String order){
+                       @Order @RequestParam(defaultValue = "desc") String order) {
         List<LitemallStorage> storageList = litemallStorageService.querySelective(key, name, page, limit, sort, order);
         int total = litemallStorageService.countSelective(key, name, page, limit, sort, order);
         Map<String, Object> data = new HashMap<>();
@@ -69,40 +46,21 @@ public class AdminStorageController {
 
     @PostMapping("/create")
     public Object create(@LoginAdmin Integer adminId, @RequestParam("file") MultipartFile file) throws IOException {
-        if(adminId == null){
+        if (adminId == null) {
             return ResponseUtil.unlogin();
         }
         String originalFilename = file.getOriginalFilename();
-        InputStream inputStream = null;
-        try {
-            inputStream = file.getInputStream();
-        } catch (IOException e) {
-            e.printStackTrace();
-            return ResponseUtil.badArgumentValue();
-        }
-        String key = generateKey(originalFilename);
-        storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), key);
-
-        String url = storageService.generateUrl(key);
-        LitemallStorage storageInfo = new LitemallStorage();
-        storageInfo.setName(originalFilename);
-        storageInfo.setSize((int)file.getSize());
-        storageInfo.setType(file.getContentType());
-        storageInfo.setAddTime(LocalDateTime.now());
-        storageInfo.setModified(LocalDateTime.now());
-        storageInfo.setKey(key);
-        storageInfo.setUrl(url);
-        litemallStorageService.add(storageInfo);
-        return ResponseUtil.ok(storageInfo);
+        storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
+        return ResponseUtil.ok();
     }
 
     @PostMapping("/read")
     public Object read(@LoginAdmin Integer adminId, @NotNull Integer id) {
-        if(adminId == null){
+        if (adminId == null) {
             return ResponseUtil.unlogin();
         }
         LitemallStorage storageInfo = litemallStorageService.findById(id);
-        if(storageInfo == null){
+        if (storageInfo == null) {
             return ResponseUtil.badArgumentValue();
         }
         return ResponseUtil.ok(storageInfo);
@@ -110,7 +68,7 @@ public class AdminStorageController {
 
     @PostMapping("/update")
     public Object update(@LoginAdmin Integer adminId, @RequestBody LitemallStorage litemallStorage) {
-        if(adminId == null){
+        if (adminId == null) {
             return ResponseUtil.unlogin();
         }
         litemallStorageService.update(litemallStorage);
@@ -119,7 +77,7 @@ public class AdminStorageController {
 
     @PostMapping("/delete")
     public Object delete(@LoginAdmin Integer adminId, @RequestBody LitemallStorage litemallStorage) {
-        if(adminId == null){
+        if (adminId == null) {
             return ResponseUtil.unlogin();
         }
         litemallStorageService.deleteByKey(litemallStorage.getKey());

+ 12 - 8
litemall-core/src/main/java/org/linlinjava/litemall/core/qcode/QCodeService.java

@@ -24,7 +24,7 @@ public class QCodeService {
     private StorageService storageService;
 
 
-    public void createGrouponShareImage(String goodName, String goodPicUrl, LitemallGroupon groupon) {
+    public String createGrouponShareImage(String goodName, String goodPicUrl, LitemallGroupon groupon) {
         try {
             //创建该商品的二维码
             File file = wxMaService.getQrcodeService().createWxaCodeUnlimit("groupon," + groupon.getId(), "pages/index/index");
@@ -33,7 +33,9 @@ public class QCodeService {
             byte[] imageData = drawPicture(inputStream, goodPicUrl, goodName, SystemConfig.getMallName());
             ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData);
             //存储分享图
-            storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(groupon.getId().toString()));
+            String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(groupon.getId().toString()));
+
+            return url;
         } catch (WxErrorException e) {
             e.printStackTrace();
         } catch (FileNotFoundException e) {
@@ -43,6 +45,8 @@ public class QCodeService {
         } catch (FontFormatException e) {
             e.printStackTrace();
         }
+
+        return "";
     }
 
 
@@ -53,9 +57,9 @@ public class QCodeService {
      * @param goodPicUrl
      * @param goodName
      */
-    public void createGoodShareImage(String goodId, String goodPicUrl, String goodName) {
+    public String createGoodShareImage(String goodId, String goodPicUrl, String goodName) {
         if (!SystemConfig.isAutoCreateShareImage())
-            return;
+            return "";
 
         try {
             //创建该商品的二维码
@@ -65,7 +69,9 @@ public class QCodeService {
             byte[] imageData = drawPicture(inputStream, goodPicUrl, goodName, SystemConfig.getMallName());
             ByteArrayInputStream inputStream2 = new ByteArrayInputStream(imageData);
             //存储分享图
-            storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(goodId));
+            String url = storageService.store(inputStream2, imageData.length, "image/jpeg", getKeyName(goodId));
+
+            return url;
         } catch (WxErrorException e) {
             e.printStackTrace();
         } catch (FileNotFoundException e) {
@@ -75,10 +81,8 @@ public class QCodeService {
         } catch (FontFormatException e) {
             e.printStackTrace();
         }
-    }
 
-    public String getShareImageUrl(String goodId) {
-        return storageService.generateUrl(getKeyName(goodId));
+        return "";
     }
 
     private String getKeyName(String goodId) {

+ 1 - 1
litemall-core/src/main/java/org/linlinjava/litemall/core/storage/Storage.java

@@ -16,7 +16,7 @@ public interface Storage {
      * @param inputStream 文件输入流
      * @param contentLength 文件长度
      * @param contentType 文件类型
-     * @param keyName   文件索引
+     * @param keyName   文件名
      */
     void store(InputStream inputStream, long contentLength, String contentType, String keyName);
 

+ 43 - 5
litemall-core/src/main/java/org/linlinjava/litemall/core/storage/StorageService.java

@@ -1,10 +1,15 @@
 package org.linlinjava.litemall.core.storage;
 
+import org.linlinjava.litemall.core.util.CharUtil;
+import org.linlinjava.litemall.db.domain.LitemallStorage;
+import org.linlinjava.litemall.db.service.LitemallStorageService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.Resource;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.InputStream;
 import java.nio.file.Path;
+import java.time.LocalDateTime;
 import java.util.stream.Stream;
 
 /**
@@ -13,6 +18,8 @@ import java.util.stream.Stream;
 public class StorageService {
     private String active;
     private Storage storage;
+    @Autowired
+    private LitemallStorageService litemallStorageService;
 
     public String getActive() {
         return active;
@@ -32,13 +39,44 @@ public class StorageService {
 
     /**
      * 存储一个文件对象
-     * @param inputStream 文件输入流
+     *
+     * @param inputStream   文件输入流
      * @param contentLength 文件长度
-     * @param contentType 文件类型
-     * @param keyName   文件索引名
+     * @param contentType   文件类型
+     * @param fileName      文件索引名
      */
-    public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
-        storage.store(inputStream, contentLength, contentType, keyName);
+    public String store(InputStream inputStream, long contentLength, String contentType, String fileName) {
+        String key = generateKey(fileName);
+        storage.store(inputStream, contentLength, contentType, key);
+
+        String url = generateUrl(key);
+        LitemallStorage storageInfo = new LitemallStorage();
+        storageInfo.setName(fileName);
+        storageInfo.setSize((int) contentLength);
+        storageInfo.setType(contentType);
+        storageInfo.setAddTime(LocalDateTime.now());
+        storageInfo.setModified(LocalDateTime.now());
+        storageInfo.setKey(key);
+        storageInfo.setUrl(url);
+        litemallStorageService.add(storageInfo);
+
+        return url;
+    }
+
+    private String generateKey(String originalFilename) {
+        int index = originalFilename.lastIndexOf('.');
+        String suffix = originalFilename.substring(index);
+
+        String key = null;
+        LitemallStorage storageInfo = null;
+
+        do {
+            key = CharUtil.getRandomString(20) + suffix;
+            storageInfo = litemallStorageService.findByKey(key);
+        }
+        while (storageInfo != null);
+
+        return key;
     }
 
     public Stream<Path> loadAll() {

+ 4 - 4
litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallGrouponService.java

@@ -27,14 +27,14 @@ public class LitemallGrouponService {
 
     public List<LitemallGroupon> queryMyGroupon(Integer userId) {
         LitemallGrouponExample example = new LitemallGrouponExample();
-        example.or().andUserIdEqualTo(userId).andCreatorUserIdEqualTo(userId).andGrouponIdEqualTo(0).andDeletedEqualTo(false);
+        example.or().andUserIdEqualTo(userId).andCreatorUserIdEqualTo(userId).andGrouponIdEqualTo(0).andDeletedEqualTo(false).andPayedEqualTo(true);
         example.orderBy("add_time desc");
         return mapper.selectByExample(example);
     }
 
     public List<LitemallGroupon> queryMyJoinGroupon(Integer userId) {
         LitemallGrouponExample example = new LitemallGrouponExample();
-        example.or().andUserIdEqualTo(userId).andGrouponIdNotEqualTo(0).andDeletedEqualTo(false);
+        example.or().andUserIdEqualTo(userId).andGrouponIdNotEqualTo(0).andDeletedEqualTo(false).andPayedEqualTo(true);
         example.orderBy("add_time desc");
         return mapper.selectByExample(example);
     }
@@ -47,7 +47,7 @@ public class LitemallGrouponService {
 
     public List<LitemallGroupon> queryJoiners(Integer id) {
         LitemallGrouponExample example = new LitemallGrouponExample();
-        example.or().andGrouponIdEqualTo(id).andDeletedEqualTo(false);
+        example.or().andGrouponIdEqualTo(id).andDeletedEqualTo(false).andPayedEqualTo(true);
         example.orderBy("add_time desc");
         return mapper.selectByExample(example);
     }
@@ -70,7 +70,7 @@ public class LitemallGrouponService {
      */
     public int countGroupon(Integer grouponId) {
         LitemallGrouponExample example = new LitemallGrouponExample();
-        example.or().andGrouponIdEqualTo(grouponId).andDeletedEqualTo(false);
+        example.or().andGrouponIdEqualTo(grouponId).andDeletedEqualTo(false).andPayedEqualTo(true);
         return (int) mapper.countByExample(example);
     }
 

+ 2 - 2
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java

@@ -628,8 +628,8 @@ public class WxOrderController {
 
                 //仅当发起者才创建分享图片
                 if (groupon.getGrouponId() == 0) {
-                    qCodeService.createGrouponShareImage(grouponRules.getGoodsName(), grouponRules.getPicUrl(), groupon);
-                    groupon.setShareUrl(qCodeService.getShareImageUrl(groupon.getId().toString()));
+                    String url = qCodeService.createGrouponShareImage(grouponRules.getGoodsName(), grouponRules.getPicUrl(), groupon);
+                    groupon.setShareUrl(url);
                 }
                 groupon.setPayed(true);
                 grouponService.update(groupon);