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")
|
);
|
}
|
}
|