Commit b1fc4893 authored by 赵啸非's avatar 赵啸非

添加公共庫

parent 99bb1321
......@@ -12,12 +12,6 @@ import com.mortals.xhx.module.matter.model.MatterDatumEntity;
*/
public interface MatterDatumService extends ICRUDService<MatterDatumEntity,Long>{
/**
* 合成doc数据模板
* @param context
* @return
*/
String mergeFormToDoc(MatterDatumEntity docFormVo, Context context);
/**
* 推荐or取消推荐
......
......@@ -143,104 +143,7 @@ public class MatterDatumServiceImpl extends AbstractCRUDServiceImpl<MatterDatumD
}
}
@Override
public String mergeFormToDoc(MatterDatumEntity docFormVo, Context context) {
String rootPath = this.filePath.endsWith("/") ? this.filePath : this.filePath + "/";
//转换表单参数为map集合
ObjectMapper mapper = new ObjectMapper();
ConfigureBuilder builder = Configure.builder();
builder.buildGrammerRegex(RegexUtils.createGeneral("{{", "}}"));
try {
Map<String, Object> data = mapper.readValue(docFormVo.getFormContent(), Map.class);
//遍历查看是否有图片数据,多选框
data.entrySet().stream().forEach(entry -> {
if (entry.getKey().indexOf("@image") != -1) {
//获取值 读取本地图片
String imagepath = uploadService.getFilePath(entry.getValue().toString());
log.info("Image path: " + imagepath);
try {
PictureRenderData pictureRenderData = Pictures.ofStream(new FileInputStream(imagepath), PictureType.JPEG)
.size(100, 120).create();
data.put(StrUtil.removePrefixIgnoreCase(entry.getKey(),"@"),pictureRenderData);
//entry.setValue(pictureRenderData);
} catch (FileNotFoundException e) {
log.error("error", e);
}
}
//构建填充复选框值
if (entry.getKey().indexOf("ck_") != -1 && entry.getKey().indexOf("<") != -1) {
//获取复选框选项,并渲染值
String ckStr = StringUtils.substringBetween(entry.getKey(), "<", ">");
//获取所有选项
List<String> checkboxs = StrUtil.splitTrim(ckStr, "-", -1);
//选中的项
List<String> ckList = Convert.toList(String.class, entry.getValue());
//合并
String ckStrs = checkboxs.stream().map(item -> {
for (String checked : ckList) {
if (checked.equals(item)) {
Map<Boolean, String> map = new HashMap<>();
map.put(true, item);
return map;
}
}
Map<Boolean, String> map = new HashMap<>();
map.put(false, item);
return map;
}).map(m -> m.entrySet().stream().map(m1 -> ExportDocUtil.createCheckBoxStrOther(m1.getKey(), m1.getValue())).findFirst().orElseGet(() -> "")
).collect(Collectors.joining(""));
entry.setValue(ckStrs);
}
//TODO 单选
//绑定动态表单
if (entry.getKey().indexOf("dt_") != -1) {
builder.bind(entry.getKey(), new MultipleRowTableRenderPolicy());
}
});
//查询是否有多选框,
//获取模板文件地址
MatterDatumEntity docTemplateEntity = this.get(docFormVo.getId(),context);
if (!ObjectUtils.isEmpty(docTemplateEntity)) {
String path = rootPath + docTemplateEntity.getTemplatePath();
String templateName = FileUtil.getName(path);
Configure config = builder.build();
XWPFTemplate template = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
template = XWPFTemplate.compile(path, config).render(data);
byteArrayOutputStream = new ByteArrayOutputStream();
template.write(byteArrayOutputStream);
MultipartFile multipartFile = new MockMultipartFile(templateName, templateName,
ContentType.APPLICATION_OCTET_STREAM.toString(), byteArrayOutputStream.toByteArray());
String mergedocPath = uploadService.saveFileUpload(multipartFile, "/mergedoc", context.getUser());
log.info("mergedocPath:" + mergedocPath);
String mergedoc = rootPath + mergedocPath;
//转换预览图片
String fileName = RandomUtil.randomNumbers(12) + ".jpg";
String preView = this.filePath + "/preview/" + fileName;
WordUtil.convertWordToJPEG(mergedoc, preView);
log.info("preView:" + mergedocPath);
//下载地址拼装
String returnStr = mergedocPath + ";" + "/preview/" + fileName;
return returnStr;
} catch (Exception e) {
log.error("渲染模板失败:", e);
} finally {
try {
template.close();
byteArrayOutputStream.close();
} catch (IOException e) {
log.error("ioEx:", e);
}
}
}
} catch (JsonProcessingException e) {
log.error("转换异常", e);
throw new AppException("表单参数转换异常:" + e.getMessage());
}
return "";
}
@Override
public void recommend(Long id) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment