| | |
| | | import com.tcm.training.admin.system.event.ActivityNoticeCreatedEvent; |
| | | import com.tcm.training.admin.system.entity.ActivityNotice; |
| | | import com.tcm.training.admin.system.mapper.ActivityNoticeMapper; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.event.TransactionalEventListener; |
| | |
| | | */ |
| | | @Service |
| | | public class ActivityNoticeDispatchService { |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(ActivityNoticeDispatchService.class); |
| | | |
| | | private static final int USER_STATUS_ENABLED = 1; |
| | | private static final int USER_DELETED_NO = 0; |
| | |
| | | break; |
| | | } |
| | | for (AppUser user : users) { |
| | | if (user.getId() == null) { |
| | | continue; |
| | | } |
| | | boolean exists = appUserActivityNoticeMapper.selectCount(new LambdaQueryWrapper<AppUserActivityNotice>() |
| | | .eq(AppUserActivityNotice::getAppUserId, user.getId()) |
| | | .eq(AppUserActivityNotice::getActivityNoticeId, event.getActivityNoticeId()) |
| | | .eq(AppUserActivityNotice::getDeleted, RECORD_DELETED_NO)) > 0; |
| | | if (exists) { |
| | | continue; |
| | | } |
| | | AppUserActivityNotice record = new AppUserActivityNotice(); |
| | | record.setAppUserId(user.getId()); |
| | | record.setActivityNoticeId(event.getActivityNoticeId()); |
| | | record.setDeleted(RECORD_DELETED_NO); |
| | | appUserActivityNoticeMapper.insert(record); |
| | | // 活动通知发布后同时写入统一系统通知表,并按双开关规则尝试极光推送。 |
| | | appSystemNoticeDispatchService.createAndPush(buildActivityNoticeCommand(user.getId(), activityNotice)); |
| | | distributeToUser(user, activityNotice); |
| | | } |
| | | if (page.getCurrent() >= page.getPages()) { |
| | | break; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 为单个 APP 用户分发活动通知,单用户失败不影响整批分发。 |
| | | */ |
| | | private void distributeToUser(AppUser user, ActivityNotice activityNotice) { |
| | | if (user == null || user.getId() == null || activityNotice == null || activityNotice.getId() == null) { |
| | | return; |
| | | } |
| | | try { |
| | | boolean exists = appUserActivityNoticeMapper.selectCount(new LambdaQueryWrapper<AppUserActivityNotice>() |
| | | .eq(AppUserActivityNotice::getAppUserId, user.getId()) |
| | | .eq(AppUserActivityNotice::getActivityNoticeId, activityNotice.getId()) |
| | | .eq(AppUserActivityNotice::getDeleted, RECORD_DELETED_NO)) > 0; |
| | | if (!exists) { |
| | | AppUserActivityNotice record = new AppUserActivityNotice(); |
| | | record.setAppUserId(user.getId()); |
| | | record.setActivityNoticeId(activityNotice.getId()); |
| | | record.setDeleted(RECORD_DELETED_NO); |
| | | appUserActivityNoticeMapper.insert(record); |
| | | } |
| | | // 活动通知发布后同时写入统一系统通知表,并按双开关规则尝试极光推送。 |
| | | appSystemNoticeDispatchService.createAndPush(buildActivityNoticeCommand(user.getId(), activityNotice)); |
| | | } catch (Exception ex) { |
| | | log.error("activity notice distribute failed, activityNoticeId={}, appUserId={}", |
| | | activityNotice.getId(), user.getId(), ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 构建活动通知统一发放命令。 |
| | | */ |
| | | private AppSystemNoticeDispatchCommand buildActivityNoticeCommand(Long appUserId, ActivityNotice activityNotice) { |