hejianhao
2026-09-01 f4e8dfd7f8f11966f4b5dc8c6661cbc3feccc4cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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<ActivityNoticeListItemVO> page(int page, int pageSize, String title) {
        LambdaQueryWrapper<ActivityNotice> qw = new LambdaQueryWrapper<ActivityNotice>()
                .eq(ActivityNotice::getDeleted, 0)
                .like(title != null && !title.trim().isEmpty(), ActivityNotice::getTitle, title == null ? null : title.trim())
                .orderByDesc(ActivityNotice::getCreatedAt)
                .orderByDesc(ActivityNotice::getId);
        IPage<ActivityNotice> result = activityNoticeMapper.selectPage(new Page<ActivityNotice>(page, pageSize), qw);
        List<ActivityNoticeListItemVO> list = new ArrayList<ActivityNoticeListItemVO>();
        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<ActivityNotice>()
                .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)<br\\s*/?>", "")
                .replaceAll("(?i)</p>", "")
                .replace("&nbsp;", "")
                .replace("&#160;", "")
                .replaceAll("<[^>]+>", "")
                .trim();
        if (!normalized.isEmpty()) {
            return false;
        }
        String lowerCaseHtml = html.toLowerCase();
        return !lowerCaseHtml.contains("<img")
                && !lowerCaseHtml.contains("<video")
                && !lowerCaseHtml.contains("<audio")
                && !lowerCaseHtml.contains("<iframe");
    }
 
    private String trimToNull(String value) {
        if (value == null) {
            return null;
        }
        String trimmed = value.trim();
        return trimmed.isEmpty() ? null : trimmed;
    }
}