xm
2024-06-14 722af26bc6fec32bb289b1df51a9016a4935610f
提交 | 用户 | 时间
722af2 1 package com.dl.framework.handler;
X 2
3 import cn.hutool.core.util.ObjectUtil;
4 import cn.hutool.http.HttpStatus;
5 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
6 import com.dl.common.core.domain.BaseEntity;
7 import com.dl.common.core.domain.BaseNewEntity;
8 import com.dl.common.core.domain.model.LoginUser;
9 import com.dl.common.exception.ServiceException;
10 import com.dl.common.helper.LoginHelper;
11 import com.dl.common.utils.StringUtils;
12 import lombok.extern.slf4j.Slf4j;
13 import org.apache.ibatis.reflection.MetaObject;
14 import org.springframework.stereotype.Component;
15
16 import java.util.Date;
17 import java.util.Objects;
18
19 /**
20  * MP注入处理器
21  *
22  * @author Lion Li
23  * @date 2021/4/25
24  */
25 @Slf4j
26 public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
27
28     @Override
29     public void insertFill(MetaObject metaObject) {
30         try {
31             if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
32                 BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
33                 Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime())
34                     ? baseEntity.getCreateTime() : new Date();
35                 baseEntity.setCreateTime(current);
36                 baseEntity.setUpdateTime(current);
37                 String username = StringUtils.isNotBlank(baseEntity.getCreateBy())
38                     ? baseEntity.getCreateBy() : getLoginUsername();
39                 // 当前已登录 且 创建人为空 则填充
40                 baseEntity.setCreateBy(username);
41                 // 当前已登录 且 更新人为空 则填充
42                 baseEntity.setUpdateBy(username);
43             }
44             if(ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseNewEntity){
45                 BaseNewEntity baseNewEntity = (BaseNewEntity) metaObject.getOriginalObject();
46                 Date currentDate = ObjectUtil.isNotNull(baseNewEntity.getCreatedDateTime())
47                     ? baseNewEntity.getCreatedDateTime() : new Date();
48                 baseNewEntity.setCreatedDateTime(currentDate);
49                 baseNewEntity.setLastUpdatedDateTime(currentDate);
50                 String usernameCreate = StringUtils.isNotBlank(baseNewEntity.getCreatedUserName())
51                     ? baseNewEntity.getCreatedUserName() : getLoginUser().getUsername();
52                 baseNewEntity.setCreatedUserName(usernameCreate);
53                 baseNewEntity.setCreatedUserId(Objects.requireNonNull(getLoginUser()).getUserId());
54                 baseNewEntity.setLastUpdatedUserId(Objects.requireNonNull(getLoginUser()).getUserId());
55
56             }
57
58
59         } catch (Exception e) {
60             throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
61         }
62     }
63
64     @Override
65     public void updateFill(MetaObject metaObject) {
66         try {
67             if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
68                 BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
69                 Date current = new Date();
70                 // 更新时间填充(不管为不为空)
71                 baseEntity.setUpdateTime(current);
72
73                 String username = getLoginUsername();
74                 // 当前已登录 更新人填充(不管为不为空)
75                 if (StringUtils.isNotBlank(username)) {
76                     baseEntity.setUpdateBy(username);
77                 }
78             }
79
80             if(ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseNewEntity){
81                 BaseNewEntity baseNewEntity = (BaseNewEntity) metaObject.getOriginalObject();
82                 Date current = new Date();
83                 baseNewEntity.setLastUpdatedDateTime(current);
84                 String username = getLoginUsername();
85                 if (StringUtils.isNotBlank(username)) {
86                     baseNewEntity.setLastUpdatedUserName(username);
87                     baseNewEntity.setLastUpdatedUserId(getLoginUser().getUserId());
88                 }
89             }
90
91         } catch (Exception e) {
92             throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
93         }
94     }
95
96     /**
97      * 获取登录用户名
98      */
99     private String getLoginUsername() {
100         LoginUser loginUser;
101         try {
102             loginUser = LoginHelper.getLoginUser();
103         } catch (Exception e) {
104             log.warn("自动注入警告 => 用户未登录");
105             return null;
106         }
107         return ObjectUtil.isNotNull(loginUser) ? loginUser.getUsername() : null;
108     }
109
110     /**
111      * 获取登录用户信息
112      */
113     private LoginUser getLoginUser() {
114         LoginUser loginUser;
115         try {
116             loginUser = LoginHelper.getLoginUser();
117         } catch (Exception e) {
118             log.warn("自动注入警告 => 用户未登录");
119             return null;
120         }
121         return ObjectUtil.isNotNull(loginUser) ? loginUser : new LoginUser();
122     }
123
124 }