线上课增加admin_id字段

This commit is contained in:
xxx 2023-11-13 10:08:52 +08:00
parent 179a7d5f62
commit ee1f009966
7 changed files with 69 additions and 3 deletions

View File

@ -15,6 +15,7 @@
*/
package xyz.playedu.api.controller.backend;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
@ -28,9 +29,11 @@ import xyz.playedu.api.event.CourseDestroyEvent;
import xyz.playedu.api.request.backend.CourseRequest;
import xyz.playedu.common.annotation.BackendPermission;
import xyz.playedu.common.annotation.Log;
import xyz.playedu.common.bus.BackendBus;
import xyz.playedu.common.constant.BPermissionConstant;
import xyz.playedu.common.constant.BusinessTypeConstant;
import xyz.playedu.common.context.BCtx;
import xyz.playedu.common.domain.AdminUser;
import xyz.playedu.common.exception.NotFoundException;
import xyz.playedu.common.service.*;
import xyz.playedu.common.types.JsonResponse;
@ -71,8 +74,12 @@ public class CourseController {
@Autowired private DepartmentService departmentService;
@Autowired private AdminUserService adminUserService;
@Autowired private ApplicationContext ctx;
@Autowired private BackendBus backendBus;
@BackendPermission(slug = BPermissionConstant.COURSE)
@GetMapping("/index")
@Log(title = "线上课-列表", businessType = BusinessTypeConstant.GET)
@ -95,6 +102,10 @@ public class CourseController {
filter.setDepIds(depIds);
filter.setIsRequired(isRequired);
if (!backendBus.isSuperAdmin()) {
filter.setAdminId(BCtx.getId());
}
PaginationResult<Course> result = courseService.paginate(page, size, filter);
HashMap<String, Object> data = new HashMap<>();
@ -107,6 +118,17 @@ public class CourseController {
data.put("categories", categoryService.id2name());
data.put("departments", departmentService.id2name());
// 操作人
data.put("admin_users", new HashMap<>());
if (!result.getData().isEmpty()) {
Map<Integer, String> adminUsers =
adminUserService
.chunks(result.getData().stream().map(Course::getAdminId).toList())
.stream()
.collect(Collectors.toMap(AdminUser::getId, AdminUser::getName));
data.put("admin_users", adminUsers);
}
return JsonResponse.data(data);
}
@ -134,7 +156,8 @@ public class CourseController {
req.getIsRequired(),
req.getIsShow(),
req.getCategoryIds(),
req.getDepIds());
req.getDepIds(),
BCtx.getId());
Date now = new Date();
int classHourCount = 0;
@ -240,6 +263,10 @@ public class CourseController {
@Log(title = "线上课-编辑", businessType = BusinessTypeConstant.GET)
public JsonResponse edit(@PathVariable(name = "id") Integer id) throws NotFoundException {
Course course = courseService.findOrFail(id);
if (!backendBus.isSuperAdmin() && !course.getAdminId().equals(BCtx.getId())) {
return JsonResponse.error("无权限操作");
}
List<Integer> depIds = courseService.getDepIdsByCourseId(course.getId());
List<Integer> categoryIds = courseService.getCategoryIdsByCourseId(course.getId());
List<CourseChapter> chapters = chapterService.getChaptersByCourseId(course.getId());
@ -280,6 +307,10 @@ public class CourseController {
@PathVariable(name = "id") Integer id, @RequestBody @Validated CourseRequest req)
throws NotFoundException {
Course course = courseService.findOrFail(id);
if (!backendBus.isSuperAdmin() && !course.getAdminId().equals(BCtx.getId())) {
return JsonResponse.error("无权限操作");
}
courseService.updateWithCategoryIdsAndDepIds(
course,
req.getTitle(),
@ -290,15 +321,24 @@ public class CourseController {
req.getPublishedAt(),
req.getCategoryIds(),
req.getDepIds());
return JsonResponse.success();
}
@BackendPermission(slug = BPermissionConstant.COURSE)
@DeleteMapping("/{id}")
@Log(title = "线上课-删除", businessType = BusinessTypeConstant.DELETE)
@SneakyThrows
public JsonResponse destroy(@PathVariable(name = "id") Integer id) {
Course course = courseService.findOrFail(id);
if (!backendBus.isSuperAdmin() && !course.getAdminId().equals(BCtx.getId())) {
return JsonResponse.error("无权限操作");
}
courseService.removeById(id);
ctx.publishEvent(new CourseDestroyEvent(this, BCtx.getId(), id));
return JsonResponse.success();
}
}

View File

@ -42,4 +42,6 @@ public class CoursePaginateFiler {
private Integer pageStart;
private Integer pageSize;
private Integer adminId;
}

View File

@ -69,6 +69,9 @@ public class Course implements Serializable {
@JsonProperty("created_at")
private Date createdAt;
@JsonProperty("admin_id")
private Integer adminId;
@JsonIgnore private Date updatedAt;
@JsonIgnore private Date deletedAt;

View File

@ -42,7 +42,8 @@ public interface CourseService extends IService<Course> {
Integer isRequired,
Integer isShow,
Integer[] categoryIds,
Integer[] depIds);
Integer[] depIds,
Integer adminId);
void updateWithCategoryIdsAndDepIds(
Course course,

View File

@ -68,7 +68,8 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
Integer isRequired,
Integer isShow,
Integer[] categoryIds,
Integer[] depIds) {
Integer[] depIds,
Integer adminId) {
// 创建课程
Course course = new Course();
course.setTitle(title);
@ -79,6 +80,7 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> impleme
course.setPublishedAt(new Date());
course.setCreatedAt(new Date());
course.setUpdatedAt(new Date());
course.setAdminId(adminId);
save(course);
// 关联分类
relateCategories(course, categoryIds);

View File

@ -75,6 +75,9 @@
<if test="title != null and title != ''">
AND `courses`.`title` LIKE concat('%',#{title},'%')
</if>
<if test="adminId != null">
AND `courses`.`admin_id` = #{adminId}
</if>
<if test="isRequired != null">
AND `courses`.`is_required` = #{isRequired}
</if>
@ -164,6 +167,9 @@
<if test="title != null and title != ''">
AND `courses`.`title` LIKE concat('%',#{title},'%')
</if>
<if test="adminId != null">
AND `courses`.`admin_id` = #{adminId}
</if>
<if test="isRequired != null">
AND `courses`.`is_required` = #{isRequired}
</if>

View File

@ -701,6 +701,18 @@ public class MigrationCheck implements CommandLineRunner {
""");
}
});
add(
new HashMap<>() {
{
put("table", "");
put("name", "20231113_10_00_add_admin_id_for_courses");
put(
"sql",
"""
ALTER TABLE `courses` add `admin_id` int(10) NOT NULL DEFAULT 0 COMMENT '管理员id';
""");
}
});
}
};