Skip to content

Commit

Permalink
优化修改描述字符串拼接方式
Browse files Browse the repository at this point in the history
  • Loading branch information
geekideaio committed Dec 6, 2019
1 parent f5c3ecd commit 95dfee0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.geekidea.updaterecord.core.factory.UpdateRecordServiceFactory;
import io.geekidea.updaterecord.core.util.IpUtil;
import io.geekidea.updaterecord.core.util.Jackson;
import io.geekidea.updaterecord.core.util.UpdateRecordUtil;
import io.geekidea.updaterecord.core.util.UuidUtil;
import io.geekidea.updaterecord.core.vo.FieldCompareVo;
import io.geekidea.updaterecord.core.vo.UpdateRecordItemVo;
Expand Down Expand Up @@ -255,7 +256,8 @@ protected void doAfterReturningHandle(JoinPoint joinPoint, Object result) throws
List<UpdateRecordTableLog> updateRecordTableLogs = new ArrayList<>();
// 所有修改列的集合
Set<UpdateRecordColumnLog> updateRecordColumnLogs = new LinkedHashSet<>();
StringBuilder builder = new StringBuilder();
// 表修改记录描述集合
List<String> updateAllDescList = new ArrayList<>();
// 修改数量统计
// 修改表总数
int tableTotal = 0;
Expand Down Expand Up @@ -311,7 +313,7 @@ protected void doAfterReturningHandle(JoinPoint joinPoint, Object result) throws

String updateDesc = updateRecordTableLog.getUpdateDesc();
if (StringUtils.isNotBlank(updateDesc)) {
builder.append(updateDesc + tableSeparator);
updateAllDescList.add(updateDesc);
}

tableTotal++;
Expand Down Expand Up @@ -340,11 +342,7 @@ protected void doAfterReturningHandle(JoinPoint joinPoint, Object result) throws
}

// 去掉最后一个分隔符
String updateAllDesc = builder.toString();
if (StringUtils.isNotBlank(updateAllDesc)){
int lastIndex = updateAllDesc.lastIndexOf(tableSeparator);
updateAllDesc = updateAllDesc.substring(0,lastIndex);
}
String updateAllDesc = UpdateRecordUtil.join(updateAllDescList,tableSeparator);

// 设置修改记录
updateRecordLog
Expand Down Expand Up @@ -421,9 +419,9 @@ private static String generateDiffMapKey(int i, UpdateRecordTableLog updateRecor
*/
protected void save(UpdateRecordLog updateRecordLog, List<UpdateRecordTableLog> updateRecordTableLogs, Set<UpdateRecordColumnLog> updateRecordColumnLogs) {
boolean format = UpdateRecordConfigProperties.DEBUG;
log.info("updateRecordLog:{}", Jackson.toJsonString(updateRecordLog, true));
log.info("updateRecordTableLogs:{}", Jackson.toJsonString(updateRecordTableLogs, true));
log.info("updateRecordColumnLogs:{}", Jackson.toJsonString(updateRecordColumnLogs, true));
log.info("updateRecordLog:{}", Jackson.toJsonString(updateRecordLog, format));
log.info("updateRecordTableLogs:{}", Jackson.toJsonString(updateRecordTableLogs, format));
log.info("updateRecordColumnLogs:{}", Jackson.toJsonString(updateRecordColumnLogs, format));
updateRecordService.save(updateRecordLog, updateRecordTableLogs, updateRecordColumnLogs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.geekidea.updaterecord.core.constant.UpdateRecordConstant;
import io.geekidea.updaterecord.core.enums.ModeEnum;
import io.geekidea.updaterecord.core.util.Jackson;
import io.geekidea.updaterecord.core.util.UpdateRecordUtil;
import io.geekidea.updaterecord.core.vo.FieldCompareVo;
import io.geekidea.updaterecord.core.vo.UpdateRecordItemVo;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -66,7 +67,7 @@ public static UpdateRecordItemVo compare(Map<String, Object> beforeMap, Map<Stri
// 修改列集合
Set<UpdateRecordColumnLog> updateRecordColumnLogs = new LinkedHashSet<>();
// 修改描述字符串
StringBuilder builder = new StringBuilder();
List<String> updateDescList = new ArrayList<>();
// 获取对象的所有可访问字段
Set<String> columnNames = beforeMap.keySet();

Expand Down Expand Up @@ -143,22 +144,18 @@ public static UpdateRecordItemVo compare(Map<String, Object> beforeMap, Map<Stri
// add
modeEnum = ModeEnum.ADD;
addCount++;
builder.append(String.format(UpdateRecordConfigProperties.ADD_MODEL_FORMAT, columnDesc, afterValueString));
updateDescList.add(String.format(UpdateRecordConfigProperties.ADD_MODEL_FORMAT, columnDesc, afterValueString));
} else if (afterValue == null || "".equals(afterValue.toString().trim())) {
// delete
modeEnum = ModeEnum.DELETE;
deleteCount++;
builder.append(String.format(UpdateRecordConfigProperties.DELETE_MODEL_FORMAT, columnDesc));
updateDescList.add(String.format(UpdateRecordConfigProperties.DELETE_MODEL_FORMAT, columnDesc));
} else {
// 两个字段有值,且不相等,则为修改
// update
modeEnum = ModeEnum.UPDATE;
updateCount++;
builder.append(String.format(UpdateRecordConfigProperties.UPDATE_MODEL_FORMAT, columnDesc, beforeValueString, afterValueString));
}

if (i < size) {
builder.append(columnSeparator);
updateDescList.add(String.format(UpdateRecordConfigProperties.UPDATE_MODEL_FORMAT, columnDesc, beforeValueString, afterValueString));
}

// 字段比较对象
Expand Down Expand Up @@ -188,11 +185,7 @@ public static UpdateRecordItemVo compare(Map<String, Object> beforeMap, Map<Stri


// 去掉最后一个分隔符
String updateDesc = builder.toString();
if (StringUtils.isNotBlank(updateDesc)){
int lastIndex = updateDesc.lastIndexOf(columnSeparator);
updateDesc = updateDesc.substring(0,lastIndex);
}
String updateDesc = UpdateRecordUtil.join(updateDescList,columnSeparator);

// 4. 得出修改部分内容
total = addCount + updateCount + deleteCount;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
*
* 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 io.geekidea.updaterecord.core.util;

import io.geekidea.updaterecord.core.constant.UpdateRecordConstant;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* 工具类
*
* @author geekidea
* @date 2019-12-06
**/
public class UpdateRecordUtil {

/**
* 将集合转换成字符串,使用分隔符串联
*
* @param list
* @param separator
* @return
*/
public static String join(List<String> list, String separator) {
if (CollectionUtils.isEmpty(list)) {
return null;
}
if (StringUtils.isEmpty(separator)) {
separator = UpdateRecordConstant.SEPARATOR;
}
return StringUtils.join(list, separator);
}

}

0 comments on commit 95dfee0

Please sign in to comment.