master
wjt 2 years ago
parent 120108817a
commit a921772e19

@ -1,5 +1,5 @@
server:
port: 80
port: 8899
spring:
redis:
#数据库索引
@ -38,11 +38,11 @@ spring:
filters:
- StripPrefix=1
- id: cdp-tool-xhs-api
uri: lb://cdp-tool-xhs-api
predicates:
- Path=/api-xhs/**
filters:
- StripPrefix=1
uri: lb://cdp-tool-xhs-api
predicates:
- Path=/api-xhs/**
filters:
- StripPrefix=1
nacos:
server-addr: 172.16.69.134:8848
# server-addr: 101.35.109.129:8848

@ -0,0 +1,48 @@
package com.baiye.controller;
import cn.hutool.core.text.CharSequenceUtil;
import com.baiye.exception.global.BadRequestException;
import com.baiye.properties.FileProperties;
import com.baiye.util.FileUtil;
import com.google.common.collect.ImmutableMap;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
/**
* @author jt
*/
@RestController
@RequestMapping("/pictures")
@RequiredArgsConstructor
@Slf4j
public class UploadController {
private final FileProperties properties;
@ApiOperation("修改头像")
@PostMapping(value = "/upload")
public ResponseEntity<Object> updateAvatar(@RequestParam("file") MultipartFile uploadFile) {
String image = "gif jpg png jpeg";
String fileType = FileUtil.getExtensionName
(uploadFile.getOriginalFilename());
if (fileType != null && !image.contains(fileType)) {
throw new BadRequestException("文件格式错误!, 仅支持 " + image + " 格式");
}
// 应改为File路径
File file = FileUtil.upload
(uploadFile, properties.getPath().getAvatar());
ImmutableMap<String, String> picture = ImmutableMap.of
("picture", file != null
? file.getName() : CharSequenceUtil.SPACE);
return new ResponseEntity<>(picture, HttpStatus.OK);
}
}

@ -63,5 +63,5 @@ swagger:
# 文件存储
storage:
download-word: C:\Users\jt\Desktop\word.xls
download-variable: C:\Users\jt\Desktop\variable.xls
download-word: C:\Users\jt\Desktop\word.xlsx
download-variable: C:\Users\jt\Desktop\wordVariable.xlsx

@ -90,5 +90,5 @@ ip:
# 文件存储
storage:
download-word: http://116.62.78.17/xhs/template/word.xls
download-variable: http://116.62.78.17/xhs/template/variable.xls
download-word: https://cdp.baiyee.vip/xhs/template/word.xlsx
download-variable: https://cdp.baiyee.vip/xhs/template/wordVariable.xlsx

@ -63,5 +63,5 @@ swagger:
# 文件存储
storage:
download-word: http://8.130.96.163:8001/xhs/template/word.xls
download-variable: http://8.130.96.163:8001/xhs/template/wordVariable.xls
download-word: http://8.130.96.163:8001/xhs/template/word.xlsx
download-variable: http://8.130.96.163:8001/xhs/template/wordVariable.xlsx

@ -30,4 +30,7 @@ public class SourceOrganize implements Serializable {
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "创建人")
private Long createBy;
}

@ -8,6 +8,7 @@ import io.swagger.annotations.ApiOperation;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @author jt
@ -29,4 +30,10 @@ public class Template implements Serializable {
@ApiModelProperty(value = "类型 1-文字模板 2-图片 3-文字变量模板")
private int templateType;
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "创建人")
private Long createBy;
}

