更新字段差异 记录

package com.threeclear.pollute.entity;

import java.lang.reflect.Field;
import java.util.Date;

import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 *
 * @author makejava
 * @since 2021-06-29 19:43:50
 */
@SuppressWarnings("serial")
@Data
public class RlGeneralIndustryEnterprise extends Model<RlGeneralIndustryEnterprise> {
    public final static Map COLUMN = new HashMap();

    {
        COLUMN.put("id", "id");
        COLUMN.put("name", "企业名");
        COLUMN.put("address", "企业地址");
       

    }

    //id
//    @TableId
    private Long id;
    //企业名
    private String name;
    //企业地址
    private String address;
   


    

    /**
     * 获取主键值
     *
     * @return 主键值
     */
    @Override
    protected Serializable pkVal() {
        return this.id;
    }

    public TGridOperationLog comparatorObject(String unityOperate, Object oldObj) throws IllegalAccessException {
        StringBuilder matter = new StringBuilder();
        StringBuilder contentOld = new StringBuilder();
        StringBuilder contentNew = new StringBuilder();

        if (oldObj != null&&"更新".equals(unityOperate)) {
            Map<String, Object> oldMap = changeValueToMap(oldObj);
            Map<String, Object> newMap = changeValueToMap(this);
            if (oldMap != null && !oldMap.isEmpty()) {
                for (Map.Entry<String, Object> entry : oldMap.entrySet()) {
                    Object oldValue = entry.getValue();
                    System.out.println("oldValue = " + oldValue);
                    Object newValue = newMap.get(entry.getKey());
                    System.out.println("newValue = " + newValue);
                    if ((oldValue != null&&!oldValue.equals(newValue))||(newValue != null && !newValue.equals(oldValue))) {
                        matter.append(entry.getKey()).append("、");
                        contentOld.append(oldValue).append("、");
                        contentNew.append(newValue).append("、");
                    }
                }
            }
            matter.deleteCharAt(matter.length() - 1);
            contentOld.deleteCharAt(contentOld.length() - 1);
            contentNew.deleteCharAt(contentOld.length() - 1);
//            matter.deleteCharAt(matter.length() - 1);
        } else {
            matter.append("-");
            contentOld.append("-");
            contentNew.append("-");
        }
        return TGridOperationLog.TGridOperationLogBuilder
                .aTGridOperationLog()
//                .withOperationTime()
                .withSourceName(name)
                .withOperationType(unityOperate)
                .withAlterColumn(String.valueOf(matter))
                .withBeforeModification(String.valueOf(contentOld))
                .withAfterModification(String.valueOf(contentNew))
                .build();
    }

    /**
     * 将类对象转换成Map
     *
     * @param entity 原对象
     * @return Map
     * @throws IllegalAccessException 类型转换时报错
     */
    private static Map<String, Object> changeValueToMap(Object entity) throws IllegalAccessException {
        Map<String, Object> resultMap = new HashMap<>();
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
//            if (PropertyUtils.isReadable(entity, name) && PropertyUtils.isWriteable(entity, name)) {
//                if (field.isAnnotationPresent(LogCompar.class)) {
//                    LogCompar anno = field.getAnnotation(LogCompar.class);
//                    //获取private对象字段值
//                    field.setAccessible(true);
//                    resultMap.put(anno.name(), field.get(entity));
//                }
//            }
            field.setAccessible(true);
            resultMap.put((String) COLUMN.get(name), field.get(entity));
        }
        return resultMap;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值