package com.bootdo.common.utils; import java.lang.reflect.Field; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.bootdo.common.constant.FlagConstant; import cn.hutool.core.date.DateUtil; public class DataSupplyUtils { private final static Logger logger = LoggerFactory.getLogger(DataSupplyUtils.class); private final static String createBy = "createBy"; private final static String createDate = "createDate"; private final static String updateBy = "updateBy"; private final static String updateDate = "updateDate"; private final static String delFlag = "delFlag"; public staticvoid deleteDataSupply(T t) { // 通过反射获取类的Class对象 @SuppressWarnings("rawtypes") Class clazz = t.getClass(); logger.info("删除对象{}时补充数据", clazz.getName()); // 获取类型及字段属性 try { Field delFlagField = clazz.getDeclaredField(delFlag); try { if (null != delFlagField) { delFlagField.setAccessible(true); delFlagField.set(t, FlagConstant.YES); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } public static void updateDataSupply(T t) { // 通过反射获取类的Class对象 @SuppressWarnings("rawtypes") Class clazz = t.getClass(); logger.info("编辑对象{}时补充数据", clazz.getName()); // 获取类型及字段属性 try { Field updateByField = clazz.getDeclaredField(updateBy); Field updateDateField = clazz.getDeclaredField(updateDate); try { if (null != updateByField) { updateByField.setAccessible(true); updateByField.set(t, ShiroUtils.getUsername()); } if (null != updateDateField) { updateDateField.setAccessible(true); updateDateField.set(t, DateUtil.date()); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } public static void createDataSupply(T t) { // 通过反射获取类的Class对象 @SuppressWarnings("rawtypes") Class clazz = t.getClass(); logger.info("创建对象{}时补充数据", clazz.getName()); // 获取类型及字段属性 try { Field createByField = clazz.getDeclaredField(createBy); Field createDateField = clazz.getDeclaredField(createDate); Field delFlagField = clazz.getDeclaredField(delFlag); try { if (null != createByField) { createByField.setAccessible(true); createByField.set(t, ShiroUtils.getUsername()); } if (null != createDateField) { createDateField.setAccessible(true); createDateField.set(t, DateUtil.date()); } if (null != delFlagField) { delFlagField.setAccessible(true); delFlagField.set(t, FlagConstant.NO); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)