硅谷课堂第十四天-直播管理模块
文章目录
一、后台系统-直播管理
上面我们已经开通了“生活类直播”。
1、获取openId与openToken
登录进入开放后台,后台首页即可获取openId与openToken
2、对接说明
1、使用HTTP协议进行信息交互,字符编码统一采用UTF-8
2、除非特殊说明,接口地址统一为:https://api.talk-fun.com/portal.php
3、除非特殊说明,同时支持GET和POST两种参数传递方式
4、除非特殊说明,返回信息支持JSON格式
5、除了sign外,其余所有请求参数值都需要进行URL编码
6、参数表中,类型一栏声明的定义为:int 代表整数类型;string 代表字符串类型,如果后面有括号,括号中的数字代表该参数的最大长度;array/object表示数组类型
7、openID、openToken参数的获取见对接流程说明
3、了解接口文档
接口文档地址:https://open.talk-fun.com/docs/getstartV2/api/live_dir.html
3.1、了解接口文档
根据接口文档,了解我们需要对接哪些接口
(1)添加直播
api名称:course.add
,SDK对应方法:courseAdd
添加直播是一定需要的
(2)更新直播信息
api名称:course.update
,SDK对应方法courseUpdate
(3)删除直播信息
api名称:course.delete
,SDK对应方法:courseDelete
(4)修改生活直播相关配置
api名称:course.updateLifeConfig
,SDK对应方法:updateLifeConfig
设置功能很多,但是我们只需要几个即可,这个接口我们需要做如下设置:
1、界面模式:pageViewMode 界面模式 1全屏模式 0二分屏 2课件模式
2、观看人数开关:number 观看人数开关;number.enable 是否开启 观看人数 0否 1是;示例:{“enable”:“1”}
3、商城开关(直播推荐课程):goodsListEdit 商品列表编辑,状态goodsListEdit.status 0覆盖,1追加,不传默认为0;示例:{“status”:1};
直播设置最终效果:
(5)按照课程ID获取访客列表
改接口在:"访客/管理员列表"下面
通过该接口统计课程观看人数信息
直播访客api名称:course.visitor.list
,SDK对应方法:courseVisitorList
3.2、下载SDK
直播平台为我们准备了SDK,我们直接使用
下载地址:https://open.talk-fun.com/docs/getstartV2/api/introduce/sdkdownload.html
已下载:当前目录/MTCloud-java-sdk-1.6.zip
5、搭建service_live模块
5.1、创建service_live模块
5.2、添加依赖
添加直播SDK需要的依赖
<!-- 直播 -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
5.3、集成代码
解压MTCloud-java-sdk-1.6.zip,复制MTCloud-java-sdk-1.6\MTCloud_java\src\com\mtcloud\sdk下面的java文件到com.atguigu.ggkt.live.mtcloud包下,如图
5.4、更改配置
更改MTCloud类配置
说明:
1、更改openID与openToken(根据自己的改)
2、该类官方已经做了接口集成,我们可以直接使用。
public class MTCloud {
/**
* 合作方ID: 合作方在欢拓平台的唯一ID
*/
public String openID = "37013";
/**
* 合作方秘钥: 合作方ID对应的参数加密秘钥
*/
public String openToken = "5cfa64c1be5f479aea8296bb4e2c37d3";
...
}
5.5、创建配置文件和启动类
(1)application.properties
# 服务端口
server.port=8306
# 服务名
spring.application.name=service-live
# 环境设置:dev、test、prod
spring.profiles.active=dev
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/glkt_live?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.mapper-locations=classpath:com/atguigu/ggkt/live/mapper/xml/*.xml
# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
mtcloud.openId=43873
mtcloud.openToken=1f3681df876eb31474be8c479b9f1ffe
(2)启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.atguigu")
@ComponentScan(basePackages = "com.atguigu")
@MapperScan("com.atguigu.ggkt.live.mapper")
public class ServiceLiveApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceLiveApplication.class, args);
}
}
5.6、生成相关代码
6、功能实现-直播课程列表接口
根据直播平台与我们自身业务设计直播相关的业务表,如:glkt_live
6.1、LiveCourseController类
@RestController
@RequestMapping(value="/admin/live/liveCourse")
public class LiveCourseController {
@Autowired
private LiveCourseService liveCourseService;
@Autowired
private LiveCourseAccountService liveCourseAccountService;
@ApiOperation(value = "获取分页列表")
@GetMapping("{page}/{limit}")
public Result index(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Long limit) {
Page<LiveCourse> pageParam = new Page<>(page, limit);
IPage<LiveCourse> pageModel = liveCourseService.selectPage(pageParam);
return Result.ok(pageModel);
}
}
6.2、LiveCourseService接口
public interface LiveCourseService extends IService<LiveCourse> {
//直播课程分页查询
IPage<LiveCourse> selectPage(Page<LiveCourse> pageParam);
}
网关中添加配置
#service-live????
#????id
spring.cloud.gateway.routes[5].id=service-live
#?????uri
spring.cloud.gateway.routes[5].uri=lb://service-live
#??????,??servicerId?auth-service?/auth/??
spring.cloud.gateway.routes[5].predicates= Path=/*/live/**
6.3、service_vod模块创建接口
(1)获取讲师信息
//根据id查询 远程调用
@ApiOperation("根据id查询")
@GetMapping("inner/getTeacher/{id}")
public Teacher getTeacherLive(@PathVariable Long id) {
Teacher teacher = teacherService.getById(id);
return teacher;
}
(2)service_course_client定义接口
@GetMapping("/admin/vod/teacher/inner/getTeacher/{id}")
Teacher getTeacherLive(@PathVariable Long id);
6.4、service_live引入依赖
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service_course_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
6.5、LiveCourseServiceImpl实现
@Service
public class LiveCourseServiceImpl extends ServiceImpl<LiveCourseMapper, LiveCourse> implements LiveCourseService {
@Autowired
private CourseFeignClient courseFeignClient;
/**
* 分页查询
* @param pageParam
* @return
*/
@Override
public IPage<LiveCourse> selectPage(Page<LiveCourse> pageParam) {
//分页查询
IPage<LiveCourse> page = baseMapper.selectPage(pageParam, null);
//获取课程讲师信息
List<LiveCourse> liveCourseList = page.getRecords();
//遍历获取直播课程list集合
for(LiveCourse liveCourse : liveCourseList) {
//每个课程讲师id
Long teacherId=liveCourse.getTeacherId();
Teacher teacher = courseFeignClient.getTeacherLive(teacherId);
//进行封装
liveCourse.getParam().put("teacherName", teacher.getName());
liveCourse.getParam().put("teacherLevel", teacher.getLevel());
}
return page;
}
}
LiveCourse
@Data
@ApiModel(description = "LiveCourse")
@TableName("live_course")
public class LiveCourse extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "课程id")
@TableField("course_id")
private Long courseId;
@ApiModelProperty(value = "直播名称")
@TableField("course_name")
private String courseName;
@ApiModelProperty(value = "直播开始时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField("start_time")
private Date startTime;
@ApiModelProperty(value = "直播结束时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField("end_time")
private Date endTime;
@ApiModelProperty(value = "主播老师id")
@TableField("teacher_id")
private Long teacherId;
@ApiModelProperty(value = "课程封面图片路径")
@TableField("cover")
private String cover;
}
7、功能实现-直播课程添加接口
7.1、添加工具类
(1)MTCloudAccountConfig类
@Data
@Component
@ConfigurationProperties(prefix = "mtcloud")
public class MTCloudAccountConfig {
private String openId;
private String openToken;
}
@ConfigurationProperties(prefix = “mtcloud”)
在配置类里找到前缀名为mtcloud的值
mtcloud.openId=43873
mtcloud.openToken=1f3681df876eb31474be8c479b9f1ffe
(2)MTCloudConfig类
@Component
public class MTCloudConfig {
@Autowired
private MTCloudAccountConfig mtCloudAccountConfig;
@Bean
public MTCloud mtCloudClient(){
return new MTCloud(mtCloudAccountConfig.getOpenId(), mtCloudAccountConfig.getOpenToken());
}
}
7.2、LiveCourseController类
/**
* 直播课程添加
* @param liveCourseFormVo
* @return
*/
@ApiOperation(value = "新增")
@PostMapping("save")
public Result save(@RequestBody LiveCourseFormVo liveCourseFormVo) {
liveCourseService.saveLive(liveCourseFormVo);
return Result.ok(null);
}
7.3、LiveCourseService接口
/**
* 直播课程添加
* @param liveCourseFormVo
* @return
*/
void saveLive(LiveCourseFormVo liveCourseFormVo);
7.4、LiveCourseServiceImpl实现
@Resource
private LiveCourseAccountService liveCourseAccountService;
@Resource
private LiveCourseDescriptionService liveCourseDescriptionService;
@Autowired
private CourseFeignClient teacherFeignClient;
@Resource
private MTCloud mtCloudClient;
@SneakyThrows
@Transactional(rollbackFor = {Exception.class})
/**
* 直播课程添加(根据MTCloud写)
* @param liveCourseFormVo
* @return
*/
@Override
public void saveLive(LiveCourseFormVo liveCourseFormVo) throws Exception {
// liveCourseFormVo- liveCourse
LiveCourse liveCourse=new LiveCourse();
BeanUtils.copyProperties(liveCourseFormVo,liveCourse);
//获取讲师信息
Teacher teacher=courseFeignClient.getTeacherLive(liveCourseFormVo.getTeacherId());
//调用方法添加直播课程
//创建map集合,封装直播课程其他参数
HashMap<Object, Object> options = new HashMap<>();
options.put("scenes", 2);//直播类型。1: 教育直播,2: 生活直播。默认 1,说明:根据平台开通的直播类型填写
options.put("password", liveCourseFormVo.getPassword());
//course_name 课程名称
//account 发起直播课程的主播账号
//start_time 课程开始时间,格式: 2015-01-10 12:00:00
// end_time 课程结束时间,格式: 2015-01-10 13:00:00
//nicekname 昵称
//accountIntro 主播介绍
//options 其他参数
String res=mtCloudClient.courseAdd(liveCourse.getCourseName(),
teacher.getId().toString(),
new DateTime(liveCourse.getStartTime()).toString("yyyy-MM-dd HH:mm:ss"),
new DateTime(liveCourse.getEndTime()).toString("yyyy-MM-dd HH:mm:ss"),
teacher.getName(), teacher.getIntro(), options);
System.out.println("return:: "+res);
//把创建之后返回结果判断
CommonResult<JSONObject> commonResult = JSON.parseObject(res, CommonResult.class);
if(Integer.parseInt(commonResult.getCode()) == MTCloud.CODE_SUCCESS) {//成功
//添加直播基本信息
JSONObject object = commonResult.getData();
liveCourse.setCourseId(object.getLong("course_id"));
baseMapper.insert(liveCourse);
//保存课程详情信息
LiveCourseDescription liveCourseDescription = new LiveCourseDescription();
liveCourseDescription.setDescription(liveCourseFormVo.getDescription());
liveCourseDescription.setLiveCourseId(liveCourse.getId());
liveCourseDescriptionService.save(liveCourseDescription);
//保存课程账号信息
LiveCourseAccount liveCourseAccount = new LiveCourseAccount();
liveCourseAccount.setLiveCourseId(liveCourse.getId());
liveCourseAccount.setZhuboAccount(object.getString("bid"));
liveCourseAccount.setZhuboPassword(liveCourseFormVo.getPassword());
liveCourseAccount.setAdminKey(object.getString("admin_key"));
liveCourseAccount.setUserKey(object.getString("user_key"));
liveCourseAccount.setZhuboKey(object.getString("zhubo_key"));
liveCourseAccountService.save(liveCourseAccount);
} else {
String getmsg = commonResult.getmsg();
throw new GgktException(20001,getmsg);
}
}
8、功能实现-直播课程删除接口
8.1、LiveCourseController类
@ApiOperation(value = "删除")
@DeleteMapping("remove/{id}")
public Result remove(@PathVariable Long id) {
liveCourseService.removeLive(id);
return Result.ok(null);
}
8.2、LiveCourseService接口
//删除直播课程
void removeLive(Long id);
8.3、LiveCourseServiceImpl实现
/**
* 删除直播(根据MTCLoud写)
* @param id
* @return
*/
@Override
public void removeLive(Long id) {
LiveCourse liveCourse=baseMapper.selectById(id);
if(liveCourse!=null){
//获取直播couseid
Long courseId=liveCourse.getCourseId();
try {
//调用方法删除平台直播课程
mtCloudClient.courseDelete(courseId.toString());
//删除表数据
baseMapper.deleteById(id);
} catch (Exception e) {
e.printStackTrace();
throw new GgktException(20001,"删除直播课程失败");
}
}
}
9、功能实现-直播课程修改接口
9.1、LiveCourseController类
//id查询直播课程基本信息
@ApiOperation(value = "获取")
@GetMapping("get/{id}")
public Result<LiveCourse> get(@PathVariable Long id) {
LiveCourse liveCourse = liveCourseService.getById(id);
return Result.ok(liveCourse);
}
//id查询直播课程基本信息和描述信息
@ApiOperation(value = "获取")
@GetMapping("getInfo/{id}")
public Result<LiveCourseFormVo> getInfo(@PathVariable Long id) {
LiveCourseFormVo liveCourseFormVo=liveCourseService.getLiveCourseFormVo(id);
return Result.ok(liveCourseFormVo);
}
//更新直播课程方法
@ApiOperation(value = "修改")
@PutMapping("update")
public Result updateById(@RequestBody LiveCourseFormVo liveCourseFormVo) {
liveCourseService.updateById(liveCourseFormVo);
return Result.ok(null);
}
LiveCourseFormVo
@Data
public class LiveCourseFormVo {
@ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "直播名称")
private String courseName;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "直播开始时间")
private Date startTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "直播结束时间")
private Date endTime;
@ApiModelProperty(value = "主播老师id")
private Long teacherId;
@ApiModelProperty(value = "主播密码")
private String password;
@ApiModelProperty(value = "课程简介")
private String description;
@ApiModelProperty(value = "课程封面图片路径")
private String cover;
}
9.2、LiveCourseService接口
//更新直播课程方法
void updateById(LiveCourseFormVo liveCourseFormVo);
//id查询直播课程基本信息和描述信息
LiveCourseFormVo getLiveCourseFormVo(Long id);
9.3、LiveCourseServiceImpl实现
@Resource
private LiveCourseAccountService liveCourseAccountService;
@Resource
private LiveCourseDescriptionService liveCourseDescriptionService;
@Autowired
private CourseFeignClient teacherFeignClient;
@Resource
private MTCloud mtCloudClient;
//更新直播课程方法(根据MTCloud写)
@Override
public void updateById(LiveCourseFormVo liveCourseFormVo) {
//根据id获取直播课程基本信息
LiveCourse liveCourse = baseMapper.selectById(liveCourseFormVo.getId());
BeanUtils.copyProperties(liveCourseFormVo,liveCourse);
//讲师
Teacher teacher =
courseFeignClient.getTeacherLive(liveCourseFormVo.getTeacherId());
// * course_id 课程ID
// * account 发起直播课程的主播账号
// * course_name 课程名称
// * start_time 课程开始时间,格式:2015-01-01 12:00:00
// * end_time 课程结束时间,格式:2015-01-01 13:00:00
// * nickname 主播的昵称
// * accountIntro 主播的简介
// * options 可选参数
HashMap<Object, Object> options = new HashMap<>();
try {
String res = mtCloudClient.courseUpdate(liveCourse.getCourseId().toString(),
teacher.getId().toString(),
liveCourse.getCourseName(),
new DateTime(liveCourse.getStartTime()).toString("yyyy-MM-dd HH:mm:ss"),
new DateTime(liveCourse.getEndTime()).toString("yyyy-MM-dd HH:mm:ss"),
teacher.getName(),
teacher.getIntro(),
options);
//返回结果转换,判断是否成功
CommonResult<JSONObject> commonResult = JSON.parseObject(res, CommonResult.class);
if(Integer.parseInt(commonResult.getCode()) == MTCloud.CODE_SUCCESS) {
JSONObject object = commonResult.getData();
//更新直播课程基本信息
liveCourse.setCourseId(object.getLong("course_id"));
baseMapper.updateById(liveCourse);
//直播课程描述信息更新
LiveCourseDescription liveCourseDescription =
liveCourseDescriptionService.getLiveCourseById(liveCourse.getId());
liveCourseDescription.setDescription(liveCourseFormVo.getDescription());
liveCourseDescriptionService.updateById(liveCourseDescription);
} else {
throw new GgktException(20001,"修改直播课程失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
//id查询直播课程基本信息和描述信息
@Override
public LiveCourseFormVo getLiveCourseFormVo(Long id) {
//获取直播课程基本信息
LiveCourse liveCourse=baseMapper.selectById(id);
//获取直播课程描述信息(自己创建的查询方法)
LiveCourseDescription liveCourseDescription=liveCourseDescriptionService.getLiveCourseById(id);
//封装
LiveCourseFormVo liveCourseFormVo = new LiveCourseFormVo();
BeanUtils.copyProperties(liveCourse, liveCourseFormVo);
liveCourseFormVo.setDescription(liveCourseDescription.getDescription());
return liveCourseFormVo;
}
9.4、LiveCourseDescriptionService添加方法
public interface LiveCourseDescriptionService extends IService<LiveCourseDescription> {
//获取直播课程描述信息
LiveCourseDescription getLiveCourseById(Long id);
}
9.5、LiveCourseDescriptionServiceImpl实现方法
@Service
public class LiveCourseDescriptionServiceImpl extends ServiceImpl<LiveCourseDescriptionMapper, LiveCourseDescription> implements LiveCourseDescriptionService {
//获取直播课程描述信息
@Override
public LiveCourseDescription getLiveCourseById(Long id) {
LambdaQueryWrapper<LiveCourseDescription> wrapper=new LambdaQueryWrapper<>();
wrapper.eq(LiveCourseDescription::getLiveCourseId,id);
LiveCourseDescription liveCourseDescription=baseMapper.selectOne(wrapper);
return liveCourseDescription;
}
}
wrapper.eq(LiveCourseDescription::getLiveCourseId,id);
第一个参数为获取直播课程id,第二个参数为传过来的id
10、功能实现-查看账号接口
10.1、LiveCourseController类
@Autowired
private LiveCourseAccountService liveCourseAccountService;
/**
* 获取直播账号信息
* @param id
* @return
*/
@ApiOperation(value = "获取直播账号信息")
@GetMapping("getLiveCourseAccount/{id}")
public Result<LiveCourseAccount> getLiveCourseAccount(@PathVariable Long id) {
LiveCourseAccount liveCourseAccount=liveCourseAccountService.getByLiveCourseId(id);
return Result.ok(liveCourseAccount);
}
}
LiveCourseAccount:
@Data
@ApiModel(description = "LiveCourseAccount")
@TableName("live_course_account")
public class LiveCourseAccount extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "直播课程id")
@TableField("live_course_id")
private Long liveCourseId;
@ApiModelProperty(value = "主播登录账号")
@TableField("zhubo_account")
private String zhuboAccount;
@ApiModelProperty(value = "主播登录密码")
@TableField("zhubo_password")
private String zhuboPassword;
@ApiModelProperty(value = "主播登录秘钥")
@TableField("zhubo_key")
private String zhuboKey;
@ApiModelProperty(value = "助教登录秘钥")
@TableField("admin_key")
private String adminKey;
@ApiModelProperty(value = "学生登录秘钥")
@TableField("user_key")
private String userKey;
}
10.2、LiveCourseAccountService接口
public interface LiveCourseAccountService extends IService<LiveCourseAccount> {
/**
* 获取直播账号信息
* @param id
* @return
*/
LiveCourseAccount getByLiveCourseId(Long id);
}
10.3、LiveCourseAccountServiceImpl实现
@Service
public class LiveCourseAccountServiceImpl extends ServiceImpl<LiveCourseAccountMapper, LiveCourseAccount> implements LiveCourseAccountService {
@Override
public LiveCourseAccount getByLiveCourseId(Long liveCourseId) {
LambdaQueryWrapper<LiveCourseAccount>wrapper=new LambdaQueryWrapper<>();
wrapper.eq(LiveCourseAccount::getLiveCourseId,id);
LiveCourseAccount liveCourseAccount=baseMapper.selectById(wrapper);
return liveCourseAccount;
}
}
11、功能实现-配置和观看记录接口
11.1、查看配置信息
(1)LiveCourseController类
/**
* 获取直播配置信息
* @param id
* @return
*/
@ApiOperation(value = "获取直播配置信息")
@GetMapping("getCourseConfig/{id}")
public Result getCourseConfig(@PathVariable Long id) {
LiveCourseConfigVo liveCourseConfigVo=liveCourseService.getCourseConfig(id);
return Result.ok(liveCourseConfigVo);
}
@Data
@ApiModel(description = "LiveCourseConfig")
public class LiveCourseConfigVo extends LiveCourseConfig {
@ApiModelProperty(value = "商品列表")
private List<LiveCourseGoods> liveCourseGoodsList;
}
@Data
@ApiModel(description = "LiveCourseConfig")
@TableName("live_course_config")
public class LiveCourseConfig extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "直播课程id")
@TableField("live_course_id")
private Long liveCourseId;
@ApiModelProperty(value = "界面模式 1全屏模式 0二分屏 2课件模式")
@TableField("page_view_mode")
private Integer pageViewMode;
@ApiModelProperty(value = "是否开启 观看人数 0否 1是")
@TableField("number_enable")
private Integer numberEnable;
@ApiModelProperty(value = "商城是否开启 0未开启 1开启")
@TableField("store_enable")
private Integer storeEnable;
@ApiModelProperty(value = "1商品列表,2商城链接,3商城二维码")
@TableField("store_type")
private Integer storeType;
}
(2)LiveCourseService添加方法
//获取配置
LiveCourseConfigVo getCourseConfig(Long id);
(3)LiveCourseServiceImpl实现
@Autowired
private LiveCourseConfigService liveCourseConfigService;
@Autowired
private LiveCourseGoodsService liveCourseGoodsService;
/**
* 获取直播配置信息(查配置表,直播商品表)
* @param id
* @return
*/
@Override
public LiveCourseConfigVo getCourseConfig(Long id) {
//根据课程id查询配置信息
LiveCourseConfig liveCourseConfig = liveCourseConfigService.getByLiveCourseId(id);
//封装 LiveCourseConfigVo
LiveCourseConfigVo liveCourseConfigVo = new LiveCourseConfigVo();
if(null != liveCourseConfig) {
//查询直播课程商品列表
List<LiveCourseGoods> liveCourseGoodsList = liveCourseGoodsService.findByLiveCourseId(id);
//封装到LiveCourseConfigVo
BeanUtils.copyProperties(liveCourseConfig, liveCourseConfigVo);
//封装商品列表
liveCourseConfigVo.setLiveCourseGoodsList(liveCourseGoodsList);
}
return liveCourseConfigVo;
}
@Data
@ApiModel(description = "LiveCourseGoods")
@TableName("live_course_goods")
public class LiveCourseGoods extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "直播课程id")
@TableField("live_course_id")
private Long liveCourseId;
@ApiModelProperty(value = "推荐点播课程id")
@TableField("goods_id")
private Long goodsId;
@ApiModelProperty(value = "商品名称")
@TableField("name")
private String name;
@ApiModelProperty(value = "图片")
@TableField("img")
private String img;
@ApiModelProperty(value = "商品现价")
@TableField("price")
private String price;
@ApiModelProperty(value = "商品原价")
@TableField("originalPrice")
private String originalPrice;
@ApiModelProperty(value = "商品标签")
@TableField("tab")
private Integer tab;
@ApiModelProperty(value = "商品链接")
@TableField("url")
private String url;
@ApiModelProperty(value = "商品状态:0下架,1上架,2推荐")
@TableField("putaway")
private String putaway;
@ApiModelProperty(value = "购买模式(1,链接购买 2,二维码购买)")
@TableField("pay")
private Integer pay;
@ApiModelProperty(value = "商品二维码")
@TableField("qrcode")
private String qrcode;
}
(4)LiveCourseConfigService添加方法
public interface LiveCourseConfigService extends IService<LiveCourseConfig> {
//根据课程id查询配置信息
LiveCourseConfig getByLiveCourseId(Long liveCourseId);
}
(5)LiveCourseConfigServiceImpl实现方法
@Service
public class LiveCourseConfigServiceImpl extends ServiceImpl<LiveCourseConfigMapper, LiveCourseConfig> implements LiveCourseConfigService {
//查看配置信息
@Override
public LiveCourseConfig getByLiveCourseId(Long liveCourseId) {
return baseMapper.selectOne(new LambdaQueryWrapper<LiveCourseConfig>().eq(
LiveCourseConfig::getLiveCourseId,
liveCourseId));
}
}
(6)LiveCourseGoodsService添加方法
public interface LiveCourseGoodsService extends IService<LiveCourseGoods> {
//获取课程商品列表
List<LiveCourseGoods> findByLiveCourseId(Long liveCourseId);
}
(7)LiveCourseGoodsServiceImpl实现方法
@Service
public class LiveCourseGoodsServiceImpl extends ServiceImpl<LiveCourseGoodsMapper, LiveCourseGoods> implements LiveCourseGoodsService {
//获取课程商品列表
@Override
public List<LiveCourseGoods> findByLiveCourseId(Long liveCourseId) {
return baseMapper.selectList(new LambdaQueryWrapper<LiveCourseGoods>()
.eq(LiveCourseGoods::getLiveCourseId, liveCourseId));
}
}
11.2、修改直播配置信息
(1)LiveCourseController添加方法
@ApiOperation(value = "修改配置")
@PutMapping("updateConfig")
public Result updateConfig(@RequestBody LiveCourseConfigVo liveCourseConfigVo) {
liveCourseService.updateConfig(liveCourseConfigVo);
return Result.ok(null);
}
(2)LiveCourseService添加方法
//修改配置
void updateConfig(LiveCourseConfigVo liveCourseConfigVo);
(3)LiveCourseServiceImpl实现方法
/**
* 修改配置
* @param liveCourseConfigVo
*/
@Override
public void updateConfig(LiveCourseConfigVo liveCourseConfigVo) throws Exception {
//1 修改直播配置表
LiveCourseConfig liveCourseConfig=new LiveCourseConfig();
BeanUtils.copyProperties(liveCourseConfigVo,liveCourseConfig);;
if(null == liveCourseConfigVo.getId()) {
liveCourseConfigService.save(liveCourseConfig);
} else {
liveCourseConfigService.updateById(liveCourseConfig);
}
//2修改直播商品表
//根据课程id删除直播商品列表
LambdaQueryWrapper<LiveCourseGoods> wrapper=new LambdaQueryWrapper<>();
wrapper.eq(LiveCourseGoods::getLiveCourseId, liveCourseConfigVo.getLiveCourseId());
liveCourseGoodsService.remove(wrapper);
//添加商品列表
if(!CollectionUtils.isEmpty(liveCourseConfigVo.getLiveCourseGoodsList())) {
liveCourseGoodsService.saveBatch(liveCourseConfigVo.getLiveCourseGoodsList());
}
//3修改在直播平台
this.updateLifeConfig(liveCourseConfigVo);
}
//修改在直播平台(上传直播配置)
private void updateLifeConfig(LiveCourseConfigVo liveCourseConfigVo) throws Exception {
LiveCourse liveCourse=baseMapper.selectById(liveCourseConfigVo.getLiveCourseId());
//封装平台方法需要参数
//参数设置
HashMap<Object,Object> options = new HashMap<Object, Object>();
//界面模式
options.put("pageViewMode", liveCourseConfigVo.getPageViewMode());
//观看人数开关
JSONObject number = new JSONObject();
number.put("enable", liveCourseConfigVo.getNumberEnable());
options.put("number", number.toJSONString());
//观看人数开关
JSONObject store = new JSONObject();
number.put("enable", liveCourseConfigVo.getStoreEnable());
number.put("type", liveCourseConfigVo.getStoreType());
options.put("store", number.toJSONString());
//商城列表
List<LiveCourseGoods> liveCourseGoodsList = liveCourseConfigVo.getLiveCourseGoodsList();
if(!CollectionUtils.isEmpty(liveCourseGoodsList)) {
List<LiveCourseGoodsView> liveCourseGoodsViewList = new ArrayList<>();
for(LiveCourseGoods liveCourseGoods : liveCourseGoodsList) {
LiveCourseGoodsView liveCourseGoodsView = new LiveCourseGoodsView();
BeanUtils.copyProperties(liveCourseGoods, liveCourseGoodsView);
liveCourseGoodsViewList.add(liveCourseGoodsView);
}
JSONObject goodsListEdit = new JSONObject();
goodsListEdit.put("status", "0");
options.put("goodsListEdit ", goodsListEdit.toJSONString());
options.put("goodsList", JSON.toJSONString(liveCourseGoodsViewList));
}
String res = mtCloudClient.courseUpdateLifeConfig(liveCourse.getCourseId().toString(), options);
CommonResult<JSONObject> commonResult = JSON.parseObject(res, CommonResult.class);
if(Integer.parseInt(commonResult.getCode()) != MTCloud.CODE_SUCCESS) {
throw new GgktException(20001,"修改配置信息失败");
}
}
saveBatch:批量插入方法
@Data
@ApiModel(description = "LiveCourseGoods")
public class LiveCourseGoodsView {
@ApiModelProperty(value = "商品名称")
private String name;
@ApiModelProperty(value = "图片")
private String img;
@ApiModelProperty(value = "商品现价")
private String price;
@ApiModelProperty(value = "商品原价")
private String originalPrice;
@ApiModelProperty(value = "商品标签")
private String tab;
@ApiModelProperty(value = "商品链接")
private String url;
@ApiModelProperty(value = "商品状态:0下架,1上架,2推荐")
private String putaway;
@ApiModelProperty(value = "购买模式(1,链接购买 2,二维码购买)")
private String pay;
@ApiModelProperty(value = "商品二维码")
private String qrcode = "";
}
11.3、获取最近直播课程
(1)LiveCourseController添加方法
/**
* 获取最近直播课程
* @return
*/
@ApiOperation(value = "获取最近的直播")
@GetMapping("findLatelyList")
public Result findLatelyList() {
List<LiveCourseVo> list=liveCourseService.getLatelyList();
return Result.ok(list);
}
@Data
public class LiveCourseVo extends LiveCourse {
@ApiModelProperty(value = "主播老师")
private Teacher teacher;
private Integer liveStatus;
@ApiModelProperty(value = "直播开始时间")
private String startTimeString;
@ApiModelProperty(value = "直播结束时间")
private String endTimeString;
}
(2)LiveCourseService添加方法
//获取最近的直播
List<LiveCourseVo> findLatelyList();
(3)LiveCourseServiceImpl实现方法
/**
* 获取最近直播课程
* @return
*/
@Override
public List<LiveCourseVo> getLatelyList() {
//获取最近直播课程
List<LiveCourseVo>liveCourseVoList=baseMapper.getLatelyList();
for(LiveCourseVo liveCourseVo : liveCourseVoList) {
//封装开始和结束时间
liveCourseVo.setStartTimeString(new DateTime(liveCourseVo.getStartTime()).toString("yyyy年MM月dd HH:mm"));
liveCourseVo.setEndTimeString(new DateTime(liveCourseVo.getEndTime()).toString("HH:mm"));
//封装讲师
Long teacherId = liveCourseVo.getTeacherId();
Teacher teacher = courseFeignClient.getTeacherLive(teacherId);
liveCourseVo.setTeacher(teacher);
//封装直播状态
liveCourseVo.setLiveStatus(this.getLiveStatus(liveCourseVo));
}
return liveCourseVoList;
}
/**
* 直播状态 0:未开始 1:直播中 2:直播结束
* @param liveCourse
* @return
*/
private int getLiveStatus(LiveCourse liveCourse) {
// 直播状态 0:未开始 1:直播中 2:直播结束
int liveStatus = 0;
Date curTime = new Date();
if(DateUtil.dateCompare(curTime, liveCourse.getStartTime())) {
liveStatus = 0;
} else if(DateUtil.dateCompare(curTime, liveCourse.getEndTime())) {
liveStatus = 1;
} else {
liveStatus = 2;
}
return liveStatus;
}
(4)LiveCourseMapper添加方法
public interface LiveCourseMapper extends BaseMapper<LiveCourse> {
//获取最近直播
List<LiveCourseVo> findLatelyList();
}
(5)LiveCourseMapper.xml编写sql语句
<?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.ggkt.live.mapper.LiveCourseMapper">
<resultMap id="liveCourseMap" type="com.atguigu.ggkt.vo.live.LiveCourseVo" autoMapping="true">
</resultMap>
<!-- 用于select查询公用抽取的列 -->
<sql id="columns">
id,course_id,course_name,start_time,end_time,teacher_id,cover,create_time,update_time,is_deleted
</sql>
<select id="findLatelyList" resultMap="liveCourseMap">
select <include refid="columns" />
from live_course
where date(start_time) >= curdate()
order by id asc
limit 5
</select>
</mapper>