@ -9,6 +9,7 @@ import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
/**
* @author jt
@ -30,4 +31,10 @@ public class Variable implements Serializable {
@ApiModelProperty(value = "变量内容")
private String variableContent;
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "创建人")
private Long createBy;
}

@ -0,0 +1,331 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baiye.util;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* File hutool
*
* @author Zheng Jie
* @date 2018-12-27
*/
public class FileUtil extends cn.hutool.core.io.FileUtil {
private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
/**
*
* <br>
* windows Linux ,
* windows \\==\
*
* <pre>
* java.io.tmpdir
* windows : C:\Users/xxx\AppData\Local\Temp\
* linux: /temp
* </pre>
*/
public static final String SYS_TEM_DIR = System.getProperty("java.io.tmpdir") + File.separator;
/**
* GB
*/
private static final int GB = 1024 * 1024 * 1024;
/**
* MB
*/
private static final int MB = 1024 * 1024;
/**
* KB
*/
private static final int KB = 1024;
/**
*
*/
private static final DecimalFormat DF = new DecimalFormat("0.00");
public static final String IMAGE = "图片";
public static final String TXT = "文档";
public static final String MUSIC = "音乐";
public static final String VIDEO = "视频";
public static final String OTHER = "其他";
/**
* MultipartFileFile
*/
public static File toFile(MultipartFile multipartFile) {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String prefix = "." + getExtensionName(fileName);
File file = null;
try {
// 用uuid作为文件名防止生成的临时文件重复
file = File.createTempFile(IdUtil.simpleUUID(), prefix);
// MultipartFile to File
multipartFile.transferTo(file);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return file;
}
/**
* .
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/**
* Java
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
/**
*
*/
public static String getSize(long size) {
String resultSize;
if (size / GB >= 1) {
//如果当前Byte的值大于等于1GB
resultSize = DF.format(size / (float) GB) + "GB ";
} else if (size / MB >= 1) {
//如果当前Byte的值大于等于1MB
resultSize = DF.format(size / (float) MB) + "MB ";
} else if (size / KB >= 1) {
//如果当前Byte的值大于等于1KB
resultSize = DF.format(size / (float) KB) + "KB ";
} else {
resultSize = size + "B ";
}
return resultSize;
}
/**
* inputStream File
*/
static File inputStreamToFile(InputStream ins, String name) throws Exception {
File file = new File(SYS_TEM_DIR + name);
if (file.exists()) {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
}
/**
*
*/
public static File upload(MultipartFile file, String filePath) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmssS");
String name = getFileNameNoEx(file.getOriginalFilename());
String suffix = getExtensionName(file.getOriginalFilename());
String nowStr = "-" + format.format(date);
try {
String fileName = name + nowStr + "." + suffix;
String path = filePath + fileName;
// getCanonicalFile 可解析正确各种路径
File dest = new File(path).getCanonicalFile();
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
if (!dest.getParentFile().mkdirs()) {
System.out.println("was not successful.");
}
}
// 文件写入
file.transferTo(dest);
return dest;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* excel
*/
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
File file = new File(tempPath);
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(list, true);
//response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是弹出下载对话框的文件名不能为中文中文请自行编码
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
ServletOutputStream out = response.getOutputStream();
// 终止后删除临时文件
file.deleteOnExit();
writer.flush(out, true);
//此处记得关闭输出Servlet流
IoUtil.close(out);
}
public static String getFileType(String type) {
String documents = "txt doc pdf ppt pps xlsx xls docx";
String music = "mp3 wav wma mpa ram ra aac aif m4a";
String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg";
String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg";
if (image.contains(type)) {
return IMAGE;
} else if (documents.contains(type)) {
return TXT;
} else if (music.contains(type)) {
return MUSIC;
} else if (video.contains(type)) {
return VIDEO;
} else {
return OTHER;
}
}
/**
*
*/
public static boolean check(File file1, File file2) {
String img1Md5 = getMd5(file1);
String img2Md5 = getMd5(file2);
return img1Md5.equals(img2Md5);
}
/**
*
*/
public static boolean check(String file1Md5, String file2Md5) {
return file1Md5.equals(file2Md5);
}
private static byte[] getByte(File file) {
// 得到文件长度
byte[] b = new byte[(int) file.length()];
try {
InputStream in = new FileInputStream(file);
try {
System.out.println(in.read(b));
} catch (IOException e) {
log.error(e.getMessage(), e);
}
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
return null;
}
return b;
}
private static String getMd5(byte[] bytes) {
// 16进制字符
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(bytes);
byte[] md = mdTemp.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
// 移位 输出字符串
for (byte byte0 : md) {
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
*
*
* @param request /
* @param response /
* @param file /
*/
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, File file, boolean deleteOnExit) {
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType("application/octet-stream");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
IOUtils.copy(fis, response.getOutputStream());
response.flushBuffer();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
if (fis != null) {
try {
fis.close();
if (deleteOnExit) {
file.deleteOnExit();
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
public static String getMd5(File file) {
return getMd5(getByte(file));
}
}

@ -1,6 +1,7 @@
package com.baiye.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baiye.core.base.api.Result;
import com.baiye.dao.VariableMapper;
@ -9,12 +10,12 @@ import com.baiye.entity.Variable;
import com.baiye.query.SourceVariableQuery;
import com.baiye.service.ResourceVariableService;
import com.baiye.util.QueryHelpUtils;
import com.baiye.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
@ -41,6 +42,10 @@ public class ResourceVariableServiceImpl implements ResourceVariableService {
}
if (CollUtil.isNotEmpty(operateVariableDTO.getSourceVariableDTO())) {
List<Variable> variables = JSONUtil.toList(JSONUtil.toJsonStr(operateVariableDTO.getSourceVariableDTO()), Variable.class);
variables.forEach(item -> {
item.setCreateTime(DateUtil.date());
item.setCreateBy(SecurityUtils.getCurrentUserId());
});
variableMapper.insertAll(variables);
}
return Result.success("操作成功");

@ -15,7 +15,6 @@ import com.baiye.entity.Template;
import com.baiye.entity.Variable;
import com.baiye.exception.global.BadRequestException;
import com.baiye.properties.FileProperties;
import com.baiye.request.RequestBaiDuApi;
import com.baiye.service.SmartWriteService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hankcs.hanlp.HanLP;
@ -76,10 +75,10 @@ public class SmartWriteServiceImpl implements SmartWriteService {
Map<String, String> mapByNumber = variables.stream().collect(Collectors.toMap(Variable::getNumber, Variable::getVariableContent));
List<String> deal = new ArrayList<>();
String[] templateSpilt = templateContent.split("&v&");
if (templateSpilt.length == 2) {
int count = (templateContent + " ").split("&v&").length - 1;
if (count == 1) {
deal = deal(mapByNumber, templateContent);
} else if (templateSpilt.length < 2) {
} else if (count == 0) {
deal.add(templateContent);
} else {
Map<String, String[]> map = new HashMap<>(4);
@ -136,10 +135,8 @@ public class SmartWriteServiceImpl implements SmartWriteService {
Word07Writer writer = new Word07Writer();
for (String templateContent : randomSeries) {
if (CollUtil.isNotEmpty(picTrues)) {
//下载图片
File file = FileUtil.file(properties.getPath().getAvatar() + picTrues.get(rand.nextInt(picTrues.size())));
//添加图片
writer.addPicture(file, 350, 250);
getFile(picTrues, writer, rand);
}
// 添加段落(标题)
if (smartWriteDTO.getSourceId() != null) {
@ -172,6 +169,20 @@ public class SmartWriteServiceImpl implements SmartWriteService {
writer.close();
}
private void getFile(List<String> picTrues, Word07Writer writer, Random rand) {
if (CollUtil.isNotEmpty(picTrues)) {
String path = picTrues.get(rand.nextInt(picTrues.size()));
File file = FileUtil.file(properties.getPath().getAvatar() + path);
try {
writer.addPicture(file, 350, 250);
} catch (Exception e) {
log.error("图片地址不存在:{}", path);
picTrues.removeIf(s -> s.equals(path));
getFile(picTrues, writer, rand);
}
}
}
private String getTitle(List<String> titles, Boolean isCreate, String templateContent) {
Random rand = new Random();
String title = titles.get(rand.nextInt(titles.size()));

@ -8,6 +8,7 @@ import com.baiye.exception.global.BadRequestException;
import com.baiye.query.SourceOrganizeQuery;
import com.baiye.service.SourceOrganizeService;
import com.baiye.util.QueryHelpUtils;
import com.baiye.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
@ -42,6 +43,7 @@ public class SourceOrganizeServiceImpl implements SourceOrganizeService {
sourceOrganize.setOrganizeName(sourceOrganizeDTO.getOrganizeName());
sourceOrganize.setOrganizeRemark(sourceOrganizeDTO.getOrganizeRemark());
sourceOrganize.setCreateTime(new Date());
sourceOrganize.setCreateBy(SecurityUtils.getCurrentUserId());
sourceOrganizeMapper.insert(sourceOrganize);
return Result.success("新增成功");
}

@ -8,7 +8,6 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baiye.User;
import com.baiye.core.base.api.Result;
import com.baiye.core.page.PageResult;
import com.baiye.dao.SourceMapper;
@ -16,7 +15,6 @@ import com.baiye.dao.TemplateMapper;
import com.baiye.dao.VariableMapper;
import com.baiye.dto.SourceDTO;
import com.baiye.dto.SourceVariableDTO;
import com.baiye.dto.UserDto;
import com.baiye.entity.Source;
import com.baiye.entity.SourceOrganize;
import com.baiye.entity.Template;
@ -36,7 +34,6 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@ -120,6 +117,8 @@ public class SourceServiceImpl implements SourceService {
template.setSourceId(source.getId());
template.setTemplateContent(pic);
template.setTemplateType(source.getSourceType());
template.setCreateTime(new Date());
template.setCreateBy(SecurityUtils.getCurrentUserId());
list.add(template);
}
templateMapper.insertAll(list);
@ -128,7 +127,7 @@ public class SourceServiceImpl implements SourceService {
//文字变量模板
addSourceByVariable(sourceDTO, source.getId(), source.getSourceType(), file);
} else {
//文字素材 和文字模板
//文字素材 和标题模板
addSourceByWord(sourceDTO, source.getId(), source.getSourceType(), file);
}
@ -181,19 +180,22 @@ public class SourceServiceImpl implements SourceService {
*/
private void addSourceByWord(SourceDTO sourceDTO, Long sourceId, Integer type, MultipartFile file) {
List<Template> list = new ArrayList<>();
//解析文件
if (CollUtil.isNotEmpty(sourceDTO.getTemplateContent())) {
for (String con : sourceDTO.getTemplateContent()) {
if (StrUtil.isBlank(con)) {
continue;
throw new BadRequestException("模板不能为空");
}
Template template = new Template();
template.setSourceId(sourceId);
template.setTemplateType(type);
template.setTemplateContent(con);
template.setCreateTime(new Date());
template.setCreateBy(SecurityUtils.getCurrentUserId());
list.add(template);
}
}
//解析文件
if (ObjectUtil.isNotNull(file) && file.getSize() > 0) {
try {
int lastIndexOf = Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf(".");
@ -204,12 +206,14 @@ public class SourceServiceImpl implements SourceService {
for (List<Object> objects : read) {
String number = String.valueOf(objects.get(0));
if (StrUtil.isBlank(number)) {
continue;
throw new BadRequestException("文件上传失败,模板不能为空");
}
Template template = new Template();
template.setSourceId(sourceId);
template.setTemplateType(type);
template.setTemplateContent(number);
template.setCreateTime(new Date());
template.setCreateBy(SecurityUtils.getCurrentUserId());
list.add(template);
}
}
@ -233,7 +237,7 @@ public class SourceServiceImpl implements SourceService {
private void addSourceByVariable(SourceDTO sourceDTO, Long sourceId, Integer type, MultipartFile file) {
List<Variable> list = new ArrayList<>();
//解析文件
if (CollUtil.isNotEmpty(sourceDTO.getVariableTemplateContentDTO())) {
for (SourceDTO.VariableTemplateContentDTO con : sourceDTO.getVariableTemplateContentDTO()) {
//变量
@ -241,32 +245,42 @@ public class SourceServiceImpl implements SourceService {
//模板
String variableTemplateContent = con.getVariableTemplateContent();
if (StrUtil.isBlank(variableTemplateContent)) {
continue;
throw new BadRequestException("模板不能为空");
}
Template template = new Template();
template.setSourceId(sourceId);
template.setTemplateType(type);
template.setTemplateContent(variableTemplateContent);
template.setCreateTime(new Date());
template.setCreateBy(SecurityUtils.getCurrentUserId());
templateMapper.insert(template);
if (CollUtil.isNotEmpty(sourceVariableDTO)) {
String[] split = variableTemplateContent.split("&v&");
if (split.length - 1 != sourceVariableDTO.size()) {
log.error("模板和变量数据不一致 {}", template);
continue;
int count = (variableTemplateContent+ " ").split("&v&").length - 1;
if (count > 1) {
if (CollUtil.isEmpty(sourceVariableDTO)) {
throw new BadRequestException("模板变量不能为空");
}
//判断模板占位符和变量是否一致
if (count != sourceVariableDTO.size()) {
throw new BadRequestException("模板占位符和模板变量数量不一致,模板为" + variableTemplateContent);
}
for (SourceVariableDTO sourceVariable : sourceVariableDTO) {
if (sourceVariable.getNumber() == null || sourceVariable.getVariableContent() == null) {
continue;
throw new BadRequestException("变量的序号和变量内容不能为空,模板为" + variableTemplateContent);
}
Variable variable = new Variable();
variable.setVariableContent(sourceVariable.getVariableContent());
variable.setNumber(sourceVariable.getNumber());
variable.setTemplateId(template.getId());
variable.setCreateTime(new Date());
variable.setCreateBy(SecurityUtils.getCurrentUserId());
list.add(variable);
}
}
}
}
//解析文件
if (ObjectUtil.isNotNull(file) && file.getSize() > 0) {
try {
int lastIndexOf = Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf(".");
@ -274,36 +288,44 @@ public class SourceServiceImpl implements SourceService {
if ("xlsx".equals(nameFormat) || "xls".equals(nameFormat)) {
ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
List<List<Object>> read = reader.read();
for (List<Object> objects : read) {
for (int index = 1; index < read.size(); index++) {
// for (List<Object> objects : read) {
List<Object> objects = read.get(index);
String variableContent = String.valueOf(objects.get(0));
if (StrUtil.isBlank(variableContent)) {
continue;
throw new BadRequestException("文件上传失败,变量模板不能为空");
}
//判断变量模板的变量数量
int count = (variableContent+ " ").split("&v&").length - 1;
if (count != objects.size()) {
throw new BadRequestException("文件上传失败,模板占位符和模板变量数量不一致,模板为" + variableContent);
}
Template template = new Template();
template.setSourceId(sourceId);
template.setTemplateType(type);
template.setTemplateContent(variableContent);
template.setCreateTime(new Date());
template.setCreateBy(SecurityUtils.getCurrentUserId());
templateMapper.insert(template);
//判断变量模板的变量数量
String[] split = variableContent.split("&v&");
if (split.length != objects.size()) {
log.error("模板和变量数据不一致 {}", variableContent);
continue;
}
for (int i = 1; i < objects.size(); i++) {
if (StrUtil.isNotEmpty(objects.get(i).toString())) {
if (count > 1) {
for (int i = 1; i < objects.size(); i++) {
if (StrUtil.isEmpty(objects.get(i).toString())) {
throw new BadRequestException("变量内容不能为空,模板为" + variableContent);
}
Variable variable = new Variable();
variable.setVariableContent(objects.get(i).toString());
variable.setNumber(String.valueOf(i));
variable.setTemplateId(template.getId());
variable.setCreateTime(new Date());
variable.setCreateBy(SecurityUtils.getCurrentUserId());
list.add(variable);
}
}
}
}
} catch (Exception e) {
log.error("读取文件错误:{}", e.getMessage());
throw new BadRequestException("读取文件错误:" + e.getMessage());
}
}
if (CollUtil.isNotEmpty(list)) {

@ -8,13 +8,16 @@
<result column="source_id" property="sourceId"/>
<result column="template_content" property="templateContent"/>
<result column="template_type" property="templateType"/>
<result column="create_time" property="createTime"/>
<result column="create_by" property="createBy"/>
</resultMap>
<insert id="insertAll">
insert into tb_template (id,source_id,template_content,template_type)
insert into tb_template (id,source_id,template_content,template_type,create_time,create_by)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id}, #{item.sourceId},#{item.templateContent},#{item.templateType})
(#{item.id},
#{item.sourceId},#{item.templateContent},#{item.templateType},#{item.createTime},#{item.createBy})
</foreach>
</insert>
</mapper>

@ -7,14 +7,16 @@
<id column="id" property="id"/>
<result column="number" property="number"/>
<result column="template_id" property="templateId"/>
<result column="variable_content" property="variableContent"></result>
<result column="variable_content" property="variableContent"/>
<result column="create_time" property="createTime"/>
<result column="create_by" property="createBy"/>
</resultMap>
<insert id="insertAll">
insert into tb_variable (id,number,template_id,variable_content)
insert into tb_variable (id,number,template_id,variable_content,create_time,create_by)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id}, #{item.number},#{item.templateId},#{item.variableContent})
(#{item.id}, #{item.number},#{item.templateId},#{item.variableContent},#{item.createTime},#{item.createBy})
</foreach>
</insert>

Loading…
Cancel
Save