From 015d8df1ac3c00dfc5e8c04e095ec2988cce63f7 Mon Sep 17 00:00:00 2001 From: none Date: Sun, 23 Apr 2023 15:04:49 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=20=E5=AD=A6=E5=91=98=E8=AF=BE=E6=97=B6?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E8=AE=B0=E5=BD=95=E5=88=A0=E9=99=A4=E7=BA=BF?= =?UTF-8?q?=E4=B8=8A=E8=AF=BE=E5=AD=A6=E4=B9=A0=E8=AE=B0=E5=BD=95=E7=9A=84?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/backend/UserController.java | 4 ++ .../UserCourseHourRecordDestroyEvent.java | 47 +++++++++++++++++ .../UserCourseHourRecordDestroyListener.java | 51 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/main/java/xyz/playedu/api/event/UserCourseHourRecordDestroyEvent.java create mode 100644 src/main/java/xyz/playedu/api/listener/UserCourseHourRecordDestroyListener.java diff --git a/src/main/java/xyz/playedu/api/controller/backend/UserController.java b/src/main/java/xyz/playedu/api/controller/backend/UserController.java index 71b7566..111d18d 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/UserController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/UserController.java @@ -33,6 +33,7 @@ import xyz.playedu.api.constant.BPermissionConstant; import xyz.playedu.api.constant.CConfig; import xyz.playedu.api.constant.SystemConstant; import xyz.playedu.api.domain.*; +import xyz.playedu.api.event.UserCourseHourRecordDestroyEvent; import xyz.playedu.api.event.UserDestroyEvent; import xyz.playedu.api.exception.NotFoundException; import xyz.playedu.api.middleware.BackendPermissionMiddleware; @@ -78,6 +79,8 @@ public class UserController { @Autowired private UserLearnDurationStatsService userLearnDurationStatsService; + @Autowired private ApplicationContext ctx; + @BackendPermissionMiddleware(slug = BPermissionConstant.USER_INDEX) @GetMapping("/index") public JsonResponse index(@RequestParam HashMap params) { @@ -524,6 +527,7 @@ public class UserController { @PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "hourId") Integer hourId) { userCourseHourRecordService.remove(id, courseId, hourId); + ctx.publishEvent(new UserCourseHourRecordDestroyEvent(this, id, courseId, hourId)); return JsonResponse.success(); } } diff --git a/src/main/java/xyz/playedu/api/event/UserCourseHourRecordDestroyEvent.java b/src/main/java/xyz/playedu/api/event/UserCourseHourRecordDestroyEvent.java new file mode 100644 index 0000000..29df33b --- /dev/null +++ b/src/main/java/xyz/playedu/api/event/UserCourseHourRecordDestroyEvent.java @@ -0,0 +1,47 @@ +/* + * Copyright 2023 杭州白书科技有限公司 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package xyz.playedu.api.event; + +import lombok.Getter; +import lombok.Setter; + +import org.springframework.context.ApplicationEvent; + +import java.util.Date; + +/** + * @Author 杭州白书科技有限公司 + * + * @create 2023/4/23 14:48 + */ +@Getter +@Setter +public class UserCourseHourRecordDestroyEvent extends ApplicationEvent { + + private Integer userId; + private Integer courseId; + private Integer hourId; + private Date at; + + public UserCourseHourRecordDestroyEvent( + Object source, Integer userId, Integer courseId, Integer hourId) { + super(source); + this.userId = userId; + this.courseId = courseId; + this.hourId = hourId; + this.at = new Date(); + } +} diff --git a/src/main/java/xyz/playedu/api/listener/UserCourseHourRecordDestroyListener.java b/src/main/java/xyz/playedu/api/listener/UserCourseHourRecordDestroyListener.java new file mode 100644 index 0000000..4eba267 --- /dev/null +++ b/src/main/java/xyz/playedu/api/listener/UserCourseHourRecordDestroyListener.java @@ -0,0 +1,51 @@ +/* + * Copyright 2023 杭州白书科技有限公司 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package xyz.playedu.api.listener; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +import xyz.playedu.api.domain.UserCourseRecord; +import xyz.playedu.api.event.UserCourseHourRecordDestroyEvent; +import xyz.playedu.api.service.UserCourseRecordService; + +/** + * @Author 杭州白书科技有限公司 + * + * @create 2023/4/23 14:51 + */ +@Component +@Slf4j +public class UserCourseHourRecordDestroyListener { + + @Autowired private UserCourseRecordService userCourseRecordService; + + @EventListener + public void updateUserCourseRecord(UserCourseHourRecordDestroyEvent e) { + UserCourseRecord record = userCourseRecordService.find(e.getUserId(), e.getCourseId()); + if (record == null) { + return; + } + userCourseRecordService.storeOrUpdate( + record.getUserId(), + record.getCourseId(), + record.getHourCount(), + record.getFinishedCount() - 1); + } +}