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);
|
}
|
}
|
|
}
|
|
}
|
}
|