zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package cn.autoform.web.service;
 
import cn.autoform.db.entity.CustomButtonEntity;
import cn.autoform.db.entity.FormresourceEntity;
import cn.autoform.db.entity.TabConfigurationEntity;
import cn.autoform.util.UUIDUtil;
import cn.autoform.util.tool.ObjectUtil;
import cn.autoform.web.service.customButton.CustomButtonService;
import cn.autoform.web.service.forminterface.FormInterfaceService;
import cn.autoform.web.service.formresource.FormResourceService;
import cn.autoform.web.service.formsetting.FormsettingService;
import cn.autoform.web.service.tabConfiguration.TabConfigurationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.*;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class FormInfoService {
 
    @Value("${server.path}")
    private String serverPath;
 
    @Value("${server.url}")
    private String serverUrl;
 
    @Resource
    private FormsettingService formsettingService;
 
    @Resource
    private FormResourceService formResourceService;
 
    @Resource
    private CustomButtonService customButtonService;
 
    @Resource
    private TabConfigurationService tabConfigurationService;
 
    @Resource
    private FormInterfaceService formInterfaceService;
 
    public void copyFormInfo(String sourceFormId, String sourceTenantId
            , String targetFormId, String targetTenantId){
        // 表单基础按钮
        formsettingService.copyFormsetting(sourceFormId, sourceTenantId, targetFormId, targetTenantId);
        // 表单自定义js存储路径
        copyJS(sourceFormId, sourceTenantId, targetFormId, targetTenantId)
                .stream()
                .forEach(path->{
                    FormresourceEntity formresourceEntity = new FormresourceEntity();
                    formresourceEntity.setTenantID(targetTenantId);
                    formresourceEntity.setFormID(targetFormId);
                    formresourceEntity.setResourceFilepath(path);
                    formResourceService.createFormresource(formresourceEntity);
                });
        // 表单自定义按钮
        List<CustomButtonEntity> customButtonEntities = customButtonService.selectCustomButton(sourceTenantId, sourceFormId);
        if(ObjectUtil.notEmpty(customButtonEntities)) {
            customButtonService.addCustomButton(customButtonEntities.stream().map(c -> {
                c.setFormID(targetFormId);
                c.setTenantID(targetTenantId);
                c.setId(null);
                return c;
            }).collect(Collectors.toList()));
        }
        // 选项卡
        TabConfigurationEntity tabConfigurationEntity = new TabConfigurationEntity();
        tabConfigurationEntity.setFormID(sourceFormId);
        tabConfigurationEntity.setTenantID(sourceTenantId);
        tabConfigurationEntity = tabConfigurationService.queryTabConfiguration(tabConfigurationEntity).stream().findFirst().orElse(null);
        if(tabConfigurationEntity != null) {
            tabConfigurationEntity.setTenantID(targetTenantId);
            tabConfigurationEntity.setFormID(targetFormId);
            tabConfigurationService.saveTabConfiguration(tabConfigurationEntity);
        }
 
        // 接口注册
        formInterfaceService.getFormInterfaceList(sourceTenantId, sourceFormId)
                .stream()
                .map(formInterfaceEntity -> {
                    formInterfaceEntity.setINTERFACE(formInterfaceEntity.getInterfaceName());
                    formInterfaceEntity.setFormID(targetFormId);
                    formInterfaceEntity.setTenantID(targetTenantId);
                    return formInterfaceEntity;
                })
                .forEach(formInterfaceService::createFormInterface);
    }
 
    public List<String> copyJS(String sourceFormId, String sourceTenantId
            , String targetFormId, String targetTenantId){
        File[] files = Paths.get(serverPath, "resources", sourceTenantId, sourceFormId, "js").toFile().listFiles();
        File target;
        String targetPath;
        List<String> url = new ArrayList<>();
        if(files == null || files.length == 0){
            return url;
        }
        Arrays.sort(files, (f1, f2)->(int)(f2.lastModified() - f1.lastModified()));
        File file = files[0];
        targetPath = Paths.get("resources", targetTenantId, targetFormId, "js", file.getName()).toString();
        target = Paths.get(serverPath, targetPath).toFile();
        copy(file, target);
        url.add(serverUrl+targetPath);
        return url;
    }
 
    public void copy(File source, File target) {
        if(!target.getParentFile().exists()){
            target.getParentFile().mkdirs();
        }
        try(InputStream is = new FileInputStream(source);
                OutputStream os = new FileOutputStream(target)){
            byte[] buffer = new byte[1024];
            int size;
            while ((size = is.read(buffer)) > 0){
                os.write(buffer, 0 , size);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        FormInfoService formInfoService = new FormInfoService();
        formInfoService.serverPath = "/Users/wangyx/Downloads";
        formInfoService.serverUrl  = "http://www.baidu.com";
        System.out.println(
                formInfoService.copyJS("aaa", "111", "bbb", "222")
        );
    }
}