Administrator 4 жил өмнө
parent
commit
9f6cb1223d

+ 2 - 1
ruoyi-admin/src/main/java/com/ruoyi/web/controller/gallery/ZbPicToPicController.java

@@ -73,6 +73,7 @@ public class ZbPicToPicController extends BaseController {
 
     /**
      * 删除图来图往
+     * id是zb_pic_to_pic表中的id
      */
     @DeleteMapping("/{id}")
     @PreAuthorize("@ss.hasPermi('system:pic:remove')")
@@ -80,6 +81,6 @@ public class ZbPicToPicController extends BaseController {
         if (id == null || id == 0) {
             return AjaxResult.error("参数不正确");
         }
-        return toAjax(zbPicToPicService.removeById(id));
+        return zbPicToPicService.removeZbPicToPic(id);
     }
 }

+ 4 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/ZbFileMapper.java

@@ -23,4 +23,8 @@ public interface ZbFileMapper extends BaseMapper<ZbFile> {
      */
     List<ZbFile> selectZbFileList(@Param("obj") ZbFile zbFile, Page<ZbFile> page);
 
+    List<ZbFile> selectByIds(String[] fileIdsArr);
+
+    Integer deleteByIds(String[] fileIdsArr);
+
 }

+ 14 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/IZbFileService.java

@@ -46,4 +46,18 @@ public interface IZbFileService extends IService<ZbFile> {
      * @return
      */
     boolean deletePicture(ZbFile zbFile);
+
+    /**
+     * 根据ids查询数据
+     * @param fileIdsArr
+     * @return
+     */
+    List<ZbFile>selectByIds(String[] fileIdsArr);
+
+    /**
+     * 删除附件表信息
+     * @param fileIdsArr
+     * @return
+     */
+    Integer deleteByIds(String[] fileIdsArr);
 }

+ 8 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/IZbPicToPicService.java

@@ -5,6 +5,7 @@ import java.util.List;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.system.domain.grallery.ZbPicToPic;
 import com.ruoyi.system.domain.grallery.ZbZip;
 import com.ruoyi.system.dto.PicToPicDto;
@@ -44,5 +45,12 @@ public interface IZbPicToPicService extends IService<ZbPicToPic> {
 
     ZbZip uploadToZip(MultipartFile[] files);
 
+    /**
+     * 后台删除图来图往
+     * @param id
+     * @return
+     */
+    AjaxResult removeZbPicToPic(Long id);
+
     PicToPicDto getDetailById(Long picId);
 }

+ 3 - 2
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/FileServiceImpl.java

@@ -171,8 +171,9 @@ public class FileServiceImpl implements IFileService {
         String name = file.getName();
         String pathname = timeFormat + File.separator + name;
         String newPath = RuoYiConfig.getProfile() + File.separator + timeFormat + name;
-        if (!newPath.equals(file.getAbsolutePath())) {
-            FileUtil.move(file, new File(newPath), false);
+        File fileNew = new File(newPath);
+        if (!fileNew.getAbsolutePath().equals(file.getAbsolutePath())) {
+            FileUtil.move(file, fileNew, false);
         }
         ZbFile xmFile = new ZbFile();
         xmFile.setCode(RandomUtil.randomString(20));

+ 34 - 4
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ZbFileServiceImpl.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.config.RuoYiConfig;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.FileUtils;
 import com.ruoyi.system.domain.ZbFile;
 import com.ruoyi.system.service.IZbFileService;
@@ -73,13 +74,22 @@ public class ZbFileServiceImpl extends ServiceImpl<ZbFileMapper, ZbFile> impleme
     public boolean deletePicture(ZbFile zbFile) {
         try {
             Integer id = zbFile.getId();
-            zbFile = baseMapper.selectById(id);
+            if (id == null){
+                return false;
+            }
+            //判断图片存放地址是否为空
+            if (StringUtils.isEmpty(zbFile.getPath())){
+                zbFile = baseMapper.selectById(id);
+            }
             File file = new File(RuoYiConfig.getProfile() + File.separator + zbFile.getPath());
             log.info("删除原始文件: " + file);
             FileUtils.forceDelete(file);
-            File fileThumbnail = new File(RuoYiConfig.getProfile() + File.separator + zbFile.getThumbnailPath());
-            log.info("删除缩略图文件: " + fileThumbnail);
-            FileUtils.forceDelete(fileThumbnail);
+            String thumbnailPath = zbFile.getThumbnailPath();
+            if (StringUtils.isNotEmpty(thumbnailPath)){
+                File fileThumbnail = new File(RuoYiConfig.getProfile() + File.separator + thumbnailPath);
+                log.info("删除缩略图文件: " + fileThumbnail);
+                FileUtils.forceDelete(fileThumbnail);
+            }
             //删除数据库附件表信息
             baseMapper.deleteById(id);
         } catch (IOException e) {
@@ -89,4 +99,24 @@ public class ZbFileServiceImpl extends ServiceImpl<ZbFileMapper, ZbFile> impleme
         }
         return true;
     }
+
+    /**
+     * 根据ids查询数据
+     * @param fileIdsArr
+     * @return
+     */
+    @Override
+    public List<ZbFile> selectByIds(String[] fileIdsArr) {
+        return  baseMapper.selectByIds(fileIdsArr);
+    }
+
+    /**
+     * 删除附件表信息
+     * @param fileIdsArr
+     * @return
+     */
+    @Override
+    public Integer deleteByIds(String[] fileIdsArr) {
+        return  baseMapper.deleteByIds(fileIdsArr);
+    }
 }

+ 1 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ZbGalleryCategoryServiceImpl.java

@@ -160,7 +160,7 @@ public class ZbGalleryCategoryServiceImpl extends ServiceImpl<ZbGalleryCategoryM
 
     @Override
     public ZbGalleryCategory getByCode(String categoryCode) {
-        return getOne(new LambdaQueryWrapper<ZbGalleryCategory>().eq(ZbGalleryCategory::getCode, categoryCode).eq(ZbGalleryCategory::getTop, 1));
+        return getOne(new LambdaQueryWrapper<ZbGalleryCategory>().eq(ZbGalleryCategory::getCode, categoryCode).eq(ZbGalleryCategory::getTop, 1).last("limit 1"));
     }
 
     @Override

+ 1 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ZbGalleryServiceImpl.java

@@ -229,6 +229,7 @@ public class ZbGalleryServiceImpl extends ServiceImpl<ZbGalleryMapper, ZbGallery
                         x -> x.getImgInfos().forEach(i -> i.setUrl(profile + i.getUrl()))
                 ).collect(Collectors.toList());*/
 
