地质所 沉降监测网建设项目
zmk
2024-05-17 8722d2bb39c6c0697647e77a879d14b28d3ef0f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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;
import com.javaweb.geo.domain.ProjectData;
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业务层处理
 *
 * @author cxy
 * @date 2024-05-16
 */
@Service
public class ProjectDataServiceImpl implements IProjectDataService {
 
 
    private static String manUploadPath = "imgupload";
 
    private static String imgUploadPath = Global.getProfile() + "\\" + manUploadPath + "\\";
 
    @Autowired
    private ProjectDataMapper projectDataMapper;
 
    /**
     * 查询项目资料
     *
     * @param id 项目资料ID
     * @return 项目资料
     */
    @Override
    public ProjectData selectProjectDataById(String id) {
        return projectDataMapper.selectProjectDataById(id);
    }
 
    /**
     * 查询项目资料列表
     *
     * @param projectData 项目资料
     * @return 项目资料
     */
    @Override
    public List<ProjectData> selectProjectDataList(ProjectData projectData) {
        if (!ObjectUtils.isEmpty(projectData.getDataType()) && projectData.getDataType().endsWith(",")) {
            projectData.setDataType(projectData.getDataType().substring(0, 1));
        }
        return projectDataMapper.selectProjectDataList(projectData);
    }
 
    /**
     * 新增项目资料
     *
     * @param projectData 项目资料
     * @return 结果
     */
    @Override
    public int insertProjectData(ProjectData projectData) {
        if (ObjectUtils.isEmpty(projectData.getId())) {
            projectData.setId(IdGenerate.nextId());
        }
        projectData.setCreateTime(DateUtils.getNowDate());
        return projectDataMapper.insertProjectData(projectData);
    }
 
    /**
     * 修改项目资料
     *
     * @param projectData 项目资料
     * @return 结果
     */
    @Override
    public int updateProjectData(ProjectData projectData) {
        projectData.setUpdateTime(DateUtils.getNowDate());
        return projectDataMapper.updateProjectData(projectData);
    }
 
    /**
     * 删除项目资料对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProjectDataByIds(String ids) {
        return projectDataMapper.deleteProjectDataByIds(Convert.toStrArray(ids));
    }
 
    /**
     * 删除项目资料信息
     *
     * @param id 项目资料ID
     * @return 结果
     */
    @Override
    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);
                }
            }
 
        }
 
    }
}