特性
CREATE DATABASE `user_center`;
USE `user_center`;
-- -----------------------------------------------------
-- Table `user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `user` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT 'Id',
`wx_id` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '微信id',
`wx_nickname` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '微信昵称',
`roles` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '角色',
`avatar_url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '头像地址',
`create_time` DATETIME NOT NULL COMMENT '创建时间',
`update_time` DATETIME NOT NULL COMMENT '修改时间',
`bonus` INT NOT NULL DEFAULT 300 COMMENT '积分',
PRIMARY KEY (`id`))
COMMENT = '分享';
-- -----------------------------------------------------
-- Table `bonus_event_log`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bonus_event_log` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT 'Id',
`user_id` INT NULL COMMENT 'user.id',
`value` INT NULL COMMENT '积分操作值',
`event` VARCHAR(20) NULL COMMENT '发生的事件',
`create_time` DATETIME NULL COMMENT '创建时间',
`description` VARCHAR(100) NULL COMMENT '描述',
PRIMARY KEY (`id`),
INDEX `fk_bonus_event_log_user1_idx` (`user_id` ASC) )
ENGINE = InnoDB
COMMENT = '积分变更记录表';
CREATE DATABASE `content_center`;
USE `content_center`;
-- -----------------------------------------------------
-- Table `share`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `share` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT 'id',
`user_id` INT NOT NULL DEFAULT 0 COMMENT '发布人id',
`title` VARCHAR(80) NOT NULL DEFAULT '' COMMENT '标题',
`create_time` DATETIME NOT NULL COMMENT '创建时间',
`update_time` DATETIME NOT NULL COMMENT '修改时间',
`is_original` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否原创 0:否 1:是',
`author` VARCHAR(45) NOT NULL DEFAULT '' COMMENT '作者',
`cover` VARCHAR(256) NOT NULL DEFAULT '' COMMENT '封面',
`summary` VARCHAR(256) NOT NULL DEFAULT '' COMMENT '概要信息',
`price` INT NOT NULL DEFAULT 0 COMMENT '价格(需要的积分)',
`download_url` VARCHAR(256) NOT NULL DEFAULT '' COMMENT '下载地址',
`buy_count` INT NOT NULL DEFAULT 0 COMMENT '下载数 ',
`show_flag` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否显示 0:否 1:是',
`audit_status` VARCHAR(10) NOT NULL DEFAULT 0 COMMENT '审核状态 NOT_YET: 待审核 PASSED:审核通过 REJECTED:审核不通过',
`reason` VARCHAR(200) NOT NULL DEFAULT '' COMMENT '审核不通过原因',
PRIMARY KEY (`id`))
ENGINE = InnoDB
COMMENT = '分享表';
-- -----------------------------------------------------
-- Table `mid_user_share`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mid_user_share` (
`id` INT NOT NULL AUTO_INCREMENT,
`share_id` INT NOT NULL COMMENT 'share.id',
`user_id` INT NOT NULL COMMENT 'user.id',
PRIMARY KEY (`id`),
INDEX `fk_mid_user_share_share1_idx` (`share_id` ASC) ,
INDEX `fk_mid_user_share_user1_idx` (`user_id` ASC) )
ENGINE = InnoDB
COMMENT = '用户-分享中间表【描述用户购买的分享】';
-- -----------------------------------------------------
-- Table `notice`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `notice` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT 'id',
`content` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '内容',
`show_flag` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否显示 0:否 1:是',
`create_time` DATETIME NOT NULL COMMENT '创建时间',
PRIMARY KEY (`id`));
https://github.com/eacdy/itmuch-miniapp
user-center
项目部分结构图
service层
controller层
content-center
ShareDTO
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "share")
public class ShareDTO {
/**
* id
*/
private Integer id;
/**
* 发布人id
*/
private Integer userId;
/**
* 标题
*/
private String title;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date updateTime;
/**
* 是否原创 0:否 1:是
*/
private Boolean isOriginal;
/**
* 作者
*/
private String author;
/**
* 封面
*/
private String cover;
/**
* 概要信息
*/
private String summary;
/**
* 价格(需要的积分)
*/
private Integer price;
/**
* 下载地址
*/
private String downloadUrl;
/**
* 下载数
*/
private Integer buyCount;
/**
* 是否显示 0:否 1:是
*/
private Boolean showFlag;
/**
* 审核状态 NOT_YET: 待审核 PASSED:审核通过 REJECTED:审核不通过
*/
private String auditStatus;
/**
* 审核不通过原因
*/
private String reason;
/**
* 发布人昵称
*/
private String wxNickname;
}
UserDTO
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserDTO {
/**
* Id
*/
private Integer id;
/**
* 微信id
*/
private String wxId;
/**
* 微信昵称
*/
private String wxNickname;
/**
* 角色
*/
private String roles;
/**
* 头像地址
*/
private String avatarUrl;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date updateTime;
/**
* 积分
*/
private Integer bonus;
}
主启动类上添加
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
ShareService
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareService {
private final ShareMapper shareMapper;
private final RestTemplate restTemplate;
public ShareDTO findById(Integer id){
// 获取分享的详情
Share share = this.shareMapper.selectByPrimaryKey(id);
// 发布人id
Integer userId = share.getUserId();
// 怎么调用用户微服务的users/{userId}
UserDTO userDTO = this.restTemplate.getForObject(
"http://localhost:8080/users/{id}",
UserDTO.class,
userId
);
// 消息的装配
ShareDTO shareDTO = new ShareDTO();
BeanUtils.copyProperties(share,shareDTO);
shareDTO.setWxNickname(userDTO.getWxNickname());
return shareDTO;
}
}
ShareController
@RestController
@RequestMapping("/shares")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareController {
private final ShareService shareService;
@GetMapping("/{id}")
public ShareDTO findById(@PathVariable Integer id){
return this.shareService.findById(id);
}
}
测试