Commit f7f3f7d6 by shentao

Merge branch 'feature' into 'dev'

2023/09/25 工具库-摘要提取次数修复、excel写入补充文本长度截取

See merge request !397
parents 6453ee8b 96c29d53
......@@ -14,6 +14,7 @@ import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.pojo.Project;
import com.zhiwei.brandkbs2.service.ProjectService;
import com.zhiwei.brandkbs2.util.RedisUtil;
import com.zhiwei.brandkbs2.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
......@@ -31,6 +32,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author cjz
......@@ -79,7 +81,7 @@ public class AppToolsetController {
String text = info.getString("content");
String articleSummaryResult = getArticleSummaryResult(text);
// 剩余次数限制
String redisKey = RedisUtil.getToolsetArticleSummaryLimitKey(UserThreadLocal.getProjectId(), UserThreadLocal.getUserId());
String redisKey = RedisUtil.getToolsetArticleSummaryLimitKey(UserThreadLocal.getProjectId());
String redisResult = redisUtil.get(redisKey);
int usedCount = 1;
if (Objects.nonNull(redisResult)){
......@@ -88,9 +90,9 @@ public class AppToolsetController {
return ResponseResult.failure("本日摘要提取次数已达上限");
}
usedCount = redisCount + 1;
redisUtil.getAndSet(redisKey, String.valueOf(usedCount));
redisUtil.setExpire(redisKey, String.valueOf(usedCount), Tools.getMillSecondNextDay(), TimeUnit.MILLISECONDS);
}else {
redisUtil.setExpire(redisKey, String.valueOf(usedCount));
redisUtil.setExpire(redisKey, String.valueOf(usedCount), Tools.getMillSecondNextDay(), TimeUnit.MILLISECONDS);
}
res.put("source", info.get("source"));
res.put("platform", info.getString("platform"));
......@@ -107,7 +109,7 @@ public class AppToolsetController {
JSONObject res = new JSONObject();
Project project = projectService.getProjectById(UserThreadLocal.getProjectId());
// 调用前剩余可用次数
String redisKey = RedisUtil.getToolsetArticleSummaryLimitKey(UserThreadLocal.getProjectId(), UserThreadLocal.getUserId());
String redisKey = RedisUtil.getToolsetArticleSummaryLimitKey(UserThreadLocal.getProjectId());
String redisResult = redisUtil.get(redisKey);
int remainingCount = ARTICLE_SUMMARY_LIMIT;
if (Objects.nonNull(redisResult)){
......@@ -117,11 +119,14 @@ public class AppToolsetController {
return ResponseResult.failure("本日摘要提取次数已达上限");
}
// excel信息提取
Map<String, String> map = new LinkedHashMap<>(50);
Map<String, String> map = new LinkedHashMap<>();
ReadExcelDTO<UploadArticleSummaryDTO> readExcel = new ReadExcelDTO<>();
readExcel.setClazz(UploadArticleSummaryDTO.class);
readExcel.setAnalysisEventListener(new ArticleSummaryListener(map, remainingCount));
readExcel.setAnalysisEventListener(new ArticleSummaryListener(map));
EasyExcelUtil.read(file, readExcel);
if (map.size() > 50){
return ResponseResult.failure("超过每次批量提取上限50");
}
int usedCount = 0; // 本次批量调用次数
List<ExportArticleSummaryDTO> datas = new ArrayList<>(50);
// 摘要提取
......@@ -140,7 +145,7 @@ public class AppToolsetController {
// 本次批量调用后剩余次数
remainingCount = remainingCount - usedCount;
// 更新已用次数
redisUtil.getAndSet(redisKey, String.valueOf(ARTICLE_SUMMARY_LIMIT - remainingCount));
redisUtil.setExpire(redisKey, String.valueOf(ARTICLE_SUMMARY_LIMIT - remainingCount), Tools.getMillSecondNextDay(), TimeUnit.MILLISECONDS);
// excel输出到指定路径
String filePath = EasyExcelUtil.generateExcelFilePath(brandkbsFilePath, project.getProjectName(), UserThreadLocal.getNickname(), "摘要提取结果");
EasyExcelUtil.write(filePath, "sheet1", ExportArticleSummaryDTO.class, datas);
......@@ -152,7 +157,7 @@ public class AppToolsetController {
@ApiOperation("摘要提取-剩余可用次数")
@GetMapping("/article-summary/remaining")
public ResponseResult getArticleSummaryRemainingCount(){
String redisKey = RedisUtil.getToolsetArticleSummaryLimitKey(UserThreadLocal.getProjectId(), UserThreadLocal.getUserId());
String redisKey = RedisUtil.getToolsetArticleSummaryLimitKey(UserThreadLocal.getProjectId());
String redisResult = redisUtil.get(redisKey);
if (Objects.nonNull(redisResult)){
return ResponseResult.success(ARTICLE_SUMMARY_LIMIT - Integer.parseInt(redisResult));
......
......@@ -107,6 +107,7 @@ public class EasyExcelUtil {
*/
public static <T> void write(String filePath, String sheetName, Class<T> clazz, List<T> datas) {
try {
formatExcelExports(clazz, datas);
EasyExcel.write(filePath, clazz).sheet(sheetName).doWrite(datas);
} catch (Exception e) {
log.error("file:{},write error:", filePath, e);
......
......@@ -74,8 +74,8 @@ public class RedisUtil {
return RedisKeyPrefix.PROJECT_WARN_NEW_CASE_CURSOR + projectId;
}
public static String getToolsetArticleSummaryLimitKey(String projectId, String userId){
return RedisKeyPrefix.TOOLSET_ARTICLE_SUMMARY_LIMIT + Tools.concat(projectId, userId);
public static String getToolsetArticleSummaryLimitKey(String projectId){
return RedisKeyPrefix.TOOLSET_ARTICLE_SUMMARY_LIMIT + projectId;
}
public void setExpire(String key, String value, long timeout, TimeUnit unit) {
......@@ -97,10 +97,6 @@ public class RedisUtil {
return stringRedisTemplate.opsForValue().get(key);
}
public void getAndSet(String key, String newValue){
stringRedisTemplate.opsForValue().getAndSet(key, newValue);
}
public Set<String> keys(String key) {
return stringRedisTemplate.keys(key);
}
......
......@@ -4,6 +4,7 @@ import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.net.InternetDomainName;
import com.zhiwei.base.category.ClassCodec;
import com.zhiwei.base.entity.subclass.CompleteText;
import com.zhiwei.base.entity.subclass.IncompleteText;
......@@ -19,6 +20,7 @@ import com.zhiwei.brandkbs2.pojo.BaseMap;
import com.zhiwei.qbjc.bean.pojo.common.MessagePlatform;
import com.zhiwei.qbjc.bean.pojo.common.Tag;
import com.zhiwei.qbjc.bean.tools.BeanTools;
import okhttp3.HttpUrl;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
......@@ -1183,4 +1185,39 @@ public class Tools {
return count != mtags.size();
}
/**
* 获取当前时间到第二天零点的毫秒数
* @return
*/
public static long getMillSecondNextDay(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis() - System.currentTimeMillis();
}
/**
* 获取url的域名
* @param url
* @return
*/
public static String topDomainOfDomain(String url) {
try {
String host = getHost(url);
return InternetDomainName.from(host).topPrivateDomain().toString();
} catch (Exception e) {
return null;
}
}
public static String getHost(String url) {
try {
return HttpUrl.parse(url).host();
} catch (Exception e) {
return null;
}
}
}
\ No newline at end of file
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