package com.fs.qw.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.his.config.FsSysConfig;
import com.fs.his.utils.ConfigUtil;
import com.fs.qw.Bean.MsgBean;
import com.fs.qw.domain.*;
import com.fs.qw.enums.MsgType;
import com.fs.qw.mapper.*;
import com.fs.qw.param.QwMsgSendParam;
import com.fs.qw.param.QwSessionParam;
import com.fs.qw.service.IQwMsgService;
import com.fs.qw.service.IQwUserService;
import com.fs.qw.vo.QwContactListVO;
import com.fs.qw.vo.QwMessageListVO;
import com.fs.qwHookApi.vo.QwHookMsgVO;
import com.fs.wxwork.dto.*;
import com.fs.wxwork.service.WxWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 企微聊天记录Service业务层处理
 *
 * @author fs
 * @date 2024-12-17
 */
@Service
public class QwMsgServiceImpl extends ServiceImpl<QwMsgMapper, QwMsg> implements IQwMsgService {
    @Autowired
    private QwMsgMapper qwMsgMapper;
    @Autowired
    private IQwUserService qwUserService;
    @Autowired
    private QwSessionMapper qwSessionMapper;
    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;
    @Autowired
    private QwUserMapper qwUserMapper;
    @Autowired
    private QwCompanyMapper qwCompanyMapper;

    @Autowired
    private ConfigUtil configUtil;
    @Autowired
    private WxWorkService wxWorkService;

    /**
     * 查询企微聊天记录
     *
     * @param msgId 企微聊天记录主键
     * @return 企微聊天记录
     */
    @Override
    public QwMsg selectQwMsgByMsgId(Long msgId) {
        return qwMsgMapper.selectQwMsgByMsgId(msgId);
    }

    /**
     * 查询企微聊天记录列表
     *
     * @param qwMsg 企微聊天记录
     * @return 企微聊天记录
     */
    @Override
    public List<QwMsg> selectQwMsgList(QwMsg qwMsg) {
        return qwMsgMapper.selectQwMsgList(qwMsg);
    }

    /**
     * 新增企微聊天记录
     *
     * @param qwMsg 企微聊天记录
     * @return 结果
     */
    @Override
    public int insertQwMsg(QwMsg qwMsg) {
        qwMsg.setCreateTime(DateUtils.getNowDate());
        return qwMsgMapper.insertQwMsg(qwMsg);
    }

    /**
     * 修改企微聊天记录
     *
     * @param qwMsg 企微聊天记录
     * @return 结果
     */
    @Override
    public int updateQwMsg(QwMsg qwMsg) {
        return qwMsgMapper.updateQwMsg(qwMsg);
    }

    /**
     * 批量删除企微聊天记录
     *
     * @param msgIds 需要删除的企微聊天记录主键
     * @return 结果
     */
    @Override
    public int deleteQwMsgByMsgIds(Long[] msgIds) {
        return qwMsgMapper.deleteQwMsgByMsgIds(msgIds);
    }

    /**
     * 删除企微聊天记录信息
     *
     * @param msgId 企微聊天记录主键
     * @return 结果
     */
    @Override
    public int deleteQwMsgByMsgId(Long msgId) {
        return qwMsgMapper.deleteQwMsgByMsgId(msgId);
    }

