Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0.2 object list dev #97

Open
wants to merge 5 commits into
base: 3.0.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mzt.logapi.starter.annotation;

import java.lang.annotation.*;

/**
* 比较对象中的唯一标识
* @author focus
*/
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface CompareID {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mzt.logapi.starter.configuration;

import lombok.Data;
import org.apache.logging.log4j.util.Strings;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -53,6 +54,16 @@ public class LogRecordProperties {
* 字段更新后的日志内容模板
*/
private String updateTemplate = "【" + FIELD_PLACEHOLDER + "】从【" + SOURCE_VALUE_PLACEHOLDER + "】修改为【" + TARGET_VALUE_PLACEHOLDER + "】";

/**
* 列表更新后,更新列表中元素内容的日志模板内容
*/
private String updateTemplateForListElement= FIELD_PLACEHOLDER + ":List内部的数据更新如下: {" +TARGET_VALUE_PLACEHOLDER+ "}";

/**
* 列表更新后, 更新列表中标识为同一个对象的Id日志模板内容
*/
private String formatUpdateForListElementWithCompareId = "[CompareId:{" +TARGET_VALUE_PLACEHOLDER+ "}]:\t";
/**
* 字段值被设置为null后的日志内容模板
*/
Expand All @@ -70,7 +81,10 @@ public class LogRecordProperties {
*/
private String ofWord = "的";


/**
* 作为标识对象唯一标识的属性的值的分隔符
*/
private String eq = "=";
public void setAddTemplate(String template) {
validatePlaceHolder(template);
this.addTemplate = template;
Expand Down Expand Up @@ -122,5 +136,18 @@ public String formatList(String fieldName, String addContent, String delContent)
}
return "";
}
public String formatUpdateForListObject(String filedLogName, String targetValue) {
if(!StringUtils.isEmpty(targetValue)){
return updateTemplateForListElement.replace(FIELD_PLACEHOLDER,filedLogName).replace(TARGET_VALUE_PLACEHOLDER,targetValue);
}
return "";
}

public String formatUpdateForListElementWithCompareId(String targetValue) {
if(!Strings.isEmpty(targetValue)){
return formatUpdateForListElementWithCompareId.replace(TARGET_VALUE_PLACEHOLDER,targetValue);
}
return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.collect.Lists;
import com.mzt.logapi.service.IFunctionService;
import com.mzt.logapi.service.impl.DiffParseFunction;
import com.mzt.logapi.starter.annotation.CompareID;
import com.mzt.logapi.starter.annotation.DiffLogField;
import com.mzt.logapi.starter.annotation.DiffLogAllFields;
import com.mzt.logapi.starter.annotation.DIffLogIgnore;
Expand Down Expand Up @@ -91,7 +93,9 @@ private void memorandum(DiffNode node, Set<DiffNode> set) {
ReflectionUtils.makeAccessible(childrenField);
Map<ElementSelector, DiffNode> children = (Map<ElementSelector, DiffNode>) ReflectionUtils.getField(childrenField, node);
assert children != null;
for (DiffNode value : children.values()) memorandum(value, set);
for (DiffNode value : children.values()){
memorandum(value, set);
}
}
}

Expand Down Expand Up @@ -144,9 +148,35 @@ public String getCollectionDiffLogContent(String filedLogName, DiffNode node, Ob
Collection<Object> delItemList = listSubtract(sourceList, targetList);
String listAddContent = listToContent(functionName, addItemList);
String listDelContent = listToContent(functionName, delItemList);
return logRecordProperties.formatList(filedLogName, listAddContent, listDelContent);

return logRecordProperties.formatList(filedLogName, listAddContent, listDelContent)
+ retainContent(filedLogName,sourceList,targetList);
}

private String retainContent(String filedLogName, Collection<Object> sourceList, Collection<Object> targetList){

Collection<Object> sources = new ArrayList<>(sourceList);
Collection<Object> targets = new ArrayList<>(targetList);

StringBuilder stringBuilder = new StringBuilder();
Iterator sourceIt = sources.iterator();

sources.stream().forEach(e->{
Object source = sourceIt.next();
Iterator targetIt = targets.iterator();
while (targetIt.hasNext()){
Object target = targetIt.next();
if(compare(source,target) && isContainsCompareId(source)){
stringBuilder.append(getCompareFieldAndValue(source))
.append(getBeanFactory().getBean(DiffParseFunction.class).diff(source,target))
.append(logRecordProperties.getListItemSeparator());
targetIt.remove();
}
}
});
return logRecordProperties.formatUpdateForListObject(filedLogName,stringBuilder.toString());
}

public String getDiffLogContent(String filedLogName, DiffNode node, Object sourceObject, Object targetObject, String functionName) {
switch (node.getState()) {
case ADDED:
Expand All @@ -163,7 +193,6 @@ public String getDiffLogContent(String filedLogName, DiffNode node, Object sourc

private Collection<Object> getListValue(DiffNode node, Object object) {
Object fieldSourceValue = getFieldValue(node, object);
//noinspection unchecked
if (fieldSourceValue != null && fieldSourceValue.getClass().isArray()) {
return new ArrayList<>(Arrays.asList((Object[]) fieldSourceValue));
}
Expand All @@ -172,10 +201,126 @@ private Collection<Object> getListValue(DiffNode node, Object object) {

private Collection<Object> listSubtract(Collection<Object> minuend, Collection<Object> subTractor) {
Collection<Object> addItemList = new ArrayList<>(minuend);
addItemList.removeAll(subTractor);
Collection<Object> delItemList = new ArrayList<>(subTractor);

Iterator addItemListIt = addItemList.iterator();
boolean removeFlag = false;

while ( addItemListIt.hasNext()){
Object source = addItemListIt.next();
Iterator delItemListIt = delItemList.iterator();

while (delItemListIt.hasNext()){
Object target = delItemListIt.next();
if(compare(source,target)){
removeFlag = true;
}
}

if(removeFlag){
addItemListIt.remove();
removeFlag = false;
}
}

return addItemList;
}


/**
* 两个对象之间的比较是否相等
*/
protected boolean compare(Object source, Object target){

if(!isContainsCompareId(source)){
return source.equals(target);
}

Map<String,Object> sourceFieldValueMap = getCompareIdFieldValueMap(source);
Map<String,Object> targetFieldValueMap = getCompareIdFieldValueMap(target);

for (Map.Entry<String,Object> entry : sourceFieldValueMap.entrySet()){
String key = entry.getKey();
Object value = entry.getValue();

if(!Objects.equals(value,targetFieldValueMap.get(key))){
return false;
}
}

return true;
}


private boolean isContainsCompareId(Object target){

Field[] fields = target.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
CompareID compareID = field.getAnnotation(CompareID.class);
if(compareID != null){
return true;
}
}
return false;
}


private String getCompareFieldAndValue(Object target){
Field[] fields = target.getClass().getDeclaredFields();
StringBuilder stringBuilder = new StringBuilder();
for (Field field : fields) {
field.setAccessible(true);
CompareID compareID = field.getAnnotation(CompareID.class);
if(compareID == null){
continue;
}

try {
Object obj = field.get(target);
DiffLogField diffLogField = field.getAnnotation(DiffLogField.class);
String compareKey = diffLogField == null ? field.getName() : diffLogField.name();
stringBuilder.append(compareKey)
.append(logRecordProperties.getEq())
.append(obj)
.append(logRecordProperties.getListItemSeparator());
} catch (Exception e) {
throw new RuntimeException(e);
}

}

return logRecordProperties.formatUpdateForListElementWithCompareId(stringBuilder.toString());
}


/**
* 获取存在比较唯一ID的注解的对象
* @return key= fieldName, value=fieldValue
*/
private Map<String,Object> getCompareIdFieldValueMap(Object source){

Map<String,Object> fieldValueMap = new HashMap<>();

Field[] fields = source.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
CompareID compareID = field.getAnnotation(CompareID.class);
if(compareID != null){
try {
fieldValueMap.put(field.getName(),field.get(source));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

return fieldValueMap;
}




private String listToContent(String functionName, Collection<Object> addItemList) {
StringBuilder listAddContent = new StringBuilder();
if (!CollectionUtils.isEmpty(addItemList)) {
Expand Down
16 changes: 16 additions & 0 deletions bizlog-server/src/main/java/com/mzt/logserver/pojo/Order.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.mzt.logserver.pojo;

import com.mzt.logapi.starter.annotation.CompareID;
import com.mzt.logapi.starter.annotation.DiffLogField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

Expand Down Expand Up @@ -35,11 +37,25 @@ public class Order {
@DiffLogField(name = "拓展信息", function = "extInfo")
private String[] extInfo;

@DiffLogField(name = "审核人列表")
private List<UserDO> auditors = new ArrayList<>();

@Data
public static class UserDO {
@DiffLogField(name = "用户ID")
@CompareID
private Long userId;
@DiffLogField(name = "用户姓名")
private String userName;
}

public Order addAuditor(Long userId, String userName){

UserDO userDO = new UserDO();
userDO.userId = userId;
userDO.userName = userName;

auditors.add(userDO);
return this;
}
}
Loading