标签系统
程序员文章站
2022-04-29 22:32:59
...
一、增加拿手的兴趣标签和身份标签,用于后续商家增值服务使用,同时建立基础的用户画像。
1、系统增加两类标签
身份标签:
• 身份标签为拿手的的用户属性,用于描述拿手的自身的特性,具体如下:
• 时尚潮人
• 数码极客
• 文艺青年
• 运动达人
• 无敌辣妈
• 御宅一族兴趣标签:
• 兴趣标签为淘宝的宝贝类目,用于了解拿手在淘宝上的消费习惯,具体标签内容如下:
• 服装鞋包
• 手机数码
• 家用电器
• 美妆饰品
• 母婴用品
• 家居建材
• 百货食品
• 运动户外
• 文化玩乐
• 生活服务
• 汽配摩托
2、拿手手机端增加打标签的页面
• 拿手手机端新增两个页面,用于拿手身份标签和兴趣标签的选择,界面原型如下,设计稿界面待更新;
• 在设定标签的过程中,有可能会发生返回的操作,所以在最后点击“开启我的白拿拿之旅”时,进行保存的操作
3、标签页面触发入口
- 新注册用户:
对于新注册的用户,完成注册或者绑定手机号完成后,直接进入上述的标签原则页,且拿手必须完成标签的选择后,才可进入到首页; - 老用户
对于已注册的用户,在返回的首页的时候(包括点击分享的链接、公众号底部菜单、其它页面返回首页等),如果未设定过身份标签和兴趣标签,则直接进入标签设定页,设定完成后才可以进入到首页;
4、保存设定的信息到拿手的标签中
• 拿手设定完成后,保存对应的身份标签和兴趣标签到该拿手的标签信息中,同时后台标签管理中,需要增加该拿手对应标签的展示
二、系统设计:
1、标签配置表:
实体类:
package com.zhibi.xiuba.user.spi.model;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* Created by QinHe on 2018-05-08.
* 底层标签配置
*/
@Entity
@Table(name = "xb_us_tag_config")
public class TagConfig implements Serializable {
private static final long serialVersionUID = -845329773606157910L;
@Id
@GeneratedValue(generator = "id_generator")
@GenericGenerator(name = "id_generator", strategy = "redis_id")
private Long id;
/**
* 父标签
*/
@ManyToOne()
@JoinColumn(updatable = false, name = "parent_id", referencedColumnName = "id")
private TagConfig parent;
/**
* 标签层级 0为最顶层
*/
private Integer level;
/**
* 用于排序
*/
private Integer sortIndex;
/**
* 标签名字
*/
private String name;
/**
* 标签描述
*/
private String description;
/**
* 当下是否可用
*/
@Type(type = "org.hibernate.type.NumericBooleanType")
private Boolean enabled;
private String dataType;
private String iconUrl;
private Date createTime;
private Date updateTime;
private Date deadLineTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Date getDeadLineTime() {
return deadLineTime;
}
public void setDeadLineTime(Date deadLineTime) {
this.deadLineTime = deadLineTime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public TagConfig getParent() {
return parent;
}
public void setParent(TagConfig parent) {
this.parent = parent;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Integer getSortIndex() {
return sortIndex;
}
public void setSortIndex(Integer sortIndex) {
this.sortIndex = sortIndex;
}
public String getIconUrl() {
return iconUrl;
}
public void setIconUrl(String iconUrl) {
this.iconUrl = iconUrl;
}
}
标签是分级的,这里面是用parent一个字段就可以表示层级关系了
2、用户标签对应表
实体类:
package com.zhibi.xiuba.user.spi.model;
import com.zhibi.xiuba.user.spi.enums.DataTypeEnum;
import com.zhibi.xiuba.utils.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* Created by QinHe on 2018-05-08.
* 增值业务配置类
*/
@Entity
@Table(name = "xb_us_tag_library")
public class TagLibrary implements Serializable {
private static final long serialVersionUID = -4645714347119745836L;
@Id
@GeneratedValue(generator = "id_generator")
@GenericGenerator(name = "id_generator", strategy = "redis_id")
private Long id;
/**
* 标签 多对一
*/
@ManyToOne
@JoinColumn(nullable = false, updatable = false, name = "tag_config_id", referencedColumnName = "id")
private TagConfig tagConfig;
/**
* 用户id
*/
private Long userId;
private String stringValue;
private Date dateValue;
private Long longValue;
@Column(columnDefinition = "bit", length = 1)
@Type(type = "org.hibernate.type.NumericBooleanType")
private Boolean booleanValue;
private Double doubleValue;
private Date createTime;
private Date updateTime;
public TagLibrary() {
}
public TagLibrary(TagConfig tagConfig, Long userId, Date createTime) {
this.tagConfig = tagConfig;
this.userId = userId;
this.createTime = createTime;
}
public void setValue(Object value, String dataType) {
if (value != null && StringUtils.isNoneBlank(dataType)) {
if (DataTypeEnum.DATA_TYPE_ENUM_STRING.getCode().equals(dataType)) {
this.stringValue = (String) value;
}
if (DataTypeEnum.DATA_TYPE_ENUM_DATE.getCode().equals(dataType)) {
this.dateValue = (Date) value;
}
if (DataTypeEnum.DATA_TYPE_ENUM_LONG.getCode().equals(dataType)) {
this.longValue = (Long) value;
}
if (DataTypeEnum.DATA_TYPE_ENUM_BOOLEAN.getCode().equals(dataType)) {
this.booleanValue = (Boolean) value;
}
if (DataTypeEnum.DATA_TYPE_ENUM_DOUBLE.getCode().equals(dataType)) {
this.doubleValue = (Double) value;
}
}
this.stringValue = value.toString();
this.updateTime = new Date();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public TagConfig getTagConfig() {
return tagConfig;
}
public void setTagConfig(TagConfig tagConfig) {
this.tagConfig = tagConfig;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getStringValue() {
return stringValue;
}
public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}
public Date getDateValue() {
return dateValue;
}
public void setDateValue(Date dateValue) {
this.dateValue = dateValue;
}
public Long getLongValue() {
return longValue;
}
public void setLongValue(Long longValue) {
this.longValue = longValue;
}
public Boolean getBooleanValue() {
return booleanValue;
}
public void setBooleanValue(Boolean booleanValue) {
this.booleanValue = booleanValue;
}
public Double getDoubleValue() {
return doubleValue;
}
public void setDoubleValue(Double doubleValue) {
this.doubleValue = doubleValue;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
3、controller方法
package com.zhibi.xiuba.controller;
import com.alibaba.fastjson.JSON;
import com.zhibi.xiuba.model.ControllerResult;
import com.zhibi.xiuba.service.ITagService;
import com.zhibi.xiuba.user.spi.enums.TagConfigIdEnum;
import com.zhibi.xiuba.user.spi.model.TagConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/user/tag/")
public class TagController {
private final ITagService tagService;
public TagController(ITagService tagService) {
this.tagService = tagService;
}
@PostMapping("/makeTagForShowker")
@ResponseBody
public ControllerResult makeTagForShowker(@RequestParam Long showkerId,
@RequestParam String identityTagJsonStr,
@RequestParam String hobbyTagJsonStr) {
List<Long> idsIdentity = tagService.findByUserIdAndTagParent(showkerId, TagConfigIdEnum.IDENTITY_TAG_ID.getCode());
List<Long> idsHobby = tagService.findByUserIdAndTagParent(showkerId, TagConfigIdEnum.HOBBY_TAG_ID.getCode());
if ((idsIdentity != null && idsIdentity.size() > 0) || (idsHobby != null && idsHobby.size() > 0)) {
return ControllerResult.build(false, "", "您已经打过标签了!");
}
if (StringUtils.isNotBlank(identityTagJsonStr)) {
List<Long> tagIds = JSON.parseArray(identityTagJsonStr, Long.class);
if (tagIds != null) {
if (tagIds.size() > 2) {
return ControllerResult.build(false, "", "最多可选两项!");
}
for (Long tagId : tagIds) {
tagService.createTagForUser(showkerId, tagId);
}
}
}
if (StringUtils.isNotBlank(hobbyTagJsonStr)) {
List<Long> tagIds = JSON.parseArray(hobbyTagJsonStr, Long.class);
if (tagIds != null) {
if (tagIds.size() > 4) {
return ControllerResult.build(false, "", "最多可选四项!");
}
for (Long tagId : tagIds) {
tagService.createTagForUser(showkerId, tagId);
}
}
}
return ControllerResult.ok();
}
@GetMapping("/getIdentityTag")
@ResponseBody
public ControllerResult getIdentityTag(Long categoryId, Boolean enable) {
List<TagConfig> list = tagService.getIdentityTag(categoryId, enable);
return ControllerResult.ok(list);
}
}
4、业务逻辑层
package com.zhibi.xiuba.service.impl;
import com.zhibi.xiuba.repository.TagConfigRepository;
import com.zhibi.xiuba.repository.TagLibraryRepository;
import com.zhibi.xiuba.service.ITagService;
import com.zhibi.xiuba.user.spi.model.TagConfig;
import com.zhibi.xiuba.user.spi.model.TagLibrary;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* Created by QinHe on 2018-07-02.
*/
@Service
public class TagService implements ITagService {
private final TagLibraryRepository tagLibraryRepository;
private final TagConfigRepository tagConfigRepository;
public TagService(TagLibraryRepository tagLibraryRepository, TagConfigRepository tagConfigRepository) {
this.tagLibraryRepository = tagLibraryRepository;
this.tagConfigRepository = tagConfigRepository;
}
@Override
public List<Long> findByUserIdAndTagParent(Long userId, Long tagParentId) {
return tagLibraryRepository.findByUserIdAndTagParent(userId, tagParentId);
}
@Override
@Cacheable(cacheNames = "Tag", key = "'categoryId_' + #categoryId+'_'+#enable")
public List<TagConfig> getIdentityTag(Long categoryId, Boolean enable) {
TagConfig tagConfig = new TagConfig();
TagConfig parent = new TagConfig();
parent.setId(categoryId);
tagConfig.setParent(parent);
if (enable != null) {
tagConfig.setEnabled(enable);
}
return tagConfigRepository.findAll(Example.of(tagConfig));
}
@Override
public void createTagForUser(Long showkerId, Long tagId) {
TagLibrary tagLibrary = new TagLibrary();
tagLibrary.setCreateTime(new Date());
tagLibrary.setUserId(showkerId);
tagLibrary.setTagConfig(tagConfigRepository.findOne(tagId));
tagLibraryRepository.save(tagLibrary);
}
}
上一篇: input标签详解,用户注册表单
下一篇: Web学习第一天