Skip to content

Commit

Permalink
feat(sirius): add remove before check reference and assign authority.
Browse files Browse the repository at this point in the history
[ice]: Change `getJobInspectPage` params to `UniformQueryPageData` type.
  • Loading branch information
yizzuide committed Nov 27, 2022
1 parent 364dc4a commit a5ef23f
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ public class UniformQueryPageData<T> extends UniformQueryData<T> {
* 查询当前页
*/
private Integer pageStart;

/**
* 每页记录数
*/
private Integer pageSize;

/**
* 排序
*/
private Integer order;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.github.yizzuide.milkomeda.ice;

import com.github.yizzuide.milkomeda.hydrogen.uniform.UniformPage;
import com.github.yizzuide.milkomeda.hydrogen.uniform.UniformQueryPageData;
import com.github.yizzuide.milkomeda.ice.inspector.JobStatInfo;
import com.github.yizzuide.milkomeda.ice.inspector.JobWrapper;
import com.github.yizzuide.milkomeda.universe.lang.Tuple;
Expand Down Expand Up @@ -120,14 +121,12 @@ public interface Ice {
boolean rePushJob(String jobId, String topic);

/**
* Get all job inspect info list.
* @param start page start index
* @param size size of per page
* @param order sorting of corresponding column, 1 is asc and -1 is desc
* @return job Inspection page data
* Get job inspect info list.
* @param queryPageData page data for query
* @return UniformPage inspection page data
* @since 3.14.0
*/
UniformPage<JobWrapper> getJobInspectPage(int start, int size, int order);
UniformPage<JobWrapper> getJobInspectPage(UniformQueryPageData<JobWrapper> queryPageData);

/**
* Get job inspect info with topic and job id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public class Job<T> implements Serializable, Cloneable {
/**
* 延迟时间,单位ms
*/
private long delay;
private Long delay;
/**
* 执行超时时间,单位ms(超时后重新投入了待处理(READY)队列)
*/
private long ttr;
private Long ttr;
/**
* 初始重试次数
*/
private int retryCount;
private Integer retryCount;
/**
* 业务数据
*/
Expand All @@ -81,7 +81,7 @@ void setStatus(JobStatus status) {
this.status = status;
}

public Job(String id, String topic, long delay, long ttr, int retryCount, T body) {
public Job(String id, String topic, Long delay, Long ttr, Integer retryCount, T body) {
this.id = id;
this.topic = topic;
this.delay = delay;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.yizzuide.milkomeda.hydrogen.uniform.UniformPage;
import com.github.yizzuide.milkomeda.ice.inspector.*;
import com.github.yizzuide.milkomeda.hydrogen.uniform.UniformQueryPageData;
import com.github.yizzuide.milkomeda.ice.inspector.JobInspector;
import com.github.yizzuide.milkomeda.ice.inspector.JobStat;
import com.github.yizzuide.milkomeda.ice.inspector.JobStatInfo;
import com.github.yizzuide.milkomeda.ice.inspector.JobWrapper;
import com.github.yizzuide.milkomeda.universe.polyfill.RedisPolyfill;
import com.github.yizzuide.milkomeda.util.DataTypeConvertUtil;
import com.github.yizzuide.milkomeda.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.RedisOperations;
Expand Down Expand Up @@ -92,6 +97,12 @@ public boolean add(Job job, boolean mergeIdWithTopic, boolean replaceWhenExists)
if (mergeIdWithTopic) {
job.setId(job.getTopic() + IceProperties.MERGE_ID_SEPARATOR + job.getId());
}
if (job.getTtr() == null) {
job.setTtr(props.getTtr().toMillis());
}
if (job.getRetryCount() == null) {
job.setRetryCount(props.getRetryCount());
}

DelayJob delayJob = new DelayJob(job);
boolean hadSetStatus = job.getStatus() != null;
Expand Down Expand Up @@ -162,13 +173,27 @@ public boolean rePushJob(String jobId, String topic) {
}

@Override
public UniformPage<JobWrapper> getJobInspectPage(int start, int size, int order) {
public UniformPage<JobWrapper> getJobInspectPage(UniformQueryPageData<JobWrapper> queryPageData) {
UniformPage<JobWrapper> page = new UniformPage<>();
if (jobInspector == null) {
page.setTotalSize(0L);
page.setPageCount(1L);
page.setList(Collections.emptyList());
return page;
}
// find entity by id?
JobWrapper entity = queryPageData.getEntity();
if (entity != null && StringUtils.isNotBlank(entity.getId())) {
JobWrapper jobWrapper = getJobInspectInfo(entity.getTopic(), entity.getId());
page.setTotalSize(1L);
page.setPageCount(1L);
page.setList(jobWrapper == null ? Collections.emptyList() : Collections.singletonList(jobWrapper));
return page;
}

int start = queryPageData.getPageStart();
int size = queryPageData.getPageSize();
int order = queryPageData.getOrder();
page.setTotalSize(jobInspector.size());
long pageCount = page.getTotalSize() / size;
page.setPageCount(page.getTotalSize() % size > 0 ? pageCount + 1 : pageCount);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.github.yizzuide.milkomeda.sirius;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.yizzuide.milkomeda.hydrogen.uniform.UniformPage;
import com.github.yizzuide.milkomeda.hydrogen.uniform.UniformQueryPageData;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;

/**
* A pageable service extends from IService
*
Expand All @@ -19,4 +24,23 @@ public interface IPageableService<T> extends IService<T> {
* @return UniformPage
*/
UniformPage<T> selectByPage(UniformQueryPageData<T> queryPageData);

/**
* Remove record row before check it reference.
* @param entity entity which has key of id value
* @return false if it has referenced
*/
boolean removeBeforeCheckRef(T entity);

/**
* Assign authority for owner
* @param ownerId owner id
* @param itemIds authority item id list
* @param conditionProvider condition which find owner
* @param generator create authority item row to owner
* @return true if success
*/
boolean assignAuthority(Serializable ownerId, List<? extends Serializable> itemIds,
SFunction<T, Object> conditionProvider,
Function<Serializable, T> generator);
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
package com.github.yizzuide.milkomeda.sirius;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yizzuide.milkomeda.hydrogen.uniform.UniformPage;
import com.github.yizzuide.milkomeda.hydrogen.uniform.UniformQueryPageData;
import com.github.yizzuide.milkomeda.universe.context.AopContextHolder;
import com.github.yizzuide.milkomeda.universe.context.ApplicationContextHolder;
import com.github.yizzuide.milkomeda.util.DataTypeConvertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -102,9 +108,7 @@ public UniformPage<T> selectByPage(UniformQueryPageData<T> queryPageData) {
if (queryMatcher.prefect() == PrefectType.EQ) {
queryWrapper.eq(fieldNonNull, columnName, fieldValue);
} else if (queryMatcher.prefect() == PrefectType.LIKE) {
if (StringUtils.isNotBlank(fieldValue.toString())) {
queryWrapper.like(fieldNonNull, columnName, fieldValue);
}
queryWrapper.like(fieldNonNull && StringUtils.isNotBlank(fieldValue.toString()), columnName, fieldValue);
} else if (queryMatcher.prefect() == PrefectType.PageDate) {
queryWrapper.ge(Objects.nonNull(queryPageData.getStartDate()), columnName, queryPageData.getStartDate());
queryWrapper.le(Objects.nonNull(queryPageData.getEndDate()), columnName, queryPageData.getEndDate());
Expand Down Expand Up @@ -160,4 +164,62 @@ public UniformPage<T> selectByPage(UniformQueryPageData<T> queryPageData) {
*/
protected void additionParseQueryMatcher(QueryWrapper<T> queryWrapper, String prefectString, String columnName, boolean isFieldNonNull, Object fieldValue) {
}

@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public boolean removeBeforeCheckRef(T entityWithID) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
Object id = tableInfo.getPropertyValue(entityWithID, tableInfo.getKeyProperty());
// check RefMatcher on entity field
for (Field field : entityWithID.getClass().getDeclaredFields()) {
RefMatcher refMatcher = field.getDeclaredAnnotation(RefMatcher.class);
if (refMatcher == null) {
continue;
}
if (refMatcher.type() == RefMatcher.RefType.SELF) {
String columnName = null;
// find from table info
for (TableFieldInfo tableFieldInfo : tableInfo.getFieldList()) {
if (field.getName().equals(tableFieldInfo.getProperty())) {
columnName = tableFieldInfo.getColumn();
break;
}
}
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(columnName, id);
Long count = this.baseMapper.selectCount(queryWrapper);
if (count > 0) {
return false;
}
break;
}
}
// check RefMatcher on mapper type
RefMatcher refMatcher = this.mapperClass.getDeclaredAnnotation(RefMatcher.class);
if(refMatcher != null && BaseMapper.class.isAssignableFrom(refMatcher.foreignMapper())) {
QueryWrapper foreignQueryWrapper = new QueryWrapper();
foreignQueryWrapper.eq(refMatcher.foreignField(), id);
BaseMapper referenceMapper = (BaseMapper) ApplicationContextHolder.get().getBean(refMatcher.foreignMapper());
if (referenceMapper.selectCount(foreignQueryWrapper) > 0) {
return false;
}
}
AopContextHolder.self(this.getClass()).removeById((Serializable) id);
return true;
}

@SuppressWarnings("unchecked")
public boolean assignAuthority(Serializable ownerId, List<? extends Serializable> itemIds, SFunction<T, Object> conditionProvider, Function<Serializable, T> generator) {
// 删除原有分配(如果不是通过LambdaQueryWrapper执行操作,创建时不添加this.baseMapper)
LambdaQueryWrapper<T> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(conditionProvider, ownerId);
AopContextHolder.self(this.getClass()).remove(lambdaQueryWrapper);

// 添加新的分配
if(CollectionUtils.isEmpty(itemIds)) {
return true;
}
List<T> sysUserRoles = itemIds.stream().map(generator).collect(Collectors.toList());
return AopContextHolder.self(this.getClass()).saveBatch(sysUserRoles);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.lang.annotation.*;

/**
* Query matcher using for {@link}
* Query matcher using for {@link IPageableService}.
*
* @since 3.14.0
* @author yizzuide
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2022 yizzuide All rights Reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.yizzuide.milkomeda.sirius;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.lang.annotation.*;

/**
* Reference matcher using for {@link IPageableService}.
*
* @since 3.14.0
* @author yizzuide
* <br>
* Create at 2022/11/10 21:55
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface RefMatcher {
/**
* Reference type.
* @return self type by default
*/
RefType type() default RefType.SELF;

/**
* Foreign field referenced it, must add on mapper type.
* @return reference field
*/
String foreignField() default "";

/**
* Foreign mapper class, must add on mapper type.
* @return mapper class
*/
Class<?> foreignMapper() default BaseMapper.class;

enum RefType {
/**
* Referenced by self, used on entity field.
*/
SELF,

/**
* Foreign Reference, used on mapper type.
*/
FOREIGN
}
}
Loading

0 comments on commit a5ef23f

Please sign in to comment.