谷粒随享
学习目标:
随堂流程图:https://kdocs.cn/join/gjxmegv?f=101
项目地址:https://gitee.com/lvhonlgong/tingshu-241229.git
git更新命令:
git pull origin master
如果修改过代码导致本地跟远端不一致,强制更新(远端覆盖本地)
git fetch --all
git reset --hard origin/master
git pull
随着智能手机和高速互联网的普及,人们开始寻求更便捷的方式来获取信息和娱乐。有声书的出现使得人们可以在旅途中、跑步时、做家务时等各种场景下,以更加灵活的方式享受阅读。
在过去,有声书主要是由专业的演员朗读,制作成录音带或CD。但随着数字化媒体的发展,听书软件应运而生,为用户提供了更多选择,包括自助出版的有声书和多样化的内容。
意义:
总的来说,听书软件的开发推动了阅读体验的数字化和个性化,为用户提供了更加便捷、多样化的阅读方式,也促进了作家和内容创作者的创作和传播。
有声书平台包含三角色:
内容创作者: 将书籍录制成有声书(音频)制作成专辑,在听书平台上发布专辑,可以达到盈利目的。
商家运营者:运营听书平台上所有的专辑,购买专辑版权。对专辑进行审核、发布、订单管理、支付管理。消费者消费(购买VIP会员,购买专辑、购买声音)可以达到盈利目的。
用户:收听平台上优质资源(知识付费),购买会员,购买专辑,购买声音
前端技术栈
参考听书软件环境安装.md
导入听书初始化项目资料中的tingshu-parent项目导入idea开发工具中即可!
第一步:
第二步:改NAT模式的子网IP:192.168.200.0
第三步:应用确定
第四步:启动虚拟机
第五步:登录虚拟机
IP:192.168.200.6
登录用户:root
登录密码:root
目前在虚拟机中安装以下容器服务都是开机自启动!
名 | URL | 账号密码 |
---|---|---|
Portainer | http://192.168.200.6:19000 | admin/admin1234567 |
MySQL | 192.168.200.6:3306 | root/root |
Redis | 192.168.200.6:6379 | |
Elasticsearch | http://192.168.200.6:9200 | elastic/111111 |
Kibana | http://192.168.200.6:5601 | elastic/111111 |
Logstash | 收集日志的后台进程,无需访问 | |
Zipkin | http://192.168.200.6:9411 | |
Nacos | http://192.168.200.6:8848/nacos | nacos/nacos |
MinIO | http://192.168.200.6:9001 | admin/admin123456 |
YAPI | http://192.168.200.6:3000 | admin@admin.com/ymfe.org |
MongoDB | 192.168.200.6:27017 | |
RabbitMQ | http://192.168.200.6:15672/ | admin/admin |
Tips:如果发现某些容器启动失败(说明该容器依赖其他容器,确保被依赖容器先正常启动),重启虚拟机
Nacos容器依赖MySQL容器-先启动MySQL容器
Kibana容器依赖ElasticSearch容器:先启动ES容
YAPI容器依赖MongoDB容器:先启动MongoDB
如果发现Docker服务重启/启动报错。将Docker服务重启,所有容器开机自启
systemctl restart docker
找到配套资料中mp-weixin-微信小程序.zip解压
配套资料\02-软件\找到安装微信开发者工具
每个同学注册申请微信小程序测试号(微信登录会使用到)测试账号申请入口https://mp.weixin.qq.com/wxamp/sandbox?doc=1
appId:测试号应用ID
appSecret:测试号对应秘钥
在微信开发者工具中导入,导入选择信任项目,注意:这里填写自己申请测试号应用ID
功能入口:运行app项目-->我的-->创作中心-->专辑-->点击 + 添加专辑。提供给内容创作者/平台运营人员。使用(该功能必须登录才能访问),专辑经过审核(文字+封面)机制,审核通过内容创作者用户录制声音(音频文件-声音管理)新增专辑,将声音关联到专辑下。其他的普通用户(网民)可以在APP/小程序中进行获取内容资源(有声书等资源)
主要功能如下:
需求:在保存专辑需要为新增专辑关联分类,三级分类数据需要采用列表展示
创建视图:
# 需求:基于视图对象封装所有1,2,3级分类数据 视图中包含1,2,3级分类ID及名称 为视图中记录设置三级分类ID作为视图主键
# 视图语法 :create [or replace] view 视图名称 as SQL; 如果变更视图中数据 变更SQL后重新再次执行创建即可
/**
编写SQL:
1.根据业务需求确定查询业务数据来自于哪些表
2.确定使用哪种关联查询方式(内连接,外连接)
3.确定表关联条件
4.判断是否需要进行筛选数据where,是否需要进行排序(order by),是否需要进行分组(group by),是否需要分组后过滤(having),limit等
5.通过查看执行计划,设计合理索引确保SQL执行效率,要求至少达到Range级别
*/
# 1.根据业务需求确定查询业务数据来自于哪些表:base_category1,base_category2,base_category3
# 2.确定使用哪种关联查询方式:选择内,外均可
# 3.确定表关联条件:二及分类表1级分类ID关联二级分类表主键;三级分类表中2级分类ID关联2级分类表主键
select *
from base_category1 bc1
inner join base_category2 bc2 on bc2.category1_id = bc1.id
inner join base_category3 bc3 on bc3.category2_id = bc2.id;
select bc3.id, bc1.id, bc1.name, bc2.id, bc2.name, bc3.id, bc3.name
from base_category1 bc1
inner join base_category2 bc2 on bc2.category1_id = bc1.id
inner join base_category3 bc3 on bc3.category2_id = bc2.id
where bc1.is_deleted = 0
and bc2.is_deleted = 0;
# 视图记录会封装到BaseCategoryView对象中,需要对对不同分类ID,名称起别名
select bc3.id,
bc1.id category1_id,
bc1.name category1_name,
bc2.id category2_id,
bc2.name category2_name,
bc3.id category3_id,
bc3.name category3_name,
bc3.create_time,
bc3.update_time,
bc3.is_deleted
from base_category1 bc1
inner join base_category2 bc2 on bc2.category1_id = bc1.id
inner join base_category3 bc3 on bc3.category2_id = bc2.id
where bc1.is_deleted = 0
and bc2.is_deleted = 0;
# 5.通过查看执行计划,设计合理索引确保SQL执行效率,要求至少达到Range级别
explain
select bc3.id,
bc1.id category1_id,
bc1.name category1_name,
bc2.id category2_id,
bc2.name category2_name,
bc3.id category3_id,
bc3.name category3_name,
bc3.create_time,
bc3.update_time,
bc3.is_deleted
from base_category1 bc1
inner join base_category2 bc2 on bc2.category1_id = bc1.id
inner join base_category3 bc3 on bc3.category2_id = bc2.id
where bc1.is_deleted = 0
and bc2.is_deleted = 0;
#6. 创建分类视图对象
create or replace view base_category_view as
select bc3.id,
bc1.id category1_id,
bc1.name category1_name,
bc2.id category2_id,
bc2.name category2_name,
bc3.id category3_id,
bc3.name category3_name,
bc3.create_time,
bc3.update_time,
bc3.is_deleted
from base_category1 bc1
inner join base_category2 bc2 on bc2.category1_id = bc1.id
inner join base_category3 bc3 on bc3.category2_id = bc2.id
where bc1.is_deleted = 0
and bc2.is_deleted = 0;
涉及到的视图对象: base_category_view ,在这张视图中存储了所有的分类数据。展示分类数据的格式如下:
[
{
"categoryName":"音乐", #一级分类名称
"categoryId":1, #一级分类ID
"categoryChild":[ #当前一级分类包含的二级分类集合
{
"categoryName":"音乐音效", #二级分类名称
"categoryId":101, #二级分类ID
"categoryChild":[ #当前二级分类包含的三级分类集合
{
"categoryName": "催眠音乐",
"categoryId": 1001
},
{
"categoryName": "放松音乐",
"categoryId": 1002
},
{
"categoryName": "提神音乐",
"categoryId": 1003
}
]
}
]
},
{
"categoryName":"有声书",
"categoryId":2,
"categoryChild":[
{
"categoryName":"男频小说",
"categoryId":104,
"categoryChild":[
{
"categoryName":"军事小说",
"categoryId":1009
}
]
}
]
}
]
YAP接口地址:http://192.168.200.6:3000/project/11/interface/api/11
在service-album
模块中BaseCategoryApiController控制器编写
package com.atguigu.tingshu.album.api;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@Tag(name = "分类管理")
@RestController
@RequestMapping(value = "/api/album")
@SuppressWarnings({"all"})
public class BaseCategoryApiController {
@Autowired
private BaseCategoryService baseCategoryService;
/**
* 查询所有分类(1、2、3级分类)
*
* @return 业务数据:[{"categoryId":1,"categoryName":"分类",categoryChild:[..]},{其他1级分类}]
*/
@Operation(summary = "查询所有分类(1、2、3级分类)")
@GetMapping("/category/getBaseCategoryList")
public Result<List<JSONObject>> getBaseCategoryList() {
List<JSONObject> list = baseCategoryService.getBaseCategoryList();
return Result.ok(list);
}
}
接口与实现类
package com.atguigu.tingshu.album.service;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
public interface BaseCategoryService extends IService<BaseCategory1> {
/**
* 查询所有分类(1、2、3级分类)
*
* @return 业务数据:[{"categoryId":1,"categoryName":"分类",categoryChild:[..]},{其他1级分类}]
*/
List<JSONObject> getBaseCategoryList();
}
package com.atguigu.tingshu.album.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.BaseCategory1Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory2Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {
@Autowired
private BaseCategory1Mapper baseCategory1Mapper;
@Autowired
private BaseCategory2Mapper baseCategory2Mapper;
@Autowired
private BaseCategory3Mapper baseCategory3Mapper;
@Autowired
private BaseCategoryViewMapper baseCategoryViewMapper;
/**
* 查询所有分类(1、2、3级分类)
*
* @return 业务数据:[{"categoryId":1,"categoryName":"分类",categoryChild:[..]},{其他1级分类}]
*/
@Override
public List<JSONObject> getBaseCategoryList() {
//1.创建响应结果集合对象-用于封装所有一级分类对象
List<JSONObject> returnList = new ArrayList<>();
//2.查询所有分类数据-查询视图即可 共计401条记录
List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
//3.处理一级分类数据
//3.1 对所有分类集合列表进行分组按照1级分类ID进行分组 得到Map<分组ID,一级分类列表>
Map<Long, List<BaseCategoryView>> category1Map = allCategoryList.stream()
.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
for (Map.Entry<Long, List<BaseCategoryView>> entry1 : category1Map.entrySet()) {
//3.2 封装一级分类对象
JSONObject jsonObject1 = new JSONObject();
//3.2.1 封装1级分类ID
Long category1Id = entry1.getKey();
jsonObject1.put("categoryId", category1Id);
//3.2.2 封装1级分类名称
String category1Name = entry1.getValue().get(0).getCategory1Name();
jsonObject1.put("categoryName", category1Name);
//4. 处理二级分类数据
List<JSONObject> jsonObject2List = new ArrayList<>();
//4.1 对"1级"分类集合按照二级分类ID进行分组
Map<Long, List<BaseCategoryView>> category2Map = entry1.getValue()
.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
//4.2 遍历"2级"分类Map
for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
//4.3 封装二级分类对象
JSONObject jsonObject2 = new JSONObject();
//4.3.1 封装2级分类ID
Long category2Id = entry2.getKey();
jsonObject2.put("categoryId", category2Id);
//4.3.2 封装2级分类名称
String category2Name = entry2.getValue().get(0).getCategory2Name();
jsonObject2.put("categoryName", category2Name);
//4.4 将2级分类对象放入二级分类集合中
jsonObject2List.add(jsonObject2);
//5. 处理三级分类数据
List<JSONObject> jsonObject3List = new ArrayList<>();
//5.1 对"2级"分类列表进行遍历
for (BaseCategoryView baseCategoryView : entry2.getValue()) {
//5.2 封装三级分类JSONOBject对象
JSONObject jsonObject3 = new JSONObject();
//5.2.1 封装3级分类ID
jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
//5.2.2 封装3级分类名称
jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
//5.3 将3级分类对象放入集合中
jsonObject3List.add(jsonObject3);
}
//5.4 将3级分类对象集合加入到二级分类对象"categoryChild"属性中
jsonObject2.put("categoryChild", jsonObject3List);
}
//4.5 将二级分类集合封装在一级分类对象中"categoryChild"属性中
jsonObject1.put("categoryChild", jsonObject2List);
returnList.add(jsonObject1);
}
return returnList;
}
}
YAPI接口地址: http://192.168.200.6:3000/project/11/interface/api/15
在BaseCategoryApiController控制器中添加代码
/**
* 根据一级分类Id获取分类属性以及属性值(标签名,标签值)列表
*
* @param category1Id
* @return
*/
@Operation(summary = "根据一级分类Id获取分类属性以及属性值(标签名,标签值)列表")
@GetMapping("/category/findAttribute/{category1Id}")
public Result<List<BaseAttribute>> getAttributesByCategory1Id(@PathVariable Long category1Id) {
List<BaseAttribute> list = baseCategoryService.getAttributesByCategory1Id(category1Id);
return Result.ok(list);
}
BaseCategoryService
/**
* 根据一级分类Id获取分类属性以及属性值(标签名,标签值)列表
*
* @param category1Id
* @return
*/
List<BaseAttribute> getAttributesByCategory1Id(Long category1Id);
BaseCategoryServiceImpl
@Autowired
private BaseAttributeMapper baseAttributeMapper;
/**
* 根据一级分类Id获取分类属性以及属性值(标签名,标签值)列表
*
* @param category1Id
* @return
*/
@Override
public List<BaseAttribute> getAttributesByCategory1Id(Long category1Id) {
//1.获取持久层接口,调用持久层动态SQL
return baseAttributeMapper.getAttributesByCategory1Id(category1Id);
}
在BaseAttributeMapper中添加方法
package com.atguigu.tingshu.album.mapper;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface BaseAttributeMapper extends BaseMapper<BaseAttribute> {
/**
* 根据一级分类Id获取分类属性以及属性值(标签名,标签值)列表
* @param category1Id
* @return
*/
List<BaseAttribute> getAttributesByCategory1Id(@Param("category1Id") Long category1Id);
}
动态SQL:
#需求:根据一级分类Id获取分类属性以及属性值(标签名,标签值)列表
select ba.id,
ba.attribute_name,
bav.id base_attribute_value_id,
bav.value_name,
bav.attribute_id
from base_attribute ba
inner join base_attribute_value bav
on bav.attribute_id = ba.id
where ba.category1_id = ?
and ba.is_deleted = 0;
# #查询执行计划
explain select ba.id,
ba.attribute_name,
bav.id base_attribute_value_id,
bav.value_name,
bav.attribute_id
from base_attribute ba
inner join base_attribute_value bav
on bav.attribute_id = ba.id
where ba.category1_id = ?
and ba.is_deleted = 0;
在resources
目录下创建mapper目录并添加配置文件BaseAttributeMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.atguigu.tingshu.album.mapper.BaseAttributeMapper">
<!--自定义结果集:封装标签标签值一对多数据-->
<resultMap id="attributeValueMap" type="com.atguigu.tingshu.model.album.BaseAttribute" autoMapping="true">
<!--配置一方-->
<id column="id" property="id"></id>
<!--配置多方集合-->
<collection property="attributeValueList" ofType="com.atguigu.tingshu.model.album.BaseAttributeValue" autoMapping="true">
<id column="base_attribute_value_id" property="id"></id>
</collection>
</resultMap>
<!--根据一级分类Id获取分类属性以及属性值(标签名,标签值)列表-->
<select id="getAttributesByCategory1Id" resultMap="attributeValueMap">
select ba.id,
ba.attribute_name,
bav.id base_attribute_value_id,
bav.value_name,
bav.attribute_id
from base_attribute ba
inner join base_attribute_value bav
on bav.attribute_id = ba.id
where ba.category1_id = #{category1Id}
and ba.is_deleted = 0
</select>
</mapper>
MinIO 是一个基于Apache License v3.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。
MinIO是一个非常轻量的服务,可以很简单的和其他应用的结合,类似 NodeJS, Redis 或者 MySQL。
特点
高性能:作为高性能对象存储,在标准硬件条件下它能达到55GB/s的读、35GB/s的写速率
可扩容:不同MinIO集群可以组成联邦,并形成一个全局的命名空间,并跨越多个数据中心
云原生:容器化、基于K8S的编排、多租户支持
Amazon S3兼容:Minio使用Amazon S3 v2 / v4 API。可以使用Minio SDK,Minio Client,AWS SDK和AWS CLI访问Minio服务器。
可对接后端存储: 除了Minio自己的文件系统,还支持DAS、 JBODs、NAS、Google云存储和Azure Blob存储。
SDK支持: 基于Minio轻量的特点,它得到类似Java、Python或Go等语言 的sdk支持
Lambda计算: Minio服务器通过其兼容AWS SNS / SQS的事件通知服务触发Lambda功能。支持的目标是消息队列,如Kafka,NATS,AMQP,MQTT,Webhooks以及Elasticsearch,Redis,Postgres和MySQL等数据库。
有操作页面
功能简单: 这一设计原则让MinIO不容易出错、更快启动
支持纠删码:MinIO使用纠删码、Checksum来防止硬件错误和静默数据污染。在最高冗余度配置下,即使丢失1/2的磁盘也能恢复数据!
存储机制
Minio使用纠删码erasure code和校验和checksum。 即便丢失一半数量(N/2)的硬盘,仍然可以恢复数据。纠删码是一种恢复丢失和损坏数据的数学算法。
docker pull minio/minio
docker run \ -p 9000:9000 \ -p 9001:9001 \ --name minio \ -d --restart=always \ -e "MINIO_ROOT_USER=admin" \ -e "MINIO_ROOT_PASSWORD=admin123456" \ -v /home/data:/data \ -v /home/config:/root/.minio \ minio/minio server /data --console-address ":9001"
浏览器访问:http://192.168.200.6:9001/minio/login,如图:
登录账户说明:安装时指定了登录账号:admin/admin123456
注意:文件上传时,需要调整一下linux 服务器的时间与windows 时间一致!
> 第一步:安装ntp服务 > yum -y install ntp > 第二步:开启开机启动服务 > systemctl enable ntpd > 第三步:启动服务 Tips:联网正常前提下如果定时同步失败,先停止服务,再启动 > systemctl stop ntpd > systemctl start ntpd > 第四步:更改时区 > timedatectl set-timezone Asia/Shanghai > 第五步:启用ntp同步 > timedatectl set-ntp yes > 第六步:同步时间 > ntpq -p > ``` ### 2.3.3 专辑图片上传 MinIO-JavaAPI:https://min.io/docs/minio/linux/developers/java/API.html Tomcat默认限制上传文件大小:1MB 通过修改配置更改:
yaml spring: servlet:
multipart: max-file-size: 10MB #单个文件最大限制 max-request-size: 20MB #多个文件最大限制
**业务需求**:在新增专辑前需要为专辑设置专辑封面,选中本机图片文件后将文件上传到MInIO,且返回上传后文件在线地址,方便用户进行预览。效果如下:  > YAPI文档地址:http://192.168.200.6:3000/project/11/interface/api/13
java package com.atguigu.tingshu.album.api;
import com.atguigu.tingshu.album.service.FileUploadService; import com.atguigu.tingshu.common.result.Result; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;
@Tag(name = "上传管理接口") @RestController @RequestMapping("api/album") public class FileUploadApiController {
@Autowired
private FileUploadService fileUploadService;
/**
* 图片(封面、头像)文件上传
* 前端提交文件参数名:file
*
* @param multipartFile
* @return
*/
@Operation(summary = "图片(封面、头像)文件上传")
@PostMapping("/fileUpload")
public Result<String> fileUpload(@RequestParam("file") MultipartFile multipartFile) {
String fileUrl = fileUploadService.fileUpload(multipartFile);
return Result.ok(fileUrl);
}
}
**配置MinIO客户端对象**
java package com.atguigu.tingshu.album.config;
import io.minio.MinioClient; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration @ConfigurationProperties(prefix = "minio") //读取节点 @Data public class MinioConstantProperties {
private String endpointUrl;
private String accessKey;
private String secreKey;
private String bucketName;
/**
* 操作MInIO客户端对象
* @return
*/
@Bean
public MinioClient minioClient() {
return
MinioClient.builder()
.endpoint(endpointUrl)
.credentials(accessKey, secreKey)
.build();
}
}
**FileUploadService**
java package com.atguigu.tingshu.album.service;
import org.springframework.web.multipart.MultipartFile;
public interface FileUploadService {
/**
* 图片(封面、头像)文件上传
* 前端提交文件参数名:file
*
* @param multipartFile
* @return
*/
String fileUpload(MultipartFile multipartFile);
}
**FileUploadServiceImpl**
java package com.atguigu.tingshu.album.service.impl;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.file.FileNameUtil; import cn.hutool.core.util.IdUtil; import com.atguigu.tingshu.album.config.MinioConstantProperties; import com.atguigu.tingshu.album.service.FileUploadService; import com.atguigu.tingshu.common.execption.GuiguException; import com.atguigu.tingshu.common.result.ResultCodeEnum; import io.minio.MinioClient; import io.minio.PutObjectArgs; import io.minio.errors.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException;
/**
@create: 2024-08-02 15:40 */ @Service public class FileUploadServiceImpl implements FileUploadService {
@Autowired private MinioClient minioClient;
@Autowired private MinioConstantProperties minioConstantProperties;
/**
@return */ @Override public String fileUpload(MultipartFile multipartFile) { //1.业务校验验证图片内容格式是否合法 try {
BufferedImage bufferedImage = ImageIO.read(multipartFile.getInputStream());
if (bufferedImage == null) {
throw new GuiguException(400, "文件格式有误");
}
//2.业务校验-验证图片大小合法
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if (width > 900 || height > 900) {
throw new GuiguException(400, "文件大小有误!");
}
} catch (Exception e) {
throw new GuiguException(400, "文件格式有误");
} try {
//3.将文件上传到MInIO
//3.1 生成带有文件夹目录文件名称 形式:/日期/随机文件名称.后缀
String folder = "/" + DateUtil.today();
String fileName = IdUtil.randomUUID();
String extName = FileNameUtil.extName(multipartFile.getOriginalFilename());
String objName = folder + "/" + fileName + "." + extName;
//3.2 调用minio客户对象上传文件方法
String bucketName = minioConstantProperties.getBucketName();
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objName).stream(
multipartFile.getInputStream(), multipartFile.getSize(), -1)
.contentType(multipartFile.getContentType())
.build());
//3.3 拼接上传文件在线路径地址
return minioConstantProperties.getEndpointUrl() + "/" + bucketName + objName;
} catch (Exception e) {
throw new GuiguException(500, "文件上传失败");
} } }
## 2.4 保存专辑
设为私密:表示不发布的意思,后续可以通过这个按钮选项实现专辑的上架-下架操作
涉及的表:
- **album_info** 专辑表
- 初始化userId 默认值1 为了后续能查到数据
- 并设置初始化状态为审核通过
- 如果是付费专辑则设置前五集为免费试看
- **album_attribute_value** 专辑属性值表
- 设置专辑Id
- **album_stat** 专辑统计表
- 初始化统计数目为0
### 2.4.1 控制层
> YAPI接口地址:http://192.168.200.6:3000/project/11/interface/api/17
**Tips**:新增的专辑需要将专辑关联到主播用户,但由于还未完成登录功能,故在获取用户工具类`AuthContextHolder`中**getUserId方法中**将获取用户ID的返回值写为固定。

在`service-album`模块中**AlbumInfoApiController**
java
package com.atguigu.tingshu.album.api;
import com.atguigu.tingshu.album.service.AlbumInfoService; import com.atguigu.tingshu.common.result.Result; import com.atguigu.tingshu.common.util.AuthContextHolder; import com.atguigu.tingshu.model.album.AlbumInfo; import com.atguigu.tingshu.vo.album.AlbumInfoVo; import com.tencentcloudapi.cat.v20180409.models.AlarmInfo; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@Tag(name = "专辑管理") @RestController @RequestMapping("api/album") @SuppressWarnings({"all"}) public class AlbumInfoApiController {
@Autowired private AlbumInfoService albumInfoService;
/**
* TODO 该接口登录才可以访问
* 内容创作者或者平台运营人员-保存专辑
*
* @param albumInfoVo 对象中属性需要进行合法验证,采用Validation框架进行校验
* @return
*/
@Operation(summary = "内容创作者或者平台运营人员-保存专辑")
@PostMapping("/albumInfo/saveAlbumInfo")
public Result saveAlbumInfo(@Validated @RequestBody AlbumInfoVo albumInfoVo) {
//1.动态获取用户ID
Long userId = AuthContextHolder.getUserId();
//2.调用业务逻辑完成新增
albumInfoService.saveAlbumInfo(albumInfoVo, userId);
return Result.ok();
}
}
### 2.4.2 业务层
**AlbumInfoService接口**
java package com.atguigu.tingshu.album.service;
import com.atguigu.tingshu.model.album.AlbumInfo; import com.atguigu.tingshu.vo.album.AlbumInfoVo; import com.baomidou.mybatisplus.extension.service.IService;
public interface AlbumInfoService extends IService {
/**
* 内容创作者或者平台运营人员-保存专辑
* @param albumInfoVo
* @param userId
*/
void saveAlbumInfo(AlbumInfoVo albumInfoVo, Long userId);
/**
* 保存专辑统计信息
* @param albumId 专辑ID
* @param statType 统计类型
* @param statNum 统计数值 0401-播放量 0402-订阅量 0403-购买量 0403-评论数'
*/
void saveAlbumInfoStat(Long albumId, String statType, int statNum);
}
**AlbumInfoServiceImpl实现类**
java package com.atguigu.tingshu.album.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollectionUtil; import com.atguigu.tingshu.album.mapper.AlbumAttributeValueMapper; import com.atguigu.tingshu.album.mapper.AlbumInfoMapper; import com.atguigu.tingshu.album.mapper.AlbumStatMapper; import com.atguigu.tingshu.album.service.AlbumInfoService; import com.atguigu.tingshu.common.constant.SystemConstant; import com.atguigu.tingshu.model.album.AlbumAttributeValue; import com.atguigu.tingshu.model.album.AlbumInfo; import com.atguigu.tingshu.model.album.AlbumStat; import com.atguigu.tingshu.model.search.AlbumInfoIndex; import com.atguigu.tingshu.vo.album.AlbumAttributeValueVo; import com.atguigu.tingshu.vo.album.AlbumInfoVo; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Slf4j @Service @SuppressWarnings({"all"}) public class AlbumInfoServiceImpl extends ServiceImpl implements AlbumInfoService {
@Autowired
private AlbumInfoMapper albumInfoMapper;
@Autowired
private AlbumAttributeValueMapper albumAttributeValueMapper;
@Autowired
private AlbumStatMapper albumStatMapper;
/**
* 内容创作者或者平台运营人员-保存专辑
*
* @param albumInfoVo
* @param userId
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void saveAlbumInfo(AlbumInfoVo albumInfoVo, Long userId) {
//1.保存专辑信息
//1.1 将专辑VO转为PO对象
AlbumInfo albumInfo = BeanUtil.copyProperties(albumInfoVo, AlbumInfo.class);
//1.2 为专辑属性赋值:用户ID,试听集数,审核状态
albumInfo.setUserId(userId);
String payType = albumInfo.getPayType();
if (ALBUM_PAY_TYPE_VIPFREE.equals(payType) || ALBUM_PAY_TYPE_REQUIRE.equals(payType)) {
//只需要对VIP免费或付费资源设置试听集
albumInfo.setTracksForFree(3);
}
albumInfo.setStatus(ALBUM_STATUS_NO_PASS);
//1.3 保存专辑,得到专辑ID
albumInfoMapper.insert(albumInfo);
Long albumId = albumInfo.getId();
//2.保存专辑标签关系信息
//2.1 获取VO中提交专辑标签关系集合
List<AlbumAttributeValueVo> albumAttributeValueVoList = albumInfoVo.getAlbumAttributeValueVoList();
if (CollUtil.isNotEmpty(albumAttributeValueVoList)) {
//2.2 为专辑标签关联专辑ID,"批量"新增专辑标签关系
for (AlbumAttributeValueVo albumAttributeValueVo : albumAttributeValueVoList) {
AlbumAttributeValue albumAttributeValue = BeanUtil.copyProperties(albumAttributeValueVo, AlbumAttributeValue.class);
albumAttributeValue.setAlbumId(albumId);
albumAttributeValueMapper.insert(albumAttributeValue);
}
}
//3.初始化专辑统计信息
this.saveAlbumInfoStat(albumId, ALBUM_STAT_PLAY, 0);
this.saveAlbumInfoStat(albumId, ALBUM_STAT_SUBSCRIBE, 0);
this.saveAlbumInfoStat(albumId, ALBUM_STAT_BUY, 0);
this.saveAlbumInfoStat(albumId, ALBUM_STAT_COMMENT, 0);
//4.TODO 对专辑中文本内容进行审核 & 索引库ES中新增记录
}
/**
* 保存专辑统计信息
*
* @param albumId 专辑ID
* @param statType 统计类型
* @param statNum 统计数值 0401-播放量 0402-订阅量 0403-购买量 0403-评论数'
*/
@Override
public void saveAlbumInfoStat(Long albumId, String statType, int statNum) {
AlbumStat albumStat = new AlbumStat();
albumStat.setAlbumId(albumId);
albumStat.setStatType(statType);
albumStat.setStatNum(statNum);
albumStatMapper.insert(albumStat);
}
}
# 3、查看专辑列表
需求:创作者中心-->查询**当前登录用户**发布的专辑列表,每个专辑包含:专辑ID,专辑封面图片、专辑名称、专辑包含声音个数、创建时间、**播放量、购买量、订阅数、评论数量**等 ;根据专辑审核状态及任意关键字进行模糊查询。

> YAPI接口文档:http://192.168.200.6:3000/project/11/interface/api/19
## 3.1 控制层
**AlbumInfoApiController控制器**
查询数据的时候,我们将页面渲染的数据封装到一个实体类中AlbumListVo,只需要返回这个类的集合即可!
java /**
@return */ @Operation(summary = "分页条件查询当前登录用户发布专辑") @PostMapping("/albumInfo/findUserAlbumPage/{page}/{limit}") public Result> findUserAlbumPage(@PathVariable Long page,
@PathVariable Long limit,
@RequestBody AlbumInfoQuery albumInfoQuery) {
//1.获取当前用户登录ID Long userId = AuthContextHolder.getUserId(); albumInfoQuery.setUserId(userId); //2.控制层封装分页参数:页码、页大小 Page pageParam = new Page<>(page, limit); //3.调用业务逻辑层完成分页查询 封装:总记录数,总页数,当前页数据 pageParam = albumInfoService.findUserAlbumPage(pageParam, albumInfoQuery); return Result.ok(pageParam); }
## 3.2 业务层
**AlbumInfoService接口**
java
/**
@return */ Page findUserAlbumPage(Page pageParam, AlbumInfoQuery albumInfoQuery);
**AlbumInfoServiceImpl实现类**
java
/**
@return */ @Override public Page findUserAlbumPage(Page pageParam, AlbumInfoQuery albumInfoQuery) { return albumInfoMapper.findUserAlbumPage(pageParam, albumInfoQuery); }
## 3.3 持久层
SQL演练:
sql
#需求:分页查询指定内容创作者专辑列表 包含专辑信息:id,封面,标题,声音数量 专辑信息信息:四项
#1.采用左外连接专辑表关联统计表查询 关联条件:统计表专辑ID关联专辑表ID 过滤条件:用户ID select * from album_info ai left join album_stat stat on stat.album_id = ai.id where ai.user_id = ?;
#2.目的将四条统计信息封装到专辑列表AlbumListVo中四个统计信息属性中 关键问题:行传列,将四条统计记录转为四列统计信息 select ai.id albumId,
ai.album_title,
ai.cover_url,
ai.include_track_count,
ai.is_finished,
ai.status,
stat.stat_type,
stat.stat_num
from album_info ai
left join album_stat stat
on stat.album_id = ai.id
where ai.user_id = ? and ai.is_deleted = 0;
#3.对专辑ID进行分组 产生新问题:违背严格模式中only_full_group_by规则 要求:select中只能出现函数及分组字段,解决办法 去掉查询统计两列信息(否) select ai.id albumId,
ai.album_title,
ai.cover_url,
ai.include_track_count,
ai.is_finished,
ai.status,
stat.stat_type,
stat.stat_num
from album_info ai
left join album_stat stat
on stat.album_id = ai.id
where ai.user_id = ? and ai.is_deleted = 0 group by ai.id;
#4.使用函数进行传列 使用if函数 select if(1=1,'a','b'); select ai.id albumId,
ai.album_title,
ai.cover_url,
ai.include_track_count,
ai.is_finished,
ai.status,
max(if(stat.stat_type='0401', stat.stat_num, 0)) playStatNum,
max(if(stat.stat_type='0402', stat.stat_num, 0)) subscribeStatNum,
max(if(stat.stat_type='0403', stat.stat_num, 0)) buyStatNum,
max(if(stat.stat_type='0404', stat.stat_num, 0)) commentStatNum
from album_info ai
left join album_stat stat
on stat.album_id = ai.id
where ai.user_id = ? and ai.is_deleted = 0 group by ai.id;
select
max(if(stat_type='0401', stat_num, 0)),
max(if(stat_type='0402', stat_num, 0)),
max(if(stat_type='0403', stat_num, 0)),
max(if(stat_type='0404', stat_num, 0))
from album_stat where album_id = 2;
#5.加入排序,逻辑删除判断 select ai.id albumId,
ai.album_title,
ai.cover_url,
ai.include_track_count,
ai.is_finished,
ai.status,
max(if(stat.stat_type='0401', stat.stat_num, 0)) playStatNum,
max(if(stat.stat_type='0402', stat.stat_num, 0)) subscribeStatNum,
max(if(stat.stat_type='0403', stat.stat_num, 0)) buyStatNum,
max(if(stat.stat_type='0404', stat.stat_num, 0)) commentStatNum
from album_info ai
left join album_stat stat
on stat.album_id = ai.id
where ai.user_id = ? and ai.is_deleted = 0 group by ai.id order by ai.id desc limit 10; #limit start(起始位置偏移量),size(显示记录数) start=(页码-1)*页大小 size=固定页大小
#6.查看执行计划 explain select ai.id albumId,
ai.album_title,
ai.cover_url,
ai.include_track_count,
ai.is_finished,
ai.status,
max(if(stat.stat_type='0401', stat.stat_num, 0)) playStatNum,
max(if(stat.stat_type='0402', stat.stat_num, 0)) subscribeStatNum,
max(if(stat.stat_type='0403', stat.stat_num, 0)) buyStatNum,
max(if(stat.stat_type='0404', stat.stat_num, 0)) commentStatNum
from album_info ai
left join album_stat stat
on stat.album_id = ai.id
where ai.user_id = ? and ai.is_deleted = 0
group by ai.id
order by ai.id desc
limit 10;
**AlbumInfoMapper **
java package com.atguigu.tingshu.album.mapper;
import com.atguigu.tingshu.model.album.AlbumInfo; import com.atguigu.tingshu.query.album.AlbumInfoQuery; import com.atguigu.tingshu.vo.album.AlbumListVo; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param;
@Mapper public interface AlbumInfoMapper extends BaseMapper {
/**
* 页条件查询当前登录用户发布专辑
* @param pageParam MP提供分页对象,自动根据页码、页大小计算SQL limit 部分
* @param albumInfoQuery 查询条件:用户ID,状态,关键字
* @return
*/
Page<AlbumListVo> findUserAlbumPage(Page<AlbumListVo> pageParam, @Param("vo") AlbumInfoQuery albumInfoQuery);
}
**AlbumInfoMapper.xml 实现**
xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<select id="findUserAlbumPage" resultType="com.atguigu.tingshu.vo.album.AlbumListVo">
select
ai.id as album_id,
ai.album_title,
ai.cover_url,
ai.include_track_count,
ai.is_finished,
ai.status,
max(if(stat_type='0401', stat_num, 0)) playStatNum,
max(if(stat_type='0402', stat_num, 0)) subscribeStatNum,
max(if(stat_type='0403', stat_num, 0)) buyStatNum,
max(if(stat_type='0404', stat_num, 0)) commentStatNum
from album_info ai inner join album_stat stat
on stat.album_id = ai.id and stat.is_deleted = 0
<where>
<if test="vo.userId != null">
and ai.user_id = #{vo.userId}
</if>
<if test="vo.status != null and vo.status != ''">
and ai.status = #{vo.status}
</if>
<if test="vo.albumTitle != null and vo.albumTitle != ''">
and ai.album_title like concat('%',#{vo.albumTitle},'%')
</if>
and ai.is_deleted = 0
</where>
group by ai.id
order by ai.id desc
</select>
# 4、删除专辑
在本项目中所有删除都采用逻辑删除,利用MybatisPlus提供逻辑删除。在表中提供逻辑删除字段:is_deleted
在Java实体类中映射逻辑删除字段,属性使用@TableLogic,调用MP提供持久层或者业务层删除方法时候,自动实现逻辑删除(修改操作)

> YAPI接口地址:http://192.168.200.6:3000/project/11/interface/api/21
## 4.1 控制层
**AlbumInfoApiController控制器**
java /**
@return */ @Operation(summary = "根据专辑ID删除专辑") @DeleteMapping("/albumInfo/removeAlbumInfo/{id}") public Result removeAlbumInfo(@PathVariable Long id) { albumInfoService.removeAlbumInfo(id); return Result.ok(); }
## 4.2 业务层
**AlbumInfoService接口**
java
/**
@Autowired private TrackInfoMapper trackInfoMapper;
/**
@return */ @Override @Transactional(rollbackFor = Exception.class) public void removeAlbumInfo(Long id) { //1.根据专辑ID查询声音表判断该专辑下是否关联声音 如果存在 不允许删除 Long count = trackInfoMapper.selectCount(
new LambdaQueryWrapper<TrackInfo>()
.eq(TrackInfo::getAlbumId, id)
); if (count > 0) {
throw new GuiguException(500, "该专辑下存在关联声音");
} //2.删除专辑 albumInfoMapper.deleteById(id);
//3.删除专辑标签关系 albumAttributeValueMapper.delete(
new LambdaQueryWrapper<AlbumAttributeValue>()
.eq(AlbumAttributeValue::getAlbumId, id)
);
//4.删除专辑统计信息 albumStatMapper.delete(
new LambdaQueryWrapper<AlbumStat>()
.eq(AlbumStat::getAlbumId, id)
);
//5.TODO 基于MQ删除存在Elasticsearch(全文搜索引擎)中数据 }
# 5、专辑修改

## 5.1 回显数据
1. 需要根据专辑id获取到对应的回显数据,需要回显专辑与属性数据,不需要回显统计数据!
2. 根据修改内容保存最新数据
> YAPI接口地址:http://192.168.200.6:3000/project/11/interface/api/23
**AlbumInfoApiController控制器**
java
/**
@return 专辑信息 */ @Operation(summary = "根据专辑ID查询专辑信息(包括专辑标签列表)") @GetMapping("/albumInfo/getAlbumInfo/{id}") public Result getAlbumInfo(@PathVariable Long id) { AlbumInfo albumInfo = albumInfoService.getAlbumInfo(id); return Result.ok(albumInfo); }
**AlbumInfoService接口**
java
/**
@return 专辑信息 */ AlbumInfo getAlbumInfo(Long id);
**AlbumInfoServiceImpl实现类**
java
/**
@return */ @Override public AlbumInfo getAlbumInfo(Long id) { //1.根据ID查询专辑 AlbumInfo albumInfo = albumInfoMapper.selectById(id); //2.根据专辑ID查询专辑标签关系列表 if (albumInfo != null) {
List<AlbumAttributeValue> albumAttributeValues = albumAttributeValueMapper
.selectList(
new LambdaQueryWrapper<AlbumAttributeValue>()
.eq(AlbumAttributeValue::getAlbumId, id)
);
albumInfo.setAlbumAttributeValueVoList(albumAttributeValues);
} return albumInfo; }
## 5.2 保存修改后数据
涉及的表:
- album_info 根据主键进行更新
- album_attribute_value 先删除所有数据,再新增数据
> YAPI接口地址:http://192.168.200.6:3000/project/11/interface/api/25
**AlbumInfoApiController控制器**
java
/**
@return */ @Operation(summary = "更新专辑信息") @PutMapping("/albumInfo/updateAlbumInfo/{id}") public Result updateAlbumInfo(@PathVariable Long id, @Validated @RequestBody AlbumInfoVo albumInfoVo) { albumInfoService.updateAlbumInfo(id, albumInfoVo); return Result.ok(); }
**AlbumInfoService接口**
java
/**
@param albumInfoVo */ void updateAlbumInfo(Long id, AlbumInfoVo albumInfoVo);
**AlbumInfoServiceImpl实现类**
java
/**
@param albumInfoVo */ @Override @Transactional(rollbackFor = Exception.class) public void updateAlbumInfo(Long id, AlbumInfoVo albumInfoVo) { //1.更新专辑信息 状态:未审核 AlbumInfo albumInfo = BeanUtil.copyProperties(albumInfoVo, AlbumInfo.class); albumInfo.setId(id); albumInfo.setStatus(ALBUM_STATUS_NO_PASS); albumInfoMapper.updateById(albumInfo);
//2.更新专辑标签关系 //2.1 根据专辑ID删除原有标签关系 albumAttributeValueMapper.delete(
new LambdaQueryWrapper<AlbumAttributeValue>()
.eq(AlbumAttributeValue::getAlbumId, id)
);
//2.2 从VO中获取提交专辑标签关系集合 再次批量保存 List albumAttributeValueVoList = albumInfoVo.getAlbumAttributeValueVoList(); if(CollUtil.isNotEmpty(albumAttributeValueVoList)){
//为专辑标签关联专辑ID,"批量"新增专辑标签关系
for (AlbumAttributeValueVo albumAttributeValueVo : albumAttributeValueVoList) {
AlbumAttributeValue albumAttributeValue = BeanUtil.copyProperties(albumAttributeValueVo, AlbumAttributeValue.class);
albumAttributeValue.setAlbumId(id);
albumAttributeValueMapper.insert(albumAttributeValue);
}
} //3.TODO 再次对内容进行审核 } ```