    @Override
    public R addQwMsg(QwHookMsgVO msgVo) {
        //查询接收者企微用户
        QwUser qwuser = qwUserService.selectQwUserByAppKey(msgVo.getKey());
        if (qwuser != null) {
            //查询发送者外部联系人
            QwExternalContact qwExternalContact = qwExternalContactMapper.selectQwExternalContactByExternalUserIdAndQwUserId(msgVo.getSender_openid(), qwuser.getCorpId(), qwuser.getQwUserId());
            if(qwExternalContact!=null){

                //查询会话
                QwSession qwSession = qwSessionMapper.selectQwSessionByExtIdAndQwUserId(qwExternalContact.getId(), qwuser.getId());
                if (qwSession == null) {
                    qwSession = new QwSession();
                    String chatId = UUID.randomUUID().toString();
                    qwSession.setChatId(chatId);
                    qwSession.setCorpId(qwuser.getCorpId());
                    qwSession.setQwExtWxId(msgVo.getSender());
                    qwSession.setQwExtId(qwExternalContact.getId().toString());
                    qwSession.setQwUserId(qwuser.getId().toString());
                    qwSession.setStatus(1);
                    qwSession.setAvatar(qwExternalContact.getAvatar());
                    qwSession.setNickName(qwExternalContact.getRemark());
                    qwSession.setCompanyId(qwuser.getCompanyId());
                    qwSession.setCompanyUserId(qwuser.getCompanyUserId());
                    qwSession.setCreateTime(new Date());
                    qwSession.setUpdateTime(new Date());
                    qwSessionMapper.insertQwSession(qwSession);
                }else {
                    qwSession.setUpdateTime(new Date());
                    qwSession.setNickName(qwExternalContact.getRemark());
                    qwSessionMapper.updateQwSession(qwSession);
                }
                QwMsg qwMsg = new QwMsg();
                qwMsg.setContent(msgVo.getContent());
                qwMsg.setSessionId(qwSession.getSessionId());
                qwMsg.setSendType(1);
                qwMsg.setCompanyId(qwuser.getCompanyId());
                qwMsg.setCompanyUserId(qwuser.getCompanyUserId());
                qwMsg.setMsgType(1);
                qwMsg.setStatus(0);
                qwMsg.setQwUserId(qwSession.getQwUserId());
                qwMsg.setQwExtId(qwSession.getQwExtId());
                qwMsg.setAvatar(qwExternalContact.getAvatar());
                qwMsg.setNickName(qwExternalContact.getRemark());
                qwMsg.setCreateTime(new Date());
                if (qwMsgMapper.insertQwMsg(qwMsg) > 0) {
                    //发送socket
                    QwMessageListVO msg = new QwMessageListVO();
                    msg.setId(qwMsg.getMsgId().toString());
                    msg.setToContactId(qwMsg.getSessionId().toString());
                    msg.setType("text");
                    msg.setStatus("succeed");
                    msg.setContent(msgVo.getContent());
                    msg.setSendTime(qwMsg.getCreateTime().getTime());
                    QWFromUser qwFromUser = new QWFromUser();
                    //用户发送
                    qwFromUser.setId(Long.parseLong(qwMsg.getQwExtId()));
                    qwFromUser.setAvatar(qwMsg.getAvatar());
                    qwFromUser.setDisplayName(qwMsg.getNickName());
                    msg.setFromUser(qwFromUser);
                    if (qwuser.getAppKey()!=null){
                        sendSocket("receiveMsg",JSONObject.toJSONString(msg),qwuser.getAppKey());
                    }
                    return R.ok().put("data", qwSession);
                }
            }


        }
        return R.error();
    }

    public void sendSocket(String cmd,String message,String appKey){
        MsgBean msgBean=new MsgBean();
        msgBean.setCmd(cmd);
        msgBean.setData(message);
        JSONObject params=new JSONObject();
        params.put("id","msg"+appKey);
        params.put("message",JSONObject.toJSONString(msgBean));
        FsSysConfig config = configUtil.getSysConfig();
        String domainName = config.getHookUrl();
        HttpRequest.post(domainName+"/app/qwmsg/receiveMsg")
                .body(JSONObject.toJSONString(params),"application/json;charset=UTF-8")
                .execute().body();
    }

