| /** | 
|  * 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; | 
|     } | 
| } |