管道基础大数据平台系统开发-【后端】-Server
1.6
13693261870
2023-01-06 d12b5b41ee33e7eb57f3c3fe00f4fae53eb93388
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.lf.server.service.data;
 
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.helper.FileHelper;
import com.lf.server.helper.StringHelper;
import com.lf.server.service.all.BaseUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.util.List;
 
/**
 * 数据上传
 * @author WWW
 */
@Service
public class DataUploadService extends BaseUploadService {
    @Autowired
    MetaService metaService;
 
    /**
     * 插入文件
     */
    public int insertFiles(MetaEntity entity, List<MetaEntity> list) {
        int count = 0;
        try {
            String temp = pathHelper.getConfig().getTempPath();
            String root = pathHelper.getUploadFullPath();
 
            for (MetaEntity mf : list) {
                String filePath = getFilePath(temp, root, mf);
 
                // 元数据
                MetaEntity me = createMetaEntity(entity, mf, filePath);
                if (metaService.insert(me) > 0) {
                    count++;
                }
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
 
        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, String filePath) {
        MetaEntity me = new MetaEntity();
        me.setEventid(StringHelper.getGuid());
        me.setDirid(entity.getDirid());
        me.setDepid(entity.getDepid());
        me.setVerid(entity.getVerid());
        me.setName(mf.getName());
        me.setType("file");
        me.setGuid(entity.getGuid());
        me.setPath(FileHelper.getRelativePath(filePath));
        me.setSizes(mf.getSizes());
        me.setCreateTime(entity.getCreateTime());
        me.setCreateUser(entity.getCreateUser());
 
        return me;
    }
}