package com.hssx.cloudmodel.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.hssx.cloudmodel.constant.Constant; import com.hssx.cloudmodel.entity.*; import com.hssx.cloudmodel.entity.vo.MouldFileVO; import com.hssx.cloudmodel.entity.vo.UserVO; import com.hssx.cloudmodel.mapper.*; import com.hssx.cloudmodel.service.MouldFileService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.hssx.cloudmodel.util.*; import lombok.extern.slf4j.Slf4j; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** *

* 服务实现类 *

* * @author 吴涛涛 * @since 2019-08-07 */ @Service @Slf4j public class MouldFileServiceImpl extends ServiceImpl implements MouldFileService { @Resource UserMapper userMapper; @Resource MouldFileMapper mouldFileMapper; @Resource MouldOperationDynamicsMapper mouldOperationDynamicsMapper; @Resource ProjectMapper projectMapper; @Resource MouldMapper mouldMapper; @Resource ProjectUserMapper projectUserMapper; @Resource ProjectApproveMapper projectApproveMapper; @Resource PartMapper partMapper; @Resource NewsNoticeMapper newsNoticeMapper; @Resource NewsNoticeUserMapper newsNoticeUserMapper; @Autowired private OpenOfficeService openOfficeService; @Resource PdfFileMapper pdfFileMapper; @Override public HttpRespMsg addFile(UserVO userVO, Integer blongType, MultipartFile file, MultipartFile file2, String path) { HttpRespMsg msg = new HttpRespMsg(); User user = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", userVO.getToken())); Mould mould = mouldMapper.selectById(userVO.getMouldId()); if (user != null) { if (file != null && !file.isEmpty()) { MouldFile projectFile = new MouldFile(); projectFile.setUplodtorId(user.getId()); projectFile.setUploadtor(user.getUsername()); projectFile.setModelId(userVO.getMouldId()); File dir = null; dir = new File(path); // D://cloud/upload 文件上传后所存储的位置,部署到服务器上时配置服务器地址即可 if (!dir.exists()) { dir.mkdirs(); } String fileName = ""; if (file != null && !file.isEmpty()) { fileName = file.getOriginalFilename(); projectFile.setFileName(fileName); projectFile.setFileSize(FileUtil.getReadableFileSize(file.getSize())); System.out.println("上传文件名称" + file.getName() + ", dir = " + dir.getAbsolutePath()); int pos = fileName.lastIndexOf("."); String rand = UUID.randomUUID().toString().replaceAll("-", ""); String sufix = fileName.substring(pos); fileName = rand + sufix; projectFile.setFileType(sufix);//文件后缀 projectFile.setFileUrl("/upload/" + fileName); projectFile.setBlongType(blongType); File saveFile = new File(dir, fileName); try { saveFile.createNewFile(); file.transferTo(saveFile); } catch (IOException e) { e.printStackTrace(); projectFile = null; } catch (Exception e) { e.printStackTrace(); projectFile = null; } //模具报废情况下,设置files2 String dFile2 = ""; if (blongType == 5 && file2 != null && !file2.isEmpty()) { String fileName2 = file2.getOriginalFilename(); projectFile.setFileName2(fileName2); projectFile.setFileSize2(FileUtil.getReadableFileSize(file2.getSize())); System.out.println("上传文件2名称" + file2.getName() + ", dir = " + dir.getAbsolutePath()); pos = fileName2.lastIndexOf("."); rand = UUID.randomUUID().toString().replaceAll("-", ""); sufix = fileName2.substring(pos); fileName2 = rand + sufix; projectFile.setFileType2(sufix);//文件后缀 projectFile.setFileUrl2("/upload/" + fileName2); File saveFile2 = new File(dir, fileName2); try { saveFile2.createNewFile(); file2.transferTo(saveFile2); } catch (IOException e) { e.printStackTrace(); projectFile = null; } catch (Exception e) { e.printStackTrace(); projectFile = null; } } //关闭openOffice openOfficeService.shutdown(); //零件文档时存上零件id if (blongType == 1 && null != userVO.getPartId()) { projectFile.setPartId(userVO.getPartId()); } //上传零件文档和上传模具文档的时候需要区分是否为2D或者3D if (userVO.getDwgType() != null) { projectFile.setDwgType(userVO.getDwgType()); } mouldFileMapper.insert(projectFile); //上传完,需要生成pdf String dFile1 = path+UUID.randomUUID().toString().replaceAll("-", "")+".pdf"; openOfficeService.start(); File testFile = new File(dFile1); if(!testFile .exists()){ openOfficeService.office2PDF(path + fileName,dFile1); } PdfFile pdfFile = new PdfFile(); pdfFile.setPdfUrl("/upload/"+dFile1.substring(path.length())); pdfFile.setFileId(projectFile.getId()); pdfFile.setType(0); pdfFile.setRafId(projectFile.getModelId()); pdfFile.setSourceFileUrl(projectFile.getFileUrl()); pdfFileMapper.insert(pdfFile); if(blongType == 5 && file2 != null && !file2.isEmpty()){ dFile2 = path+UUID.randomUUID().toString().replaceAll("-", "")+".pdf"; testFile = new File(dFile2); if(!testFile .exists()){ openOfficeService.office2PDF(path + fileName,dFile2); } pdfFile = new PdfFile(); pdfFile.setPdfUrl("/upload/"+dFile2.substring(path.length())); pdfFile.setFileId(projectFile.getId()); pdfFile.setType(0); pdfFile.setRafId(projectFile.getModelId()); pdfFile.setSourceFileUrl(projectFile.getFileUrl2()); pdfFileMapper.insert(pdfFile); } Project project = projectMapper.selectById(mould.getProjectId()); Integer count = projectApproveMapper.selectCount(new QueryWrapper().eq("approver_id", user.getId()).eq("project_id", project.getId())); // if(count>0){ // if (user.getSubordinateType() == 0) { // //上传人为资产方审批人,自动审核通过 // projectFile.setState(1); // MouldOperationDynamics dynamics = new MouldOperationDynamics(); // dynamics.setMouldId(userVO.getMouldId()); // dynamics.setBelongType(blongType); // dynamics.setFileId(projectFile.getId()); // if (null != projectFile.getFileName2()) { // dynamics.setFileName(projectFile.getFileName() + "," + projectFile.getFileName2()); // } else { // dynamics.setFileName(projectFile.getFileName()); // } // dynamics.setOperatorId(user.getId()); // dynamics.setOperatorName(user.getUsername()); // dynamics.setContent(Constant.APPROVAL); // mouldOperationDynamicsMapper.insert(dynamics); // } else if (user.getSubordinateType() == 1) { // //上传人为生产方,自动审核通过 // projectFile.setState(2); // //上传人为资产方审批人,自动审核通过 // MouldOperationDynamics dynamics = new MouldOperationDynamics(); // dynamics.setMouldId(userVO.getMouldId()); // dynamics.setBelongType(blongType); // dynamics.setFileId(projectFile.getId()); // if (null != projectFile.getFileName2()) { // dynamics.setFileName(projectFile.getFileName() + "," + projectFile.getFileName2()); // } else { // dynamics.setFileName(projectFile.getFileName()); // } // dynamics.setOperatorId(user.getId()); // dynamics.setOperatorName(user.getUsername()); // dynamics.setContent(Constant.APPROVAL); // mouldOperationDynamicsMapper.insert(dynamics); // } else { // projectFile.setState(0); // } // }else{ projectFile.setState(0); // } //添加上传记录 MouldOperationDynamics dynamics = new MouldOperationDynamics(); dynamics.setContent(Constant.UPLOAD); dynamics.setFileName(file.getOriginalFilename()); dynamics.setOperatorId(user.getId()); dynamics.setFileId(projectFile.getId()); dynamics.setOperatorName(user.getUsername()); dynamics.setMouldId(userVO.getMouldId()); dynamics.setBelongType(blongType); mouldOperationDynamicsMapper.insert(dynamics); //添加消息通知记录 if (mould.getProjectId() != null) { List projectApproves = projectApproveMapper.selectList(new QueryWrapper().eq("project_id", project.getId())); String content = ""; NewsNotice newsNotice = new NewsNotice(); newsNotice.setRefId(projectFile.getModelId()); newsNotice.setProjectId(project.getId()); newsNotice.setBelongType(projectFile.getBlongType()); newsNotice.setProjectName(project.getProjectName() + "-" + mould.getModelName()); newsNotice.setNoticeType(Constant.APPROVEL_TYPE); if (blongType == 0) { content = "模具文档"; } else if (blongType == 1) { content = "零件文档"; } else if (blongType == 2) { content = "试模验收文档"; } else if (blongType == 3) { content = "保养方案文档"; } else if (blongType == 5) { content = "模具报废文档"; } newsNotice.setContent("有新的" + content +"上传,待您审批。"); newsNoticeMapper.insert(newsNotice); for (ProjectApprove projectApprove : projectApproves) { NewsNoticeUser newsNoticeUser = new NewsNoticeUser(); newsNoticeUser.setUserId(projectApprove.getApproverId()); newsNoticeUser.setNewsId(newsNotice.getId()); newsNoticeUserMapper.insert(newsNoticeUser); } } } msg.data = projectFile; } else { //模具更新操作 MouldFile projectFile = new MouldFile(); projectFile.setUplodtorId(user.getId()); projectFile.setUploadtor(user.getUsername()); projectFile.setModelId(userVO.getMouldId()); projectFile.setBlongType(blongType); mouldFileMapper.insert(projectFile); //添加上传记录 // MouldOperationDynamics dynamics = new MouldOperationDynamics(); // dynamics.setContent(Constant.UPDATE); // dynamics.setOperatorId(user.getId()); // dynamics.setOperatorName(user.getUsername()); // dynamics.setMouldId(userVO.getMouldId()); // dynamics.setBelongType(blongType); // mouldOperationDynamicsMapper.insert(dynamics); Project project = projectMapper.selectOne(new QueryWrapper().eq("id", mould.getProjectId())); List projectApproves = projectApproveMapper.selectList(new QueryWrapper().eq("project_id", project.getId())); String content = "模具更新"; NewsNotice newsNotice = new NewsNotice(); newsNotice.setRefId(projectFile.getModelId()); newsNotice.setProjectId(project.getId()); newsNotice.setBelongType(projectFile.getBlongType()); newsNotice.setProjectName(project.getProjectName() + "-" + mould.getModelName()); newsNotice.setNoticeType(Constant.APPROVEL_TYPE); newsNotice.setContent("有新的" + content + "申请记录,待您审批。"); newsNoticeMapper.insert(newsNotice); for (ProjectApprove projectApprove : projectApproves) { NewsNoticeUser newsNoticeUser = new NewsNoticeUser(); newsNoticeUser.setUserId(projectApprove.getApproverId()); newsNoticeUser.setNewsId(newsNotice.getId()); newsNoticeUserMapper.insert(newsNoticeUser); } } } else { msg.setError("用户不存在或者未登录"); } return msg; } @Override public HttpRespMsg check(Integer mouldFileId, Integer isPass, UserVO userVO) { HttpRespMsg msg = new HttpRespMsg(); User user = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", userVO.getToken())); MouldFile mf = new MouldFile(); mf.setId(mouldFileId); MouldFile oldData = mouldFileMapper.selectById(mouldFileId); Mould md = mouldMapper.selectById(oldData.getModelId()); String approverNames = projectApproveMapper.selectList(new QueryWrapper().eq("project_id", md.getProjectId())) .stream().map(ProjectApprove::getApproverName).collect(Collectors.joining(",")); MouldOperationDynamics dynamics = new MouldOperationDynamics(); if (user != null) { if (user.getSubordinateType() == 0) { //资产方 if (isPass == 1) { if (oldData.getState() == 2) {//生产方也审核通过了 mf.setState(3); //审批通过 dynamics.setIsPass(1); //模具更新的审批 if (oldData.getBlongType() == 5) { //将模具修改为已报废 Mould mould = new Mould(); mould.setId(oldData.getModelId()); mould.setState(4 + "");//4已报废状态 mouldMapper.updateById(mould); } } else { mf.setState(1); dynamics.setIsPass(1); } } else { mf.setState(-1); dynamics.setIsPass(0); } mouldFileMapper.updateById(mf); if (oldData.getBlongType() == 4) { if (mf.getState() == 3) { Integer count = mouldOperationDynamicsMapper.selectCount(new QueryWrapper().eq("belong_type", 4).eq("is_pass", 1).eq("mould_id", oldData.getModelId())); if (count == 0) { dynamics.setMouldId(oldData.getModelId()); dynamics.setApplicantName(oldData.getUploadtor()); dynamics.setApplicantId(oldData.getUplodtorId()); dynamics.setBelongType(oldData.getBlongType()); dynamics.setContent(Constant.APPROVAL); dynamics.setOperatorName(approverNames); dynamics.setIsPass(1); mouldOperationDynamicsMapper.insert(dynamics); } } else if (mf.getState() < 0) { Integer count = mouldOperationDynamicsMapper.selectCount(new QueryWrapper().eq("belong_type", 4).eq("is_pass", 0).eq("mould_id", oldData.getModelId())); if (count == 0) { dynamics.setMouldId(oldData.getModelId()); dynamics.setApplicantName(oldData.getUploadtor()); dynamics.setApplicantId(oldData.getUplodtorId()); dynamics.setBelongType(oldData.getBlongType()); dynamics.setContent(Constant.APPROVAL); dynamics.setOperatorName(approverNames); mouldOperationDynamicsMapper.insert(dynamics); } } } else { //添加审批记录 dynamics.setMouldId(oldData.getModelId()); dynamics.setBelongType(oldData.getBlongType()); dynamics.setFileId(oldData.getId()); if (null != oldData.getFileName2()) { dynamics.setFileName(oldData.getFileName() + "," + oldData.getFileName2()); } else { dynamics.setFileName(oldData.getFileName()); } dynamics.setOperatorId(user.getId()); dynamics.setOperatorName(user.getUsername()); dynamics.setContent(Constant.APPROVAL); mouldOperationDynamicsMapper.insert(dynamics); } } else if (user.getSubordinateType() == 1) { //生产方 if (isPass == 1) { if (oldData.getState() == 1) {//资产方也审核通过了 mf.setState(3); dynamics.setIsPass(1); //将模具修改为已报废 if (oldData.getBlongType() == 5) { Mould mould = new Mould(); mould.setId(oldData.getModelId()); mould.setState(4 + "");//4已报废状态 mouldMapper.updateById(mould); } } else { mf.setState(2); dynamics.setIsPass(1); } } else { mf.setState(-2); dynamics.setIsPass(0); } mouldFileMapper.updateById(mf); if (oldData.getBlongType() == 4) { if (mf.getState() == 3) { Integer count = mouldOperationDynamicsMapper.selectCount(new QueryWrapper().eq("belong_type", 4).eq("is_pass", 1).eq("mould_id", oldData.getModelId())); if (count == 0) { dynamics.setMouldId(oldData.getModelId()); dynamics.setApplicantName(oldData.getUploadtor()); dynamics.setApplicantId(oldData.getUplodtorId()); dynamics.setBelongType(oldData.getBlongType()); dynamics.setContent(Constant.APPROVAL); dynamics.setOperatorName(approverNames); dynamics.setIsPass(1); mouldOperationDynamicsMapper.insert(dynamics); } } else if (mf.getState() < 0) { Integer count = mouldOperationDynamicsMapper.selectCount(new QueryWrapper().eq("belong_type", 4).eq("is_pass", 0).eq("mould_id", oldData.getModelId())); if (count == 0) { dynamics.setMouldId(oldData.getModelId()); dynamics.setApplicantName(oldData.getUploadtor()); dynamics.setApplicantId(oldData.getUplodtorId()); dynamics.setBelongType(oldData.getBlongType()); dynamics.setContent(Constant.APPROVAL); dynamics.setOperatorName(approverNames); mouldOperationDynamicsMapper.insert(dynamics); } } } else { //添加审批记录 dynamics.setMouldId(oldData.getModelId()); dynamics.setBelongType(oldData.getBlongType()); dynamics.setFileId(oldData.getId()); if (null != oldData.getFileName2()) { dynamics.setFileName(oldData.getFileName() + "," + oldData.getFileName2()); } else { dynamics.setFileName(oldData.getFileName()); } dynamics.setOperatorId(user.getId()); dynamics.setOperatorName(user.getUsername()); dynamics.setContent(Constant.APPROVAL); mouldOperationDynamicsMapper.insert(dynamics); } } else { msg.setError("只有生产方和资产方审批人才能审核"); } } return msg; } @Override public HttpRespMsg dowloadFile(MouldFile projectFile, String token) { HttpRespMsg msg = new HttpRespMsg(); User user = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", token)); //添加上传记录 MouldFile file = mouldFileMapper.selectById(projectFile.getId()); MouldOperationDynamics dynamics = new MouldOperationDynamics(); dynamics.setContent(Constant.DOWNLOAD); dynamics.setApplicantId(file.getUplodtorId()); dynamics.setApplicantName(file.getUploadtor()); dynamics.setFileName(file.getFileName()); dynamics.setOperatorId(user.getId()); dynamics.setOperatorName(user.getUsername()); dynamics.setMouldId(file.getModelId()); dynamics.setFileId(file.getId()); dynamics.setBelongType(file.getBlongType()); mouldOperationDynamicsMapper.insert(dynamics); return msg; } @Override public HttpRespMsg getAllFileList(int mouldId, UserVO userVO) { List dataList = new ArrayList(); int maxBlongType = 5; for (int i = 0; i <= maxBlongType; i++) { HashMap data = new HashMap<>(); data.put("blongType", i); List list = mouldFileMapper.selectList(new QueryWrapper().eq("model_id", mouldId).eq("blong_type", i).orderByDesc("id")); data.put("list", list); dataList.add(data); } HttpRespMsg msg = new HttpRespMsg(); msg.data = dataList; return msg; } @Override public HttpRespMsg getFileList(int mouldId, Integer blongType, UserVO userVO, String path) { HttpRespMsg msg = new HttpRespMsg(); User user = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", userVO.getToken())); List list = new ArrayList<>(); List ids = new ArrayList<>(); // if(blongType == 4){ // List recursion = recursion(mouldId, ids); // list = mouldFileMapper.selectList(new QueryWrapper().in("model_id", recursion).orderByDesc("id")); // }else{ list = mouldFileMapper.selectList(new QueryWrapper().eq("model_id", mouldId).eq("blong_type", blongType).orderByDesc("id")); // } // for (MouldFile mouldFile : list) { // mouldFile.setFileUrl(path+mouldFile.getFileUrl().substring("/upload/".length())); // } msg.data = list; return msg; } @Override public HttpRespMsg delFile(MouldFile mouldFile,String token) { HttpRespMsg msg = new HttpRespMsg(); User currentUser = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", token)); if(currentUser == null){ msg.setError("用户不存在或者未登录"); }else{ if(Constant.SYS_ID .equals(currentUser.getParentId())){ //判断是不是资产方管理员 msg.data = mouldFileMapper.deleteById(mouldFile.getId()); }else{ msg.setError("您没有该权限!"); } } return msg; } @Override public HttpRespMsg getListByUserAndProjectId(UserVO userVO, PageUtil page) { HttpRespMsg msg = new HttpRespMsg(); List proIds = new ArrayList<>(); proIds.add(-1); List list = new ArrayList<>(); User currentUser = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", userVO.getToken())); PageHelper.startPage(page.getPageNum(), page.getPageSize()); if (currentUser != null) { if (Constant.SYS_ID .equals(currentUser.getParentId())) { //此时是admin,查询他所创建的项目 QueryWrapper qw = new QueryWrapper<>(); List projects = projectMapper.selectList(qw.eq("creator_id", currentUser.getId())); for (Project pro : projects) { proIds.add(pro.getId()); } list = mouldFileMapper.getFileListByProjectId(userVO, proIds); } else if (Constant.SYS_PARENT_ID .equals(currentUser.getParentId())) { //系统管理员 List projects = projectMapper.selectList(new QueryWrapper()); for (Project pro : projects) { proIds.add(pro.getId()); } list = mouldFileMapper.getFileListByProjectId(userVO, proIds); } else { QueryWrapper qw = new QueryWrapper<>(); qw.eq("manager_id", userVO.getId()); List projects = projectMapper.selectList(qw); if (projects.size() > 0) { for (Project project : projects) { proIds.add(project.getId()); } } // //充当普通人员参与的项目 List projectUsers = projectUserMapper.selectList(new QueryWrapper().eq("user_id", currentUser.getId())); if (projectUsers.size() > 0) { for (ProjectUser projectUser : projectUsers) { proIds.add(projectUser.getProjectId()); } } //充当审批人员参与的项目 List projectss = projectApproveMapper.selectList(new QueryWrapper().eq("approver_id", currentUser.getId())); if (projectss.size() > 0) { for (ProjectApprove projectUser : projectss) { proIds.add(projectUser.getProjectId()); } } list = mouldFileMapper.getFileListByProjectId(userVO, proIds); } PageInfo pageInfos = new PageInfo<>(list); msg.data = pageInfos; } else { msg.setError("用户不存在或者未登录"); } return msg; } @Override public HttpRespMsg dowloadFileList(UserVO userVO, HttpServletRequest request, HttpServletResponse response, String downloadPath, String path) throws IOException { HttpRespMsg msg = new HttpRespMsg(); User currentUser = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", userVO.getToken())); List sourceFileUrls = new ArrayList<>(); if (null != userVO.getIds()) { List ids = ListUtil.convertIntegerIdsArrayToList(userVO.getIds()); List mouldFiles = new ArrayList<>(); List newFolders = new ArrayList<>(); for (Integer id : ids) { Mould mould = mouldMapper.selectById(id); if (0 == userVO.getDwgType()) { mouldFiles = mouldFileMapper.selectList(new QueryWrapper().eq("model_id", id).eq("state", 3).ne("blong_type", 4)); } else if (1 == userVO.getDwgType()) { //模具3D mouldFiles = mouldFileMapper.selectList(new QueryWrapper().eq("model_id", id).eq("state", 3).isNull("part_id").eq("dwg_type", 1).eq("blong_type", 0)); } else if (2 == userVO.getDwgType()) { //模具2D mouldFiles = mouldFileMapper.selectList(new QueryWrapper().eq("model_id", id).eq("state", 3).isNull("part_id").eq("dwg_type", 0).eq("blong_type", 0)); } else if (3 == userVO.getDwgType()) { //零件3D mouldFiles = mouldFileMapper.selectList(new QueryWrapper().eq("model_id", id).eq("state", 3).isNotNull("part_id").eq("dwg_type", 1).eq("blong_type", 1)); } else if (4 == userVO.getDwgType()) { //零件2D mouldFiles = mouldFileMapper.selectList(new QueryWrapper().eq("model_id", id).eq("state", 3).isNotNull("part_id").eq("dwg_type", 0).eq("blong_type", 1)); } else { mouldFiles = mouldFileMapper.selectList(new QueryWrapper().eq("model_id", id).eq("state", 3).eq("blong_type", 3)); } if(mouldFiles.size()==0){ continue; } // feedBackDirectMultiDownload(request, response, downloadPath, mould, mouldFiles, path); String folderPath = path + mould.getModelNo() + mould.getModelName(); for (MouldFile mouldFile : mouldFiles) { sourceFileUrls.add(path.substring(0, path.length() - "/upload/".length()) + mouldFile.getFileUrl()); //添加下载动态 MouldOperationDynamics mod = new MouldOperationDynamics(); mod.setApplicantId(mouldFile.getUplodtorId()); mod.setApplicantName(mouldFile.getUploadtor()); mod.setFileName(mouldFile.getFileName()); mod.setOperatorId(currentUser.getId()); mod.setOperatorName(currentUser.getUsername()); mod.setContent(Constant.DOWNLOAD); mod.setFileId(mouldFile.getId()); mod.setMouldId(mould.getId()); mod.setBelongType(mouldFile.getBlongType()); mouldOperationDynamicsMapper.insert(mod); } if (sourceFileUrls.size() > 0) { String folder = FileCopyToFolderUtil.copy(sourceFileUrls, folderPath); newFolders.add(folder); } } try { // 生成的压缩文件 if(newFolders.size() > 0){ ZipFile zipFile = null; SimpleDateFormat dateSdf2 = new SimpleDateFormat("yyyy年MM月dd+HH时mm分ss秒"); String fileName = dateSdf2.format(new Date()) + ".zip"; zipFile = new ZipFile(path + fileName); ZipParameters parameters = new ZipParameters(); // 压缩方式 parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩级别 parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 要打包的文件夹 if(newFolders.size()>0){ for (String newFolder : newFolders) { zipFile.addFolder(newFolder, parameters); } } msg.data = "/upload/"+fileName; }else{ msg.setError("暂无文件"); return msg; } // File[] fs = currentFile.listFiles(); // // 遍历test文件夹下所有的文件、文件夹 // for (File f : fs) { // if (f.isDirectory()) { // zipFile.addFolder(f.getPath(), parameters); // } else { // zipFile.addFile(f, parameters); // } // } } catch (Exception e) { e.printStackTrace(); } // String filePath = this.download(downloadPath, mould, mouldFiles, path, currentUser); } return msg; } @Override public HttpRespMsg addPartFile(UserVO userVO, String path, MultipartFile[] files) { HttpRespMsg msg = new HttpRespMsg(); Mould mould = mouldMapper.selectById(userVO.getMouldId()); User user = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", userVO.getToken())); if (user != null) { List partLists = partMapper.selectList(new QueryWrapper().eq("mould_id", userVO.getMouldId())); openOfficeService.start(); for (MultipartFile file : files) { MouldFile partFile = new MouldFile(); partFile.setModelId(userVO.getMouldId()); partFile.setBlongType(1); File dir = null; dir = new File(path); // D://cloud/upload 文件上传后所存储的位置,部署到服务器上时配置服务器地址即可 if (!dir.exists()) { dir.mkdirs(); } String fileName = ""; // if (!file.isEmpty()) { fileName = file.getOriginalFilename(); String fileNamePrex = fileName.substring(0, fileName.lastIndexOf(".")); String[] split = fileNamePrex.split("\\+"); System.out.println("split" + split); for (Part part : partLists) { if (part.getPartNo().equals(split[0])) { partFile.setPartId(part.getId()); if (Constant.PART_FILE_2D.equals(split[1])) { MouldFile mouldFile = mouldFileMapper.selectOne(new QueryWrapper().eq("part_id", part.getId()).eq("dwg_type", 0)); if (mouldFile != null) { //把之前的文件删除 mouldFileMapper.deleteById(mouldFile); } partFile.setDwgType(0); } else if (Constant.PART_FILE_3D.equals(split[1])) { MouldFile mouldFile = mouldFileMapper.selectOne(new QueryWrapper().eq("part_id", part.getId()).eq("dwg_type", 1)); if (mouldFile != null) { //把之前的文件删除 mouldFileMapper.deleteById(mouldFile); } partFile.setDwgType(1); } } } partFile.setFileName(fileName); partFile.setFileSize(FileUtil.getReadableFileSize(file.getSize())); System.out.println("上传文件名称" + file.getName() + ", dir = " + dir.getAbsolutePath()); int pos = fileName.lastIndexOf("."); String rand = UUID.randomUUID().toString().replaceAll("-", ""); String sufix = fileName.substring(pos); fileName = rand + sufix; partFile.setFileType(sufix);//文件后缀 partFile.setFileUrl("/upload/" + fileName); partFile.setUplodtorId(user.getId()); partFile.setUploadtor(user.getUsername()); File saveFile = new File(dir, fileName); mouldFileMapper.insert(partFile); try { saveFile.createNewFile(); file.transferTo(saveFile); //上传完,需要生成pdf String dFile1 = path+UUID.randomUUID().toString().replaceAll("-", "")+".pdf"; File newFile = new File(dFile1); if(!newFile .exists()){ openOfficeService.office2PDF(path + fileName,dFile1); PdfFile pdfFile = new PdfFile(); pdfFile.setPdfUrl("/upload/"+dFile1.substring(path.length())); pdfFile.setFileId(partFile.getId()); pdfFile.setType(1); pdfFile.setRafId(partFile.getModelId()); pdfFile.setSourceFileUrl(partFile.getFileUrl()); pdfFileMapper.insert(pdfFile); } } catch (IOException e) { e.printStackTrace(); partFile = null; } catch (Exception e) { e.printStackTrace(); partFile = null; } //添加上传记录 MouldOperationDynamics dynamics = new MouldOperationDynamics(); dynamics.setContent(Constant.UPLOAD); dynamics.setFileName(file.getOriginalFilename()); dynamics.setOperatorId(user.getId()); dynamics.setOperatorName(user.getUsername()); dynamics.setMouldId(userVO.getMouldId()); dynamics.setFileId(partFile.getId()); mouldOperationDynamicsMapper.insert(dynamics); Project project = projectMapper.selectOne(new QueryWrapper().eq("id", mould.getProjectId())); List projectApproves = projectApproveMapper.selectList(new QueryWrapper().eq("project_id", project.getId())); String content = "零件文档"; NewsNotice newsNotice = new NewsNotice(); newsNotice.setRefId(mould.getId()); newsNotice.setProjectId(project.getId()); newsNotice.setBelongType(1); newsNotice.setProjectName(project.getProjectName() + "-" + mould.getModelName()); newsNotice.setNoticeType(Constant.APPROVEL_TYPE); newsNotice.setContent("有新的" + content + "上传,待您审批。"); newsNoticeMapper.insert(newsNotice); for (ProjectApprove projectApprove : projectApproves) { NewsNoticeUser newsNoticeUser = new NewsNoticeUser(); newsNoticeUser.setUserId(projectApprove.getApproverId()); newsNoticeUser.setNewsId(newsNotice.getId()); newsNoticeUserMapper.insert(newsNoticeUser); } // } msg.data = partFile; } openOfficeService.shutdown(); } else { msg.setError("用户不存在或者未登录"); } return msg; } @Override public HttpRespMsg downloadFileListExcel(UserVO userVO, HttpServletResponse response, String downloadPath) throws ParseException { HttpRespMsg msg = new HttpRespMsg(); List> list = new ArrayList>(); //标题 List titleList = new ArrayList(); titleList.add("文件名"); titleList.add("下载者姓名"); titleList.add("文件类型"); titleList.add("下载时间"); list.add(titleList); QueryWrapper qw = new QueryWrapper(); qw.eq("content", Constant.DOWNLOAD); // String start = userVO.getStartTime().replace("Z", " UTC");//是空格+UTC // String end = userVO.getEndTime().replace("Z", " UTC");//是空格+UTC // DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // Date date1 = df.parse(start); // System.out.println(date1); // Date date2 = df.parse(end); // System.out.println(date2); // SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.CHINA); // Calendar calendar = new GregorianCalendar(); // calendar.setTime(date1); // calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动 date=calendar.getTime(); // Date startDate1 = calendar.getTime(); // calendar.setTime(date2); // calendar.add(calendar.DATE, -1); // Date startDate = df1.parse(date1.toString()); // Date endDate2 = calendar.getTime(); // Date endDate = df1.parse(date2.toString()); SimpleDateFormat dateSdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat dateSdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat dateSdf2 = new SimpleDateFormat("yyyy年MM月dd+HH时mm分ss秒"); // String startDate = dateSdf.format(startDate1); // String endDate = dateSdf.format(endDate2); List mouldOperationDynamics = new ArrayList<>(); if (userVO.getStartTime() != null && userVO.getEndTime() != null) { mouldOperationDynamics = mouldOperationDynamicsMapper.selectOperationDynamicsList(userVO); } for (MouldOperationDynamics dynamic : mouldOperationDynamics) { List rowList = new ArrayList(); //文件名 rowList.add(dynamic.getFileName()); //操作者名字 rowList.add(dynamic.getOperatorName()); //文件类型 if (dynamic.getBelongType() == 0) { rowList.add("模具文档"); } else if (dynamic.getBelongType() == 1) { rowList.add("零件文档"); } else if (dynamic.getBelongType() == 2) { rowList.add("试模验收"); } else if (dynamic.getBelongType() == 3) { rowList.add("保养方案"); } else if (dynamic.getBelongType() == 4) { rowList.add("模具更新"); } else if (dynamic.getBelongType() == 5) { rowList.add("模具报废"); } //下载时间 rowList.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(dynamic.getIndate()) ); list.add(rowList); } Mould mould = mouldMapper.selectById(userVO.getMouldId()); String fileUrl = ExcelUtil.exportGeneralExcelByTitleAndList(response, mould.getModelNo() + mould.getModelName() + dateSdf2.format(new Date()), list, downloadPath); msg.data = fileUrl; return msg; } @Override public HttpRespMsg mouldUpdateCheck(UserVO userVO) { HttpRespMsg msg = new HttpRespMsg(); User user = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", userVO.getToken())); boolean isCanCreate = false; if (user == null) { msg.setError("用户不存在或者未登录"); return msg; } else { MouldOperationDynamics dynamics = mouldOperationDynamicsMapper.selectOne(new QueryWrapper().eq("belongType", 4).eq("mould_id", userVO.getMouldId()).orderByDesc("id")); if (null != dynamics) { if (0 == dynamics.getIsPass()) { isCanCreate = true; } } else { isCanCreate = true; } } msg.data = isCanCreate; return msg; } //生成.zip文件 public String download(String downloadPath, Mould vo, List mouldFiles, String oldFilePath, User user) { //需要压缩的文件--包括文件地址和文件名 // 要生成的压缩文件地址和文件名称 String desPath = oldFilePath + vo.getModelNo() + vo.getModelName() + ".zip"; File zipFile = new File(desPath); ZipOutputStream zipStream = null; FileInputStream zipSource = null; BufferedInputStream bufferStream = null; try { //构造最终压缩包的输出流 zipStream = new ZipOutputStream(new FileOutputStream(zipFile)); if (mouldFiles.size() > 0) { log.info("mouldFiles------------->" + mouldFiles); for (int i = 0; i < mouldFiles.size(); i++) { //添加下载动态 MouldOperationDynamics mod = new MouldOperationDynamics(); mod.setApplicantId(mouldFiles.get(i).getUplodtorId()); mod.setApplicantName(mouldFiles.get(i).getUploadtor()); mod.setFileName(mouldFiles.get(i).getFileName()); mod.setOperatorId(user.getId()); mod.setOperatorName(user.getUsername()); mod.setContent(Constant.DOWNLOAD); mod.setFileId(mouldFiles.get(i).getId()); mod.setMouldId(vo.getId()); mod.setBelongType(mouldFiles.get(i).getBlongType()); mouldOperationDynamicsMapper.insert(mod); File file = new File(oldFilePath.substring(0, oldFilePath.length() - "/upload/".length()) + mouldFiles.get(i).getFileUrl()); log.info("file=========>" + oldFilePath.substring(0, oldFilePath.length() - "/upload/".length()) + mouldFiles.get(i).getFileUrl()); //将需要压缩的文件格式化为输入流 zipSource = new FileInputStream(file); //压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样 ZipEntry zipEntry = new ZipEntry(mouldFiles.get(i).getFileName()); //定位该压缩条目位置,开始写入文件到压缩包中 zipStream.putNextEntry(zipEntry); //输入缓冲流 bufferStream = new BufferedInputStream(zipSource, 1024 * 10); int read = 0; //创建读写缓冲区 byte[] buf = new byte[1024 * 10]; while ((read = bufferStream.read(buf, 0, 1024 * 10)) != -1) { zipStream.write(buf, 0, read); } } } } catch (Exception e) { e.printStackTrace(); } finally { //关闭流 try { if (null != bufferStream) bufferStream.close(); if (null != zipStream) zipStream.close(); if (null != zipSource) zipSource.close(); } catch (IOException e) { e.printStackTrace(); } } return "/upload/" + vo.getModelNo() + vo.getModelName() + ".zip"; } }