Home Database Mysql Tutorial 最代码网站中关于动态表event的设计思路

最代码网站中关于动态表event的设计思路

Jun 07, 2016 pm 03:57 PM
event code about dynamic Ideas website design

原文:最代码网站中关于动态表event的设计思路 为了能将最代码整站用户的操作都展现出来,需要设计一种动态类型,既可以根据业务无限扩展,也可以指定某些用户行为是可以产生多少牛币交换的,这样就在原先javaniu的零散的表设计基础上抽象出event表 表结构如

原文:最代码网站中关于动态表event的设计思路

为了能将最代码整站用户的操作都展现出来,需要设计一种动态类型,既可以根据业务无限扩展,也可以指定某些用户行为是可以产生多少牛币交换的,这样就在原先javaniu的零散的表设计基础上抽象出event表

表结构如下:

CREATE TABLE `javaniu_event` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `create_time` datetime NOT NULL,
  `update_time` datetime default NULL,
  `event_rule_id` bigint(20) NOT NULL default '0' COMMENT '用户注册\r\n下载代码\r\n浏览分享\r\n浏览寻求\r\n收藏分享\r\n收藏寻求\r\n浏览活动\r\n追加悬赏\r\n加入活动\r\n拜师\r\n关注用户\r\n发表心情\r\n发表寻求\r\n评论寻求\r\n评论代码\r\n上传代码\r\n下载代码\r\n分享代码\r\n关注用户\r\n浏览分享\r\n浏览寻求\r\n管理员删除代码\r\n收藏分享\r\n收藏寻求\r\n获取勋章\r\n拜师傅\r\n发起活动\r\n浏览活动\r\n加入活动\r\n追加悬赏\r\n连续一周发表心情\r\n用户周贡献排行\r\n用户月贡献排行\r\n用户年贡献排行\r\n代码下载周排行\r\n代码下载月排行\r\n代码下载年排行',
  `user_id` bigint(20) NOT NULL default '0',
  `source_user_id` bigint(20) NOT NULL default '0',
  `source_id` bigint(20) NOT NULL default '0',
  `target_id` bigint(20) NOT NULL default '0',
  `status` int(2) NOT NULL default '0' COMMENT '-1删除0待审核2正常',
  `type` int(2) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `create_time` (`create_time`),
  KEY `userid_status` (`user_id`,`status`),
  KEY `event_rule_id_source_id` (`event_rule_id`,`source_id`),
  KEY `event_rule_id_status` (`event_rule_id`,`status`),
  KEY `type_source_id` (`type`,`source_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Copy after login

关联表event_rule可以指定牛币规则及其动态显示信息,结构如下:

CREATE TABLE `javaniu_event_rule` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `create_time` datetime NOT NULL,
  `update_time` datetime default NULL,
  `type` int(1) NOT NULL COMMENT '注册+1\r\n发表心情+1\r\n连续一周发表心情+5\r\n分享代码+1\r\n分享代码被下载+n(n为分享者者自定义牛币)\r\n寻求代码-2\r\n上传代码+1\r\n上传代码被下载+1\r\n代码被设为最佳+n(n为寻求者者自定义牛币)\r\n删除代码-1\r\n无效寻求-2\r\n无效代码-2\r\n管理员奖赏+n\r\n管理员惩罚-n\r\n周top10+5\r\n月top10+10\r\n年top10+100\r\n信息完善+1\r\n包月vip+100\r\n师傅赠送+n牛币\r\n授予徽章+5牛币\r\n',
  `name` varchar(100) NOT NULL,
  `niubi` int(11) NOT NULL,
  `extend_json` varchar(1000) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Copy after login

event的用户行为数据模型抽象如下:

模型一:用户a通过事件x产生动态0=user_id_a 0 0 0
a=>x=>0
模型二:用户a通过事件x产生产生用户a的数据1=user_id_a 0 0 1
a=>x=>1
模型三:用户a通过事件x对用户b的数据1产生用户a的数据2=user_id_a b 1 2
a=>x+b+1=>2
模型四:用户a通过事件x对用户b的数据1产生动态0=user_id_a b 1 0
a=>x+b+1=>0
模型五:用户a通过事件x对用户b产生动态0=user_id_a b 0 0
a=>x+b=>0

排列组合:
user_id source_user_id source_id target_id
user_id
user_id source_user_id
user_id source_user_id source_id
user_id source_user_id source_id target_id

这样就囊括了所有会出现的用户event,只要在java层做业务转换即可:

最核心的event数据转换java类源码:

package com.zuidaima.core.service.impl;

	private void setSourceAndTarget(Event event, EventRule _eventRule) {
		try {
			EventRule eventRule = new EventRule();
			eventRule.setCreateTime(_eventRule.getCreateTime());
			eventRule.setExtendJson(_eventRule.getExtendJson());
			eventRule.setId(_eventRule.getId());
			eventRule.setName(_eventRule.getName());
			eventRule.setNiubi(_eventRule.getNiubi());
			eventRule.setType(_eventRule.getType());
			eventRule.setUpdateTime(_eventRule.getUpdateTime());
			BaseEntity source = null;
			BaseEntity target = null;
			long sourceId = event.getSourceId();
			long targetId = event.getTargetId();
			JSONObject extend = eventRule.getExtend();
			extend = eventRule.getExtend();
			String description = (String) extend.get("description");
			String _description = null;
			Answer answer = null;
			Project project = null;
			switch (eventRule.getType()) {
			case ModuleConstants.EVENT_TYPE_RULE_PROJECT_CREATE:
			case ModuleConstants.EVENT_TYPE_RULE_PROJECT_DELETE_BY_USER:
			case ModuleConstants.EVENT_TYPE_RULE_PROJECT_DELETE_BY_ADMIN:
			case ModuleConstants.EVENT_TYPE_RULE_PROJECT_VIEW:
			case ModuleConstants.EVENT_TYPE_RULE_PROJECT_COLLECT:
			case ModuleConstants.EVENT_TYPE_RULE_PROJECT_REWARD:
				if (sourceId > 0) {
					source = projectService.findOneById(sourceId);
				}
				if (targetId > 0) {
					target = projectService.findOneById(targetId);
				}
				project = (Project) target;
				if (source != null) {
					project = (Project) source;
				}
				if (project == null) {
					return;
				}
				_description = String.format(
						description,
						ModuleConstants.PROJECT_TYPE_DESC_MAP.get(
								project.getType()).getDesc());
				break;
			case ModuleConstants.EVENT_TYPE_RULE_POST_CREATE:
			case ModuleConstants.EVENT_TYPE_RULE_POST_DELETE_BY_USER:
			case ModuleConstants.EVENT_TYPE_RULE_POST_DELETE_BY_ADMIN:
				if (sourceId > 0) {
					source = postService.findOneById(sourceId);
				}
				if (targetId > 0) {
					target = postService.findOneById(targetId);
				}
				Post post = (Post) target;
				if (source != null) {
					post = (Post) source;
				}
				_description = String.format(description,
						ModuleConstants.POST_TYPE_DESC_MAP.get(post.getType()));
				break;
			// case ModuleConstants.EVENT_TYPE_RULE_GROUP_CREATE://暂时没有这种动态
			case ModuleConstants.EVENT_TYPE_RULE_GROUP_JOIN_IN:
			case ModuleConstants.EVENT_TYPE_RULE_GROUP_DELETE_BY_USER:
				// case
				// ModuleConstants.EVENT_TYPE_RULE_GROUP_DELETE_BY_ADMIN://暂时没有这种动态
				if (sourceId > 0) {
					source = groupService.findOneById(sourceId);
				}
				if (targetId > 0) {
					target = groupService.findOneById(targetId);
				}
				Group group = (Group) source;

				_description = String
						.format(description,
								ModuleConstants.GROUP_TYPE_DESC_MAP.get(group
										.getType()));
				break;
			case ModuleConstants.EVENT_TYPE_RULE_COMMENT_CREATE:
			case ModuleConstants.EVENT_TYPE_RULE_COMMENT_DELETE_BY_USER:
			case ModuleConstants.EVENT_TYPE_RULE_COMMENT_DELETE_BY_ADMIN:
				target = commentService.findOneById(targetId);
				Comment comment = (Comment) target;
				int commentType = comment.getType();
				if (commentType == ModuleConstants.COMMENT_TYPE_ANSWER) {
					source = answerService.findOneById(sourceId);
					answer = (Answer) source;
					project = (Project) answer.getTarget();
					_description = String.format(
							description,
							ModuleConstants.PROJECT_TYPE_DESC_MAP.get(
									project.getType()).getDesc());
				} else if (commentType == ModuleConstants.COMMENT_TYPE_PROJECT) {
					source = projectService.findOneById(sourceId);
					project = (Project) source;
					_description = String.format(
							description,
							ModuleConstants.PROJECT_TYPE_DESC_MAP.get(
									project.getType()).getDesc());
				} else if (commentType == ModuleConstants.COMMENT_TYPE_POST) {
					source = postService.findOneById(sourceId);
					post = (Post) source;
					_description = String.format(description,
							ModuleConstants.POST_TYPE_DESC_MAP.get(post
									.getType()));
				} else {

				}
				break;
			case ModuleConstants.EVENT_TYPE_RULE_ANSWER_CREATE:
			case ModuleConstants.EVENT_TYPE_RULE_ANSWER_BEEN_SET_PERFECT:
				source = projectService.findOneById(sourceId);
				target = answerService.findOneById(targetId);
				project = (Project) source;
				_description = String.format(
						description,
						ModuleConstants.PROJECT_TYPE_DESC_MAP.get(
								project.getType()).getDesc());
				break;
			case ModuleConstants.EVENT_TYPE_RULE_ANSWER_GET:
			case ModuleConstants.EVENT_TYPE_RULE_ANSWER_DELETE_BY_USER:
			case ModuleConstants.EVENT_TYPE_RULE_ANSWER_DELETE_BY_ADMIN:
				source = answerService.findOneById(sourceId);
				answer = (Answer) source;
				Project _project = (Project) answer.getTarget();
				_description = String.format(
						description,
						ModuleConstants.PROJECT_TYPE_DESC_MAP.get(
								_project.getType()).getDesc());
				break;
			case ModuleConstants.EVENT_TYPE_RULE_REPUTATION_CREATE:
				if (sourceId > 0) {
					source = reputationService.findOneById(sourceId);
				}
				if (targetId > 0) {
					target = reputationService.findOneById(targetId);
				}
				break;
			case ModuleConstants.EVENT_TYPE_RULE_USER_FOLLOW:
				source = userService.findOneById(sourceId);
				User _user = (User) source;
				_description = String
						.format(description,
								"<a href='/user/n/" &#43; _user.getName()
										&#43; ".htm'>" &#43; _user.getName() &#43; "</a>");
				break;
			case ModuleConstants.EVENT_TYPE_RULE_MENTION_COMMENT:
				target = commentService.findOneById(targetId);
				comment = (Comment) target;
				commentType = comment.getType();
				if (commentType == ModuleConstants.COMMENT_TYPE_ANSWER) {
					source = answerService.findOneById(sourceId);
					answer = (Answer) source;
					project = (Project) answer.getTarget();
					_description = String.format(
							description,
							ModuleConstants.PROJECT_TYPE_DESC_MAP.get(
									project.getType()).getDesc());
				} else if (commentType == ModuleConstants.COMMENT_TYPE_PROJECT) {
					source = projectService.findOneById(sourceId);
					project = (Project) source;
					_description = String.format(
							description,
							ModuleConstants.PROJECT_TYPE_DESC_MAP.get(
									project.getType()).getDesc());
				} else if (commentType == ModuleConstants.COMMENT_TYPE_POST) {
					source = postService.findOneById(sourceId);
					post = (Post) source;
					_description = String.format(description,
							ModuleConstants.POST_TYPE_DESC_MAP.get(post
									.getType()));
				} else {

				}
				break;
			case ModuleConstants.EVENT_TYPE_RULE_MENTION_POST:
				source = postService.findOneById(sourceId);
				break;
			default:
				_description = description;
			}

			extend.put("description", _description);
			eventRule.setExtend(extend);
			eventRule.setExtendJson(extend.toString());
			event.setEventRule(eventRule);

			event.setSource(source);
			event.setTarget(target);
		} catch (Exception e) {
			logger.error("Fail to setSourceAndTarget event:" &#43; event);
		}
	}
Copy after login

freemarker显示层转换核心代码:

<#switch event.eventRule.type>
				<#case event_type_rule_post_create>
					<@event_post_macro event.target/>
					<#break>
				<#case event_type_rule_project_create>
					<@event_project_macro event event.target/>
					<#break>
				<#case event_type_rule_project_view>
				<#case event_type_rule_project_collect>
				<#case event_type_rule_project_reward>
					<@event_project_macro event event.source/>
					<#break>
				<#case event_type_rule_comment_create>
					<@event_comment_macro event event.target/>
					<#break>
				<#case event_type_rule_answer_create>
					<@event_answer_macro event event.target/>
					<#break>
				<#case event_type_rule_answer_get>
				<#case event_type_rule_answer_been_set_perfect>
					<@event_answer_macro event event.source/>
					<#break>
				<#case event_type_rule_mention_comment>
					<@event_comment_macro event event.target/>
					<#break>
				<#case event_type_rule_mention_post>
					<@event_post_macro event.source/>
					<#break>
				</#switch>
Copy after login

比如其中一种event type的freemarker macro代码如下:

<!--event post-->
<#macro event_post_macro post>
	<div class="content margin_top5">
		${post.contentExt}
		<span class="comments_count">
			<a target="_blank" href="/mood/${post.id}/comment.htm" rel="nofollow"><img src="/static/imghw/default1.png"  data-src="/resource/img/comment.gif"  class="lazy"  alt="${post.thirdSort}个评论">  ${post.thirdSort}</a>
		</span>
	</div>
</#macro>
Copy after login

这样的设计符合高内聚低耦合的设计思路,未来可以根据业务实现无限扩张,当然代价就是event表越来越大,但可以通过分库分表来分担压力,大家可以参考下,有好的意见可以留言。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert VirtualBox fixed disk to dynamic disk and vice versa Convert VirtualBox fixed disk to dynamic disk and vice versa Mar 25, 2024 am 09:36 AM

When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

Retro trend! HMD and Heineken jointly launch flip phone: transparent shell design Retro trend! HMD and Heineken jointly launch flip phone: transparent shell design Apr 17, 2024 pm 06:50 PM

According to news on April 17, HMD teamed up with the well-known beer brand Heineken and the creative company Bodega to launch a unique flip phone - The Boring Phone. This phone is not only full of innovation in design, but also returns to nature in terms of functionality, aiming to lead people back to real interpersonal interactions and enjoy the pure time of drinking with friends. Boring mobile phone adopts a unique transparent flip design, showing a simple yet elegant aesthetic. It is equipped with a 2.8-inch QVGA display inside and a 1.77-inch display outside, providing users with a basic visual interaction experience. In terms of photography, although it is only equipped with a 30-megapixel camera, it is enough to handle simple daily tasks.

ZTE 5G portable Wi-Fi U50S goes on sale for NT$899 at first launch: top speed 500Mbps ZTE 5G portable Wi-Fi U50S goes on sale for NT$899 at first launch: top speed 500Mbps Apr 26, 2024 pm 03:46 PM

According to news on April 26, ZTE’s 5G portable Wi-Fi U50S is now officially on sale, starting at 899 yuan. In terms of appearance design, ZTE U50S Portable Wi-Fi is simple and stylish, easy to hold and pack. Its size is 159/73/18mm and is easy to carry, allowing you to enjoy 5G high-speed network anytime and anywhere, achieving an unimpeded mobile office and entertainment experience. ZTE 5G portable Wi-Fi U50S supports the advanced Wi-Fi 6 protocol with a peak rate of up to 1800Mbps. It relies on the Snapdragon X55 high-performance 5G platform to provide users with an extremely fast network experience. Not only does it support the 5G dual-mode SA+NSA network environment and Sub-6GHz frequency band, the measured network speed can even reach an astonishing 500Mbps, which is easily satisfactory.

Teclast M50 Mini tablet is here: 8.7-inch IPS screen, 5000mAh battery Teclast M50 Mini tablet is here: 8.7-inch IPS screen, 5000mAh battery Apr 04, 2024 am 08:31 AM

According to news on April 3, Taipower’s upcoming M50 Mini tablet computer is a device with rich functions and powerful performance. This new 8-inch small tablet is equipped with an 8.7-inch IPS screen, providing users with an excellent visual experience. Its metal body design is not only beautiful but also enhances the durability of the device. In terms of performance, the M50Mini is equipped with the Unisoc T606 eight-core processor, which has two A75 cores and six A55 cores, ensuring a smooth and efficient running experience. At the same time, the tablet is also equipped with a 6GB+128GB storage solution and supports 8GB memory expansion, which meets users’ needs for storage and multi-tasking. In terms of battery life, M50Mini is equipped with a 5000mAh battery and supports Ty

Honor Magic V3 debuts AI defocus eye protection technology: effectively alleviates the development of myopia Honor Magic V3 debuts AI defocus eye protection technology: effectively alleviates the development of myopia Jul 18, 2024 am 09:27 AM

According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of ​​​​sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

How to design the end page of ppt to be attractive enough How to design the end page of ppt to be attractive enough Mar 20, 2024 pm 12:30 PM

At work, ppt is an office software often used by professionals. A complete ppt must have a good ending page. Different professional requirements give different ppt production characteristics. Regarding the production of the end page, how can we design it more attractively? Let’s take a look at how to design the end page of ppt! The design of the ppt end page can be adjusted in terms of text and animation, and you can choose a simple or dazzling style according to your needs. Next, we will focus on how to use innovative expression methods to create a ppt end page that meets the requirements. So let’s start today’s tutorial. 1. For the production of the end page, any text in the picture can be used. The important thing about the end page is that it means that my presentation is over. 2. In addition to these words,

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Jun 12, 2024 pm 08:38 PM

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not

Create and run Linux ".a" files Create and run Linux ".a" files Mar 20, 2024 pm 04:46 PM

Working with files in the Linux operating system requires the use of various commands and techniques that enable developers to efficiently create and execute files, code, programs, scripts, and other things. In the Linux environment, files with the extension &quot;.a&quot; have great importance as static libraries. These libraries play an important role in software development, allowing developers to efficiently manage and share common functionality across multiple programs. For effective software development in a Linux environment, it is crucial to understand how to create and run &quot;.a&quot; files. This article will introduce how to comprehensively install and configure the Linux &quot;.a&quot; file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux &quot;.a&quot; file. What is L

See all articles