|
|
|
|
@@ -1,20 +1,22 @@
|
|
|
|
|
package cn.iocoder.yudao.framework.mybatis.core.handler;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.biz.system.sequence.SequenceCommonApi;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
|
|
|
|
import cn.iocoder.yudao.framework.mybatis.core.annotation.BusinessCode;
|
|
|
|
|
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
|
|
|
|
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BusinessBaseDO;
|
|
|
|
|
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
|
|
|
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
|
|
|
import org.apache.ibatis.reflection.MetaObject;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.util.ReflectionUtils;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.USER_NOT_SET_DEPT;
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
|
|
@@ -26,8 +28,12 @@ import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUti
|
|
|
|
|
*
|
|
|
|
|
* @author hexiaowu
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class DefaultDBFieldHandler implements MetaObjectHandler {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private SequenceCommonApi sequenceCommonApi;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void insertFill(MetaObject metaObject) {
|
|
|
|
|
if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseDO baseDO) {
|
|
|
|
|
@@ -55,26 +61,10 @@ public class DefaultDBFieldHandler implements MetaObjectHandler {
|
|
|
|
|
if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BusinessBaseDO businessBaseDO) {
|
|
|
|
|
// 公司编号、公司名称、部门编号、部门名称、岗位编号等字段,默认不填充
|
|
|
|
|
// 需要在业务层手动设置
|
|
|
|
|
LoginUser loginUser = getLoginUser();
|
|
|
|
|
Long visitCompanyId = loginUser.getVisitCompanyId();
|
|
|
|
|
Long visitDeptId = loginUser.getVisitDeptId();
|
|
|
|
|
loginUser.getInfo().getOrDefault(LoginUser.INFO_KEY_POST_IDS,"[]");
|
|
|
|
|
// 更加合理的写法
|
|
|
|
|
Set<Long> postIds = new HashSet<>(JSONUtil.parseArray(
|
|
|
|
|
loginUser.getInfo().getOrDefault(LoginUser.INFO_KEY_POST_IDS, "[]")
|
|
|
|
|
).toList(Long.class));
|
|
|
|
|
// 如果 visitCompanyId 不存在,不能进行业务办理
|
|
|
|
|
if (Objects.isNull(visitCompanyId) || Objects.isNull(visitDeptId)) {
|
|
|
|
|
throw exception(USER_NOT_SET_DEPT);
|
|
|
|
|
}
|
|
|
|
|
businessBaseDO.setCompanyId(visitCompanyId);
|
|
|
|
|
businessBaseDO.setCompanyName(loginUser.getVisitCompanyName());
|
|
|
|
|
businessBaseDO.setDeptId(visitDeptId);
|
|
|
|
|
businessBaseDO.setDeptName(loginUser.getVisitDeptName());
|
|
|
|
|
// 暂时没有具体业务要求,岗位默认当前用户第一个 todo chenbowen
|
|
|
|
|
businessBaseDO.setPostId(postIds.isEmpty() ? 0L : postIds.iterator().next());
|
|
|
|
|
// 多租户编号,默认不填充
|
|
|
|
|
businessBaseDO.setTenantId(loginUser.getTenantId());
|
|
|
|
|
autoFillDeptInfo(businessBaseDO);
|
|
|
|
|
|
|
|
|
|
// 自动填充带 @BusinessCode 注解的字段序列
|
|
|
|
|
autoFillBusinessCode(businessBaseDO);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -93,4 +83,64 @@ public class DefaultDBFieldHandler implements MetaObjectHandler {
|
|
|
|
|
setFieldValByName("updater", userId.toString(), metaObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void autoFillBusinessCode(BusinessBaseDO businessBaseDO) {
|
|
|
|
|
Class<?> clazz = businessBaseDO.getClass();
|
|
|
|
|
ReflectionUtils.doWithFields(clazz, field -> {
|
|
|
|
|
BusinessCode businessCode = field.getAnnotation(BusinessCode.class);
|
|
|
|
|
if (businessCode == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
Object codeVal = field.get(businessBaseDO);
|
|
|
|
|
// 如果已经手动设置了 code 字段,则不再自动填充
|
|
|
|
|
if (codeVal != null && !codeVal.toString().isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 获取 sequenceCode
|
|
|
|
|
String sequenceCode = businessCode.value();
|
|
|
|
|
// 获取 circulationValue
|
|
|
|
|
String circulationValue = null;
|
|
|
|
|
if (!businessCode.circulationValueField().isEmpty()) {
|
|
|
|
|
Field cvField = ReflectionUtils.findField(clazz, businessCode.circulationValueField());
|
|
|
|
|
if (cvField != null) {
|
|
|
|
|
cvField.setAccessible(true);
|
|
|
|
|
Object cvVal = cvField.get(businessBaseDO);
|
|
|
|
|
circulationValue = cvVal != null ? cvVal.toString() : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 直接获取 inputStrs 属性
|
|
|
|
|
List<String> inputStrs = Optional.ofNullable(businessBaseDO.getInputStrs()).orElse(new ArrayList<>());
|
|
|
|
|
// 调用远程服务获取序列号
|
|
|
|
|
if (sequenceCommonApi != null) {
|
|
|
|
|
CommonResult<String> result = sequenceCommonApi.getNextSequence(sequenceCode, circulationValue, inputStrs);
|
|
|
|
|
if (result != null && result.isSuccess() && result.getData() != null) {
|
|
|
|
|
field.set(businessBaseDO, result.getData());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void autoFillDeptInfo(BusinessBaseDO businessBaseDO) {
|
|
|
|
|
LoginUser loginUser = getLoginUser();
|
|
|
|
|
Long visitCompanyId = loginUser.getVisitCompanyId();
|
|
|
|
|
Long visitDeptId = loginUser.getVisitDeptId();
|
|
|
|
|
loginUser.getInfo().getOrDefault(LoginUser.INFO_KEY_POST_IDS,"[]");
|
|
|
|
|
// 更加合理的写法
|
|
|
|
|
Set<Long> postIds = new HashSet<>(JSONUtil.parseArray(
|
|
|
|
|
loginUser.getInfo().getOrDefault(LoginUser.INFO_KEY_POST_IDS, "[]")
|
|
|
|
|
).toList(Long.class));
|
|
|
|
|
// 如果 visitCompanyId 不存在,不能进行业务办理
|
|
|
|
|
if (Objects.isNull(visitCompanyId) || Objects.isNull(visitDeptId)) {
|
|
|
|
|
throw exception(USER_NOT_SET_DEPT);
|
|
|
|
|
}
|
|
|
|
|
businessBaseDO.setCompanyId(visitCompanyId);
|
|
|
|
|
businessBaseDO.setCompanyName(loginUser.getVisitCompanyName());
|
|
|
|
|
businessBaseDO.setDeptId(visitDeptId);
|
|
|
|
|
businessBaseDO.setDeptName(loginUser.getVisitDeptName());
|
|
|
|
|
// 暂时没有具体业务要求,岗位默认当前用户第一个 todo chenbowen
|
|
|
|
|
businessBaseDO.setPostId(postIds.isEmpty() ? 0L : postIds.iterator().next());
|
|
|
|
|
// 多租户编号,默认不填充
|
|
|
|
|
businessBaseDO.setTenantId(loginUser.getTenantId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|