package com.zcloud.service.bus.impl; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.zcloud.dto.TenCorpDto; import com.zcloud.service.mq.DockSendMessageService; import com.zcloud.util.*; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.zcloud.entity.Page; import com.zcloud.entity.PageData; import com.zcloud.mapper.datasource.bus.ImgFilesMapper; import com.zcloud.service.bus.ImgFilesService; import org.springframework.web.multipart.MultipartFile; /** * 说明:图片管理 * 作者:luoxiaobao * 时间:2020-12-24 * 官网:www.zcloudchina.com */ @Service @Transactional //开启事物 public class ImgFilesServiceImpl implements ImgFilesService{ @Autowired private ImgFilesMapper imgfilesMapper; @Autowired private DockSendMessageService dockSendMessageService; /**新增 * @param pd * @throws Exception */ public void save(PageData pd)throws Exception{ imgfilesMapper.save(pd); } /**删除 * @param pd * @throws Exception */ @Override public void delete(PageData pd){ imgfilesMapper.delete(pd); } /**删除 * @param pd * @throws Exception */ public void deleteList(PageData pd)throws Exception{ imgfilesMapper.deleteList(pd); } /**修改 * @param pd * @throws Exception */ public void edit(PageData pd)throws Exception{ imgfilesMapper.edit(pd); } /**列表 * @param page * @throws Exception */ public List list(Page page)throws Exception{ return imgfilesMapper.datalistPage(page); } /**列表(全部) * @param pd * @throws Exception */ public List listAll(PageData pd)throws Exception{ return imgfilesMapper.listAll(pd); } /**通过id获取数据 * @param pd * @throws Exception */ public PageData findById(PageData pd)throws Exception{ return imgfilesMapper.findById(pd); } /**批量删除 * @param ArrayDATA_IDS * @throws Exception */ public void deleteAll(String[] ArrayDATA_IDS)throws Exception{ imgfilesMapper.deleteAll(ArrayDATA_IDS); } /**列表(全部) * @param pd * @throws Exception */ public List listAllByIds(PageData pd)throws Exception{ return imgfilesMapper.listAllByIds(pd); } /** * 根据标识 和 类型获取 附件信息 * @param key * @param type * @return * @throws Exception */ public List getListByKeyAndType (Object key ,String type) throws Exception{ if(!Tools.isEmpty(key)){ PageData pd = new PageData(); pd.put("FOREIGN_KEY",key); pd.put("TYPE", type); return imgfilesMapper.listAll(pd); } return new ArrayList<>(); } /** * 根据标识 和 类型获取 附件信息 * @param key * @param type * @return * @throws Exception */ public List getListByKeyAndTypeV2 (PageData pd, String key ,String type) throws Exception{ if(!Tools.isEmpty(pd)){ PageData img = new PageData(); pd.put("FOREIGN_KEY",pd.get(key)); pd.put("TYPE", type); return imgfilesMapper.listAll(pd); } return null; } @Override public void deleteInspectionHiddenFile(PageData pd) throws Exception { imgfilesMapper.deleteInspectionHiddenFile(pd); } @Override public void deleteByPath(PageData pd) throws Exception { imgfilesMapper.deleteByPath(pd); } @Override public void hideImg(PageData forward) { imgfilesMapper.hideImg(forward); } public List getListByKeyAndType (Object key) throws Exception{ if(!Tools.isEmpty(key)){ PageData pd = new PageData(); pd.put("FOREIGN_KEY",key); return imgfilesMapper.listAll(pd); } return new ArrayList<>(); } @Override public void uploadPicture(MultipartFile[] files, String TYPE, String FOREIGN_KEY) { if (files != null && files.length > 0) { for (int i = 0; i < files.length; i++) { String suffixName = files[i].getOriginalFilename().substring(files[i].getOriginalFilename().lastIndexOf(".")+1).toLowerCase(); if (!"json".equals(suffixName) && !"pdf".equals(suffixName) && !"jpg".equals(suffixName) && !"jpeg".equals(suffixName) && !"png".equals(suffixName) && !"mp4".equals(suffixName)) { return ; } } for (int i = 0; i < files.length; i++) { MultipartFile file = files[i]; // 保存文件 File tempFile = new File(file.getOriginalFilename()); try { FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile); } catch (IOException e) { throw new RuntimeException(e); } if(!FileUpload.isImage(tempFile) && !TYPE.equals("2")) {//四色图改为json文件,不用判断类型 return ; } tempFile.delete(); if(TYPE.equals("2")) {//先删除原四色图 PageData pd2 = new PageData(); pd2.put("FOREIGN_KEY",FOREIGN_KEY); pd2.put("TYPE",TYPE); List four = null; try { four = listAll(pd2); } catch (Exception e) { throw new RuntimeException(e); } for (PageData pageData : four) { File old = new File(PathUtil.getProjectpath()+pageData.getString("FILEPATH")); old.delete(); try { delete(pageData); } catch (Exception e) { throw new RuntimeException(e); } } } String ffile = DateUtil.getDays(); // String filePath = PathUtil.getProjectpath() + Const.FILEPATHFILE + ffile; //文件上传路径 // String fileName = FileUpload.fileUp(file, filePath, this.get32UUID()); //执行上传 String fileName = UuidUtil.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); try { Smb.sshSftp(file, fileName, Const.FILEPATHYHTP + Jurisdiction.getCORPINFO_ID() + "/" + ffile); } catch (Exception e) { throw new RuntimeException(e); } PageData pd = new PageData(); pd.put("IMGFILES_ID", UuidUtil.get32UUID()); pd.put("FILEPATH", Const.FILEPATHYHTP + Jurisdiction.getCORPINFO_ID() + "/" + ffile + "/" + fileName); pd.put("TYPE", TYPE); pd.put("FOREIGN_KEY", FOREIGN_KEY); try { save(pd); } catch (Exception e) { throw new RuntimeException(e); } } } } }