jinlin
2024-01-15 1a7af6fff5185bb257c16b0445140c93263a3331
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有,侵权必究!
 */
 
package com.zt.core.config.mybatis;
 
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zt.common.entity.*;
import com.zt.common.exception.RenException;
import com.zt.common.utils.SpringContextUtils;
import com.zt.core.context.User;
import com.zt.core.context.UserContext;
import com.zt.core.sys.model.SysDept;
import com.zt.modules.sys.service.SysDeptService;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
 
import java.util.Date;
 
/**
 * 公共字段,自动填充值
 *
 * @author hehz
 */
@Component("fieldHandler")
public class FieldMetaObjectHandler implements MetaObjectHandler {
 
    // @Autowired
    private SysDeptService sysDeptService;
 
    @Override
    public void insertFill(MetaObject metaObject) {
        Object object = metaObject.getOriginalObject();
        User user = UserContext.getUser();
        Date date = new Date();
 
        if (object instanceof PlatformLogEntity) {
            PlatformLogEntity entity = (PlatformLogEntity) object;
            entity.setCreator(user.getId());// 创建者
            entity.setCreateDate(date);// 创建时间
        }
        if (object instanceof PlatformEntity) {
            PlatformEntity entity = (PlatformEntity) object;
            entity.setDelete(false);
            entity.setUpdater(user.getId());// 更新者
            entity.setUpdateDate(date);// 更新时间
        }
        if (object instanceof TenantEntity) {
            TenantEntity entity = (TenantEntity) object;
            entity.setTenantId(user.getTenantId());
        }
        this.setBusiLevelIds(object);
    }
 
    @Override
    public void updateFill(MetaObject metaObject) {
        Object object = metaObject.getOriginalObject();
        if (object instanceof PlatformEntity) {
            PlatformEntity entity = (PlatformEntity) object;
            entity.setUpdater(UserContext.getUserId());// 更新者
            entity.setUpdateDate(new Date());// 更新时间
        }
 
        this.setBusiLevelIds(object);
    }
 
    private void setBusiLevelIds(Object object) {
        User user = UserContext.getUser();
        if (object instanceof IBusiLevel) {// 到业务层级:deptId
            IBusiLevel entity = (IBusiLevel) object;
            if (isEmpty(entity.getDeptId()) || user.getDeptId().equals(entity.getDeptId())) {
                // 没有设置部门id,或者跟当前用户相同,那么保存为当前登录用户的
                entity.setDeptId(user.getDeptId());
                entity.setCompanyId(user.getCompanyId());
                entity.setTenantId(user.getTenantId());
            }
//            else {// 设置了不同于当前用户的
//                SysDept dept = getSysDeptService().get(entity.getDeptId());
//                if (dept == null) {
//                    throw new RenException("部门信息缺失!");
//                }
//                entity.setCompanyId(dept.getCompanyId());// 公司
//                entity.setTenantId(dept.getTenantId());// 租户
//            }
        } else if (object instanceof ICompanyLevel) {// 到公司层级
            ICompanyLevel entity = (ICompanyLevel) object;
            if (isEmpty(entity.getCompanyId()) || user.getCompanyId().equals(entity.getCompanyId())) {
                entity.setCompanyId(user.getCompanyId());
                entity.setTenantId(user.getTenantId());
            }
//            else if (isEmpty(entity.getTenantId())) {// 租户没有设置
//                SysDept company = getSysDeptService().get(entity.getCompanyId());
//                if (company == null) {
//                    throw new RenException("公司信息缺失!");
//                }
//                entity.setTenantId(company.getTenantId());// 租户
//            }
        } else if (object instanceof ITenantLevel) {// 到租户层级
            ITenantLevel entity = (ITenantLevel) object;
            if (isEmpty(entity.getTenantId())) {
                entity.setTenantId(user.getTenantId());
            }
        }
    }
 
    private boolean isEmpty(Long value) {
        return value == null || value == 0;
    }
 
    private SysDeptService getSysDeptService() {
        if (sysDeptService == null) {
            sysDeptService = SpringContextUtils.getBean(SysDeptService.class);
        }
        return sysDeptService;
    }
}