package com.mosty.hczx.mybatisplus; import cn.hutool.http.HttpStatus; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.mosty.common.base.exception.BusinessException; import com.mosty.common.base.util.IpUtil; import com.mosty.common.token.UserInfo; import com.mosty.common.token.UserInfoManager; import org.apache.ibatis.reflection.MetaObject; import java.sql.Timestamp; import java.util.Objects; /** * MybatisPlus注入处理器 * * @author Lhh * @date 2021/4/25 */ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { try { UserInfo userInfo = UserInfoManager.getUser(); //根据属性名字设置要填充的值 if (metaObject.hasGetter("xtCjip")) { if (metaObject.getValue("xtCjip") == null) { this.strictInsertFill(metaObject, "xtCjip", String.class, IpUtil.getIpAddress()); } } //根据属性名字设置要填充的值 if (metaObject.hasGetter("xtSjzt")) { if (metaObject.getValue("xtSjzt") == null) { this.strictInsertFill(metaObject, "xtSjzt", String.class, "1"); } } if (metaObject.hasGetter("xtScbz")) { if (metaObject.getValue("xtScbz") == null) { this.strictInsertFill(metaObject, "xtScbz", String.class, "0"); } } if (metaObject.hasGetter("xtCjsj")) { if (metaObject.getValue("xtCjsj") == null) { this.strictInsertFill(metaObject, "xtCjsj", Timestamp.class, new Timestamp(System.currentTimeMillis())); } } if (Objects.nonNull(userInfo)) { if (metaObject.hasGetter("xtCjrId")) { if (metaObject.getValue("xtCjrId") == null) { this.strictInsertFill(metaObject, "xtCjrId", String.class, String.valueOf(userInfo.userId)); } } if (metaObject.hasGetter("xtCjr")) { if (metaObject.getValue("xtCjr") == null) { this.strictInsertFill(metaObject, "xtCjr", String.class, String.valueOf(userInfo.userName)); } } if (metaObject.hasGetter("xtCjbmdm")) { if (metaObject.getValue("xtCjbmdm") == null) { this.strictInsertFill(metaObject, "xtCjbmdm", String.class, String.valueOf(userInfo.getDeptId())); } } if (metaObject.hasGetter("xtCjbmmc")) { if (metaObject.getValue("xtCjbmmc") == null) { this.strictInsertFill(metaObject, "xtCjbmmc", String.class, userInfo.getDeptName()); } } } } catch (Exception e) { throw new BusinessException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); } } @Override public void updateFill(MetaObject metaObject) { try { UserInfo userInfo = UserInfoManager.getUser(); if (metaObject.hasGetter("xtZhgxip")) { if (metaObject.getValue("xtZhgxip") == null) { this.strictUpdateFill(metaObject, "xtZhgxip", String.class, IpUtil.getIpAddress()); } } if (metaObject.hasGetter("xtZhgxsj")) { if (metaObject.getValue("xtZhgxsj") == null) { this.strictUpdateFill(metaObject, "xtZhgxsj", Timestamp.class, new Timestamp(System.currentTimeMillis())); } } if (Objects.nonNull(userInfo)) { if (metaObject.hasGetter("xtZhgxrid")) { if (metaObject.getValue("xtZhgxrid") == null) { this.strictUpdateFill(metaObject, "xtZhgxrid", String.class, String.valueOf(userInfo.userId)); } } if (metaObject.hasGetter("xtZhgxr")) { if (metaObject.getValue("xtZhgxr") == null) { this.strictUpdateFill(metaObject, "xtZhgxr", String.class, userInfo.userName); } } if (metaObject.hasGetter("xtZhgxbmdm")) { if (metaObject.getValue("xtZhgxbmdm") == null) { this.strictUpdateFill(metaObject, "xtZhgxbmdm", String.class, String.valueOf(userInfo.getDeptId())); } } if (metaObject.hasGetter("xtZhgxbm")) { if (metaObject.getValue("xtZhgxbm") == null) { this.strictUpdateFill(metaObject, "xtZhgxbm", String.class, userInfo.getDeptName()); } } } } catch (Exception e) { throw new BusinessException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); } } }