feat: 基础信息工具类

新增和修改时快速设置通用列
This commit is contained in:
徐晨晨 2022-07-05 10:45:06 +08:00
parent cd1d203d87
commit f154d361a8
2 changed files with 188 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package com.cnbm.admin.enums;
/**
* <p>
* 否有效枚举
* </P>
*
* @author xcc
* @date 2022年7月5日
* @since 1.0
*/
public enum WhetherEnum {
NO(0, ""),
YES(1, "");
private final Integer value;
private final String label;
private final String remark;
WhetherEnum(final int value, final String label) {
this(value, label, null);
}
WhetherEnum(final int value, final String label, final String remark) {
this.value = value;
this.label = label;
this.remark = remark;
}
/**
* @return 数据值
*/
public Integer getValue() {
return value;
}
/**
* @return 标签名
*/
public String getLabel() {
return label;
}
/**
* @return 备注
*/
public String getRemark() {
return remark;
}
}

View File

@ -0,0 +1,136 @@
/*
* Copyright (c) 2018.
* http://www.ulabcare.com
*/
package com.cnbm.admin.utils;
import com.cnbm.admin.entity.LoginUser;
import com.cnbm.admin.entity.SysUserEntity;
import com.cnbm.admin.enums.WhetherEnum;
import lombok.Builder;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 接口支持基类
*
* @author jiff
* @date 2018/11/1
* @since 1.0
*/
public abstract class BaseSupportUtils {
/**
* 获取当前登录用户信息
*
* @return
*/
private static SysUserEntity getLoginUser() {
//登录用户信息
UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
return loginUser.getSysUserEntity();
}
/**
* 设置公共字段值一般用于创建新记录包含以下字段
*
* <p>
* {@link CommonField#enabled}<br>
* {@link CommonField#valid}<br>
* {@link CommonField#creatorId}<br>
* {@link CommonField#creatorName}<br>
* {@link CommonField#createTime}<br>
* {@link CommonField#updaterId}<br>
* {@link CommonField#updaterName}<br>
* {@link CommonField#updateTime}<br>
* </p>
*
* @param t 需要设置的对象
* @param ignoreProperties 忽略的字段
* @param <T>
*/
public static <T extends Serializable> T setCommonField(T t, String... ignoreProperties) {
CommonField commonField = CommonField.builder()
.enabled(WhetherEnum.YES.getValue())
.valid(WhetherEnum.YES.getValue())
.createTime(LocalDateTime.now())
.creatorId(getLoginUser().getId())
.creatorName(getLoginUser().getUsername())
.updateTime(LocalDateTime.now())
.updaterId(getLoginUser().getId())
.updaterName(getLoginUser().getUsername())
.build();
BeanUtils.copyProperties(commonField, t, ignoreProperties);
return t;
}
/**
* 设置更新的公共字段值一般用于更新记录包含以下字段
*
* <p>
* {@link CommonField#updaterId}<br>
* {@link CommonField#updaterName}<br>
* {@link CommonField#updateTime}<br>
* </p>
*
* @param t 需要设置的对象
* @param <T>
*/
public static <T extends Serializable> T setUpdateCommonField(T t) {
CommonField commonField = CommonField.builder()
.updaterId(getLoginUser().getId())
.updaterName(getLoginUser().getUsername())
.updateTime(LocalDateTime.now())
.build();
BeanUtils.copyProperties(commonField, t, "enabled", "valid");
return t;
}
@Data
@Builder
private static class CommonField implements Serializable {
/**
* 启用状态:0 停用1启用
*/
private Integer enabled;
/**
* 删除标志是否有效:1 可用 0不可用
*/
private Integer valid;
/**
* 创建人
*/
private Long creatorId;
/**
* 创建人
*/
private String creatorName;
/**
* 创建时间
*/
private LocalDateTime createTime;
/**
* 更新人
*/
private Long updaterId;
/**
* 更新人
*/
private String updaterName;
/**
* 更新时间
*/
private LocalDateTime updateTime;
}
}