管道基础大数据平台系统开发-【后端】-Server
1
sws
2022-11-26 ab849f796bdc17236a95ea5fe5c166fb8f24a75c
src/main/java/com/lf/server/service/data/DataQueryService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,85 @@
package com.lf.server.service.data;
import com.lf.server.entity.all.SettingData;
import com.lf.server.entity.sys.AttachEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.FileHelper;
import com.lf.server.helper.PathHelper;
import com.lf.server.service.sys.AttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * æ•°æ®æ£€ç´¢æœåŠ¡
 * @author WWW
 */
@Service
public class DataQueryService {
    @Autowired
    PathHelper pathHelper;
    @Autowired
    AttachService attachService;
    /**
     * ä¸Šä¼ æ–‡ä»¶
     */
    public Integer uploadFiles(UserEntity ue, String tabName, String eventid, MultipartFile[] files, HttpServletRequest req) throws IOException {
        List<AttachEntity> list = new ArrayList<AttachEntity>();
        for (MultipartFile file : files) {
            if (file.getSize() > SettingData.MAX_FILE_SIZE) {
                continue;
            }
            // ä¼ è¾“文件
            String fileName = file.getOriginalFilename();
            String filePath = pathHelper.getTempPath() + File.separator + fileName;
            File newFile = new File(filePath);
            file.transferTo(newFile);
            double sizes = FileHelper.sizeToMb(file.getSize());
            // èŽ·å–MD5
            String md5 = FileHelper.getFileMd5(filePath);
            AttachEntity entity = attachService.selectByGuid(md5);
            AttachEntity ae = getAttachEntity(ue, tabName, eventid, fileName, md5, sizes);
            if (entity != null) {
                ae.setPath(entity.getPath());
                newFile.delete();
            }else {
                String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath();
                newFile.renameTo(new File(targetPath));
            }
            list.add(ae);
        }
        return attachService.inserts(list);
    }
    /**
     * èŽ·å–é™„ä»¶å®žä½“ç±»
     */
    protected AttachEntity getAttachEntity(UserEntity ue, String tabName, String eventid, String fileName, String md5, double sizes) {
        AttachEntity entity = new AttachEntity();
        entity.setTab(tabName);
        entity.setTabGuid(eventid);
        entity.setName(fileName);
        entity.setGuid(md5);
        String subPath = PathHelper.getUploadPath() + File.separator + md5;
        entity.setPath(subPath);
        entity.setSizes(sizes);
        if (ue != null) {
            entity.setCreateUser(ue.getId());
        }
        return entity;
    }
}