门户端应用组查询优化

pull/48/head
shao1121353141 2023-09-13 22:30:40 +08:00
parent 09f188b6f2
commit 96a966208c
10 changed files with 73 additions and 107 deletions

View File

@ -17,6 +17,8 @@
*/
package cn.topiam.employee.common.repository.app;
import java.util.List;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
@ -26,9 +28,6 @@ import org.springframework.transaction.annotation.Transactional;
import cn.topiam.employee.common.entity.app.AppGroupAssociationEntity;
import cn.topiam.employee.support.repository.LogicDeleteRepository;
import java.util.List;
import static cn.topiam.employee.support.repository.domain.LogicDeleteEntity.SOFT_DELETE_SET;
/**
@ -55,7 +54,6 @@ public interface AppGroupAssociationRepository extends
+ " WHERE app_id = :appId and group_id = :groupId", nativeQuery = true)
void deleteByGroupIdAndAppId(@Param("groupId") Long groupId, @Param("appId") Long appId);
/**
*
*
@ -65,11 +63,9 @@ public interface AppGroupAssociationRepository extends
@Modifying
@Transactional(rollbackFor = Exception.class)
@Query(value = "UPDATE app_group_association SET " + SOFT_DELETE_SET
+ " WHERE app_id = :appId", nativeQuery = true)
+ " WHERE app_id = :appId", nativeQuery = true)
void deleteAllByAppId(@Param(value = "appId") Long appId);
/**
* ID
*
@ -77,7 +73,6 @@ public interface AppGroupAssociationRepository extends
*/
void deleteByAppId(Long appId);
/**
* ID
*

View File

@ -17,6 +17,8 @@
*/
package cn.topiam.employee.common.repository.app;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@ -38,4 +40,11 @@ public interface AppGroupRepositoryCustomized {
* @return {@link Page}
*/
Page<AppGroupPO> getAppGroupList(AppGroupQuery query, Pageable pageable);
/**
*
*
* @return {@link List}
*/
List<AppGroupPO> getAppGroupList(AppGroupQuery query);
}

View File

@ -81,5 +81,32 @@ public class AppGroupRepositoryCustomizedImpl implements AppGroupRepositoryCusto
return new PageImpl<>(list, pageable, count);
}
/**
*
*
* @return {@link List}
*/
@Override
public List<AppGroupPO> getAppGroupList(AppGroupQuery query) {
//@formatter:off
StringBuilder builder = new StringBuilder("SELECT `group`.id_, `group`.name_, `group`.code_, `group`.type_, `group`.create_time, `group`.remark_, IFNULL( ass.app_count, 0) AS app_count FROM app_group `group` LEFT JOIN(SELECT aga.group_id, COUNT(*) AS `app_count` FROM app_group_association aga WHERE aga.is_deleted = '0' GROUP BY aga.group_id ) ass ON `group`.id_ = ass.group_id WHERE is_deleted = '0'");
//分组名称
if (StringUtils.isNoneBlank(query.getName())) {
builder.append(" AND `group`.name_ like '%").append(query.getName()).append("%'");
}
//分组编码
if (StringUtils.isNoneBlank(query.getCode())) {
builder.append(" AND `group`.code_ like '%").append(query.getCode()).append("%'");
}
//分组类型
if (ObjectUtils.isNotEmpty(query.getType())) {
builder.append(" AND `group`.type_ like '%").append(query.getType().getCode()).append("%'");
}
builder.append(" ORDER BY `group`.create_time DESC");
//@formatter:on
List<AppGroupPO> list = jdbcTemplate.query(builder.toString(), new AppGroupPoMapper());
return list;
}
private final JdbcTemplate jdbcTemplate;
}

View File

