LoveChatService.java 1.91 KB
package com.pipihelper.project.feishu.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.pipihelper.project.feishu.bo.LoveChatVO;
import com.pipihelper.project.feishu.dao.LoveChatDao;
import com.pipihelper.project.feishu.entity.LoveChat;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LoveChatService {

    @Autowired
    private LoveChatDao loveChatDao;

    public List<LoveChatVO> list(String title, Integer pageNum, Integer pageSize) {

        if(StringUtils.isBlank(title)){
            title = null;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<LoveChat> chatList = loveChatDao.findList(title);
        if (CollectionUtil.isEmpty(chatList)) {
            return new ArrayList<>();
        }
        return chatList.stream().map(it -> {
            LoveChatVO loveChatVO = new LoveChatVO();
            loveChatVO.setTitle(it.getTitle());
            loveChatVO.setId(it.getId());
            List<SpeedContent> speedContentList = JSON.parseArray(it.getContent(), SpeedContent.class);
            loveChatVO.setLoveChatDetails(speedContentList.stream().map(speedContent -> {
                LoveChatVO.LoveChatDetail chatDetail = new LoveChatVO.LoveChatDetail();
                chatDetail.setType("me".equals(speedContent.getObject()) ? 1 : 2);
                chatDetail.setText(speedContent.getPlan());
                return chatDetail;
            }).collect(Collectors.toList()));
            return loveChatVO;
        }).collect(Collectors.toList());
    }

    @Data
    private static class SpeedContent {
        private String object;
        private String plan;
    }
}