/** * Copyright (c) 2018 人人开源 All rights reserved. * * https://www.renren.io * * 版权所有,侵权必究! */ package com.example.server.entity; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.example.client.entity.PlatformEntity; import com.example.client.entity.PlatformLogEntity; import com.example.client.entity.TenantEntity; import com.example.server.user.model.SysUser; import com.example.server.utils.CacheUtils; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Date; /** * 公共字段,自动填充值 * * @author hehz */ @Component("fieldHandler") public class FieldMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { Object object = metaObject.getOriginalObject(); SysUser user = (SysUser) CacheUtils.get("user","user"); LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 将当前日期格式化为字符串 String date = currentDate.format(formatter); if (object instanceof PlatformLogEntity) { PlatformLogEntity entity = (PlatformLogEntity) object; entity.setCreator(user.getUserId());// 创建者 entity.setCreateDate(date);// 创建时间 } if (object instanceof PlatformEntity) { PlatformEntity entity = (PlatformEntity) object; entity.setDelete(false); entity.setUpdater(user.getUserId());// 更新者 entity.setUpdateDate(date);// 更新时间 } if (object instanceof TenantEntity) { TenantEntity entity = (TenantEntity) object; entity.setTenantId(user.getTenantId()); } } @Override public void updateFill(MetaObject metaObject) { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 将当前日期格式化为字符串 String date = currentDate.format(formatter); SysUser user =(SysUser) CacheUtils.get("user","user"); Object object = metaObject.getOriginalObject(); if (object instanceof PlatformEntity) { PlatformEntity entity = (PlatformEntity) object; entity.setUpdater(user.getUserId());// 更新者 entity.setUpdateDate(date);// 更新时间 } } private boolean isEmpty(Long value) { return value == null || value == 0; } }