zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
commit | author | age
a18bfa 1 package cn.autoform.web.service;
Z 2
3 import cn.autoform.db.entity.CustomButtonEntity;
4 import cn.autoform.db.entity.FormresourceEntity;
5 import cn.autoform.db.entity.TabConfigurationEntity;
6 import cn.autoform.util.UUIDUtil;
7 import cn.autoform.util.tool.ObjectUtil;
8 import cn.autoform.web.service.customButton.CustomButtonService;
9 import cn.autoform.web.service.forminterface.FormInterfaceService;
10 import cn.autoform.web.service.formresource.FormResourceService;
11 import cn.autoform.web.service.formsetting.FormsettingService;
12 import cn.autoform.web.service.tabConfiguration.TabConfigurationService;
13 import org.springframework.beans.factory.annotation.Value;
14 import org.springframework.stereotype.Service;
15
16 import javax.annotation.Resource;
17 import java.io.*;
18 import java.nio.file.Paths;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.stream.Collectors;
23
24 @Service
25 public class FormInfoService {
26
27     @Value("${server.path}")
28     private String serverPath;
29
30     @Value("${server.url}")
31     private String serverUrl;
32
33     @Resource
34     private FormsettingService formsettingService;
35
36     @Resource
37     private FormResourceService formResourceService;
38
39     @Resource
40     private CustomButtonService customButtonService;
41
42     @Resource
43     private TabConfigurationService tabConfigurationService;
44
45     @Resource
46     private FormInterfaceService formInterfaceService;
47
48     public void copyFormInfo(String sourceFormId, String sourceTenantId
49             , String targetFormId, String targetTenantId){
50         // 表单基础按钮
51         formsettingService.copyFormsetting(sourceFormId, sourceTenantId, targetFormId, targetTenantId);
52         // 表单自定义js存储路径
53         copyJS(sourceFormId, sourceTenantId, targetFormId, targetTenantId)
54                 .stream()
55                 .forEach(path->{
56                     FormresourceEntity formresourceEntity = new FormresourceEntity();
57                     formresourceEntity.setTenantID(targetTenantId);
58                     formresourceEntity.setFormID(targetFormId);
59                     formresourceEntity.setResourceFilepath(path);
60                     formResourceService.createFormresource(formresourceEntity);
61                 });
62         // 表单自定义按钮
63         List<CustomButtonEntity> customButtonEntities = customButtonService.selectCustomButton(sourceTenantId, sourceFormId);
64         if(ObjectUtil.notEmpty(customButtonEntities)) {
65             customButtonService.addCustomButton(customButtonEntities.stream().map(c -> {
66                 c.setFormID(targetFormId);
67                 c.setTenantID(targetTenantId);
68                 c.setId(null);
69                 return c;
70             }).collect(Collectors.toList()));
71         }
72         // 选项卡
73         TabConfigurationEntity tabConfigurationEntity = new TabConfigurationEntity();
74         tabConfigurationEntity.setFormID(sourceFormId);
75         tabConfigurationEntity.setTenantID(sourceTenantId);
76         tabConfigurationEntity = tabConfigurationService.queryTabConfiguration(tabConfigurationEntity).stream().findFirst().orElse(null);
77         if(tabConfigurationEntity != null) {
78             tabConfigurationEntity.setTenantID(targetTenantId);
79             tabConfigurationEntity.setFormID(targetFormId);
80             tabConfigurationService.saveTabConfiguration(tabConfigurationEntity);
81         }
82
83         // 接口注册
84         formInterfaceService.getFormInterfaceList(sourceTenantId, sourceFormId)
85                 .stream()
86                 .map(formInterfaceEntity -> {
87                     formInterfaceEntity.setINTERFACE(formInterfaceEntity.getInterfaceName());
88                     formInterfaceEntity.setFormID(targetFormId);
89                     formInterfaceEntity.setTenantID(targetTenantId);
90                     return formInterfaceEntity;
91                 })
92                 .forEach(formInterfaceService::createFormInterface);
93     }
94
95     public List<String> copyJS(String sourceFormId, String sourceTenantId
96             , String targetFormId, String targetTenantId){
97         File[] files = Paths.get(serverPath, "resources", sourceTenantId, sourceFormId, "js").toFile().listFiles();
98         File target;
99         String targetPath;
100         List<String> url = new ArrayList<>();
101         if(files == null || files.length == 0){
102             return url;
103         }
104         Arrays.sort(files, (f1, f2)->(int)(f2.lastModified() - f1.lastModified()));
105         File file = files[0];
106         targetPath = Paths.get("resources", targetTenantId, targetFormId, "js", file.getName()).toString();
107         target = Paths.get(serverPath, targetPath).toFile();
108         copy(file, target);
109         url.add(serverUrl+targetPath);
110         return url;
111     }
112
113     public void copy(File source, File target) {
114         if(!target.getParentFile().exists()){
115             target.getParentFile().mkdirs();
116         }
117         try(InputStream is = new FileInputStream(source);
118                 OutputStream os = new FileOutputStream(target)){
119             byte[] buffer = new byte[1024];
120             int size;
121             while ((size = is.read(buffer)) > 0){
122                 os.write(buffer, 0 , size);
123             }
124             os.flush();
125         } catch (FileNotFoundException e) {
126             e.printStackTrace();
127         } catch (IOException e) {
128             e.printStackTrace();
129         }
130     }
131
132     public static void main(String[] args) {
133         FormInfoService formInfoService = new FormInfoService();
134         formInfoService.serverPath = "/Users/wangyx/Downloads";
135         formInfoService.serverUrl  = "http://www.baidu.com";
136         System.out.println(
137                 formInfoService.copyJS("aaa", "111", "bbb", "222")
138         );
139     }
140 }