hejianhao
2026-08-28 f208fd1df2629384485df6c88aec08a275f7fd12
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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.CommonQuestionSaveRequest;
import com.tcm.training.admin.system.entity.CommonQuestion;
import com.tcm.training.admin.system.mapper.CommonQuestionMapper;
import com.tcm.training.admin.system.vo.CommonQuestionDetailVO;
import com.tcm.training.admin.system.vo.CommonQuestionListItemVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
public class CommonQuestionAppService {
 
    private final CommonQuestionMapper commonQuestionMapper;
 
    public CommonQuestionAppService(CommonQuestionMapper commonQuestionMapper) {
        this.commonQuestionMapper = commonQuestionMapper;
    }
 
    public PageResponse<CommonQuestionListItemVO> page(int page, int pageSize, String title) {
        LambdaQueryWrapper<CommonQuestion> queryWrapper = new LambdaQueryWrapper<CommonQuestion>()
                .eq(CommonQuestion::getDeleted, 0)
                .like(title != null && !title.trim().isEmpty(), CommonQuestion::getTitle, title == null ? null : title.trim())
                .orderByDesc(CommonQuestion::getCreatedAt)
                .orderByDesc(CommonQuestion::getId);
        IPage<CommonQuestion> result = commonQuestionMapper.selectPage(new Page<CommonQuestion>(page, pageSize), queryWrapper);
        List<CommonQuestionListItemVO> list = new ArrayList<CommonQuestionListItemVO>();
        for (CommonQuestion item : result.getRecords()) {
            list.add(toListItem(item));
        }
        return PageResponse.of(list, result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    public CommonQuestionDetailVO detail(Long id) {
        return toDetailVO(requireItem(id));
    }
 
    @Transactional
    public CommonQuestionDetailVO create(CommonQuestionSaveRequest request) {
        CommonQuestion item = new CommonQuestion();
        fillEntity(item, request);
        item.setDeleted(0);
        commonQuestionMapper.insert(item);
        return detail(item.getId());
    }
 
    @Transactional
    public CommonQuestionDetailVO update(Long id, CommonQuestionSaveRequest request) {
        CommonQuestion item = requireItem(id);
        fillEntity(item, request);
        commonQuestionMapper.updateById(item);
        return detail(id);
    }
 
    @Transactional
    public void delete(Long id) {
        requireItem(id);
        commonQuestionMapper.deleteById(id);
    }
 
    private void fillEntity(CommonQuestion item, CommonQuestionSaveRequest request) {
        item.setTitle(normalizeTitle(request.getTitle()));
        item.setContentHtml(normalizeContent(request.getContentHtml()));
    }
 
    private CommonQuestion requireItem(Long id) {
        CommonQuestion item = commonQuestionMapper.selectOne(new LambdaQueryWrapper<CommonQuestion>()
                .eq(CommonQuestion::getId, id)
                .eq(CommonQuestion::getDeleted, 0)
                .last("limit 1"));
        if (item == null) {
            throw new IllegalArgumentException("常见问题不存在");
        }
        return item;
    }
 
    private CommonQuestionListItemVO toListItem(CommonQuestion item) {
        CommonQuestionListItemVO vo = new CommonQuestionListItemVO();
        vo.setId(item.getId());
        vo.setTitle(item.getTitle());
        vo.setCreatedAt(item.getCreatedAt());
        return vo;
    }
 
    private CommonQuestionDetailVO toDetailVO(CommonQuestion item) {
        CommonQuestionDetailVO vo = new CommonQuestionDetailVO();
        vo.setId(item.getId());
        vo.setTitle(item.getTitle());
        vo.setContentHtml(item.getContentHtml());
        vo.setCreatedAt(item.getCreatedAt());
        vo.setUpdatedAt(item.getUpdatedAt());
        return vo;
    }
 
    private String normalizeTitle(String title) {
        String trimmed = trimToNull(title);
        if (trimmed == null) {
            throw new IllegalArgumentException("请输入问题");
        }
        if (trimmed.length() > 15) {
            throw new IllegalArgumentException("请输入15个字以内");
        }
        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;
    }
}