+        //获取预览地址
         List<GalleryDto> galleryDtos = baseMapper.selectGalleryDtoThumbnail(categorys, pageInfo)
                 .stream().peek(
                         x -> x.getImgInfos().forEach(i -> i.setUrl(profile + i.getUrl()))

+ 92 - 3
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ZbPicToPicServiceImpl.java

@@ -8,8 +8,11 @@ import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.config.RuoYiConfig;
+import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.utils.DateUtils;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.FileUtils;
+import com.ruoyi.system.domain.ZbFile;
 import com.ruoyi.system.domain.grallery.ZbPicToPic;
 import com.ruoyi.system.domain.grallery.ZbZip;
 import com.ruoyi.system.dto.PicToPicDto;
@@ -20,7 +23,10 @@ import com.ruoyi.system.service.IZbFileService;
 import com.ruoyi.system.service.IZbPicToPicService;
 import com.ruoyi.system.service.IZbZipService;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
@@ -38,6 +44,7 @@ import java.util.zip.ZipOutputStream;
  * @author ruoyi
  * @date 2020-12-02
  */
+@Slf4j
 @Service
 @RequiredArgsConstructor
 public class ZbPicToPicServiceImpl extends ServiceImpl<ZbPicToPicMapper, ZbPicToPic> implements IZbPicToPicService {
@@ -99,13 +106,18 @@ public class ZbPicToPicServiceImpl extends ServiceImpl<ZbPicToPicMapper, ZbPicTo
         String pathname = timeFormat + RandomUtil.randomString(20) + ".zip";
 
         File file = new File(profile + File.separator + pathname);
+        //检查或者创建父级文件夹
         FileUtils.checkOrCreateParentDir(file);
+        ZipOutputStream zipOutputStream = null;
         try {
             FileOutputStream fileOutputStream = new FileOutputStream(file);
-            ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
+            zipOutputStream = new ZipOutputStream(fileOutputStream);
             for (MultipartFile multipartFile : files) {
                 String originalFilename = multipartFile.getOriginalFilename();
-                zipOutputStream.putNextEntry(new ZipEntry(originalFilename));
+                if (originalFilename != null) {
+                    ZipEntry zipEntry = new ZipEntry(originalFilename);
+                    zipOutputStream.putNextEntry(zipEntry);
+                }
                 zipOutputStream.write(multipartFile.getBytes());
                 zipOutputStream.flush();
             }
@@ -113,9 +125,16 @@ public class ZbPicToPicServiceImpl extends ServiceImpl<ZbPicToPicMapper, ZbPicTo
             zipOutputStream.close();
         } catch (IOException e) {
             e.printStackTrace();
+        }finally {
+            if (zipOutputStream != null){
+                try {
+                    zipOutputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
         fileService.upload(file);
-
         ZbZip zbZip = new ZbZip();
         zbZip.setFileIds(CollectionUtil.join(upload.stream().map(UploadResult::getFileId).collect(Collectors.toList()), ","));
         zbZip.setFileNum(files.length);
@@ -128,6 +147,76 @@ public class ZbPicToPicServiceImpl extends ServiceImpl<ZbPicToPicMapper, ZbPicTo
         return zbZip;
     }
 
+    /**
+     * 后台删除图来图往
+     * @param id
+     * @return
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public AjaxResult removeZbPicToPic(Long id) {
+        //先去查询zb_pic_to_pic表信息
+        PicToPicDto picToPicDto = getDetailById(id);
+        if (ObjectUtil.isNotEmpty(picToPicDto)){
+            //获取压缩包id
+            Long zipId = picToPicDto.getZipId();
+            //查询压缩包信息
+            ZbZip zbZip = zbZipService.getById(zipId);
+            if (ObjectUtil.isNotEmpty(zbZip)){
+                //获取附件表ids
+                String fileIds = zbZip.getFileIds();
+                //获取附件表信息
+                String[] fileIdsArr = fileIds.split(",");
+                //先查询,再删除
+                List<ZbFile> zbFiles = zbFileService.selectByIds(fileIdsArr);
+                //删除附件表信息
+                Integer integer = zbFileService.deleteByIds(fileIdsArr);
+                if (integer != fileIdsArr.length){
+                    return AjaxResult.error("删除系统附件信息失败");
+                }
+                //删除表zb_zip
+                if (!zbZipService.removeById(zbZip)){
+                    return AjaxResult.error("删除压缩包信息失败");
+                }
+                //删除表zb_pic_to_pic
+                if (baseMapper.deleteById(id)  < 1){
+                    return AjaxResult.error("删除图来图往信息失败");
+                }
+                //获取附件表上传图片地址
+                for (ZbFile zbFile : zbFiles) {
+                    String path = zbFile.getPath();
+                    if (StringUtils.isNotEmpty(path)){
+                        File file = new File(RuoYiConfig.getProfile() + File.separator + path);
+                        log.info("删除原始文件: " + file);
+                        try {
+                            FileUtils.forceDelete(file);
+                            String thumbnailPath = zbFile.getThumbnailPath();
+                            if (StringUtils.isNotEmpty(thumbnailPath)){
+                                File fileThumbnail = new File(RuoYiConfig.getProfile() + File.separator + thumbnailPath);
+                                log.info("删除缩略图文件: " + fileThumbnail);
+                                FileUtils.forceDelete(fileThumbnail);
+                            }
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                }
+                String path = zbZip.getPath();
+                if (StringUtils.isNotEmpty(path)){
+                    //删除压缩包表数据
+                    File file = new File(RuoYiConfig.getProfile() + File.separator + path);
+                    log.info("删除压缩包文件: " + file);
+                    try {
+                        FileUtils.forceDelete(file);
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }
+        return AjaxResult.error();
+    }
+
     @Override
     public PicToPicDto getDetailById(Long picId) {
         PicToPicDto detail = baseMapper.getDetailById(picId);

+ 18 - 2
ruoyi-system/src/main/resources/mapper/system/ZbFileMapper.xml

@@ -10,14 +10,14 @@
         <result property="name" column="name"/>
         <result property="url" column="url"/>
         <result property="path" column="path"/>
-        <result property="hash" column="hash"/>
+        <result property="fileHash" column="file_hash"/>
         <result property="size" column="size"/>
         <result property="image" column="image"/>
         <result property="uploadTime" column="upload_time"/>
     </resultMap>
 
     <sql id="selectZbFileVo">
-        select id, code, name, url, path, hash, size, image, upload_time from zb_file
+        select id, code, name, url, path, file_hash, size, image, upload_time from zb_file
     </sql>
 
     <select id="selectZbFileList" parameterType="com.ruoyi.system.domain.ZbFile" resultMap="ZbFileResult">
@@ -52,4 +52,20 @@
 
         </where>
     </select>
+
+
+    <select id="selectByIds" parameterType="java.lang.String" resultMap="ZbFileResult">
+        <include refid="selectZbFileVo"/>
+        where id in
+        <foreach collection="array" item="id" open="(" close=")" separator=",">
+            #{id}
+        </foreach>
+    </select>
+
+    <delete id="deleteByIds" parameterType="java.lang.String">
+        delete from zb_file where id in
+        <foreach collection="array" item="id" open="(" separator="," close=")">
+            #{id}
+        </foreach>
+    </delete>
 </mapper>

+ 3 - 2
ruoyi-system/src/main/resources/mapper/system/ZbGalleryMapper.xml

@@ -105,7 +105,7 @@
         zgc.name categoryName
         FROM
         zb_gallery zg
-        JOIN zb_gallery_img zgi ON zg.id = zgi.gallert_id
+        LEFT JOIN zb_gallery_img zgi ON zg.id = zgi.gallert_id
         LEFT JOIN zb_file zf ON zf.id = zgi.thumb_id
         left join zb_gallery_category zgc on zgc.id = zg.category_id
         <if test="categoryIds != null and categoryIds.size() > 0">
@@ -126,7 +126,7 @@
         zgc.name categoryName
         FROM
         zb_gallery zg
-        JOIN zb_gallery_img zgi ON zg.id = zgi.gallert_id
+        LEFT JOIN zb_gallery_img zgi ON zg.id = zgi.gallert_id
         LEFT JOIN zb_file zf ON zf.id = zgi.thumb_id
         left join zb_gallery_category zgc on zgc.id = zg.category_id
         <if test="categoryIds != null and categoryIds.size() > 0">
@@ -135,6 +135,7 @@
                 #{item}
             </foreach>
         </if>
+        ORDER BY zg.name
     </select>
 
     <select id="checkCategoryHasValue" resultType="java.lang.Integer">