地质所 沉降监测网建设项目
suerwei
2024-05-17 576e64f01f88f2b3d4cd6bd35f875280f311aa73
javaweb-plus/javaweb-cms/src/main/java/com/javaweb/geo/service/impl/ProjectDataServiceImpl.java
@@ -1,9 +1,17 @@
package com.javaweb.geo.service.impl;
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.javaweb.common.config.Global;
import com.javaweb.common.core.domain.AjaxResult;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.IdGenerate;
import com.javaweb.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.javaweb.geo.mapper.ProjectDataMapper;
@@ -11,6 +19,9 @@
import com.javaweb.geo.service.IProjectDataService;
import com.javaweb.common.core.text.Convert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
/**
 * 项目资料Service业务层处理
@@ -20,6 +31,12 @@
 */
@Service
public class ProjectDataServiceImpl implements IProjectDataService {
    private static String manUploadPath = "imgupload";
    private static String imgUploadPath = Global.getProfile() + "\\" + manUploadPath + "\\";
    @Autowired
    private ProjectDataMapper projectDataMapper;
@@ -42,8 +59,8 @@
     */
    @Override
    public List<ProjectData> selectProjectDataList(ProjectData projectData) {
        if (!ObjectUtils.isEmpty(projectData.getDataType()) && projectData.getDataType().endsWith(",")){
            projectData.setDataType(projectData.getDataType().substring(0,1));
        if (!ObjectUtils.isEmpty(projectData.getDataType()) && projectData.getDataType().endsWith(",")) {
            projectData.setDataType(projectData.getDataType().substring(0, 1));
        }
        return projectDataMapper.selectProjectDataList(projectData);
    }
@@ -96,4 +113,84 @@
    public int deleteProjectDataById(String id) {
        return projectDataMapper.deleteProjectDataById(id);
    }
    @Override
    public AjaxResult uploadZIP(MultipartFile file, HttpServletRequest request) {
        String uploadPath = Global.getProfile() + "\\";
        String originalFilename = file.getOriginalFilename();
        String fieldname = request.getParameter("fieldname");
        if (StringUtils.isEmpty(fieldname)) {
            fieldname = "filename";
        }
        //获取后缀.zip  保存的文件名
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String UID = IdGenerate.nextId();
        String dFileName = UID + substring;
        File file2 = new File(uploadPath + dFileName);
        try {
            file.transferTo(file2);
        } catch (Exception e) {
            return AjaxResult.warn("附件上传失败!");
        }
        //解压缩
        try {
            ZipUtil.unzip(file2, Charset.forName("GBK"));
        } catch (Exception e) {
            return AjaxResult.warn("附件解压缩失败!");
        }
        String moveDir = Global.getProfile() + "\\" + UID + "\\" + originalFilename.replace(substring, "");
        List<String> movedFiles = moveFile(moveDir);
        updateFilePath(fieldname, movedFiles);
        FileUtil.del(new File(uploadPath + UID));
        FileUtil.del(file2);
        return AjaxResult.success("上传成功");
    }
    //移动 文件
    private List<String> moveFile(String moveDir) {
        List<String> list = new ArrayList<>();
        File file = new File(moveDir);
        String detpath = imgUploadPath;
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            int len = files.length;
            for (int i = 0; i < len; i++) {
                String filename = files[i].getName();
                list.add(filename);
                FileUtil.copy(files[i], new File(detpath + filename), true);
            }
        }
        return list;
    }
    private void updateFilePath(String filedname, List<String> filesPath) {
        for (String fileName : filesPath) {
            String substring = fileName.substring(fileName.lastIndexOf("."));
            String docFileName = fileName.replace(substring, "").trim();
            ProjectData param = new ProjectData();
            if (filedname.equals("filename")) {
                param.setName(docFileName);
            }
            List<ProjectData> list = projectDataMapper.selectProjectDataList(param);
            if (!ObjectUtils.isEmpty(list)) {
                for (ProjectData item : list) {
                    ProjectData entity = new ProjectData();
                    entity.setId(item.getId());
                    entity.setDataUrl(manUploadPath + "/" + fileName);
                    projectDataMapper.updateProjectData(entity);
                }
            }
        }
    }
}