This commit is contained in:
2023-03-02 13:22:49 +08:00
parent 8c0e43d9f7
commit 57b79b4f0e
11 changed files with 232 additions and 29 deletions

View File

@@ -0,0 +1,155 @@
/*
* Copyright (c) 2018.
* http://www.ulabcare.com
*/
package com.cnbm.admin.base;
import com.cnbm.admin.entity.LoginUser;
import com.cnbm.admin.enums.WhetherEnum;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.NamedThreadLocal;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* 接口支持基类
*
* @author jiff
* @date 2018/11/1
* @since 1.0
*/
@Service
public class BaseSupport {
/**
* 获取当前登录用户信息
*
* @return
*/
protected LoginUser getLoginUser() {
UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
if (Objects.isNull(authentication)) {
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
return loginUser;
}
return null;
}
/**
* 设置公共字段值,一般用于创建新记录,包含以下字段:
*
* <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 <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().getSysUserEntity().getId())
.creatorName(getLoginUser().getUsername())
.updateTime(LocalDateTime.now())
.updaterId(getLoginUser().getSysUserEntity().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 <T extends Serializable> T setUpdateCommonField(T t) {
CommonField commonField = CommonField.builder()
.updaterId(getLoginUser().getSysUserEntity().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;
}
}