@ -25,14 +25,11 @@ import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.google.common.collect.Lists;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import cn.topiam.employee.application.ApplicationService;
import cn.topiam.employee.application.ApplicationServiceLoader;
import cn.topiam.employee.common.entity.app.AppEntity;
import cn.topiam.employee.common.entity.app.QAppEntity;
import cn.topiam.employee.common.entity.app.query.AppQuery;
import cn.topiam.employee.console.pojo.result.app.AppGetResult;
import cn.topiam.employee.console.pojo.result.app.AppListResult;
import cn.topiam.employee.console.pojo.update.app.AppUpdateParam;
@ -48,24 +45,6 @@ import cn.topiam.employee.support.repository.page.domain.Page;
@Mapper(componentModel = "spring")
public interface AppConverter {
/**
* Querydsl Predicate
*
* @param query {@link AppQuery} query
* @return {@link Predicate}
*/
default Predicate queryAppListParamConvertToPredicate(AppQuery query) {
QAppEntity application = QAppEntity.appEntity;
Predicate predicate = ExpressionUtils.and(application.isNotNull(),
application.deleted.eq(Boolean.FALSE));
//查询条件
//@formatter:off
predicate = StringUtils.isBlank(query.getName()) ? predicate : ExpressionUtils.and(predicate, application.name.like("%" + query.getName() + "%"));
predicate = Objects.isNull(query.getProtocol()) ? predicate : ExpressionUtils.and(predicate, application.protocol.eq(query.getProtocol()));
//@formatter:on
return predicate;
}
/**
*
*
@ -138,7 +117,7 @@ public interface AppConverter {
* @param groupIds {@link List}
* @return {@link AppGetResult}
*/
default AppGetResult entityConvertToAppResult(AppEntity entity,List<Long> groupIds) {
default AppGetResult entityConvertToAppResult(AppEntity entity, List<Long> groupIds) {
if (entity == null) {
return null;
}

View File

@ -106,10 +106,9 @@ public class AppGetResult implements Serializable {
@Parameter(description = "备注")
private String remark;
/**
* ID
*/
@Parameter(description = "应用组ID集合")
private List<Long> groupIds;
private List<Long> groupIds;
}

View File

@ -157,7 +157,7 @@ public class AppServiceImpl implements AppService {
if (optional.isPresent()) {
AppEntity entity = optional.get();
List<Long> groupIds = appGroupAssociationRepository.findGroupIdByAppId(id);
return appConverter.entityConvertToAppResult(entity,groupIds);
return appConverter.entityConvertToAppResult(entity, groupIds);
}
return null;

View File

@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.topiam.employee.common.entity.app.query.AppGroupQuery;
import cn.topiam.employee.portal.pojo.query.GetAppListQuery;
import cn.topiam.employee.portal.pojo.result.AppGroupListResult;
import cn.topiam.employee.portal.pojo.result.GetAppListResult;
@ -68,8 +69,8 @@ public class AppController {
*/
@Operation(summary = "获取分组应用列表")
@GetMapping(value = "/group_list")
public ApiRestResult<List<AppGroupListResult>> getAppGroupList() {
List<AppGroupListResult> list = appService.getAppGroupList();
public ApiRestResult<List<AppGroupListResult>> getAppGroupList(AppGroupQuery appGroupQuery) {
List<AppGroupListResult> list = appService.getAppGroupList(appGroupQuery);
return ApiRestResult.ok(list);
}

View File

@ -17,19 +17,13 @@
*/
package cn.topiam.employee.portal.converter;
import java.util.ArrayList;
import java.util.List;
import com.querydsl.core.types.ExpressionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.querydsl.core.types.Predicate;
import com.google.common.collect.Lists;
import cn.topiam.employee.common.entity.app.AppGroupAssociationEntity;
import cn.topiam.employee.common.entity.app.AppGroupEntity;
import cn.topiam.employee.common.entity.app.QAppGroupAssociationEntity;
import cn.topiam.employee.common.entity.app.QAppGroupEntity;
import cn.topiam.employee.common.entity.app.po.AppGroupPO;
import cn.topiam.employee.portal.pojo.result.AppGroupListResult;
/**
@ -42,52 +36,26 @@ import cn.topiam.employee.portal.pojo.result.AppGroupListResult;
public interface AppGroupConverter {
/**
* queryPredicate
*
*
* @return {@link Predicate}
* @param appGroupPoList {@link List}
* @return {@link List}
*/
default Predicate getQueryPredicate() {
QAppGroupEntity appGroup = QAppGroupEntity.appGroupEntity;
return ExpressionUtils.and(appGroup.isNotNull(), appGroup.deleted.eq(Boolean.FALSE));
}
/**
* Predicate
*
* @return {@link Predicate}
*/
default Predicate queryAppGroupAssociationPredicate() {
QAppGroupAssociationEntity appGroupAssociation = QAppGroupAssociationEntity.appGroupAssociationEntity;
return ExpressionUtils.and(appGroupAssociation.isNotNull(), appGroupAssociation.deleted.eq(Boolean.FALSE));
default List<AppGroupListResult> entityConvertToAppGroupListResult(List<AppGroupPO> appGroupPoList) {
List<AppGroupListResult> list = Lists.newArrayList();
for (AppGroupPO po : appGroupPoList) {
AppGroupListResult result = entityConvertToAppGroupListResult(po);
list.add(result);
}
return list;
}
/**
*
*
* @param list {@link AppGroupEntity}
* @param appGroupAssociationList {@link AppGroupAssociationEntity}
* @param appGroupPo {@link AppGroupPO}
* @return {@link AppGroupListResult}
*/
default List<AppGroupListResult> entityConvertToAppGroupListResult(List<AppGroupEntity> list,
List<AppGroupAssociationEntity> appGroupAssociationList) {
List<AppGroupListResult> results = new ArrayList<>();
for (AppGroupEntity entity : list) {
AppGroupListResult result = appGroupEntityConverterToResult(entity);
long count = appGroupAssociationList.stream()
.filter(t -> t.getGroupId().equals(entity.getId())).count();
result.setAppCount(Integer.valueOf(Long.toString(count)));
results.add(result);
}
return results;
}
/**
* Result
*
* @param entity {@link AppGroupEntity}
* @return {@link AppGroupEntity}
*/
@Mapping(target = "appCount", ignore = true)
AppGroupListResult appGroupEntityConverterToResult(AppGroupEntity entity);
AppGroupListResult entityConvertToAppGroupListResult(AppGroupPO appGroupPo);
}

View File

@ -19,6 +19,7 @@ package cn.topiam.employee.portal.service;
import java.util.List;
import cn.topiam.employee.common.entity.app.query.AppGroupQuery;
import cn.topiam.employee.portal.pojo.query.GetAppListQuery;
import cn.topiam.employee.portal.pojo.result.AppGroupListResult;
import cn.topiam.employee.portal.pojo.result.GetAppListResult;
@ -44,7 +45,8 @@ public interface AppService {
/**
*
*
* @param appGroupQuery {@link AppGroupQuery}
* @return {@link AppGroupListResult}
*/
List<AppGroupListResult> getAppGroupList();
List<AppGroupListResult> getAppGroupList(AppGroupQuery appGroupQuery);
}

View File

@ -22,12 +22,9 @@ import java.util.List;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;
import com.querydsl.core.types.Predicate;
import cn.topiam.employee.common.entity.app.AppEntity;
import cn.topiam.employee.common.entity.app.AppGroupAssociationEntity;
import cn.topiam.employee.common.entity.app.AppGroupEntity;
import cn.topiam.employee.common.repository.app.AppGroupAssociationRepository;
import cn.topiam.employee.common.entity.app.po.AppGroupPO;
import cn.topiam.employee.common.entity.app.query.AppGroupQuery;
import cn.topiam.employee.common.repository.app.AppGroupRepository;
import cn.topiam.employee.common.repository.app.AppRepository;
import cn.topiam.employee.portal.converter.AppConverter;
@ -67,51 +64,40 @@ public class AppServiceImpl implements AppService {
/**
*
*
* @param appGroupQuery {@link AppGroupQuery}
* @return {@link AppGroupListResult}
*/
@Override
public List<AppGroupListResult> getAppGroupList() {
Predicate predicate = appGroupConverter.getQueryPredicate();
Predicate appGroupAssociationPredicate = appGroupConverter
.queryAppGroupAssociationPredicate();
List<AppGroupAssociationEntity> appGroupAssociationList = (List<AppGroupAssociationEntity>) appGroupAssociationRepository
.findAll(appGroupAssociationPredicate);
public List<AppGroupListResult> getAppGroupList(AppGroupQuery appGroupQuery) {
//查询映射
List<AppGroupEntity> list = (List<AppGroupEntity>) appGroupRepository.findAll(predicate);
return appGroupConverter.entityConvertToAppGroupListResult(list, appGroupAssociationList);
List<AppGroupPO> list = appGroupRepository.getAppGroupList(appGroupQuery);
return appGroupConverter.entityConvertToAppGroupListResult(list);
}
/**
* AppRepository
*/
private final AppRepository appRepository;
private final AppRepository appRepository;
/**
* AppGroupRepository
*/
private final AppGroupRepository appGroupRepository;
/**
* AppGroupAssociationRepository
*/
private final AppGroupAssociationRepository appGroupAssociationRepository;
private final AppGroupRepository appGroupRepository;
/**
* AppConverter
*/
private final AppConverter appConverter;
private final AppConverter appConverter;
/**
* AppGroupConverter
*/
private final AppGroupConverter appGroupConverter;
private final AppGroupConverter appGroupConverter;
public AppServiceImpl(AppRepository appRepository, AppGroupRepository appGroupRepository,
AppGroupAssociationRepository appGroupAssociationRepository,
AppConverter appConverter, AppGroupConverter appGroupConverter) {
this.appRepository = appRepository;
this.appGroupRepository = appGroupRepository;
this.appGroupAssociationRepository = appGroupAssociationRepository;
this.appConverter = appConverter;
this.appGroupConverter = appGroupConverter;
}