詹子聪 %!s(int64=5) %!d(string=hai) anos
pai
achega
20080978ab

+ 1 - 0
src/main/java/com/miekir/shibei/controller/api/CouponController.java

@@ -146,6 +146,7 @@ public class CouponController {
 
         List<String> photoUrlList = new ArrayList<String>();
         int maxPageNum = nameArray.length - pageSize + 1;
+        // 如果只有一页,就把所有的返回去
         if (maxPageNum <= 1) {
             for (String s : nameArray) {
                 photoUrlList.add(String.format(FileTool.FORMATTER_IMAGE_URL, s));

+ 56 - 3
src/main/java/com/miekir/shibei/controller/api/GoodsController.java

@@ -2,9 +2,7 @@ package com.miekir.shibei.controller.api;
 
 import com.alibaba.fastjson.JSON;
 import com.miekir.shibei.bean.*;
-import com.miekir.shibei.bean.db.GoodsBean;
-import com.miekir.shibei.bean.db.SystemBean;
-import com.miekir.shibei.bean.db.User;
+import com.miekir.shibei.bean.db.*;
 import com.miekir.shibei.repository.FavRepository;
 import com.miekir.shibei.repository.GoodsRepository;
 import com.miekir.shibei.repository.SystemRepository;
@@ -115,7 +113,62 @@ public class GoodsController {
             return JSON.toJSONString(responseResult);
         }
 
+
+
+        // 删除插图
+        Set<String> contentImageList = dbGoodsBean.contentImageUrlList;
+        if (contentImageList != null && contentImageList.size() > 0) {
+            for (String imgUrl : contentImageList) {
+                String fileUrl = imgUrl;
+                if (fileUrl.startsWith("auto/")) {
+                    fileUrl = fileUrl.substring("auto/".length());
+                }
+
+                File file = new File(FileTool.TARGET_DIR_AUTO_IMAGES, fileUrl);
+                if (file.exists()) {
+                    try {
+                        file.delete();
+                    } catch (Exception imgException) {
+                        imgException.printStackTrace();
+                    }
+                }
+            }
+        }
+
+        // 删除torrent
+        Set<MagnetBean> magnetBeanList = dbGoodsBean.magnetBeanList;
+        if (magnetBeanList != null && magnetBeanList.size() > 0) {
+            for (MagnetBean magnetBean :  magnetBeanList) {
+                String torrentUrl = magnetBean.url;
+                if (!TextUtils.isEmpty(torrentUrl)) {
+                    if (torrentUrl.startsWith("torrent/")) {
+                        torrentUrl = torrentUrl.substring("torrent/".length());
+                    }
+
+                    File file = new File(FileTool.TARGET_DIR_AUTO_TORRENT, torrentUrl);
+                    if (file.exists()) {
+                        try {
+                            file.delete();
+                        } catch (Exception imgException) {
+                            imgException.printStackTrace();
+                        }
+                    }
+                }
+            }
+        }
+
+        // 删除收藏
+        try {
+            List<FavBean> favBeanList = favRepository.findFavListByGoodsId(dbGoodsBean.id);
+            if (favBeanList != null && favBeanList.size() > 0) {
+                favRepository.delete(favBeanList);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
         try {
+            // 这里数据库一对多关系的多会被删除,但自己手动插入的和本地文件还是需要自己处理
             goodsRepository.delete(dbGoodsBean);
         } catch (Exception e) {
             e.printStackTrace();

+ 6 - 0
src/main/java/com/miekir/shibei/repository/FavRepository.java

@@ -18,6 +18,7 @@ import java.util.List;
 // 添加注解
 @Repository
 public interface FavRepository extends JpaRepository<FavBean, Long> {
+    // 查询某个人的收藏
     @Transactional
     @Query(value="select favBean.goodsId from FavBean favBean where favBean.email=:email")
     public List<Long> findFavListByEmail(@Param("email") String email);
@@ -26,6 +27,11 @@ public interface FavRepository extends JpaRepository<FavBean, Long> {
     @Query(value="select favBean from FavBean favBean where favBean.email=:email and favBean.goodsId=:goodsId")
     public FavBean findFavByEmailAndGoodsId(@Param("email") String email, @Param("goodsId") Long goodsId);
 
+    // 查询某件商品被收藏的记录
+    @Transactional
+    @Query(value="select favBean from FavBean favBean where favBean.goodsId=:goodsId")
+    public List<FavBean> findFavListByGoodsId(@Param("goodsId") Long goodsId);
+
     // 使用原生SQL MYSQL 不支持TOP,要用连接符号|连接,否则会被当成字符串常量拼成%?1%
     @Query(value="SELECT goodsId FROM t_fav where email = ?1 ORDER BY id DESC limit ?2, ?3", nativeQuery = true)
     public List<BigInteger> getFavList(String email, int startNum, int pageSize);