课程信息回显
# 一、后端实现
# 1、业务层
接口:CourseService.java
CourseInfoForm getCourseInfoFormById(String id);
1
实现:CourseServiceImpl.java
@Override
public CourseInfoForm getCourseInfoFormById(String id) {
Course course = this.getById(id);
if(course == null){
throw new GuliException(20001, "数据不存在");
}
CourseInfoForm courseInfoForm = new CourseInfoForm();
BeanUtils.copyProperties(course, courseInfoForm);
CourseDescription courseDescription = courseDescriptionService.getById(id);
if(course != null){
courseInfoForm.setDescription(courseDescription.getDescription());
}
return courseInfoForm;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 2、web层
@ApiOperation(value = "根据ID查询课程")
@GetMapping("course-info/{id}")
public R getById(
@ApiParam(name = "id", value = "课程ID", required = true)
@PathVariable String id){
CourseInfoForm courseInfoForm = courseService.getCourseInfoFormById(id);
return R.ok().data("item", courseInfoForm);
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 3、Swagger中测试
# 二、前端实现
# 1、定义api
api/edu/course.js
getCourseInfoById(id) {
return request({
url: `${api_name}/course-info/${id}`,
method: 'get'
})
}
1
2
3
4
5
6
2
3
4
5
6
# 2、组件js
init() {
if (this.$route.params && this.$route.params.id) {
const id = this.$route.params.id
//根据id获取课程基本信息
this.fetchCourseInfoById(id)
}
......
},
fetchCourseInfoById(id) {
course.getCourseInfoById(id).then(response => {
this.courseInfo = response.data.item
}).catch((response) => {
this.$message({
type: 'error',
message: response.message
})
})
},
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 三、解决级联下拉菜单回显问题
# 1、数据库中增加冗余列
subject_parent_id 课程专业父级ID
1
# 2、pojo中增加属性
entity.Course.java
form.CourseInfo.java
@ApiModelProperty(value = "课程专业父级ID")
private String subjectParentId;
1
2
2
# 3、vue组件中绑定数据
edu/course/infoinfo.vue
<el-select v-model="courseInfo.subjectParentId" ......
1
# 4、修改init方法
将 this.initSubjectList() 和 this.initTeacherList()移至else
init() {
if (this.$route.params && this.$route.params.id) {
const id = this.$route.params.id
// 根据id获取课程基本信息
this.fetchCourseInfoById(id)
} else {
this.courseInfo = { ...defaultForm }
// 初始化分类列表
this.initSubjectList()
// 获取讲师列表
this.initTeacherList()
}
},
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 5、修改fetchCourseInfoById方法
fetchCourseInfoById(id) {
course.getCourseInfoById(id).then(responseCourse => {
this.courseInfo = responseCourse.data.item
// 初始化分类列表
subject.getNestedTreeList().then(responseSubject => {
this.subjectNestedList = responseSubject.data.items
for (let i = 0; i < this.subjectNestedList.length; i++) {
if (this.subjectNestedList[i].id === this.courseInfo.subjectParentId) {
this.subSubjectList = this.subjectNestedList[i].children
}
}
})
// 获取讲师列表
this.initTeacherList()
}).catch((response) => {
this.$message({
type: 'error',
message: response.message
})
})
},
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 6、金额的显示
金融系统中关于货币的定义
- 存储精度:4位,根据金额的体量灵活定义,基金类的需要更大的精度
- 运算精度:8位,一般是存储精度的两倍
- 显示精度:2位
common中定义常量类
package com.guli.common.constants;
public class PriceConstants {
public static final int STORE_SCALE = 4; //存储精度
public static final int CAL_SCALE = 8; //运算精度
public static final int DISPLAY_SCALE = 2; //显示精度
public static final BigDecimal ZERO = new BigDecimal("0.0000"); //系统级别的0
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
service中设置精度
@Override
public CourseInfoForm getCourseInfoFormById(String id) {
......
//设置显示精度:舍弃多余的位数
courseInfoForm.setPrice(courseInfoForm.getPrice()
.setScale(PriceConstants.DISPLAY_SCALE, BigDecimal.ROUND_FLOOR));
return courseInfoForm;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
编辑 (opens new window)
上次更新: 2024/06/15, 15:12:25