地质所 沉降监测网建设项目
zmk
2024-05-18 b9b712a7701eb83c7dbd8367c534d4c880503adb
javaweb-plus/javaweb-cms/src/main/java/com/javaweb/geo/service/impl/ProjectDataServiceImpl.java
@@ -12,12 +12,17 @@
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.IdGenerate;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.geo.domain.Hole;
import com.javaweb.geo.domain.HoleMedia;
import com.javaweb.geo.mapper.HoleMapper;
import com.javaweb.geo.mapper.HoleMediaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.javaweb.geo.mapper.ProjectDataMapper;
import com.javaweb.geo.domain.ProjectData;
import com.javaweb.geo.service.IProjectDataService;
import com.javaweb.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
@@ -33,12 +38,23 @@
public class ProjectDataServiceImpl implements IProjectDataService {
    private static String manUploadPath = "imgupload";
    private static String zipfile = "zipfile";
    private static String zipfile2 = "zipfile2";
    private static String imgUploadPath = Global.getProfile() + "\\" + manUploadPath + "\\";
    //zip 原始文件
    private static String ZIPUploadPath = Global.getProfile() + "\\" + zipfile + "\\";
    //解压文件
    private static String ZIPUploadPath2 = Global.getProfile() + "\\" + zipfile2 + "\\";
    @Autowired
    private ProjectDataMapper projectDataMapper;
    @Autowired
    private HoleMapper holeMapper;
    @Autowired
    private HoleMediaMapper mediaMapper;
    /**
     * 查询项目资料
@@ -116,21 +132,17 @@
    @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";
        }
        //传入的钻孔的id
        String projectDataId = request.getParameter("projectDataId");
        //获取后缀.zip  保存的文件名
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String UID = IdGenerate.nextId();
        String dFileName = UID + substring;
        File file2 = new File(uploadPath + dFileName);
        File file2 = new File(ZIPUploadPath + dFileName);
        try {
            file.transferTo(file2);
        } catch (Exception e) {
@@ -143,53 +155,60 @@
        } 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);
        //移动到zipfile2  存储解压完毕的文件
        String moveDir = ZIPUploadPath + UID;
        moveFile(moveDir);
        //更新数据库
        updateFilePath(projectDataId, ZIPUploadPath2 + UID +"\\" + originalFilename.replace(substring, "") );
        ProjectData projectData = new ProjectData();
        //更新  projectData  url
        String dataUrl = zipfile + "/" + UID +".zip";
        projectData.setDataUrl(dataUrl);
        projectData.setId(projectDataId);
        projectDataMapper.updateProjectData(projectData);
        FileUtil.del(new File(ZIPUploadPath + UID));
        return AjaxResult.success("上传成功");
    }
    //移动 文件
    private List<String> moveFile(String moveDir) {
        List<String> list = new ArrayList<>();
    private void moveFile(String moveDir) {
        File file = new File(moveDir);
        String detpath = imgUploadPath;
        if (file.isDirectory()) {
            File[] files = file.listFiles();
        File dest = new File(ZIPUploadPath2);
        FileUtil.move(file,dest,true);
    }
    @Transactional(rollbackFor = Exception.class)
    public void updateFilePath(String projectDataId, String dirpath) {
        ProjectData projectData = projectDataMapper.selectProjectDataById(projectDataId);
        String projectId= projectData.getProjectId();
        File fileDir = new File(dirpath);
        if (fileDir.isDirectory()) {
            File[] files = fileDir.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;
    }
                String filepath = files[i].getPath();
                String url = filepath.replace(Global.getProfile(),"").replace("\\","/");
    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);
                HoleMedia media = new HoleMedia();
                media.setIds(IdGenerate.nextId());
                media.setRecordId(projectDataId);
                media.setProjectId(projectId);
                media.setName(filename);
                media.setInternetPath(url);
                media.setCreateTime(DateUtils.getNowDate());
                mediaMapper.insertHoleMedia(media);
            }
            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);
                }
            }
        }
    }