优化代码

pull/59/head
awenes 2023-09-26 13:40:11 +08:00
parent dcbd19e4c8
commit f5534abb08
9 changed files with 143 additions and 38 deletions

View File

@ -108,4 +108,5 @@ public interface AppGroupRepository extends LogicDeleteRepository<AppGroupEntity
* @return {@link AppGroupEntity}
*/
Optional<AppGroupEntity> findByCode(@Param("code") String code);
}

View File

@ -47,4 +47,22 @@ public interface AppGroupRepositoryCustomized {
* @return {@link List}
*/
List<AppGroupPO> getAppGroupList(AppGroupQuery query);
/**
*
*
* @param userId {@link Long}
* @param query {@link AppGroupQuery}
* @return {@link List}
*/
List<AppGroupPO> getAppGroupList(Long userId, AppGroupQuery query);
/**
*
*
* @param groupId {@link Long}
* @param userId {@link Long}
* @return {@link Long}
*/
Long getAppCount(String groupId, Long userId);
}

View File

@ -74,7 +74,7 @@ public interface AppRepository extends LogicDeleteRepository<AppEntity, Long>,
@Modifying
@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
@Query(value = "UPDATE app SET is_enabled = :enabled WHERE id_ = :id", nativeQuery = true)
@Query(value = "UPDATE AppEntity SET enabled = :enabled WHERE id = :id")
Integer updateAppStatus(@Param(value = "id") Long id,
@Param(value = "enabled") Boolean enabled);
@ -106,17 +106,9 @@ public interface AppRepository extends LogicDeleteRepository<AppEntity, Long>,
*/
@NotNull
@Cacheable
@Query(value = "SELECT * FROM app WHERE id_ = :id", nativeQuery = true)
@Query(value = "SELECT AppEntity FROM AppEntity WHERE id = :id")
Optional<AppEntity> findByIdContainsDeleted(@NotNull @Param(value = "id") Long id);
/**
*
* @return {@link AppEntity}
*/
@NotNull
@Query(value = "SELECT a.* from app a LEFT JOIN app_group ag on a.group_id =ag.id_ WHERE a.group_id IS NOT NULL", nativeQuery = true)
List<AppEntity> getAppListByGroup();
/**
* clientId
*
@ -136,12 +128,4 @@ public interface AppRepository extends LogicDeleteRepository<AppEntity, Long>,
@Cacheable
Optional<AppEntity> findByCode(String appCode);
/**
*
*
* @param id {@link Long}
* @param enabled {@link Boolean}
* @return {@link Boolean}
*/
}

View File

@ -54,4 +54,13 @@ public interface AppRepositoryCustomized {
* @return {@link List}
*/
Page<AppEntity> getAppList(AppQuery appQuery, Pageable pageable);
/**
*
*
* @param userId {@link Long}
* @return {@link Long}
*/
Long getAppCount(Long userId);
}

View File

@ -44,6 +44,26 @@ import lombok.AllArgsConstructor;
@AllArgsConstructor
public class AppGroupRepositoryCustomizedImpl implements AppGroupRepositoryCustomized {
private StringBuilder getBaseAppGroupListSql(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 INNER JOIN app ON aga.app_id = app.id_ AND app.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
return builder;
}
/**
*
*
@ -76,24 +96,28 @@ public class AppGroupRepositoryCustomizedImpl implements AppGroupRepositoryCusto
return jdbcTemplate.query(getBaseAppGroupListSql(query).toString(), new AppGroupPoMapper());
}
private StringBuilder getBaseAppGroupListSql(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 INNER JOIN app ON aga.app_id = app.id_ AND app.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
return builder;
/**
*
*
* @param userId {@link Long}
* @param query {@link AppGroupQuery}
* @return {@link List}
*/
@Override
public List<AppGroupPO> getAppGroupList(Long userId, AppGroupQuery query) {
return null;
}
/**
*
*
* @param groupId {@link Long}
* @param userId {@link Long}
* @return {@link Long}
*/
@Override
public Long getAppCount(String groupId, Long userId) {
return null;
}
private final JdbcTemplate jdbcTemplate;

View File

@ -94,7 +94,7 @@ public class AppRepositoryCustomizedImpl implements AppRepositoryCustomized {
.append(",").append(pageable.getPageSize()).toString(),
paramMap, new AppEntityMapper());
//@formatter:off
String countSql = "SELECT count(*) FROM (" + sql + ") app_account_";
String countSql = "SELECT count(*) FROM (" + sql + ") app_";
//@formatter:on
Integer count = namedParameterJdbcTemplate.queryForObject(countSql, paramMap,
Integer.class);
@ -139,6 +139,17 @@ public class AppRepositoryCustomizedImpl implements AppRepositoryCustomized {
return new PageImpl<>(list, pageable, Objects.requireNonNull(count).longValue());
}
/**
*
*
* @param userId {@link Long}
* @return {@link Long}
*/
@Override
public Long getAppCount(Long userId) {
return null;
}
/**
* JdbcTemplate
*/

View File

@ -19,6 +19,7 @@ package cn.topiam.employee.portal.controller;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -49,6 +50,24 @@ import static cn.topiam.employee.common.constant.AppConstants.APP_PATH;
@RequestMapping(value = APP_PATH)
@AllArgsConstructor
public class AppController {
/**
*
*
* @return {@link GetAppListResult}
*/
@Operation(summary = "获取应用数量")
@GetMapping(value = "/count")
public ApiRestResult<String> getAppCount(String groupId) {
Long count;
if (StringUtils.isEmpty(groupId)) {
count = appService.getAppCount();
} else {
count = appService.getAppCount(groupId);
}
return ApiRestResult.ok(count.toString());
}
/**
*
*

View File

@ -49,4 +49,19 @@ public interface AppService {
* @return {@link AppGroupListResult}
*/
List<AppGroupListResult> getAppGroupList(AppGroupQuery appGroupQuery);
/**
*
*
* @param groupId {@link String}
* @return {@link Long}
*/
Long getAppCount(String groupId);
/**
*
*
* @return {@link Long}
*/
Long getAppCount();
}

View File

@ -68,10 +68,34 @@ public class AppServiceImpl implements AppService {
*/
@Override
public List<AppGroupListResult> getAppGroupList(AppGroupQuery query) {
List<AppGroupPO> list = appGroupRepository.getAppGroupList(query);
Long userId = Long.valueOf(SecurityUtils.getCurrentUserId());
List<AppGroupPO> list = appGroupRepository.getAppGroupList(userId, query);
return appGroupConverter.entityConvertToAppGroupListResult(list);
}
/**
*
*
* @param groupId {@link String}
* @return {@link Integer}
*/
@Override
public Long getAppCount(String groupId) {
Long userId = Long.valueOf(SecurityUtils.getCurrentUserId());
return appGroupRepository.getAppCount(groupId, userId);
}
/**
*
*
* @return {@link Long}
*/
@Override
public Long getAppCount() {
Long userId = Long.valueOf(SecurityUtils.getCurrentUserId());
return appRepository.getAppCount(userId);
}
/**
* AppRepository
*/