fix: 记录操作日志时截取指定长度
This commit is contained in:
parent
c5dacdbec8
commit
7c3ccd698c
|
|
@ -7,6 +7,7 @@ import com.mzt.logapi.service.ILogRecordService;
|
||||||
import com.nflg.product.base.core.conmon.util.SessionUtil;
|
import com.nflg.product.base.core.conmon.util.SessionUtil;
|
||||||
import com.nflg.product.bomnew.pojo.entity.BomNewLogEntity;
|
import com.nflg.product.bomnew.pojo.entity.BomNewLogEntity;
|
||||||
import com.nflg.product.bomnew.service.BomNewLogService;
|
import com.nflg.product.bomnew.service.BomNewLogService;
|
||||||
|
import com.nflg.product.bomnew.util.StringUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
@ -24,13 +25,13 @@ public class DbLogRecordServiceImpl implements ILogRecordService {
|
||||||
BomNewLogEntity logEnt = new BomNewLogEntity();
|
BomNewLogEntity logEnt = new BomNewLogEntity();
|
||||||
try {
|
try {
|
||||||
logEnt.setRowId(IdWorker.getId());
|
logEnt.setRowId(IdWorker.getId());
|
||||||
logEnt.setModelName(logRecord.getTenant());
|
logEnt.setModelName(StringUtil.trimText(logRecord.getTenant(),60));
|
||||||
logEnt.setOpBizNo(logRecord.getBizNo());
|
logEnt.setOpBizNo(StringUtil.trimText(logRecord.getBizNo(),30));
|
||||||
logEnt.setOpAction(logRecord.getType());
|
logEnt.setOpAction(StringUtil.trimText(logRecord.getType(),30));
|
||||||
logEnt.setOpContent(logRecord.getAction());
|
logEnt.setOpContent(StringUtil.trimText(logRecord.getAction(),600));
|
||||||
logEnt.setOpContentExt(logRecord.getExtra());
|
logEnt.setOpContentExt(StringUtil.trimText(logRecord.getExtra(),4000));
|
||||||
logEnt.setOpUserJobNo(SessionUtil.getUserCode());
|
logEnt.setOpUserJobNo(StringUtil.trimText(SessionUtil.getUserCode(),20));
|
||||||
logEnt.setOpUserName(SessionUtil.getUserName());
|
logEnt.setOpUserName(StringUtil.trimText(SessionUtil.getUserName(),20));
|
||||||
logEnt.setOpTime(LocalDateTime.now());
|
logEnt.setOpTime(LocalDateTime.now());
|
||||||
logEnt.setDptName(SessionUtil.getDepartName());
|
logEnt.setDptName(SessionUtil.getDepartName());
|
||||||
logService.save(logEnt);
|
logService.save(logEnt);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,107 @@
|
||||||
|
package com.nflg.product.bomnew.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 曹鹏飞
|
||||||
|
* @date 2024-03-29 16:52:05
|
||||||
|
*/
|
||||||
|
public class StringUtil {
|
||||||
|
|
||||||
|
public static String subString(String input,int maxLen){
|
||||||
|
if (input == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int byteCount = 0;
|
||||||
|
int charCount = 0;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
// 遍历字符串的每一个字符
|
||||||
|
for (int i = 0; i < input.length(); i++) {
|
||||||
|
char c = input.charAt(i);
|
||||||
|
|
||||||
|
// 检查字符是否是双字节或多字节字符,这里简单地通过Character.isHighSurrogate检查,适用于大部分情况
|
||||||
|
if (Character.isHighSurrogate(c)) {
|
||||||
|
// 如果是高代理项,则下一个字符也是有效部分,一起计算长度
|
||||||
|
if (i + 1 < input.length() && Character.isLowSurrogate(input.charAt(i + 1))) {
|
||||||
|
sb.append(c);
|
||||||
|
sb.append(input.charAt(i + 1));
|
||||||
|
i++; // 跳过低代理项
|
||||||
|
byteCount += 3; // 中文等宽字体一般认为占3个长度单位
|
||||||
|
charCount += 1; // Unicode字符数增加1
|
||||||
|
} else {
|
||||||
|
// 不完整的 surrogate pair,跳过
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else if (c > '\u00ff') { // 这里假设非代理项的非ASCII字符都按3个长度单位计算
|
||||||
|
sb.append(c);
|
||||||
|
byteCount += 3;
|
||||||
|
charCount += 1;
|
||||||
|
} else {
|
||||||
|
sb.append(c);
|
||||||
|
byteCount += 1;
|
||||||
|
charCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byteCount > maxLen) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 截取文本,只取30个有效长度,每个中文占用3个长度,其余字符占用1个长度。
|
||||||
|
*
|
||||||
|
* @param text 需要截取的文本
|
||||||
|
* @param maxLength 有效长度上限
|
||||||
|
* @return 截取后的文本
|
||||||
|
*/
|
||||||
|
public static String trimText(String text, int maxLength) {
|
||||||
|
if (text == null || text.isEmpty()) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lengthCount = 0; // 用于记录有效长度
|
||||||
|
StringBuilder trimmedStringBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
for (char c : text.toCharArray()) {
|
||||||
|
if (isChineseCharacter(c)) {
|
||||||
|
// 如果是中文字符,检查剩余有效长度是否足够3个长度
|
||||||
|
if (lengthCount + 3 <= maxLength) {
|
||||||
|
trimmedStringBuilder.append(c);
|
||||||
|
lengthCount += 3;
|
||||||
|
} else {
|
||||||
|
// 不足3个长度,直接返回已截取的文本
|
||||||
|
return trimmedStringBuilder.toString();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 非中文字符,检查剩余有效长度是否足够1个长度
|
||||||
|
if (lengthCount + 1 <= maxLength) {
|
||||||
|
trimmedStringBuilder.append(c);
|
||||||
|
lengthCount++;
|
||||||
|
} else {
|
||||||
|
// 不足1个长度,直接返回已截取的文本
|
||||||
|
return trimmedStringBuilder.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return trimmedStringBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查字符是否为中文字符。
|
||||||
|
*
|
||||||
|
* @param ch 需要检查的字符
|
||||||
|
* @return 如果是中文字符则返回true,否则返回false
|
||||||
|
*/
|
||||||
|
private static boolean isChineseCharacter(char ch) {
|
||||||
|
Character.UnicodeBlock block = Character.UnicodeBlock.of(ch);
|
||||||
|
return block == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|
||||||
|
|| block == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|
||||||
|
|| block == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|
||||||
|
|| block == Character.UnicodeBlock.GENERAL_PUNCTUATION
|
||||||
|
|| block == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
package com.nflg.product.bomnew.service.test;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||||
|
import com.nflg.product.bomnew.constant.EBomStatusEnum;
|
||||||
|
import com.nflg.product.bomnew.pojo.entity.BomNewLogEntity;
|
||||||
|
import com.nflg.product.bomnew.util.StringUtil;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 曹鹏飞
|
||||||
|
* @date 2024-03-06 14:24:10
|
||||||
|
*/
|
||||||
|
public class OtherTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1(){
|
||||||
|
Integer d=2;
|
||||||
|
System.out.println("他人创建的"+ EBomStatusEnum.findByValue(2).getDescription() +"数据");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test2(){
|
||||||
|
BomNewLogEntity logEnt=new BomNewLogEntity();
|
||||||
|
logEnt.setRowId(IdWorker.getId());
|
||||||
|
System.out.println("数据:"+ JSON.toJSONString(logEnt));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test3(){
|
||||||
|
String data1="12345ds67八九dsf";
|
||||||
|
String data2="12345ds67";
|
||||||
|
Assert.assertEquals(data2,StringUtil.trimText(data1,9));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test4(){
|
||||||
|
String data1="12345ds67八九dsf";
|
||||||
|
String data2="12345ds67";
|
||||||
|
Assert.assertEquals(data2,StringUtil.trimText(data1,11));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test5(){
|
||||||
|
String data1="12345ds67八九dsf";
|
||||||
|
String data2="12345ds67八九";
|
||||||
|
Assert.assertEquals(data2,StringUtil.trimText(data1,15));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test6(){
|
||||||
|
String data1="12345ds67八九dsf";
|
||||||
|
String data2="12345ds67八九d";
|
||||||
|
Assert.assertEquals(data2,StringUtil.trimText(data1,16));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test7(){
|
||||||
|
String data1="12345ds67八九dsf";
|
||||||
|
String data2="12345ds67八九dsf";
|
||||||
|
Assert.assertEquals(data2,StringUtil.trimText(data1,50));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test8(){
|
||||||
|
String data1="12345ds67八九dsf";
|
||||||
|
String data2="1";
|
||||||
|
Assert.assertEquals(data2,StringUtil.trimText(data1,1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test9(){
|
||||||
|
String data1="12345ds67八九dsf";
|
||||||
|
String data2="";
|
||||||
|
Assert.assertEquals(data2,StringUtil.trimText(data1,0));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue