小红书模块新增接口城市映射
parent
cce6c66fb7
commit
0c86ae64e2
@ -0,0 +1,23 @@
|
||||
package com.baiye.core.constant;
|
||||
|
||||
/**
|
||||
* @author wjt
|
||||
* @date 2023/3/21
|
||||
* <p>
|
||||
* 文件类型常量
|
||||
*/
|
||||
public class DefaultFileTypeConstants {
|
||||
|
||||
private DefaultFileTypeConstants() {
|
||||
}
|
||||
|
||||
/**
|
||||
* excel 类型
|
||||
*/
|
||||
public static final String[] EXCEL = {"xlsx", "xls"};
|
||||
|
||||
/**
|
||||
* 图
|
||||
*/
|
||||
public static final String[] IMAGE = {"gif", "jpg", "jpeg", "png"};
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.baiye.controller;
|
||||
|
||||
import com.baiye.annotation.Inner;
|
||||
import com.baiye.core.base.api.Result;
|
||||
import com.baiye.service.CityInfoService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* @author wjt
|
||||
* @date 2023/3/20
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/xhs")
|
||||
@Api(tags = "小红书城市管理")
|
||||
public class CityInfoController {
|
||||
|
||||
private final CityInfoService cityInfoService;
|
||||
|
||||
@GetMapping("/query")
|
||||
@ApiOperation("查询")
|
||||
@Inner(value = false)
|
||||
public Result<Object> queryCity(String id) {
|
||||
return Result.data(cityInfoService.queryCity(id));
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
@ApiOperation("导入城市")
|
||||
@Inner(value = false)
|
||||
public Result<Object> addCity(@RequestParam(value = "file", required = false) MultipartFile file) {
|
||||
return cityInfoService.addCity(file);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.baiye.controller;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baiye.annotation.Inner;
|
||||
import com.baiye.core.base.api.Result;
|
||||
import com.baiye.dto.IntellectSmartDTO;
|
||||
import com.baiye.dto.SourceDTO;
|
||||
import com.baiye.service.IntellectSmartService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author jt
|
||||
* 智能生成小工具
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/xhs/intellect")
|
||||
@Api(tags = "小红书智能文案")
|
||||
public class IntellectSmartController {
|
||||
|
||||
private final IntellectSmartService intellectSmartService;
|
||||
|
||||
@PostMapping("/create")
|
||||
@ApiOperation("智能文案生成")
|
||||
@Inner(value = false)
|
||||
public Result<Object> smartCreate(@RequestParam(value = "titleFile", required = false) MultipartFile titleFile,
|
||||
@RequestParam(value = "textFile", required = false) MultipartFile textFile,
|
||||
@RequestParam(value = "variableFile", required = false) MultipartFile variableFile,
|
||||
@RequestParam(value = "intellectSmartDTO") String intellectSmartDTO) {
|
||||
return intellectSmartService.smartCreate(titleFile, textFile, variableFile, JSONUtil.toBean(intellectSmartDTO, IntellectSmartDTO.class));
|
||||
}
|
||||
|
||||
@PostMapping("/write")
|
||||
@ApiOperation("智能文案导出")
|
||||
@Inner(value = false)
|
||||
public void smartWrite(HttpServletResponse response,
|
||||
@RequestParam(value = "titleFile", required = false) MultipartFile titleFile,
|
||||
@RequestParam(value = "textFile", required = false) MultipartFile textFile,
|
||||
@RequestParam(value = "variableFile", required = false) MultipartFile variableFile,
|
||||
@RequestParam(value = "intellectSmartDTO") String intellectSmartDTO) {
|
||||
intellectSmartService.smartWrite(response, titleFile, textFile, variableFile, JSONUtil.toBean(intellectSmartDTO, IntellectSmartDTO.class));
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baiye.dao;
|
||||
|
||||
import com.baiye.entity.CityInfo;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author wjt
|
||||
* @date 2023/3/20
|
||||
*/
|
||||
@Mapper
|
||||
public interface CityInfoMapper extends BaseMapper<CityInfo> {
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package com.baiye.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jt
|
||||
*/
|
||||
@Data
|
||||
public class IntellectSmartDTO {
|
||||
/**
|
||||
* 缓存key
|
||||
*/
|
||||
private String redisKey;
|
||||
/**
|
||||
* 导出数量
|
||||
*/
|
||||
private Integer num;
|
||||
/**
|
||||
* 是否智能生成
|
||||
*/
|
||||
private Boolean isCreate;
|
||||
/**
|
||||
* 标题模板
|
||||
*/
|
||||
private List<String> titleTemplate;
|
||||
/**
|
||||
* 图片模板
|
||||
*/
|
||||
private List<String> pictureTemplate;
|
||||
/**
|
||||
* 文字模板
|
||||
*/
|
||||
private List<String> textTemplate;
|
||||
/**
|
||||
* 变量模板
|
||||
*/
|
||||
private List<VariableTemplate> variableTemplate;
|
||||
|
||||
|
||||
@Data
|
||||
public static class VariableTemplate {
|
||||
/**
|
||||
* 变量模板内容
|
||||
*/
|
||||
private String variableTemplateContent;
|
||||
/**
|
||||
* 变量对象
|
||||
*/
|
||||
private List<Variable> variable;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Variable {
|
||||
/**
|
||||
* 序号
|
||||
*/
|
||||
private String number;
|
||||
/**
|
||||
* 变量内容
|
||||
*/
|
||||
private String variableContent;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baiye.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author wjt
|
||||
* @date 2023/3/20
|
||||
*/
|
||||
@Data
|
||||
@TableName("tb_city_info")
|
||||
@ApiOperation("城市信息")
|
||||
public class CityInfo implements Serializable {
|
||||
|
||||
@TableId
|
||||
@ApiModelProperty(value = "")
|
||||
private String id;
|
||||
|
||||
@ApiModelProperty(value = "城市")
|
||||
private String city;
|
||||
|
||||
@ApiModelProperty(value = "创建人")
|
||||
private String createBy;
|
||||
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
private Date createTime;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baiye.service;
|
||||
|
||||
import com.baiye.core.base.api.Result;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* @author wjt
|
||||
* @date 2023/3/20
|
||||
*/
|
||||
public interface CityInfoService {
|
||||
|
||||
String queryCity(String nid);
|
||||
|
||||
Result<Object> addCity(MultipartFile file);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baiye.service;
|
||||
|
||||
import com.baiye.core.base.api.Result;
|
||||
import com.baiye.dto.IntellectSmartDTO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author jt
|
||||
*/
|
||||
public interface IntellectSmartService {
|
||||
|
||||
Result<Object> smartCreate(MultipartFile titleFile, MultipartFile textFile, MultipartFile variableFile, IntellectSmartDTO intellectSmartDTO);
|
||||
|
||||
void smartWrite(HttpServletResponse response, MultipartFile titleFile, MultipartFile textFile, MultipartFile variableFile, IntellectSmartDTO intellectSmartDTO);
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.baiye.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.poi.excel.ExcelReader;
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
import com.baiye.core.base.api.Result;
|
||||
import com.baiye.core.constant.DefaultFileTypeConstants;
|
||||
import com.baiye.dao.CityInfoMapper;
|
||||
import com.baiye.entity.CityInfo;
|
||||
import com.baiye.service.CityInfoService;
|
||||
import com.baiye.util.FileUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* @author wjt
|
||||
* @date 2023/3/20
|
||||
*/
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class CityInfoServiceImpl extends ServiceImpl<CityInfoMapper, CityInfo> implements CityInfoService {
|
||||
private final CityInfoMapper cityInfoMapper;
|
||||
|
||||
@Override
|
||||
public String queryCity(String nid) {
|
||||
CityInfo cityInfo = cityInfoMapper.selectById(nid);
|
||||
if (ObjectUtil.isNotEmpty(cityInfo)) {
|
||||
return cityInfo.getCity();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Object> addCity(MultipartFile file) {
|
||||
try {
|
||||
String type = FileUtil.getExtensionName(file.getOriginalFilename());
|
||||
boolean contain = Arrays.asList(DefaultFileTypeConstants.EXCEL).contains(type);
|
||||
if (contain) {
|
||||
ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
|
||||
List<List<Object>> read = reader.read();
|
||||
List<CityInfo> cityInfos = new ArrayList<>();
|
||||
for (int index = 1; index < read.size(); index++) {
|
||||
List<Object> objects = read.get(index);
|
||||
CityInfo cityInfo = new CityInfo();
|
||||
String nid = objects.get(0).toString();
|
||||
String city = objects.get(1).toString();
|
||||
cityInfo.setId(nid);
|
||||
cityInfo.setCity(city);
|
||||
cityInfo.setCreateBy(null);
|
||||
cityInfo.setCreateTime(DateUtil.date());
|
||||
cityInfos.add(cityInfo);
|
||||
}
|
||||
this.saveBatch(cityInfos);
|
||||
} else {
|
||||
return Result.fail("文件格式错误");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("读取文件错误:{}", e.getMessage());
|
||||
return Result.fail("读取文件失败");
|
||||
}
|
||||
return Result.success("上传成功");
|
||||
}
|
||||
}
|
@ -0,0 +1,344 @@
|
||||
package com.baiye.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.poi.excel.ExcelReader;
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
import cn.hutool.poi.word.Word07Writer;
|
||||
import com.baiye.core.base.api.Result;
|
||||
import com.baiye.core.util.RedisUtils;
|
||||
import com.baiye.dto.IntellectSmartDTO;
|
||||
import com.baiye.exception.global.BadRequestException;
|
||||
import com.baiye.properties.FileProperties;
|
||||
import com.baiye.service.IntellectSmartService;
|
||||
import com.hankcs.hanlp.HanLP;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.poi.xwpf.usermodel.BreakType;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author jt
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class IntellectSmartServiceImpl implements IntellectSmartService {
|
||||
private final RedisUtils redisUtils;
|
||||
private final FileProperties properties;
|
||||
|
||||
@Override
|
||||
public Result<Object> smartCreate(MultipartFile titleFile, MultipartFile textFile, MultipartFile variableFile, IntellectSmartDTO intellectSmartDTO) {
|
||||
|
||||
List<String> templates = new ArrayList<>();
|
||||
if (ObjectUtil.isNull(textFile) && CollUtil.isEmpty(intellectSmartDTO.getTextTemplate()) && ObjectUtil.isNull(variableFile) && CollUtil.isEmpty(intellectSmartDTO.getVariableTemplate())) {
|
||||
throw new BadRequestException("文案模板不能为空,请检查");
|
||||
}
|
||||
if (ObjectUtil.isNull(titleFile) && CollUtil.isEmpty(intellectSmartDTO.getTitleTemplate())) {
|
||||
throw new BadRequestException("标题不能为空,请检查");
|
||||
}
|
||||
if (ObjectUtil.isNotNull(textFile) || CollUtil.isNotEmpty(intellectSmartDTO.getTextTemplate())) {
|
||||
templates.addAll(getTextTemplate(textFile, intellectSmartDTO.getTextTemplate()));
|
||||
}
|
||||
|
||||
if (ObjectUtil.isNotNull(variableFile) || CollUtil.isNotEmpty(intellectSmartDTO.getVariableTemplate())) {
|
||||
templates.addAll(getVariableTemplate(variableFile, intellectSmartDTO.getVariableTemplate()));
|
||||
}
|
||||
|
||||
String redisKey = RandomUtil.randomString(8);
|
||||
redisUtils.set(redisKey, templates, 60 * 60);
|
||||
Map<String, Object> data = new HashMap<>(1);
|
||||
data.put("redisKey", redisKey);
|
||||
data.put("num", templates.size());
|
||||
return Result.data(data);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void smartWrite(HttpServletResponse response, MultipartFile titleFile, MultipartFile textFile, MultipartFile variableFile, IntellectSmartDTO intellectSmartDTO) {
|
||||
Object value = redisUtils.get(intellectSmartDTO.getRedisKey());
|
||||
if (value == null) {
|
||||
throw new BadRequestException("超时操作,请重新生成物料");
|
||||
}
|
||||
List<String> templateList = JSONUtil.toList(JSONUtil.toJsonStr(value), String.class);
|
||||
Collections.shuffle(templateList);
|
||||
int num = intellectSmartDTO.getNum();
|
||||
if (num > templateList.size()) {
|
||||
num = templateList.size();
|
||||
}
|
||||
//需要生成文案的文字模板
|
||||
List<String> randomSeries = templateList.subList(0, num);
|
||||
if (CollUtil.isEmpty(randomSeries)) {
|
||||
throw new BadRequestException("没有可生成的文案");
|
||||
}
|
||||
//获取图片地址
|
||||
List<String> pictureTemplates = intellectSmartDTO.getPictureTemplate();
|
||||
//获取标题
|
||||
List<String> titleTemplates = getTextTemplate(titleFile, intellectSmartDTO.getTitleTemplate());
|
||||
|
||||
//写文件
|
||||
Word07Writer writer = new Word07Writer();
|
||||
Random rand = new Random();
|
||||
for (String templateContent : randomSeries) {
|
||||
if (CollUtil.isNotEmpty(pictureTemplates)) {
|
||||
//添加图片
|
||||
getFile(pictureTemplates, writer, rand);
|
||||
}
|
||||
//添加标题
|
||||
String title = getTitle(titleTemplates, intellectSmartDTO.getIsCreate(), templateContent);
|
||||
writer.addText(new Font("方正小标宋简体", Font.PLAIN, 36), title);
|
||||
// 添加段落(正文)
|
||||
writer.addText(new Font("宋体", Font.PLAIN, 22), templateContent);
|
||||
writer.getDoc().createParagraph().createRun().addBreak(BreakType.PAGE);
|
||||
}
|
||||
try {
|
||||
//输出到页面下载
|
||||
ServletOutputStream os = response.getOutputStream();
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("application/msword");
|
||||
String fileName = "write.docx";
|
||||
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
|
||||
writer.flush(os);
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
writer.close();
|
||||
// delPic(pictureTemplates);
|
||||
}
|
||||
|
||||
private void delPic(List<String> pictureTemplates) {
|
||||
for (String path : pictureTemplates) {
|
||||
String s = properties.getPath().getAvatar() + path;
|
||||
FileUtil.del(s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的文字模板
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<String> getTextTemplate(MultipartFile textFile, List<String> textTemplates) {
|
||||
List<String> list = new ArrayList<>();
|
||||
if (CollUtil.isNotEmpty(textTemplates)) {
|
||||
list.addAll(textTemplates);
|
||||
}
|
||||
//解析文件
|
||||
if (ObjectUtil.isNotNull(textFile) && textFile.getSize() > 0) {
|
||||
try {
|
||||
int lastIndexOf = Objects.requireNonNull(textFile.getOriginalFilename()).lastIndexOf(".");
|
||||
String nameFormat = textFile.getOriginalFilename().substring(lastIndexOf + 1);
|
||||
if ("xlsx".equals(nameFormat) || "xls".equals(nameFormat)) {
|
||||
ExcelReader reader = ExcelUtil.getReader(textFile.getInputStream());
|
||||
List<List<Object>> read = reader.read(1, reader.getRowCount());
|
||||
for (List<Object> objects : read) {
|
||||
String number = String.valueOf(objects.get(0));
|
||||
if (StrUtil.isNotBlank(number)) {
|
||||
list.add(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("读取文件错误");
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的变量模板
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<String> getVariableTemplate(MultipartFile variableFile, List<IntellectSmartDTO.VariableTemplate> variableTemplates) {
|
||||
List<String> list = new ArrayList<>();
|
||||
|
||||
//解析文件
|
||||
if (ObjectUtil.isNotNull(variableFile) && variableFile.getSize() > 0) {
|
||||
try {
|
||||
int lastIndexOf = Objects.requireNonNull(variableFile.getOriginalFilename()).lastIndexOf(".");
|
||||
String nameFormat = variableFile.getOriginalFilename().substring(lastIndexOf + 1);
|
||||
if ("xlsx".equals(nameFormat) || "xls".equals(nameFormat)) {
|
||||
ExcelReader reader = ExcelUtil.getReader(variableFile.getInputStream());
|
||||
List<List<Object>> read = reader.read();
|
||||
for (int index = 1; index < read.size(); index++) {
|
||||
List<Object> objects = read.get(index);
|
||||
String variableContent = String.valueOf(objects.get(0));
|
||||
if (StrUtil.isBlank(variableContent)) {
|
||||
throw new BadRequestException("文件上传失败,变量模板不能为空");
|
||||
}
|
||||
|
||||
//判断变量模板的变量数量
|
||||
int count = (variableContent + " ").split("&v&").length - 1;
|
||||
if (count != objects.size() - 1) {
|
||||
throw new BadRequestException("文件上传失败,模板占位符和模板变量数量不一致,模板为" + variableContent);
|
||||
}
|
||||
if (count > 0) {
|
||||
Map<String, String> mapByNumber = new HashMap<>(4);
|
||||
for (int i = 1; i < objects.size(); i++) {
|
||||
if (StrUtil.isEmpty(objects.get(i).toString())) {
|
||||
throw new BadRequestException("变量内容不能为空,模板为" + variableContent);
|
||||
}
|
||||
mapByNumber.put(String.valueOf(i), String.valueOf(objects.get(i)));
|
||||
}
|
||||
list.addAll(dealVariableTemplate(count, mapByNumber, variableContent));
|
||||
} else {
|
||||
list.add(variableContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("读取文件错误:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (CollUtil.isNotEmpty(variableTemplates)) {
|
||||
for (IntellectSmartDTO.VariableTemplate variableTemplate : variableTemplates) {
|
||||
String variableTemplateContent = variableTemplate.getVariableTemplateContent();
|
||||
if (StrUtil.isEmpty(variableTemplateContent)) {
|
||||
continue;
|
||||
}
|
||||
int count = (variableTemplateContent + " ").split("&v&").length - 1;
|
||||
if (count > 0) {
|
||||
List<IntellectSmartDTO.Variable> variables = variableTemplate.getVariable();
|
||||
if (CollUtil.isEmpty(variables)) {
|
||||
throw new BadRequestException("变量不能为空,请检查");
|
||||
}
|
||||
if (count != variables.size()) {
|
||||
throw new BadRequestException("变量模板占位符和变量数量不一致,请检查");
|
||||
}
|
||||
if (variables.size() > 4) {
|
||||
throw new BadRequestException("最大支持4个变量,请检查");
|
||||
}
|
||||
Map<String, String> mapByNumber = variables.stream().collect(Collectors.toMap(IntellectSmartDTO.Variable::getNumber, IntellectSmartDTO.Variable::getVariableContent));
|
||||
list.addAll(dealVariableTemplate(count, mapByNumber, variableTemplateContent));
|
||||
} else {
|
||||
list.add(variableTemplateContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private List<String> dealVariableTemplate(Integer count, Map<String, String> mapByNumber, String variableTemplateContent) {
|
||||
List<String> list = new ArrayList<>();
|
||||
if (count == 1) {
|
||||
list = deal(mapByNumber, variableTemplateContent);
|
||||
} else {
|
||||
Map<String, String[]> map = new HashMap<>(4);
|
||||
//切割变量
|
||||
for (String key : mapByNumber.keySet()) {
|
||||
String s = mapByNumber.get(key);
|
||||
String[] split = s.split("\\\\");
|
||||
map.put(key, split);
|
||||
}
|
||||
String collect = String.join("", map.keySet());
|
||||
list = letterCombinations(collect, map, variableTemplateContent.replace("&v&", "{}"));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<String> deal(Map<String, String> mapByNumber, String text) {
|
||||
List<String> list = new ArrayList<>();
|
||||
String[] split = null;
|
||||
for (String value : mapByNumber.values()) {
|
||||
split = value.split("\\\\");
|
||||
}
|
||||
if (split != null) {
|
||||
for (String s : split) {
|
||||
String replace = text.replace("&v&", s);
|
||||
list.add(replace);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public static List<String> letterCombinations(String digits, Map<String, String[]> phoneMap, String text) {
|
||||
List<String> combinations = new ArrayList<>();
|
||||
if (digits.length() == 0) {
|
||||
return combinations;
|
||||
}
|
||||
backtrack(combinations, phoneMap, text, digits, 0, new StringBuffer());
|
||||
return combinations;
|
||||
}
|
||||
|
||||
public static void backtrack(List<String> combinations, Map<String, String[]> phoneMap, String text, String digits, int index, StringBuffer combination) {
|
||||
if (index == digits.length()) {
|
||||
String[] split = combination.toString().split(",");
|
||||
String format = StrUtil.format(text, CollUtil.toList(split).toArray());
|
||||
combinations.add(format);
|
||||
} else {
|
||||
String string = String.valueOf(index + 1);
|
||||
String[] strings = phoneMap.get(string);
|
||||
for (String s : strings) {
|
||||
combination.append(s).append(",");
|
||||
backtrack(combinations, phoneMap, digits, text, index + 1, combination);
|
||||
// combination.deleteCharAt(index);
|
||||
combination.deleteCharAt(combination.length() - 1);
|
||||
combination.replace(combination.lastIndexOf(",") + 1, combination.length(), "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
picTrues.removeIf(s -> s.equals(path));
|
||||
getFile(picTrues, writer, rand);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getTitle(List<String> titles, Boolean isCreate, String templateContent) {
|
||||
Random rand = new Random();
|
||||
if (CollUtil.isEmpty(titles)) {
|
||||
return null;
|
||||
}
|
||||
String title = titles.get(rand.nextInt(titles.size()));
|
||||
int count = (title + " ").split("&v&").length - 1;
|
||||
if (isCreate) {
|
||||
if (count > 0) {
|
||||
List<String> mainIdea = getMainIdea(templateContent, count);
|
||||
return StrUtil.format(title.replace("&v&", "{}"), mainIdea.toArray());
|
||||
}
|
||||
return title;
|
||||
} else {
|
||||
if (count <= 0) {
|
||||
return title;
|
||||
} else {
|
||||
titles.removeIf(s -> s.equals(title));
|
||||
return getTitle(titles, false, templateContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关键字提取
|
||||
*
|
||||
* @param content 文本内容
|
||||
* @param size 提取字数
|
||||
*/
|
||||
public List<String> getMainIdea(String content, Integer size) {
|
||||
return HanLP.extractKeyword(content, size);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue