Commit b803fedb authored by suhuiguang's avatar suhuiguang

reafact(common): 自动填充优化

1.删除无用操作redis逻辑
parent 7fd03c9c
...@@ -20,5 +20,7 @@ public interface BizConstant { ...@@ -20,5 +20,7 @@ public interface BizConstant {
* 地址 * 地址
*/ */
public final static String ADDRESS = "address"; public final static String ADDRESS = "address";
String PIPE_LENGTH = "pipeLengthText";
} }
package com.yeejoin.amos.boot.biz.config; package com.yeejoin.amos.boot.biz.config;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.yeejoin.amos.boot.biz.common.annotation.FillCommonUserField; import com.yeejoin.amos.boot.biz.common.annotation.FillCommonUserField;
import com.yeejoin.amos.boot.biz.common.annotation.PipeLengthField; import com.yeejoin.amos.boot.biz.common.annotation.PipeLengthField;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams; import com.yeejoin.amos.boot.biz.common.constants.BizConstant;
import com.yeejoin.amos.boot.biz.common.utils.RedisKey;
import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.feign.privilege.model.AgencyUserModel;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.typroject.tyboot.core.foundation.context.RequestContext; import org.typroject.tyboot.core.foundation.context.RequestContext;
import java.lang.reflect.Field;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.Arrays; import java.util.Arrays;
...@@ -30,42 +23,63 @@ import java.util.Objects; ...@@ -30,42 +23,63 @@ import java.util.Objects;
@Component @Component
public class MetaHandler implements MetaObjectHandler { public class MetaHandler implements MetaObjectHandler {
@Autowired
private RedisUtils redisUtils;
protected String getToken() {
String authToken = RequestContext.getToken();
// if (authToken == null) {
// authToken = request.getHeader("X-Access-Token");
// }
return authToken;
}
/** /**
* 新增数据拦截 * 新增数据拦截
* *
* @param metaObject * @param metaObject 原始对象
*/ */
@Override @Override
public void insertFill(MetaObject metaObject) { public void insertFill(MetaObject metaObject) {
Date currentDate = new Date(); Date currentDate = new Date();
Class<?> clazz = getaClass(metaObject);
autoFillUser(clazz.getAnnotation(FillCommonUserField.class), metaObject, currentDate);
autoFillPipeLength(clazz.getAnnotation(PipeLengthField.class), metaObject);
this.setFieldValByName("createDate", currentDate, metaObject);
}
/**
* 更新拦截
*
* @param metaObject 原始对象
*/
@Override
public void updateFill(MetaObject metaObject) {
Date currentDate = new Date();
Class<?> clazz = getaClass(metaObject);
autoFillUser(clazz.getAnnotation(FillCommonUserField.class), metaObject, currentDate);
autoFillPipeLength(clazz.getAnnotation(PipeLengthField.class), metaObject);
}
private void autoFillUser(FillCommonUserField clazz, MetaObject metaObject, Date currentDate) {
if (clazz == null || clazz.isAutoFill()) {
recInfoUpdate(metaObject, currentDate);
}
}
private void autoFillPipeLength(PipeLengthField clazz, MetaObject metaObject) {
if (clazz != null) {
processAutoFill(metaObject, clazz);
}
}
private void recInfoUpdate(MetaObject metaObject, Date currentDate) {
this.setFieldValByName("recUserId", RequestContext.getExeUserId(), metaObject);
this.setFieldValByName("recDate", currentDate, metaObject);
this.setFieldValByName("updateTime", currentDate, metaObject);
}
private static Class<?> getaClass(MetaObject metaObject) {
Object originalObj = metaObject.getOriginalObject(); Object originalObj = metaObject.getOriginalObject();
Class<?> clazz = originalObj.getClass(); Class<?> clazz = originalObj.getClass();
// 处理 Map 封装的情况(update(entity, wrapper) 方式) // 处理 Map 封装的情况(update(entity, wrapper) 方式)
if (originalObj instanceof Map) { if (originalObj instanceof Map) {
Map<?,?> map = (Map<?,?>) originalObj; Map<?, ?> map = (Map<?, ?>) originalObj;
Object et = map.get(Constants.ENTITY); // MyBatis-Plus 的实体key Object et = map.get(Constants.ENTITY); // MyBatis-Plus 的实体key
clazz = (et != null) ? et.getClass() : clazz; clazz = (et != null) ? et.getClass() : clazz;
} }
FillCommonUserField annotation = clazz.getAnnotation(FillCommonUserField.class); return clazz;
if (annotation == null || annotation.isAutoFill()) {
autoFillUser(metaObject, metaObject.getOriginalObject());
}
PipeLengthField pipeLengthField = clazz.getAnnotation(PipeLengthField.class);
if(pipeLengthField != null){
processAutoFill(metaObject, pipeLengthField);
}
this.setFieldValByName("createDate", currentDate, metaObject);
} }
...@@ -121,139 +135,13 @@ public class MetaHandler implements MetaObjectHandler { ...@@ -121,139 +135,13 @@ public class MetaHandler implements MetaObjectHandler {
} }
private Object convertValue(Object sourceValue, PipeLengthField pipeLengthField) { private Object convertValue(Object sourceValue, PipeLengthField pipeLengthField) {
// 示例:针对pipeLength的特殊处理 // 示例:针对pipeLength的特殊处理
if ("pipeLengthText".equals(pipeLengthField.sourceField())) { if (BizConstant.PIPE_LENGTH.equals(pipeLengthField.sourceField())) {
return Arrays.stream(sourceValue.toString().split("/")).filter(Objects::nonNull) return Arrays.stream(sourceValue.toString().split("/")).filter(Objects::nonNull)
.map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(3, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString(); .map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(3, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
} }
// 可扩展其他转换规则 // 可扩展其他转换规则
return sourceValue; return sourceValue;
} }
private void autoFillUser(MetaObject metaObject, Object entity) {
//获取用户信息 以及当前用户登录公司部门,角色
String userId = RequestContext.getExeUserId();
ReginParams reginParams =
JSONObject.parseObject(null != redisUtils.get(RedisKey.buildReginKey(userId, getToken())) ?
redisUtils.get(RedisKey.buildReginKey(userId, getToken())).toString() : null, ReginParams.class);
if (ObjectUtils.isEmpty(reginParams)) {
return;
}
AgencyUserModel agencyUserModel = reginParams.getUserModel();
if (ObjectUtils.isEmpty(reginParams) || ObjectUtils.isEmpty(agencyUserModel)) {
return;
}
// 更新rec字段
recInfoUpdate(metaObject, agencyUserModel);
if (isExistField("allotmentTime", entity)) {
Date currentDate = new Date();
this.setFieldValByName("recDate", currentDate, metaObject);
}
//以下为装备中转移过来的
if (isExistField("userId", entity) && isStringField(metaObject,"userId")) {
this.setFieldValByName("userId", String.valueOf(agencyUserModel.getUserId()), metaObject);
}
if (isExistField("creatorId", entity)) {
this.setFieldValByName("creatorId", Long.valueOf(agencyUserModel.getUserId()), metaObject);
}
if (isExistField("userName", entity)) {
this.setFieldValByName("userName", agencyUserModel.getRealName(), metaObject);
}
// if (isExistField("companyName", entity)) {
// this.setFieldValByName("companyName", reginParams.getCompany().getCompanyName(), metaObject);
// }
if (isExistField("orgCode", entity) && !isHasValue("orgCode", entity)) {
this.setFieldValByName("orgCode", reginParams.getCompany().getOrgCode(), metaObject);
}
if (isExistField("departmentName", entity)) {
this.setFieldValByName("departmentName", ObjectUtils.isEmpty(reginParams.getDepartment()) ? "" : reginParams.getDepartment().getDepartmentName(), metaObject);
}
if (isExistField("departmentOrgcode", entity)) {
this.setFieldValByName("departmentOrgcode", ObjectUtils.isEmpty(reginParams.getDepartment()) ? "" : reginParams.getDepartment().getOrgCode(), metaObject);
}
}
private void recInfoUpdate(MetaObject metaObject, AgencyUserModel agencyUserModel) {
this.setFieldValByName("recUserId", agencyUserModel.getUserId(), metaObject);
this.setFieldValByName("recUserName", agencyUserModel.getRealName(), metaObject);
Date currentDate = new Date();
this.setFieldValByName("recDate", currentDate, metaObject);
this.setFieldValByName("updateTime", currentDate, metaObject);
}
private Boolean isExistField(String field, Object obj) {
if (obj == null || StringUtils.isEmpty(field)) {
return null;
}
Object o = JSONObject.toJSON(obj);
JSONObject jsonObj = new JSONObject();
if (o instanceof JSONObject) {
jsonObj = (JSONObject) o;
}
return jsonObj.containsKey(field);
}
private Boolean isHasValue(String field, Object obj) {
if (obj == null || StringUtils.isEmpty(field)) {
return false;
}
Object o = JSONObject.toJSON(obj);
JSONObject jsonObj = new JSONObject();
if (o instanceof JSONObject) {
jsonObj = (JSONObject) o;
}
return StringUtils.isNotEmpty(jsonObj.getString(field));
}
private Boolean isStringField(MetaObject metaObject, String fieldName) {
Class clazz = metaObject.getOriginalObject().getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (fieldName.equals(field.getName()) && ("class java.lang.String").equals(field.getGenericType().toString())) {
return true;
}
}
return false;
}
/**
* 更新拦截
*
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
Object originalObj = metaObject.getOriginalObject();
Class<?> clazz = originalObj.getClass();
// 处理 Map 封装的情况(update(entity, wrapper) 方式)
if (originalObj instanceof Map) {
Map<?,?> map = (Map<?,?>) originalObj;
Object et = map.get(Constants.ENTITY); // MyBatis-Plus 的实体key
clazz = (et != null) ? et.getClass() : clazz;
}
FillCommonUserField annotation = clazz.getAnnotation(FillCommonUserField.class);
if (annotation == null || annotation.isAutoFill()) {
String userId = RequestContext.getExeUserId();
ReginParams reginParams =
JSONObject.parseObject(null != redisUtils.get(RedisKey.buildReginKey(userId, getToken())) ?
redisUtils.get(RedisKey.buildReginKey(userId, getToken())).toString() : null, ReginParams.class);
if (ObjectUtils.isEmpty(reginParams)) {
return;
}
AgencyUserModel agencyUserModel = reginParams.getUserModel();
recInfoUpdate(metaObject, agencyUserModel);
}
PipeLengthField pipeLengthField = clazz.getAnnotation(PipeLengthField.class);
if(pipeLengthField != null){
processAutoFill(metaObject, pipeLengthField);
}
}
} }
\ No newline at end of file
...@@ -2507,7 +2507,7 @@ public class DataHandlerServiceImpl { ...@@ -2507,7 +2507,7 @@ public class DataHandlerServiceImpl {
.select(BaseEntity::getSequenceNbr) .select(BaseEntity::getSequenceNbr)
); );
watch.start("安装告知" + notices.size()); watch.start("安装告知" + notices.size());
notices.parallelStream().forEach(n->{ notices.forEach(n->{
JSONObject jsonObject = commonServiceImpl.queryHistoryData(n.getSequenceNbr()); JSONObject jsonObject = commonServiceImpl.queryHistoryData(n.getSequenceNbr());
if(jsonObject != null){ if(jsonObject != null){
JSONArray jsonArray = jsonObject.getJSONArray("deviceList"); JSONArray jsonArray = jsonObject.getJSONArray("deviceList");
...@@ -2530,7 +2530,7 @@ public class DataHandlerServiceImpl { ...@@ -2530,7 +2530,7 @@ public class DataHandlerServiceImpl {
.ne(JgUseRegistration::getProjectContraptionId, "") .ne(JgUseRegistration::getProjectContraptionId, "")
.select(BaseEntity::getSequenceNbr)); .select(BaseEntity::getSequenceNbr));
watch.start("使用登记" + useRegistrations.size() ); watch.start("使用登记" + useRegistrations.size() );
useRegistrations.parallelStream().forEach(u->{ useRegistrations.forEach(u->{
JSONObject jsonObject = commonServiceImpl.queryHistoryData(u.getSequenceNbr()); JSONObject jsonObject = commonServiceImpl.queryHistoryData(u.getSequenceNbr());
if(jsonObject != null){ if(jsonObject != null){
String pipelistKey; String pipelistKey;
...@@ -2561,7 +2561,7 @@ public class DataHandlerServiceImpl { ...@@ -2561,7 +2561,7 @@ public class DataHandlerServiceImpl {
.ne(JgChangeRegistrationReform::getProjectContraptionId, "") .ne(JgChangeRegistrationReform::getProjectContraptionId, "")
.select(JgChangeRegistrationReform::getApplyNo)); .select(JgChangeRegistrationReform::getApplyNo));
watch.start("改造变更登记" + changeRegistrationReforms.size()); watch.start("改造变更登记" + changeRegistrationReforms.size());
changeRegistrationReforms.parallelStream().forEach(u->{ changeRegistrationReforms.forEach(u->{
JSONObject jsonObject = commonServiceImpl.queryHistoryData(u.getApplyNo()); JSONObject jsonObject = commonServiceImpl.queryHistoryData(u.getApplyNo());
if(jsonObject != null){ if(jsonObject != null){
JSONArray jsonArray = jsonObject.getJSONArray("equipmentLists"); JSONArray jsonArray = jsonObject.getJSONArray("equipmentLists");
...@@ -2586,7 +2586,7 @@ public class DataHandlerServiceImpl { ...@@ -2586,7 +2586,7 @@ public class DataHandlerServiceImpl {
.ne(JgReformNotice::getProjectContraptionId, "") .ne(JgReformNotice::getProjectContraptionId, "")
.select(BaseEntity::getSequenceNbr)); .select(BaseEntity::getSequenceNbr));
watch.start("改造告知" + reformNotices.size()); watch.start("改造告知" + reformNotices.size());
reformNotices.parallelStream().forEach(u->{ reformNotices.forEach(u->{
JSONObject jsonObject = commonServiceImpl.queryHistoryData(u.getSequenceNbr()); JSONObject jsonObject = commonServiceImpl.queryHistoryData(u.getSequenceNbr());
if(jsonObject != null){ if(jsonObject != null){
JSONArray jsonArray = jsonObject.getJSONArray("deviceList"); JSONArray jsonArray = jsonObject.getJSONArray("deviceList");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment