package com.tcm.training.admin.system.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tcm.training.admin.common.PageResponse;
import com.tcm.training.admin.system.dto.ActivityNoticeCreateRequest;
import com.tcm.training.admin.system.entity.ActivityNotice;
import com.tcm.training.admin.system.event.ActivityNoticeCreatedEvent;
import com.tcm.training.admin.system.mapper.ActivityNoticeMapper;
import com.tcm.training.admin.system.vo.ActivityNoticeListItemVO;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service
public class ActivityNoticeAppService {
private final ActivityNoticeMapper activityNoticeMapper;
private final ActivityNoticeDispatchService activityNoticeDispatchService;
private final ApplicationEventPublisher applicationEventPublisher;
public ActivityNoticeAppService(ActivityNoticeMapper activityNoticeMapper,
ActivityNoticeDispatchService activityNoticeDispatchService,
ApplicationEventPublisher applicationEventPublisher) {
this.activityNoticeMapper = activityNoticeMapper;
this.activityNoticeDispatchService = activityNoticeDispatchService;
this.applicationEventPublisher = applicationEventPublisher;
}
public PageResponse page(int page, int pageSize, String title) {
LambdaQueryWrapper qw = new LambdaQueryWrapper()
.eq(ActivityNotice::getDeleted, 0)
.like(title != null && !title.trim().isEmpty(), ActivityNotice::getTitle, title == null ? null : title.trim())
.orderByDesc(ActivityNotice::getCreatedAt)
.orderByDesc(ActivityNotice::getId);
IPage result = activityNoticeMapper.selectPage(new Page(page, pageSize), qw);
List list = new ArrayList();
for (ActivityNotice item : result.getRecords()) {
list.add(toListItem(item));
}
return PageResponse.of(list, result.getTotal(), result.getCurrent(), result.getSize());
}
@Transactional
public ActivityNoticeListItemVO create(ActivityNoticeCreateRequest request) {
ActivityNotice item = new ActivityNotice();
item.setTitle(normalizeTitle(request.getTitle()));
item.setContentHtml(normalizeContent(request.getContentHtml()));
item.setDeleted(0);
activityNoticeMapper.insert(item);
applicationEventPublisher.publishEvent(new ActivityNoticeCreatedEvent(item.getId()));
return toListItem(requireItem(item.getId()));
}
@Transactional
public void delete(Long id) {
requireItem(id);
activityNoticeDispatchService.hideDistributedRecords(id);
activityNoticeMapper.deleteById(id);
}
private ActivityNotice requireItem(Long id) {
ActivityNotice item = activityNoticeMapper.selectOne(new LambdaQueryWrapper()
.eq(ActivityNotice::getId, id)
.eq(ActivityNotice::getDeleted, 0)
.last("limit 1"));
if (item == null) {
throw new IllegalArgumentException("活动通知不存在");
}
return item;
}
private ActivityNoticeListItemVO toListItem(ActivityNotice item) {
ActivityNoticeListItemVO vo = new ActivityNoticeListItemVO();
vo.setId(item.getId());
vo.setTitle(item.getTitle());
vo.setCreatedAt(item.getCreatedAt());
return vo;
}
private String normalizeTitle(String title) {
String trimmed = trimToNull(title);
if (trimmed == null) {
throw new IllegalArgumentException("请输入通知标题");
}
return trimmed;
}
private String normalizeContent(String contentHtml) {
String trimmed = trimToNull(contentHtml);
if (trimmed == null || isBlankRichText(trimmed)) {
throw new IllegalArgumentException("请输入通知内容");
}
return trimmed;
}
private boolean isBlankRichText(String html) {
String normalized = html
.replaceAll("(?i)
", "")
.replaceAll("(?i)
", "")
.replace(" ", "")
.replace(" ", "")
.replaceAll("<[^>]+>", "")
.trim();
if (!normalized.isEmpty()) {
return false;
}
String lowerCaseHtml = html.toLowerCase();
return !lowerCaseHtml.contains("![]()