hejianhao
2026-08-20 2f5f94f57693c8e056ee89c505bb5d784ecddc1d
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.tcm.training.admin.live.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.live.dto.ProjectIntroductionSaveRequest;
import com.tcm.training.admin.live.entity.ProjectIntroduction;
import com.tcm.training.admin.live.mapper.ProjectIntroductionMapper;
import com.tcm.training.admin.live.vo.ProjectIntroductionDetailVO;
import com.tcm.training.admin.live.vo.ProjectIntroductionListItemVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
public class ProjectIntroductionAppService {
 
    private final ProjectIntroductionMapper projectIntroductionMapper;
 
    public ProjectIntroductionAppService(ProjectIntroductionMapper projectIntroductionMapper) {
        this.projectIntroductionMapper = projectIntroductionMapper;
    }
 
    public PageResponse<ProjectIntroductionListItemVO> page(int page,
                                                             int pageSize,
                                                             String keyword,
                                                             String category,
                                                             Integer contentType,
                                                             Integer status) {
        LambdaQueryWrapper<ProjectIntroduction> qw = new LambdaQueryWrapper<ProjectIntroduction>()
                .eq(ProjectIntroduction::getDeleted, 0)
                .orderByDesc(ProjectIntroduction::getId);
 
        String trimmedKeyword = trimToNull(keyword);
        if (trimmedKeyword != null) {
            qw.like(ProjectIntroduction::getTitle, trimmedKeyword);
        }
        if (category != null && !category.trim().isEmpty()) {
            qw.eq(ProjectIntroduction::getCategory, category.trim());
        }
        if (contentType != null) {
            qw.eq(ProjectIntroduction::getContentType, contentType);
        }
        if (status != null) {
            qw.eq(ProjectIntroduction::getStatus, status);
        }
 
        IPage<ProjectIntroduction> result = projectIntroductionMapper.selectPage(new Page<ProjectIntroduction>(page, pageSize), qw);
        List<ProjectIntroductionListItemVO> list = new ArrayList<ProjectIntroductionListItemVO>();
        for (ProjectIntroduction item : result.getRecords()) {
            list.add(toListItem(item));
        }
        return PageResponse.of(list, result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    public ProjectIntroductionDetailVO detail(Long id) {
        return toDetailVO(requireItem(id));
    }
 
    @Transactional
    public ProjectIntroductionDetailVO create(ProjectIntroductionSaveRequest req) {
        ProjectIntroduction item = new ProjectIntroduction();
        fillEntity(item, req);
        item.setClickCount(0);
        item.setDeleted(0);
        projectIntroductionMapper.insert(item);
        return detail(item.getId());
    }
 
    @Transactional
    public ProjectIntroductionDetailVO update(Long id, ProjectIntroductionSaveRequest req) {
        ProjectIntroduction item = requireItem(id);
        fillEntity(item, req);
        projectIntroductionMapper.updateById(item);
        return detail(id);
    }
 
    @Transactional
    public void delete(Long id) {
        requireItem(id);
        projectIntroductionMapper.deleteById(id);
    }
 
    private void fillEntity(ProjectIntroduction item, ProjectIntroductionSaveRequest req) {
        int contentType = validateContentType(req.getContentType());
        String category = validateCategory(req.getCategory());
 
        item.setTitle(req.getTitle().trim());
        item.setCoverImg(req.getCoverImg().trim());
        item.setCategory(category);
        item.setContentType(contentType);
        item.setStatus(req.getStatus());
 
        if (contentType == ProjectIntroduction.CONTENT_TYPE_ARTICLE) {
            String content = trimToNull(req.getContent());
            if (content == null) {
                throw new IllegalArgumentException("请输入内容");
            }
            item.setContent(content);
            item.setVideoUrl(null);
            item.setVideoFileName(null);
            return;
        }
 
        String videoUrl = trimToNull(req.getVideoUrl());
        if (videoUrl == null) {
            throw new IllegalArgumentException("请填写视频地址或上传本地视频");
        }
        item.setContent(null);
        item.setVideoUrl(videoUrl);
        item.setVideoFileName(trimToNull(req.getVideoFileName()));
    }
 
    private int validateContentType(Integer contentType) {
        if (contentType == null) {
            throw new IllegalArgumentException("请选择类型");
        }
        if (contentType != ProjectIntroduction.CONTENT_TYPE_ARTICLE
                && contentType != ProjectIntroduction.CONTENT_TYPE_VIDEO) {
            throw new IllegalArgumentException("类型不合法");
        }
        return contentType;
    }
 
    private String validateCategory(String category) {
        if (category == null || category.trim().isEmpty()) {
            throw new IllegalArgumentException("请选择类别");
        }
        String trimmed = category.trim();
        if (!ProjectIntroduction.CATEGORY_PLATFORM.equals(trimmed)
                && !ProjectIntroduction.CATEGORY_MARKETING_MODEL.equals(trimmed)
                && !ProjectIntroduction.CATEGORY_MARKETING_ACTION.equals(trimmed)) {
            throw new IllegalArgumentException("类别不合法");
        }
        return trimmed;
    }
 
    private ProjectIntroduction requireItem(Long id) {
        ProjectIntroduction item = projectIntroductionMapper.selectOne(new LambdaQueryWrapper<ProjectIntroduction>()
                .eq(ProjectIntroduction::getId, id)
                .eq(ProjectIntroduction::getDeleted, 0)
                .last("limit 1"));
        if (item == null) {
            throw new IllegalArgumentException("项目介绍不存在");
        }
        return item;
    }
 
    private ProjectIntroductionListItemVO toListItem(ProjectIntroduction item) {
        ProjectIntroductionListItemVO vo = new ProjectIntroductionListItemVO();
        vo.setId(item.getId());
        vo.setTitle(item.getTitle());
        vo.setCoverImg(item.getCoverImg());
        vo.setCategory(item.getCategory());
        vo.setContentType(item.getContentType());
        vo.setStatus(item.getStatus());
        vo.setClickCount(defaultNumber(item.getClickCount()));
        vo.setCreatedAt(item.getCreatedAt());
        return vo;
    }
 
    private ProjectIntroductionDetailVO toDetailVO(ProjectIntroduction item) {
        ProjectIntroductionDetailVO vo = new ProjectIntroductionDetailVO();
        vo.setId(item.getId());
        vo.setTitle(item.getTitle());
        vo.setCoverImg(item.getCoverImg());
        vo.setCategory(item.getCategory());
        vo.setContentType(item.getContentType());
        vo.setContent(item.getContent());
        vo.setVideoUrl(item.getVideoUrl());
        vo.setVideoFileName(item.getVideoFileName());
        vo.setStatus(item.getStatus());
        vo.setClickCount(defaultNumber(item.getClickCount()));
        vo.setCreatedAt(item.getCreatedAt());
        vo.setUpdatedAt(item.getUpdatedAt());
        return vo;
    }
 
    private Integer defaultNumber(Integer value) {
        return value == null ? 0 : value;
    }
 
    private String trimToNull(String value) {
        if (value == null) {
            return null;
        }
        String trimmed = value.trim();
        return trimmed.isEmpty() ? null : trimmed;
    }
}