Skip to content

Commit

Permalink
refactor: 调整部分 SQL 语句,以兼容 PostgreSQL 数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Feb 19, 2024
1 parent 3405868 commit bf60d48
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
19 changes: 11 additions & 8 deletions continew-admin-monitor/src/main/resources/mapper/LogMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
SELECT
(SELECT COUNT(*) FROM sys_log) AS pvCount,
(SELECT COUNT(DISTINCT ip) FROM sys_log) AS ipCount,
(SELECT COUNT(*) FROM sys_log WHERE DATE(create_time) = CURDATE()) AS todayPvCount,
(SELECT COUNT(*) FROM sys_log WHERE DATE(create_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)) AS yesterdayPvCount
(SELECT COUNT(*) FROM sys_log WHERE DATE(create_time) = CURRENT_DATE) AS todayPvCount,
(SELECT COUNT(*) FROM sys_log WHERE DATE(create_time) = CURRENT_DATE - 1) AS yesterdayPvCount
</select>

<select id="selectListDashboardAccessTrend"
Expand All @@ -16,8 +16,8 @@
COUNT(*) AS pvCount,
COUNT(DISTINCT ip) AS ipCount
FROM sys_log
WHERE DATE(create_time) != CURRENT_DATE
GROUP BY DATE(create_time)
HAVING date != CURDATE()
ORDER BY DATE(create_time) DESC
LIMIT #{days}
</select>
Expand All @@ -27,22 +27,25 @@
SELECT
module,
COUNT(*) AS pvCount,
SUM(CASE WHEN DATE(create_time) = CURDATE() THEN 1 ELSE 0 END) AS todayPvCount,
SUM(CASE WHEN DATE(create_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN 1 ELSE 0 END) AS yesterdayPvCount
SUM(CASE WHEN DATE(create_time) = CURRENT_DATE THEN 1 ELSE 0 END) AS todayPvCount,
SUM(CASE WHEN DATE(create_time) = CURRENT_DATE - 1 THEN 1 ELSE 0 END) AS yesterdayPvCount
FROM sys_log
WHERE module != '验证码' AND module != '登录'
GROUP BY module
HAVING module != '验证码' AND module != '登录'
ORDER BY pvCount DESC
LIMIT 10
</select>

<select id="selectListDashboardGeoDistribution" resultType="java.util.Map">
SELECT
SUBSTRING_INDEX(address, ' ', 1) AS name,
CASE
WHEN POSITION(' ' IN address) > 0 THEN SUBSTRING(address FROM 1 FOR POSITION(' ' IN address) - 1)
ELSE address
END AS name,
COUNT(DISTINCT ip) AS value
FROM sys_log
GROUP BY name
ORDER BY COUNT(DISTINCT ip) DESC
ORDER BY value DESC
LIMIT 10
</select>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.charles7c.continew.admin.common.enums.DisEnableStatusEnum;
Expand All @@ -31,6 +33,8 @@
import top.charles7c.continew.admin.system.service.RoleDeptService;
import top.charles7c.continew.admin.system.service.UserService;
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.data.core.enums.DatabaseType;
import top.charles7c.continew.starter.data.core.util.MetaUtils;
import top.charles7c.continew.starter.extension.crud.service.impl.BaseServiceImpl;

import java.util.ArrayList;
Expand Down Expand Up @@ -155,7 +159,8 @@ private DeptDO getByParentId(Long parentId) {
* @return 子部门列表
*/
private List<DeptDO> listChildren(Long id) {
return baseMapper.lambdaQuery().apply("find_in_set(%s, ancestors)".formatted(id)).list();
DatabaseType databaseType = MetaUtils.getDatabaseTypeOrDefault(SpringUtil.getBean(DynamicRoutingDataSource.class), DatabaseType.MYSQL);
return baseMapper.lambdaQuery().apply(databaseType.findInSet(id, "ancestors")).list();
}

/**
Expand All @@ -169,8 +174,9 @@ private Long countChildren(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return 0L;
}
DatabaseType databaseType = MetaUtils.getDatabaseTypeOrDefault(SpringUtil.getBean(DynamicRoutingDataSource.class), DatabaseType.MYSQL);
return ids.stream()
.mapToLong(id -> baseMapper.lambdaQuery().apply("find_in_set(%s, ancestors)".formatted(id)).count())
.mapToLong(id -> baseMapper.lambdaQuery().apply(databaseType.findInSet(id, "ancestors")).count())
.sum();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ INSERT INTO "sys_dept"
VALUES
(1, 'Xxx科技有限公司', 0, '0', '系统初始部门', 1, 1, true, 1, NOW(), NULL, NULL),
(547887852587843590, '天津总部', 1, '0,1', NULL, 1, 1, false, 1, NOW(), NULL, NULL),
(547888008188133385, '研发部', 547887852587843590, '0,1,2', NULL, 1, 1, false, 1, NOW(), NULL, NULL),
(547888460711591948, 'UI部', 547887852587843590, '0,1,2', NULL, 2, 1, false, 1, NOW(), NULL, NULL),
(547888483713155087, '测试部', 547887852587843590, '0,1,2', NULL, 3, 1, false, 1, NOW(), NULL, NULL),
(547888505959743506, '运维部', 547887852587843590, '0,1,2', NULL, 4, 1, false, 1, NOW(), NULL, NULL),
(547888556819873814, '研发一组', 547888008188133385, '0,1,2,3', NULL, 1, 1, false, 1, NOW(), NULL, NULL),
(547888580614160409, '研发二组', 547888008188133385, '0,1,2,3', NULL, 2, 2, false, 1, NOW(), NULL, NULL);
(547888008188133385, '研发部', 547887852587843590, '0,1,547887852587843590', NULL, 1, 1, false, 1, NOW(), NULL, NULL),
(547888460711591948, 'UI部', 547887852587843590, '0,1,547887852587843590', NULL, 2, 1, false, 1, NOW(), NULL, NULL),
(547888483713155087, '测试部', 547887852587843590, '0,1,547887852587843590', NULL, 3, 1, false, 1, NOW(), NULL, NULL),
(547888505959743506, '运维部', 547887852587843590, '0,1,547887852587843590', NULL, 4, 1, false, 1, NOW(), NULL, NULL),
(547888556819873814, '研发一组', 547888008188133385, '0,1,547887852587843590,547888008188133385', NULL, 1, 1, false, 1, NOW(), NULL, NULL),
(547888580614160409, '研发二组', 547888008188133385, '0,1,547887852587843590,547888008188133385', NULL, 2, 2, false, 1, NOW(), NULL, NULL);

-- 初始化默认角色
INSERT INTO "sys_role"
Expand Down

0 comments on commit bf60d48

Please sign in to comment.