一、信息传输类
UserDTO
鉴于用户id、昵称和头像在后端传递中多次出现,将其封装为UserDTO类
import lombok.Data;
@Data
public class UserDTO {
private Integer id;
private String nickname;
private String profilepic;
}
UserSolutionDTO
封装解决方案详情界面所需的作者有关的统计数据
import lombok.Data;
import java.util.List;
@Data
public class UserSolutionDTO {
private UserDTO userDTO;
private Integer view; //用户方案总浏览量
private Integer solutionCount; //发布的方案数
private Integer fanCount; //粉丝数
private Integer commentedCount; //被评论数
private Integer collectedCount; //被收藏数
private List<SolutionDTO> solutionList; //按时间排序的解决方案
}
SolutionInfoDTO
封装解决方案详情界面所需要的属性
import lombok.Data;
import java.time.LocalDateTime;
@Data //解决方案详情
public class SolutionInfoDTO {
private SolutionDTO solutionDTO; //方案id,name,view
private UserSolutionDTO userSolutionDTO; //用户相关数据
private String detail; //内容
private Double score; //分数
private Integer count; //打分人数
private Integer collections; //该方案收藏数
private LocalDateTime createTime; //方案发布时间
}
SolutionDTO
封装解决方案id,name,view属性
import lombok.Data;
@Data
public class SolutionDTO {
private Integer id;
private String name;
private Integer view;
}
SolutionAllDTO
封装解决方案社区和用户个人主页解决方案部分展示所需的属性
import lombok.Data;
import java.time.LocalDate;
@Data //方案社区&个人主页方案
public class SolutionAllDTO {
private Integer id;
private String name;
private UserDTO userDTO; //用户id,nickname,profilepic
private String detail; //方案内容
private Double score; //评分
private Integer view; //浏览量
private Integer comments; //评论数
private Integer collections; //收藏数
private LocalDate createTime; //发布时间
private String relativeTime; //相对时间
}
SolCenterDTO
封装用户个人中心解决方案仓库中所需属性
import lombok.Data;
@Data //个人中心方案管理
public class SolCenterDTO {
private Integer id; //方案id
private String name; //名称
private String detail; //详情
private Integer view; //浏览量
private String statement; //状态
private String relativeTime;//相对时间
private Integer guideId; //引导id
private String guideName; //引导名称
private Integer collections;//收藏数
private Integer comments; //评论数
}
二、工具类
TimeUtil
将传入的LocalDateTime转化为相对时间
import java.time.Duration;
import java.time.LocalDateTime;
public class TimeUtil {
public static String createRelative(LocalDateTime localDateTime) {
Duration duration = Duration.between(localDateTime,LocalDateTime.now());
if (duration.toDays() < 1) {
if (duration.toHours() < 1) {
if (duration.toMinutes() < 1) {
return duration.toSeconds() + "秒前";
}
else {
return duration.toMinutes() + "分钟前";
}
}
else {
return duration.toHours() + "小时前";
}
}
else {
return localDateTime.toLocalDate().toString();
}
}
}
Md2Text
将传入的markdown格式的字符串转化为纯文本,需要导入flexmark,lang,lang3依赖
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
public class Md2Text {
public static String convert(String md) {
String html = convertMd(md);
return convertHtml(html);
}
public static String convertMd(String md) {
MutableDataSet options = new MutableDataSet();
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
Node document = parser.parse(md);
return renderer.render(document);
}
private static String convertHtml(String html)
{
if (StringUtils.isEmpty(html))
{
return "";
}
Document document = Jsoup.parse(html);
Document.OutputSettings outputSettings = new Document.OutputSettings().prettyPrint(false);
document.outputSettings(outputSettings);
document.select("br").append("\\n");
document.select("p").prepend("\\n");
document.select("p").append("\\n");
String newHtml = document.html().replaceAll("\\\\n", "\n");
String plainText = Jsoup.clean(newHtml, "", Whitelist.none(), outputSettings);
return StringEscapeUtils.unescapeHtml(plainText.trim());
}
}
三、service实现类
编辑解决方案方法:insert
用户编辑解决方案后选择保存草稿或者发布方案都会调用该方法。用户编辑解决方案的入口为创新引导过程过程中生成解决方案以及解决方案仓库中编辑解决方案。方法首先判断传入的解决方案是否合理:通过参数solution的guideId属性,查找数据库中guide表以及solution表,若未找到对应的创新引导,则认为创新引导不存在,返回错误信息;若未找到对应的解决方案,则认为用户希望新增解决方案,将参数solution插入数据库。根据参数solution的状态判断用户执行的操作,若状态为“未发布”则认为用户执行保存草稿操作,若状态为“待审核”则为发布方案操作,若状态为“已发布”和“回收”,则返回错误信息。
@Override //用户编辑方案
public Result insert(Solution solution) {
QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("guide_id",solution.getGuideId());
Guide guide;
Solution one;
try {
guide = guideService.getById(solution.getGuideId());
one = solutionService.getOne(queryWrapper);
}catch (Exception e) {
throw new ServiceException(Constants.CODE_500,"系统错误");
}
if (guide == null || !guide.getUserId().equals(solution.getUserId()))
return Result.error(Constants.CODE_600,"引导不存在");
//插入方案
if (one == null) {
if (solutionService.saveOrUpdate(solution))
return Result.success();
return Result.error(Constants.CODE_600,"保存失败");
}
if (solution.getStatement().equals("已发布") || solution.getStatement().equals("回收"))
return Result.error();
//更新方案
solution.setId(one.getId());
//用户保存草稿
if (solution.getStatement().equals("未发布")) {
if (one.getStatement().equals("回收") || one.getStatement().equals("待审核") || one.getStatement().equals("已发布"))
return Result.error(Constants.CODE_600,"方案状态不符合");
}
//用户发布方案
else if (solution.getStatement().equals("待审核")) {
if (one.getStatement().equals("回收"))
return Result.error(Constants.CODE_600,"方案状态不符合");
else if (one.getStatement().equals("已发布")) {
solution.setView(0);
if (solutionService.saveOrUpdate(solution)) {
commentService.deleteBySolution(solution.getId());
scoreService.deleteBySolution(solution.getId());
return Result.success("修改成功");
} else
return Result.error(Constants.CODE_500, "修改失败");
}
}
if (solutionService.saveOrUpdate(solution))
return Result.success();
return Result.error(Constants.CODE_600,"保存失败");
}
获取用户解决方案仓库信息:getCenter
解决方案仓库需要SolCenterDTO的属性,通过用户id查找解决方案列表,根据解决方案列表中每一个solution创建对应的SolCenterDTO对象,其中方案详情需要通过Md2Text转为纯文本,创建时间需通过TimeUtil生成相对时间。
@Override //个人中心方案管理
public List<SolCenterDTO> getCenter(Integer userId) {
QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id",userId);
List<Solution> solutionList = solutionService.list(queryWrapper);
solutionList.sort(Comparator.comparing(Solution::getCreateTime).reversed());
List<SolCenterDTO> solCenterDTOS = new ArrayList<>();
for (Solution solution : solutionList) {
SolCenterDTO solCenterDTO = new SolCenterDTO();
solCenterDTO.setId(solution.getId());
solCenterDTO.setName(solution.getName());
solCenterDTO.setView(solution.getView());
solCenterDTO.setDetail(Md2Text.convert(solution.getDetail()));
solCenterDTO.setGuideId(solution.getGuideId());
solCenterDTO.setStatement(solution.getStatement());
solCenterDTO.setGuideName(guideService.getById(solution.getGuideId()).getName());
solCenterDTO.setCollections(collectionService.getCount(solution.getId()));
solCenterDTO.setComments(commentService.getCount(solution.getId()));
solCenterDTO.setRelativeTime(TimeUtil.createRelative(solution.getCreateTime()));
solCenterDTOS.add(solCenterDTO);
}
return solCenterDTOS;
}
获取方案详情:getDetailById
解决方案详情信息通过SolutionInfoDTO传递,其中SolutionInfoDTO对象的userSolutionDTO属性由UserServiceImpl中的getUserSolutionDTO方法赋值。
@Override //获取方案详情
public SolutionInfoDTO getDetailById(Integer id) {
if (id == null)
throw new ServiceException(Constants.CODE_500,"系统错误");
Solution solutionById;
try {
solutionById = solutionService.getById(id);
}catch (Exception e) {
throw new ServiceException(Constants.CODE_500,"系统错误");
}
if (solutionById == null)
throw new ServiceException(Constants.CODE_600,"未找到方案");
SolutionInfoDTO solution = new SolutionInfoDTO();
SolutionDTO solutionDTO = new SolutionDTO();
//设置solutionDTO
solutionDTO.setId(id);
solutionDTO.setName(solutionById.getName());
solutionDTO.setView(solutionById.getView());
solution.setSolutionDTO(solutionDTO);
solution.setDetail(solutionById.getDetail());
solution.setUserSolutionDTO(userService.getUserSolutionDTO(solutionById.getUserId(),id));
solution.setMine(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId().equals(solution.getUserSolutionDTO().getUserDTO().getId()));
solution.setCreateTime(solutionById.getCreateTime());
solution.setRelativeTime(TimeUtil.createRelative(solutionById.getCreateTime()));
solution.setCollections(collectionService.getCount(id));
return solution;
}
根据引导id查找方案:findByGuide
@Override //根据引导id查找方案
public Result findByGuide(Integer guide_id) {
QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("guide_id",guide_id);
return Result.success(solutionService.getOne(queryWrapper));
}
根据用户id查找其已发布方案:findByUserId
@Override //查找用户的已发布方案
public List<Solution> findByUserId(Integer userId) {
QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id",userId);
queryWrapper.eq("statement","已发布");
List<Solution> result = solutionService.list(queryWrapper);
result.sort(Comparator.comparing(Solution::getCreateTime).reversed());
return result;
}
根据用户id查找其所有方案的浏览量总和:getViewByUser
@Override //根据用户id查找其所有方案的浏览量总和
public int getViewByUser(Integer userId) {
QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id",userId);
List<Solution> solutionList = solutionService.findByUserId(userId);
int view = 0;
for (Solution solution : solutionList) {
if (solution.getStatement().equals("已发布"))
view += solution.getView();
}
return view;
}