|  | @@ -0,0 +1,88 @@
 | 
	
		
			
				|  |  | +package com.ym.mec.biz.service.impl;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.alibaba.druid.sql.visitor.functions.Char;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.dto.VipGroupActivityAddDto;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.enums.VipGroupActivityTypeEnum;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.service.LogService;
 | 
	
		
			
				|  |  | +import com.ym.mec.util.date.DateUtil;
 | 
	
		
			
				|  |  | +import io.swagger.annotations.ApiModelProperty;
 | 
	
		
			
				|  |  | +import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | +import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.lang.reflect.Field;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * @Author qnc99
 | 
	
		
			
				|  |  | + * @Date 2020/12/15 0015
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +@Service
 | 
	
		
			
				|  |  | +public class LogServiceImpl implements LogService {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static Set<Class<?>> FINAL_CLASS = new HashSet<>(Arrays.asList(int.class,Integer.class,long.class,Long.class,
 | 
	
		
			
				|  |  | +            float.class,Float.class,double.class,Double.class,
 | 
	
		
			
				|  |  | +            char.class, Char.class,boolean.class, Boolean.class,
 | 
	
		
			
				|  |  | +            byte.class,Byte.class,short.class, Short.class, Enum.class));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private Field[] getAllFields(Object object){
 | 
	
		
			
				|  |  | +        Class clazz = object.getClass();
 | 
	
		
			
				|  |  | +        List<Field> fieldList = new ArrayList<>();
 | 
	
		
			
				|  |  | +        while (clazz != null){
 | 
	
		
			
				|  |  | +            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
 | 
	
		
			
				|  |  | +            clazz = clazz.getSuperclass();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        Field[] fields = new Field[fieldList.size()];
 | 
	
		
			
				|  |  | +        fieldList.toArray(fields);
 | 
	
		
			
				|  |  | +        return fields;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public void entityVariationAnalysis(Object oldEntity, Object newEntity) {
 | 
	
		
			
				|  |  | +        List<String> changes = new ArrayList<>();
 | 
	
		
			
				|  |  | +        Field[] allFields = getAllFields(oldEntity);
 | 
	
		
			
				|  |  | +        for (Field field : allFields) {
 | 
	
		
			
				|  |  | +            ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
 | 
	
		
			
				|  |  | +            if(Objects.isNull(annotation)){
 | 
	
		
			
				|  |  | +                continue;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            field.setAccessible(true);
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                String oldValue = "", newValue = "";
 | 
	
		
			
				|  |  | +                if(field.getType().equals(Date.class)){
 | 
	
		
			
				|  |  | +                    if(Objects.nonNull(field.get(oldEntity))){
 | 
	
		
			
				|  |  | +                        oldValue = DateUtil.dateToString((Date) field.get(oldEntity), DateUtil.EXPANDED_DATE_TIME_FORMAT);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    if(Objects.nonNull(field.get(newEntity))){
 | 
	
		
			
				|  |  | +                        newValue = DateUtil.dateToString((Date) field.get(newEntity), DateUtil.EXPANDED_DATE_TIME_FORMAT);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }else{
 | 
	
		
			
				|  |  | +                    oldValue = String.valueOf(field.get(oldEntity));
 | 
	
		
			
				|  |  | +                    newValue = String.valueOf(field.get(newEntity));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                if(!oldValue.equals(newValue)){
 | 
	
		
			
				|  |  | +                    changes.add(annotation.value() + ":" + oldValue + " -> " + newValue);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            } catch (IllegalAccessException e) {
 | 
	
		
			
				|  |  | +                e.printStackTrace();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        System.out.println(StringUtils.join(changes, ";"));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static void main(String[] args) {
 | 
	
		
			
				|  |  | +        VipGroupActivityAddDto vipGroupActivityAddDto = new VipGroupActivityAddDto();
 | 
	
		
			
				|  |  | +        vipGroupActivityAddDto.setId(1);
 | 
	
		
			
				|  |  | +        vipGroupActivityAddDto.setStartTime(DateUtil.stringToDate("2020-12-01", "yyyy-MM-dd"));
 | 
	
		
			
				|  |  | +        vipGroupActivityAddDto.setType(VipGroupActivityTypeEnum.BASE_ACTIVITY);
 | 
	
		
			
				|  |  | +        vipGroupActivityAddDto.setSalarySettlementJson("默认课酬");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        VipGroupActivityAddDto vipGroupActivityAddDto1 = new VipGroupActivityAddDto();
 | 
	
		
			
				|  |  | +        vipGroupActivityAddDto1.setId(1);
 | 
	
		
			
				|  |  | +        vipGroupActivityAddDto1.setStartTime(DateUtil.stringToDate("2020-12-11", "yyyy-MM-dd"));
 | 
	
		
			
				|  |  | +        vipGroupActivityAddDto1.setType(VipGroupActivityTypeEnum.DISCOUNT);
 | 
	
		
			
				|  |  | +        vipGroupActivityAddDto1.setSalarySettlementJson("固定课酬");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        LogServiceImpl logService = new LogServiceImpl();
 | 
	
		
			
				|  |  | +        logService.entityVariationAnalysis(vipGroupActivityAddDto, vipGroupActivityAddDto1);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |