管道基础大数据平台系统开发-【后端】-Server
1.5
13693261870
2023-01-05 e5f61db766b3eaa28506835e3581cbd2fdc8533d
src/main/java/com/lf/server/service/data/DataUploadService.java
@@ -1,8 +1,9 @@
package com.lf.server.service.data;
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.entity.data.MetaFileEntity;
import com.lf.server.helper.FileHelper;
import com.lf.server.service.all.BaseUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
@@ -14,24 +15,32 @@
 */
@Service
public class DataUploadService extends BaseUploadService {
    @Autowired
    MetaService metaService;
    /**
     * 插入文件
     */
    public int insertFiles(MetaEntity entity, List<MetaFileEntity> list) {
    public int insertFiles(MetaEntity entity, List<MetaEntity> list) {
        int count = 0;
        try {
            String temp = pathHelper.getConfig().getTempPath();
            String root = pathHelper.getUploadFullPath();
            for (MetaFileEntity mf : list) {
                // 移动文件
                File file = new File(temp + File.separator + mf.getPath());
                File newFile = new File(root + File.separator + mf.getName());
                file.renameTo(newFile);
            for (MetaEntity mf : list) {
                String filePath = getFilePath(temp, root, mf);
                // 元数据
                MetaEntity me = createMetaEntity(entity, mf);
                if (metaService.insert(me) < 1) {
                    continue;
                }
                // 元数据文件
                MetaEntity mfe = createMetaFileEntity(me, mf, filePath);
                if (metaService.insert(mfe) > 0) {
                    count++;
                }
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
@@ -39,4 +48,59 @@
        return count;
    }
    /**
     * 获取文件路径
     */
    private String getFilePath(String temp, String root, MetaEntity mf) {
        // 移动文件
        File file = new File(temp + File.separator + mf.getPath());
        File newFile = new File(root + File.separator + mf.getGuid());
        MetaEntity old = metaService.selectByGuid(mf.getGuid());
        String filePath = null;
        if (null == old) {
            filePath = newFile.getPath();
            file.renameTo(newFile);
        } else {
            filePath = old.getPath();
            file.delete();
        }
        return filePath;
    }
    /**
     * 创建元数据实体
     */
    private MetaEntity createMetaEntity(MetaEntity entity, MetaEntity mf) {
        MetaEntity me = new MetaEntity();
        me.setDepid(entity.getDepid());
        me.setDirid(entity.getDirid());
        me.setVerid(entity.getVerid());
        me.setType("file");
        me.setName(mf.getName());
        me.setSizes(mf.getSizes());
        me.setCreateTime(entity.getCreateTime());
        me.setCreateUser(entity.getCreateUser());
        return me;
    }
    /**
     * 创建元数据文件实体
     */
    private MetaEntity createMetaFileEntity(MetaEntity me, MetaEntity entity, String filePath) {
        MetaEntity mfe = new MetaEntity();
        mfe.setName(entity.getName());
        mfe.setMetaid(me.getId());
        mfe.setGuid(entity.getGuid());
        mfe.setSizes(entity.getSizes());
        mfe.setPath(FileHelper.getRelativePath(filePath));
        mfe.setCreateUser(me.getCreateUser());
        mfe.setCreateTime(me.getCreateTime());
        return mfe;
    }
}