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);
|
item.setVideoDescription(null);
|
return;
|
}
|
|
String videoUrl = trimToNull(req.getVideoUrl());
|
if (videoUrl == null) {
|
throw new IllegalArgumentException("请填写视频地址或上传本地视频");
|
}
|
String videoDescription = trimToNull(req.getVideoDescription());
|
if (videoDescription == null) {
|
throw new IllegalArgumentException("请输入视频介绍");
|
}
|
item.setContent(null);
|
item.setVideoUrl(videoUrl);
|
item.setVideoFileName(trimToNull(req.getVideoFileName()));
|
item.setVideoDescription(videoDescription);
|
}
|
|
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.setVideoDescription(item.getVideoDescription());
|
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;
|
}
|
}
|