package com.emotion.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.emotion.common.PageResult; import com.emotion.dto.request.EpicScriptCreateRequest; import com.emotion.dto.request.EpicScriptPageRequest; import com.emotion.dto.request.EpicScriptUpdateRequest; import com.emotion.dto.response.EpicScriptResponse; import com.emotion.entity.EpicScript; import com.emotion.mapper.EpicScriptMapper; import com.emotion.service.EpicScriptService; import com.emotion.service.LifePathService; import com.emotion.util.UserContextHolder; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.stream.Collectors; /** * 爽文剧本服务实现类 * * @author huazhongmin * @date 2025-12-22 */ @Service public class EpicScriptServiceImpl extends ServiceImpl implements EpicScriptService { private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @Autowired @Lazy private LifePathService lifePathService; @Override public PageResult getPageByCurrentUser(EpicScriptPageRequest request) { String currentUserId = UserContextHolder.getCurrentUserId(); if (currentUserId == null) { return new PageResult<>(); } Page page = new Page<>(request.getCurrent(), request.getSize()); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(EpicScript::getUserId, currentUserId) .eq(EpicScript::getIsDeleted, 0); // 风格筛选 if (StringUtils.hasText(request.getStyle())) { wrapper.eq(EpicScript::getStyle, request.getStyle()); } // 篇幅筛选 if (StringUtils.hasText(request.getLength())) { wrapper.eq(EpicScript::getLength, request.getLength()); } // 关键词搜索 if (StringUtils.hasText(request.getKeyword())) { wrapper.and(w -> w.like(EpicScript::getTitle, request.getKeyword()) .or().like(EpicScript::getTheme, request.getKeyword())); } // 按创建时间倒序排列 wrapper.orderByDesc(EpicScript::getCreateTime); Page resultPage = this.page(page, wrapper); List responses = resultPage.getRecords().stream() .map(this::convertToResponse) .collect(Collectors.toList()); PageResult pageResult = new PageResult<>(); pageResult.setCurrent(resultPage.getCurrent()); pageResult.setSize(resultPage.getSize()); pageResult.setTotal(resultPage.getTotal()); pageResult.setPages(resultPage.getPages()); pageResult.setRecords(responses); return pageResult; } @Override public List getListByCurrentUser() { String currentUserId = UserContextHolder.getCurrentUserId(); if (currentUserId == null) { return List.of(); } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(EpicScript::getUserId, currentUserId) .eq(EpicScript::getIsDeleted, 0) .orderByDesc(EpicScript::getCreateTime); return this.list(wrapper).stream() .map(this::convertToResponse) .collect(Collectors.toList()); } @Override public EpicScriptResponse getScriptById(String id) { EpicScript script = this.getById(id); if (script == null || script.getIsDeleted() == 1) { return null; } // 验证权限 String currentUserId = UserContextHolder.getCurrentUserId(); if (!script.getUserId().equals(currentUserId)) { return null; } return convertToResponse(script); } @Override public EpicScriptResponse createScript(EpicScriptCreateRequest request) { String currentUserId = UserContextHolder.getCurrentUserId(); if (currentUserId == null) { return null; } EpicScript script = new EpicScript(); script.setUserId(currentUserId); script.setTitle(request.getTitle()); script.setTheme(request.getTheme()); script.setStyle(StringUtils.hasText(request.getStyle()) ? request.getStyle() : "career"); script.setLength(StringUtils.hasText(request.getLength()) ? request.getLength() : "medium"); script.setPlotIntro(request.getPlotIntro()); script.setPlotTurning(request.getPlotTurning()); script.setPlotClimax(request.getPlotClimax()); script.setPlotEnding(request.getPlotEnding()); script.setPlotJson(request.getPlotJson()); script.setIsSelected(request.getIsSelected() != null && request.getIsSelected() ? 1 : 0); this.save(script); return convertToResponse(script); } @Override public EpicScriptResponse updateScript(EpicScriptUpdateRequest request) { EpicScript script = this.getById(request.getId()); if (script == null || script.getIsDeleted() == 1) { return null; } // 验证权限 String currentUserId = UserContextHolder.getCurrentUserId(); if (!script.getUserId().equals(currentUserId)) { return null; } // 更新字段 if (StringUtils.hasText(request.getTitle())) { script.setTitle(request.getTitle()); } if (request.getTheme() != null) { script.setTheme(request.getTheme()); } if (StringUtils.hasText(request.getStyle())) { script.setStyle(request.getStyle()); } if (StringUtils.hasText(request.getLength())) { script.setLength(request.getLength()); } if (request.getPlotIntro() != null) { script.setPlotIntro(request.getPlotIntro()); } if (request.getPlotTurning() != null) { script.setPlotTurning(request.getPlotTurning()); } if (request.getPlotClimax() != null) { script.setPlotClimax(request.getPlotClimax()); } if (request.getPlotEnding() != null) { script.setPlotEnding(request.getPlotEnding()); } if (request.getPlotJson() != null) { script.setPlotJson(request.getPlotJson()); } if (request.getIsSelected() != null) { script.setIsSelected(request.getIsSelected() ? 1 : 0); } this.updateById(script); return convertToResponse(script); } @Override public EpicScriptResponse selectScript(String id) { String currentUserId = UserContextHolder.getCurrentUserId(); if (currentUserId == null) { return null; } // 先取消所有选中 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(EpicScript::getUserId, currentUserId) .eq(EpicScript::getIsSelected, 1) .eq(EpicScript::getIsDeleted, 0); List selectedScripts = this.list(wrapper); for (EpicScript s : selectedScripts) { s.setIsSelected(0); this.updateById(s); } // 选中指定剧本 EpicScript script = this.getById(id); if (script == null || script.getIsDeleted() == 1) { return null; } if (!script.getUserId().equals(currentUserId)) { return null; } script.setIsSelected(1); this.updateById(script); return convertToResponse(script); } @Override public boolean deleteScript(String id) { EpicScript script = this.getById(id); if (script == null || script.getIsDeleted() == 1) { return false; } // 验证权限 String currentUserId = UserContextHolder.getCurrentUserId(); if (!script.getUserId().equals(currentUserId)) { return false; } // 删除关联的路径 lifePathService.deleteByScriptId(id); script.setIsDeleted(1); return this.updateById(script); } /** * 转换为响应对象 */ private EpicScriptResponse convertToResponse(EpicScript script) { EpicScriptResponse response = new EpicScriptResponse(); BeanUtils.copyProperties(script, response); response.setId(script.getId()); response.setIsSelected(script.getIsSelected() != null && script.getIsSelected() == 1); if (script.getCreateTime() != null) { response.setCreateTime(script.getCreateTime().format(DATE_TIME_FORMATTER)); } if (script.getUpdateTime() != null) { response.setUpdateTime(script.getUpdateTime().format(DATE_TIME_FORMATTER)); } return response; } }