    @Override
    public R addAiMsg(QwSession session, String content, Integer msgType,QwUser user) {
        if (session == null) {
            return R.error();
        }
        QwMsg qwMsg = new QwMsg();
        qwMsg.setContent(content);
        qwMsg.setSessionId(session.getSessionId());
        qwMsg.setSendType(2);
        qwMsg.setCompanyId(session.getCompanyId());
        qwMsg.setCompanyUserId(session.getCompanyUserId());
        qwMsg.setStatus(0);
        qwMsg.setMsgType(msgType);
        qwMsg.setQwUserId(session.getQwUserId());
        qwMsg.setQwExtId(session.getQwExtId());
        qwMsg.setAvatar(session.getAvatar());
        qwMsg.setNickName(session.getNickName());
        qwMsg.setCreateTime(new Date());
        if (qwMsgMapper.insertQwMsg(qwMsg)>0 && user!=null && user.getAppKey()!=null){
            //发送socket
            QwMessageListVO msg = new QwMessageListVO();
            msg.setId(qwMsg.getMsgId().toString());
            msg.setToContactId(qwMsg.getSessionId().toString());
            msg.setType("text");
            msg.setStatus("succeed");
            msg.setContent(content);
            msg.setSendTime(qwMsg.getCreateTime().getTime());
            QWFromUser qwFromUser = new QWFromUser();
            //客服发送
            qwFromUser.setId(Long.parseLong(qwMsg.getQwUserId()));
            qwFromUser.setDisplayName(user.getQwUserName());
            qwFromUser.setAvatar(user.getAvatar());
            msg.setFromUser(qwFromUser);
            sendSocket("receiveMsg",JSONObject.toJSONString(msg),user.getAppKey());
        }
        return R.ok();
    }

