feat: add community post categories and channels
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
CREATE TABLE IF NOT EXISTS `la_community_post_category` (
|
||||
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(50) NOT NULL DEFAULT '' COMMENT '分类名称',
|
||||
`icon` varchar(500) NOT NULL DEFAULT '' COMMENT '分类图标',
|
||||
`sort` int unsigned NOT NULL DEFAULT 0 COMMENT '排序',
|
||||
`post_count` int unsigned NOT NULL DEFAULT 0 COMMENT '帖子数',
|
||||
`status` tinyint unsigned NOT NULL DEFAULT 1 COMMENT '0禁用1启用',
|
||||
`create_time` int unsigned NOT NULL DEFAULT 0,
|
||||
`update_time` int unsigned NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_name` (`name`),
|
||||
KEY `idx_status_sort` (`status`, `sort`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='社区帖子分类';
|
||||
|
||||
SET @has_category_id = (
|
||||
SELECT COUNT(*)
|
||||
FROM `information_schema`.`columns`
|
||||
WHERE `table_schema` = DATABASE()
|
||||
AND `table_name` = 'la_community_post'
|
||||
AND `column_name` = 'category_id'
|
||||
);
|
||||
SET @sql = IF(
|
||||
@has_category_id = 0,
|
||||
'ALTER TABLE `la_community_post` ADD COLUMN `category_id` int unsigned NOT NULL DEFAULT 0 COMMENT ''帖子主分类ID'' AFTER `post_type`',
|
||||
'SELECT ''la_community_post.category_id already exists'''
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @has_is_topic = (
|
||||
SELECT COUNT(*)
|
||||
FROM `information_schema`.`columns`
|
||||
WHERE `table_schema` = DATABASE()
|
||||
AND `table_name` = 'la_community_post'
|
||||
AND `column_name` = 'is_topic'
|
||||
);
|
||||
SET @sql = IF(
|
||||
@has_is_topic = 0,
|
||||
'ALTER TABLE `la_community_post` ADD COLUMN `is_topic` tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''是否话题 0否 1是'' AFTER `is_recommend`',
|
||||
'SELECT ''la_community_post.is_topic already exists'''
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @has_category_index = (
|
||||
SELECT COUNT(*)
|
||||
FROM `information_schema`.`statistics`
|
||||
WHERE `table_schema` = DATABASE()
|
||||
AND `table_name` = 'la_community_post'
|
||||
AND `index_name` = 'idx_category'
|
||||
);
|
||||
SET @sql = IF(
|
||||
@has_category_index = 0,
|
||||
'ALTER TABLE `la_community_post` ADD INDEX `idx_category` (`category_id`)',
|
||||
'SELECT ''la_community_post.idx_category already exists'''
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @has_recommend_index = (
|
||||
SELECT COUNT(*)
|
||||
FROM `information_schema`.`statistics`
|
||||
WHERE `table_schema` = DATABASE()
|
||||
AND `table_name` = 'la_community_post'
|
||||
AND `index_name` = 'idx_status_recommend'
|
||||
);
|
||||
SET @sql = IF(
|
||||
@has_recommend_index = 0,
|
||||
'ALTER TABLE `la_community_post` ADD INDEX `idx_status_recommend` (`status`, `is_recommend`, `create_time`)',
|
||||
'SELECT ''la_community_post.idx_status_recommend already exists'''
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @has_hot_index = (
|
||||
SELECT COUNT(*)
|
||||
FROM `information_schema`.`statistics`
|
||||
WHERE `table_schema` = DATABASE()
|
||||
AND `table_name` = 'la_community_post'
|
||||
AND `index_name` = 'idx_status_hot'
|
||||
);
|
||||
SET @sql = IF(
|
||||
@has_hot_index = 0,
|
||||
'ALTER TABLE `la_community_post` ADD INDEX `idx_status_hot` (`status`, `is_hot`, `create_time`)',
|
||||
'SELECT ''la_community_post.idx_status_hot already exists'''
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @has_topic_index = (
|
||||
SELECT COUNT(*)
|
||||
FROM `information_schema`.`statistics`
|
||||
WHERE `table_schema` = DATABASE()
|
||||
AND `table_name` = 'la_community_post'
|
||||
AND `index_name` = 'idx_status_topic'
|
||||
);
|
||||
SET @sql = IF(
|
||||
@has_topic_index = 0,
|
||||
'ALTER TABLE `la_community_post` ADD INDEX `idx_status_topic` (`status`, `is_topic`, `create_time`)',
|
||||
'SELECT ''la_community_post.idx_status_topic already exists'''
|
||||
);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
INSERT IGNORE INTO `la_community_post_category`
|
||||
(`id`, `name`, `icon`, `sort`, `post_count`, `status`, `create_time`, `update_time`)
|
||||
SELECT
|
||||
`id`, `name`, `icon`, `sort`, `post_count`, `status`, `create_time`, UNIX_TIMESTAMP()
|
||||
FROM `la_community_tag`;
|
||||
|
||||
UPDATE `la_community_post` post
|
||||
INNER JOIN (
|
||||
SELECT ranked.`post_id`, ranked.`tag_id` AS `category_id`
|
||||
FROM (
|
||||
SELECT
|
||||
post_tag.`post_id`,
|
||||
post_tag.`tag_id`,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY post_tag.`post_id`
|
||||
ORDER BY tag.`sort` DESC, post_tag.`id` ASC
|
||||
) AS row_number
|
||||
FROM `la_community_post_tag` post_tag
|
||||
INNER JOIN `la_community_tag` tag ON tag.`id` = post_tag.`tag_id`
|
||||
INNER JOIN `la_community_post_category` category ON category.`id` = post_tag.`tag_id`
|
||||
) ranked
|
||||
WHERE ranked.row_number = 1
|
||||
) selected_category ON selected_category.`post_id` = post.`id`
|
||||
SET post.`category_id` = selected_category.`category_id`
|
||||
WHERE post.`category_id` = 0;
|
||||
|
||||
UPDATE `la_community_post` post
|
||||
SET post.`is_topic` = 1
|
||||
WHERE post.`is_topic` = 0
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM `la_community_post_tag` post_tag
|
||||
WHERE post_tag.`post_id` = post.`id`
|
||||
);
|
||||
|
||||
UPDATE `la_community_post_category` category
|
||||
LEFT JOIN (
|
||||
SELECT `category_id`, COUNT(*) AS `post_count`
|
||||
FROM `la_community_post`
|
||||
WHERE `delete_time` IS NULL
|
||||
AND `category_id` > 0
|
||||
GROUP BY `category_id`
|
||||
) post_stat ON post_stat.`category_id` = category.`id`
|
||||
SET category.`post_count` = COALESCE(post_stat.`post_count`, 0),
|
||||
category.`update_time` = UNIX_TIMESTAMP();
|
||||
|
||||
SET @community_parent_id = COALESCE(
|
||||
(SELECT `pid` FROM `la_system_menu` WHERE `perms` = 'community.communityPost/lists' LIMIT 1),
|
||||
(SELECT `pid` FROM `la_system_menu` WHERE `component` = 'community/post/index' LIMIT 1),
|
||||
(SELECT `pid` FROM `la_system_menu` WHERE `perms` = 'community.communityTag/lists' LIMIT 1),
|
||||
(SELECT `id` FROM `la_system_menu` WHERE `type` = 'M' AND `paths` = 'community' LIMIT 1),
|
||||
0
|
||||
);
|
||||
|
||||
INSERT INTO `la_system_menu`
|
||||
(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
|
||||
SELECT
|
||||
@community_parent_id, 'C', '帖子分类', '', 0, 'community.communityCategory/lists', 'category', 'community/category/index', '', '', 0, 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
|
||||
WHERE @community_parent_id > 0
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM `la_system_menu` WHERE `perms` = 'community.communityCategory/lists'
|
||||
);
|
||||
|
||||
SET @category_menu_id = (
|
||||
SELECT `id` FROM `la_system_menu`
|
||||
WHERE `perms` = 'community.communityCategory/lists'
|
||||
LIMIT 1
|
||||
);
|
||||
|
||||
INSERT INTO `la_system_menu`
|
||||
(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
|
||||
SELECT @category_menu_id, 'A', permission.`name`, '', 0, permission.`perms`, '', '', '', '', 0, 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
|
||||
FROM (
|
||||
SELECT '分类选项' AS `name`, 'community.communityCategory/all' AS `perms`
|
||||
UNION ALL SELECT '分类详情', 'community.communityCategory/detail'
|
||||
UNION ALL SELECT '分类新增', 'community.communityCategory/add'
|
||||
UNION ALL SELECT '分类编辑', 'community.communityCategory/edit'
|
||||
UNION ALL SELECT '分类删除', 'community.communityCategory/delete'
|
||||
) permission
|
||||
WHERE @category_menu_id IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM `la_system_menu` existing_menu WHERE existing_menu.`perms` = permission.`perms`
|
||||
);
|
||||
|
||||
SET @post_menu_id = COALESCE(
|
||||
(SELECT `id` FROM `la_system_menu` WHERE `perms` = 'community.communityPost/lists' LIMIT 1),
|
||||
(SELECT `id` FROM `la_system_menu` WHERE `component` = 'community/post/index' LIMIT 1)
|
||||
);
|
||||
|
||||
INSERT INTO `la_system_menu`
|
||||
(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
|
||||
SELECT @post_menu_id, 'A', permission.`name`, '', 0, permission.`perms`, '', '', '', '', 0, 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
|
||||
FROM (
|
||||
SELECT '设置推荐' AS `name`, 'community.communityPost/setRecommend' AS `perms`
|
||||
UNION ALL SELECT '设置话题', 'community.communityPost/setTopic'
|
||||
UNION ALL SELECT '设置分类', 'community.communityPost/setCategory'
|
||||
) permission
|
||||
WHERE @post_menu_id IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM `la_system_menu` existing_menu WHERE existing_menu.`perms` = permission.`perms`
|
||||
);
|
||||
|
||||
INSERT INTO `la_system_role_menu` (`role_id`, `menu_id`)
|
||||
SELECT 1, menu.`id`
|
||||
FROM `la_system_menu` menu
|
||||
WHERE menu.`perms` IN (
|
||||
'community.communityCategory/lists',
|
||||
'community.communityCategory/all',
|
||||
'community.communityCategory/detail',
|
||||
'community.communityCategory/add',
|
||||
'community.communityCategory/edit',
|
||||
'community.communityCategory/delete',
|
||||
'community.communityPost/setRecommend',
|
||||
'community.communityPost/setTopic',
|
||||
'community.communityPost/setCategory'
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM `la_system_role_menu` role_menu
|
||||
WHERE role_menu.`role_id` = 1
|
||||
AND role_menu.`menu_id` = menu.`id`
|
||||
);
|
||||
Reference in New Issue
Block a user