5 新增课程
5.1 需求分析
5.1.1 业务流程
5.1.2 数据模型
5.2 接口定义
5.3 接口开发
根据需求分析,新增课程表单中包括了课程基本信息、课程营销信息,需要分别向课程基本信息表、课程营销表保证数据。
5.3.1 保存课程基本信息
编写service接口实现类,实现向课程基本信息表保存数据
@Override
public CourseBaseInfoDto creatCourseBase(Long companyID, AddCourseDto addCourseDto) {
//参数的合法性校验
//合法性校验
if (StringUtils.isBlank(dto.getName())) {
throw new RuntimeException("课程名称为空");
}
if (StringUtils.isBlank(dto.getMt())) {
throw new RuntimeException("课程分类为空");
}
if (StringUtils.isBlank(dto.getSt())) {
throw new RuntimeException("课程分类为空");
}
if (StringUtils.isBlank(dto.getGrade())) {
throw new RuntimeException("课程等级为空");
}
if (StringUtils.isBlank(dto.getTeachmode())) {
throw new RuntimeException("教育模式为空");
}
if (StringUtils.isBlank(dto.getUsers())) {
throw new RuntimeException("适应人群为空");
}
if (StringUtils.isBlank(dto.getCharge())) {
//向课程基本信息表course_base写入数据
//向课程基本信息表course_base写入数据
CourseBase courseBaseNew = new CourseBase();
//将传入的页面参数dto放到courseBaseNew这个对象中
// courseBaseNew.setName(dto.getName());
// courseBaseNew.setMt(dto.getMt());
// ......
//上边的从原始对象中get拿数据向新对象set,属性比较多,麻烦
BeanUtils.copyProperties(dto,courseBaseNew); //将前者dto的属性值拷贝给courseBaseNew,拷贝属性名一致的
courseBaseNew.setCompanyId(companyID); //不是dto传来的值
courseBaseNew.setCreateDate(LocalDateTime.now()); //创建时间
//审核状态默认为未提交
courseBaseNew.setAuditStatus("202002"); //查看dictionary表未提交对应的码
//发布状态为未发布
courseBaseNew.setStatus("203001"); //同上
//插入数据库
int insert = courseBaseMapper.insert(courseBaseNew);
if(insert<0){
throw new RuntimeException("添加课程失败");
}
//向课程营销表course_market写入数据
//--to do
return null;
}
5.3.2 保存营销信息
@Override
public CourseBaseInfoDto creatCourseBase(Long companyID, AddCourseDto addCourseDto) {
//--------------
//向课程营销表保存课程营销信息
//课程营销信息
CourseMarket courseMarketNew = new CourseMarket();
Long courseId = courseBaseNew.getId();
BeanUtils.copyProperties(dto,courseMarketNew);
courseMarketNew.setId(courseId);
int i = saveCourseMarket(courseMarketNew); //保存课堂营销信息
if(i<=0){
throw new RuntimeException("保存课堂营销信息失败");
}
//查询课程基本信息及营销信息并返回
return getCourseBaseInfo(courseId);
}
//保存课堂营销信息
private int saveCourseMarket(CourseMarket courseMarketNew){
//收费规则
String charge = courseMarketNew.getCharge();
if(StringUtils.isBlank(charge)){
throw new RuntimeException("收费规则没有选择");
}
//收费规则为收费
if(charge.equals("201001")){
if(courseMarketNew.getPrice()==null || courseMarketNew.getPrice().floatValue()<=0){
throw new RuntimeException("课程收费价格不能为空且必须大于0");
}
}
//根据id从课堂营销查询
CourseMarket courseMarketObj = courseMarketMapper.selectById(courseMarketNew.getId());
if(courseMarketObj == null){
return courseMarketMapper.insert(courseMarketNew); //插入courseMarketNew的数据
}else{
BeanUtils.copyProperties(courseMarketNew,courseMarketObj);
courseMarketObj.setId(courseMarketNew.getId());
return courseMarketMapper.updateById(courseMarketObj);
}
}
//根据课程id查询课程基本信息,包括基本信息和营销信息
private CourseBaseInfoDto getCourseBaseInfo(Long courseId){
CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
CourseBase courseBase = courseBaseMapper.selectById(courseId);
if(courseBase == null){
return null;
}
CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
if(courseMarket !=null){
BeanUtils.copyProperties(courseMarket,courseBaseInfoDto);
}
//查询分类名称并设置
CourseCategory courseCategoryBySt = courseCategoryMapper.selectById(courseBase.getSt());
courseBaseInfoDto.setStName(courseCategoryBySt.getName());
CourseCategory courseCategoryByMt = courseCategoryMapper.selectById(courseBase.getMt());
courseBaseInfoDto.setMtName(courseCategoryByMt.getName());
return courseBaseInfoDto;
}
}
5.4 接口测试
访问:http://localhost:63040/content/swagger-ui.html
或者xc-content-api.http添加下面的内容
### 新增课程
POST {{content_host}}/content/content/course
Content-Type: application/json
{
"charge": "201000",
"description": "冲冲冲",
"grade": "204001",
"mt": "1-1",
"name": "java网络编程高级",
"originalPrice": 0,
"phone": "18888888",
"pic": "暂无",
"price": 0,
"qq": "22333",
"st": "1-1-1",
"tags": "",
"teachmode": "200002",
"users": "初级人员",
"validDays": 365,
"wechat": "223344"
}
2024-05-08T190437.200.json
{
“id”: 125,
“companyId”: 12345678,
“companyName”: null,
“name”: “java网络编程高级”,
“users”: “初级人员”,
“tags”: “”,
“mt”: “1-1”,
“st”: “1-1-1”,
“grade”: “204001”,
“teachmode”: “200002”,
“description”: “冲冲冲”,
“pic”: “暂无”,
“createDate”: “2024-05-08 19:04:37”,
“changeDate”: null,
“createPeople”: null,
“changePeople”: null,
“auditStatus”: “202002”,
“status”: “203001”,
“charge”: “201000”,
“price”: 0.0,
“originalPrice”: 0.0,
“qq”: “22333”,
“wechat”: “223344”,
“phone”: “18888888”,
“validDays”: 365,
“mtName”: “前端开发”,
“stName”: “HTML/CSS”
}