    @Override
    public R sendMsg(QwMsgSendParam param) {
        if (StringUtils.isBlank(param.getContent())) {
            return R.error("消息内容不能为空");
        }

        if (Objects.isNull(param.getSessionId())) {
            return R.error("会话ID不能为空");
        }

        if (Objects.isNull(param.getMsgType())) {
            return R.error("消息类型不能为空");
        }

        // 查询会话
        QwSession qwSession = qwSessionMapper.selectQwSessionBySessionId(param.getSessionId());
        if (Objects.isNull(qwSession)) {
            return R.error("会话不存在");
        }

        // 外部联系人
        QwExternalContact qwExternalContact = qwExternalContactMapper.selectQwExternalContactById(Long.valueOf(qwSession.getQwExtId()));
        if (Objects.isNull(qwExternalContact)) {
            return R.error("联系人不存在");
        }

        // 企微用户
        QwUser qwUser = qwUserMapper.selectQwUserById(Long.parseLong(qwSession.getQwUserId()));
        if (Objects.isNull(qwUser)) {
            return R.error("用户不存在");
        }

        Long serverId = qwUser.getServerId();
        String uuid = qwUser.getUid();
        String openId = qwExternalContact.getExternalUserId();
        String sCorpId = qwUser.getCorpId();

        WxWorkUserId2VidDTO params = new WxWorkUserId2VidDTO();
        params.setOpenid(Collections.singletonList(openId));
        params.setUuid(uuid);
        params.setScorpid(sCorpId);
        WxWorkResponseDTO<List<WxWorkVid2UserIdRespDTO>> listWxWorkResponseDTO = wxWorkService.UserId2Vid(params, serverId);

        if (listWxWorkResponseDTO.getErrcode() != 0) {
            return R.error(listWxWorkResponseDTO.getErrmsg());
        }
        long sendUserId = listWxWorkResponseDTO.getData().get(0).getUser_id();
        String msgJson;

        MsgType msgType = MsgType.getMsgType(param.getMsgType());
        // 发送消息  文本
        if (MsgType.TEXT == msgType) {
            WxWorkSendTextMsgDTO textMsgDTO = new WxWorkSendTextMsgDTO();
            textMsgDTO.setUuid(uuid);
            textMsgDTO.setSend_userid(sendUserId);
            textMsgDTO.setIsRoom(false);
            textMsgDTO.setContent(param.getContent());
            WxWorkResponseDTO<WxWorkSendTextMsgRespDTO> msgRespDTOWxWorkResponseDTO = wxWorkService.SendTextMsg(textMsgDTO, serverId);

            if (msgRespDTOWxWorkResponseDTO.getErrcode() != 0) {
                return R.error(msgRespDTOWxWorkResponseDTO.getErrmsg());
            }
            msgJson = JSONObject.toJSONString(textMsgDTO);
        }

        // 图片
        else if (MsgType.IMAGE == msgType) {
            WxCdnUploadImgLinkDTO linkDTO = new WxCdnUploadImgLinkDTO();
            linkDTO.setUuid(uuid);
            linkDTO.setUrl(param.getContent());
            WxWorkResponseDTO<WxCdnUploadImgLinkResp> imgLinkResp = wxWorkService.cdnUploadImgLink(linkDTO, serverId);
            if (imgLinkResp.getErrcode() != 0) {
                return R.error(imgLinkResp.getErrmsg());
            }
            WxCdnUploadImgLinkResp data = imgLinkResp.getData();

            // 发送图片消息
            WxwSendCDNImgMsgDTO imgMsgDTO = new WxwSendCDNImgMsgDTO();
            imgMsgDTO.setUuid(uuid);
            imgMsgDTO.setSend_userid(sendUserId);
            imgMsgDTO.setIsRoom(false);
            imgMsgDTO.setCdnkey(data.getCdn_key());
            imgMsgDTO.setAeskey(data.getAes_key());
            imgMsgDTO.setMd5(data.getMd5());
            imgMsgDTO.setFileSize(data.getSize());
            WxWorkResponseDTO<WxwSendCDNImgMsgRespDTO> imgMsgResp = wxWorkService.SendCDNImgMsg(imgMsgDTO, serverId);
            if (imgMsgResp.getErrcode() != 0) {
                return R.error(imgMsgResp.getErrmsg());
            }
            msgJson = JSONObject.toJSONString(imgMsgDTO);
        }
        // 小程序
        else if (MsgType.MINI_PROGRAM == msgType) {
            QwCompany qwCompany = qwCompanyMapper.selectQwCompanyByCorpId(qwUser.getCorpId());
            if (Objects.isNull(qwCompany)) {
                return R.error("企微主体不存在");
            }

            String miniAppParam = qwCompany.getMiniAppParam();
            if (StringUtils.isBlank(miniAppParam)) {
                return R.error("配置信息不存在");
            }

            String pagepath = param.getContent();
            JSONObject paramJson = JSONObject.parseObject(miniAppParam);

            // 发送小程序消息
            WxWorkSendAppMsgDTO appMsgDTO = new WxWorkSendAppMsgDTO();
            appMsgDTO.setUuid(uuid);
            appMsgDTO.setSend_userid(sendUserId);
            appMsgDTO.setDesc(paramJson.getString("title"));
            appMsgDTO.setTitle(paramJson.getString("appName"));
            appMsgDTO.setWeappIconUrl(paramJson.getString("weappIconUrl"));
            appMsgDTO.setPagepath(pagepath);
            appMsgDTO.setUsername(paramJson.getString("username"));
            appMsgDTO.setAppid(paramJson.getString("appid"));
            appMsgDTO.setCdnkey(paramJson.getString("cdnkey"));
            appMsgDTO.setMd5(paramJson.getString("md5"));
            appMsgDTO.setAeskey(paramJson.getString("aeskey"));
            appMsgDTO.setFileSize(paramJson.getInteger("fileSize"));
            appMsgDTO.setIsRoom(false);
            WxWorkResponseDTO<WxWorkSendAppMsgRespDTO> appMsgResp = wxWorkService.SendAppMsg(appMsgDTO, serverId);
            if (appMsgResp.getErrcode() != 0) {
                return R.error(appMsgResp.getErrmsg());
            }

            JSONObject json = new JSONObject();
            json.put("appid", paramJson.getString("appid"));
            json.put("appName", paramJson.getString("appName"));
            json.put("weappIconUrl", paramJson.getString("weappIconUrl"));
            json.put("desc", paramJson.getString("desc"));
            json.put("pagepath", pagepath);
            json.put("title", paramJson.getString("title"));
            json.put("thumbnail", paramJson.getString("thumbnail"));

            msgJson = json.toJSONString();
            param.setContent(msgJson);
        } else {
            return R.error("暂不支持的消息类型");
        }

        // 消息保存本地数据库
        QwMsg qwMsg = new QwMsg();
        qwMsg.setContent(param.getContent());
        qwMsg.setSessionId(qwSession.getSessionId());
        qwMsg.setSendType(2);
        qwMsg.setCompanyId(qwUser.getCompanyId());
        qwMsg.setCompanyUserId(qwUser.getCompanyUserId());
        qwMsg.setMsgType(param.getMsgType());
        qwMsg.setMsgJson(msgJson);
        qwMsg.setStatus(0);
        qwMsg.setQwUserId(qwSession.getQwUserId());
        qwMsg.setQwExtId(qwSession.getQwExtId());
        qwMsg.setAvatar(qwExternalContact.getAvatar());
        qwMsg.setNickName(qwExternalContact.getRemark());
        qwMsg.setCreateTime(new Date());
        qwMsgMapper.insertQwMsg(qwMsg);

        // 组装返回消息结构
        QwMessageListVO listVO = new QwMessageListVO();
        QWFromUser qwFromUser = new QWFromUser();
        qwFromUser.setId(Long.parseLong(qwMsg.getQwUserId()));
        qwFromUser.setDisplayName(qwUser.getQwUserName());
        qwFromUser.setAvatar(qwUser.getAvatar());
        listVO.setType(msgType.getValue());
        listVO.setStatus("succeed");
        listVO.setFromUser(qwFromUser);
        listVO.setSendTime(qwMsg.getCreateTime().getTime());
        listVO.setId(qwMsg.getMsgId().toString());
        listVO.setContent(qwMsg.getContent());
        listVO.setToContactId(String.valueOf(param.getSessionId()));
        listVO.setAppKey(qwUser.getAppKey());
        return R.ok().put("data", listVO);
    }

    @Override
    public List<QwUser> qwUserList(Long userId) {
        LambdaQueryWrapper<QwUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QwUser::getCompanyUserId, userId);
        List<QwUser> qwUsers = qwUserMapper.selectList(lambdaQueryWrapper);
        return qwUsers;
    }


    @Override
    public List<QwMsg> getQwMessageList(Long companyId) {
        LambdaQueryWrapper<QwUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QwUser::getCompanyId, companyId);
        List<QwUser> qwUsers = qwUserMapper.selectList(lambdaQueryWrapper);
        List<String> qwUserIds = qwUsers.stream().map(x -> x.getQwUserId()).collect(Collectors.toList());
        LambdaQueryWrapper<QwSession> sessionWrapper = new LambdaQueryWrapper<>();
        if (CollectionUtil.isEmpty(qwUserIds)) {
            return null;
        }
        sessionWrapper.in(QwSession::getQwUserId, qwUserIds);
        List<QwSession> qwSessions = qwSessionMapper.selectList(sessionWrapper);
        List<Long> sessionIds = qwSessions.stream().map(x -> x.getSessionId()).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(sessionIds)) {
            return null;
        }
        LambdaQueryWrapper<QwMsg> msgWrapper = new LambdaQueryWrapper<>();
        msgWrapper.in(QwMsg::getSessionId, sessionIds);
        msgWrapper.orderByDesc(QwMsg::getCreateTime);
        List<QwMsg> qwMsgs = qwMsgMapper.selectList(msgWrapper);
        return qwMsgs;
    }

    @Override
    public List<QwContactListVO> selectQwConversationByUserId(Long userId) {
        // 查询会话列表
        List<QwContactListVO> contactList = qwSessionMapper.selectContactListByQwUserId(userId);
        if (contactList.isEmpty()) {
            return new ArrayList<>();
        }

        ArrayList<QwContactListVO> qwContactListVOS = new ArrayList<>();
        for (QwContactListVO listVO : contactList) {
            LambdaQueryWrapper<QwMsg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.select(QwMsg.class, q -> !q.getColumn().equals("remark"));
            lambdaQueryWrapper.eq(QwMsg::getSessionId, Integer.parseInt(listVO.getConversationId()));
            lambdaQueryWrapper.orderByDesc(QwMsg::getMsgId);
            lambdaQueryWrapper.last("limit 1");
            List<QwMsg> qwMsgs = qwMsgMapper.selectList(lambdaQueryWrapper);
            if (CollectionUtil.isEmpty(qwMsgs)){
                listVO.setLastContent("");
                listVO.setLastSendTime(new Date().getTime());
                qwContactListVOS.add(listVO);
                break;
            }
            QwMsg qwMsg = qwMsgs.get(0);
            if (qwMsg.getMsgType() == 1) {
                listVO.setType("text");
            } else if (qwMsg.getMsgType() == 2) {
                listVO.setType("image");
            } else if (qwMsg.getMsgType() == 3) {
                listVO.setType("emotionDynamic");
            } else if (qwMsg.getMsgType() == 4) {
                listVO.setType("voice");
            } else if (qwMsg.getMsgType() == 5) {
                listVO.setType("miniprogram");
            }
            listVO.setMsgId(qwMsg.getMsgId());
            listVO.setLastContent(qwMsgs.get(0).getContent());
            listVO.setLastSendTime(qwMsgs.get(0).getCreateTime().getTime());
            listVO.setUnread(0);
            qwContactListVOS.add(listVO);
        }
        return qwContactListVOS;
    }

    @Override
    public List<QwMsg> selectQwMsgBySession(QwSessionParam param) {
        LambdaQueryWrapper<QwMsg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(QwMsg.class, q -> !q.getColumn().equals("remark"));
        lambdaQueryWrapper.eq(QwMsg::getSessionId, param.getConversationId());
        if (Objects.nonNull(param.getMsgId())) {
            lambdaQueryWrapper.gt(QwMsg::getMsgId, param.getMsgId());
        }
        lambdaQueryWrapper.orderByDesc(QwMsg::getMsgId);
        List<QwMsg> records = qwMsgMapper.selectList(lambdaQueryWrapper);
        return records;
    }

    @Override
    public List<QwMessageListVO> getQwMessageListBySession(QwSessionParam param , List<QwMsg> list) {
        QwUser user = qwUserMapper.selectQwUserById(param.getUserId());
        List<QwMessageListVO> qwMessageVOS = new ArrayList<>();
        for (QwMsg record : list) {
            QwMessageListVO listVO = new QwMessageListVO();
            String type = "text";
            MsgType msgType = MsgType.getMsgType(record.getMsgType());
            if (Objects.nonNull(record.getMsgType())) {
                type = msgType.getValue();
            }
            listVO.setType(type);
            listVO.setStatus("succeed");
            QWFromUser qwFromUser = new QWFromUser();
            //用户发送
            if (record.getSendType() == 1) {
                qwFromUser.setId(Long.parseLong(record.getQwExtId()));
                qwFromUser.setAvatar(record.getAvatar());
                qwFromUser.setDisplayName(record.getNickName());
            }else if(record.getSendType() == 2){
                qwFromUser.setId(Long.parseLong(record.getQwUserId()));
                qwFromUser.setDisplayName(user.getQwUserName());
                qwFromUser.setAvatar(user.getAvatar());
            }
            listVO.setExtId(record.getQwExtId());
            listVO.setFromUser(qwFromUser);
            listVO.setSendTime(record.getCreateTime().getTime());
            listVO.setId(record.getMsgId().toString());
            listVO.setContent(record.getContent());
            listVO.setToContactId(param.getConversationId());
            listVO.setAppKey(user.getAppKey());
            qwMessageVOS.add(listVO);
        }
        return qwMessageVOS;
    }


    @Override
    public QwContactListVO selectQwSessionBycId(String sessionId, Long qwUserId) {
        QwSession session = qwSessionMapper.selectQwSessionBySessionId(Long.parseLong(sessionId));
        QwContactListVO listVO = new QwContactListVO();
        listVO.setId(Long.parseLong(session.getQwUserId()));
        listVO.setAvatar(session.getAvatar());
        listVO.setConversationId(session.getSessionId().toString());
        listVO.setDisplayName(session.getNickName());
        listVO.setIndex(session.getNickName().substring(0, 1));
        LambdaQueryWrapper<QwMsg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QwMsg::getSessionId, session.getSessionId());
        lambdaQueryWrapper.orderByDesc(QwMsg::getMsgId);
        lambdaQueryWrapper.last("limit 1");
        List<QwMsg> qwMsgs = qwMsgMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtil.isEmpty(qwMsgs)){
            listVO.setLastContent("");
            listVO.setLastSendTime(new Date().getTime());
        }else {
            listVO.setLastContent(qwMsgs.get(0).getContent());
            listVO.setLastSendTime(qwMsgs.get(0).getCreateTime().getTime());
        }
        listVO.setUnread(0);
        return listVO;
    }
}
