Helius
2021-05-26 65247a73bbeced41f2e4fd1ac3def2170ee98fe9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.xcong.excoin.modules.otc.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.excoin.common.LoginUserUtils;
import com.xcong.excoin.common.response.Result;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberSettingDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberSettingEntity;
import com.xcong.excoin.modules.otc.dao.OtcMsgHistoryDao;
import com.xcong.excoin.modules.otc.dao.OtcMsgUserListDao;
import com.xcong.excoin.modules.otc.dto.ChatBoxDto;
import com.xcong.excoin.modules.otc.dto.ConnectDto;
import com.xcong.excoin.modules.otc.dto.MsgListDto;
import com.xcong.excoin.modules.otc.dto.SendMsgDto;
import com.xcong.excoin.modules.otc.entity.OtcMsgHistoryEntity;
import com.xcong.excoin.modules.otc.entity.OtcMsgUserListEntity;
import com.xcong.excoin.modules.otc.service.OtcMsgService;
import com.xcong.excoin.modules.otc.vo.ChatBoxVo;
import com.xcong.excoin.modules.otc.vo.MsgListVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.List;
 
@Service
@RequiredArgsConstructor
public class OtcMsgServiceImpl extends ServiceImpl<OtcMsgUserListDao, OtcMsgUserListEntity> implements OtcMsgService {
 
    private final OtcMsgUserListDao otcMsgUserListDao;
    private final OtcMsgHistoryDao otcMsgHistoryDao;
 
    private final MemberSettingDao memberSettingDao;
    private final MemberDao memberDao;
 
 
    @Override
    public IPage<MsgListVo> getMsgList(MsgListDto msgListDto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        Long memberId = member.getId();
//        Long memberId = 445L;
        Page<MsgListVo> page = new Page<>(msgListDto.getPageNum(), msgListDto.getPageSize());
 
        OtcMsgUserListEntity otcMsgUserListEntity = new OtcMsgUserListEntity();
        otcMsgUserListEntity.setMemberId(memberId);
        IPage<MsgListVo> msgList = otcMsgUserListDao.getMsgList(otcMsgUserListEntity, page);
        List<MsgListVo> records = msgList.getRecords();
        if(CollUtil.isNotEmpty(records)){
            for(MsgListVo msgListVo : records){
                long targetId = msgListVo.getTargetId();
                if(memberId == targetId){
                    MemberEntity memberEntity = memberDao.selectById(msgListVo.getMemberId());
                    msgListVo.setNickname(memberEntity.getName());
                }else{
                    MemberEntity memberEntity = memberDao.selectById(targetId);
                    msgListVo.setNickname(memberEntity.getName());
                }
            }
        }
        return msgList;
    }
 
    @Override
    @Transactional
    public Result getChatBox(ChatBoxDto chatBoxDto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
//        MemberEntity member = memberDao.selectById(443L);
        //对话是否存在
        long id = chatBoxDto.getId();
        if(ObjectUtil.isEmpty(id)){
            return Result.fail("请下拉刷新");
        }
        OtcMsgUserListEntity otcMsgUserListEntity = otcMsgUserListDao.selectById(id);
        if(ObjectUtil.isEmpty(otcMsgUserListEntity)){
            return Result.fail("请下拉刷新");
        }
        //更新为已读
        otcMsgUserListEntity.setIsRead(OtcMsgUserListEntity.ISREAD_TWO);
        otcMsgUserListDao.updateById(otcMsgUserListEntity);
 
        long memberId = otcMsgUserListEntity.getMemberId();
        long targetId = otcMsgUserListEntity.getTargetId();
        Page<ChatBoxVo> page = new Page<>(chatBoxDto.getPageNum(), chatBoxDto.getPageSize());
        OtcMsgHistoryEntity otcMsgHistoryEntity = new OtcMsgHistoryEntity();
        otcMsgHistoryEntity.setMemberId(memberId);
        otcMsgHistoryEntity.setTargetId(targetId);
        IPage<ChatBoxVo> chatBoxVos= otcMsgHistoryDao.getChatBoxMsgList(page,otcMsgHistoryEntity);
        List<ChatBoxVo> records = chatBoxVos.getRecords();
        if(CollUtil.isNotEmpty(records)){
            for(ChatBoxVo chatBoxVo : records){
                long memberIds = chatBoxVo.getMemberId();
                if(memberIds == member.getId()){
                    chatBoxVo.setIsSelf(1);
                }else{
                    chatBoxVo.setIsSelf(2);
                }
            }
        }
        return Result.ok(chatBoxVos);
    }
 
    @Override
    @Transactional
    public Result sendMsg(SendMsgDto sendMsgDto) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        Long memberId = member.getId();
//        Long memberId = 445L;
        long targetId = sendMsgDto.getTargetId();
        if(ObjectUtil.isEmpty(targetId)){
            return Result.fail("对话用户不存在");
        }
        MemberEntity memberEntity = memberDao.selectById(targetId);
        if(ObjectUtil.isEmpty(memberEntity)){
            return Result.fail("对话用户不存在");
        }
        String msg = sendMsgDto.getMsg();
        if(StrUtil.isEmpty(msg)){
            return Result.fail("请输入发送消息");
        }
        Integer msgType = sendMsgDto.getMsgType();
        if(ObjectUtil.isEmpty(msgType)){
            return Result.fail("请输入发送消息");
        }
        /**
         * 是否有记录
         *  有就更新,没有就新增
         * 在消息列表中增加一条记录
         * 在历史记录中增加一条记录
         * 增加一个提醒的
         */
//        QueryWrapper<OtcMsgHistoryEntity> objectQueryWrapper = new QueryWrapper<>();
//        objectQueryWrapper.eq("member_id",memberId);
//        objectQueryWrapper.eq("target_id",targetId);
//        OtcMsgUserListEntity otcMsgUserListEntity = otcMsgUserListDao.selectById(objectQueryWrapper);
        List<OtcMsgUserListEntity> otcMsgUserListEntitys = otcMsgUserListDao.selectListByMemberIdAndTargetId(memberId,targetId);
        if(CollUtil.isEmpty(otcMsgUserListEntitys)){
            OtcMsgUserListEntity otcMsgUserListEntity = new OtcMsgUserListEntity();
            otcMsgUserListEntity.setMemberId(memberId);
            otcMsgUserListEntity.setTargetId(targetId);
            otcMsgUserListEntity.setIsRead(OtcMsgUserListEntity.ISREAD_ONE);
            otcMsgUserListEntity.setLastMsgTime(DateUtil.date());
            otcMsgUserListDao.insert(otcMsgUserListEntity);
        }else{
            OtcMsgUserListEntity otcMsgUserListEntity = otcMsgUserListEntitys.get(0);
            otcMsgUserListEntity.setMemberId(memberId);
            otcMsgUserListEntity.setTargetId(targetId);
            otcMsgUserListEntity.setIsRead(OtcMsgUserListEntity.ISREAD_ONE);
            otcMsgUserListEntity.setLastMsgTime(DateUtil.date());
            otcMsgUserListDao.updateById(otcMsgUserListEntity);
        }
 
        //历史消息中增加新纪录
        OtcMsgHistoryEntity otcMsgHistoryEntity = new OtcMsgHistoryEntity();
        otcMsgHistoryEntity.setMemberId(memberId);
        otcMsgHistoryEntity.setTargetId(targetId);
        otcMsgHistoryEntity.setMsg(msg);
        otcMsgHistoryEntity.setMsgType(msgType);
        otcMsgHistoryDao.insert(otcMsgHistoryEntity);
 
        //增加一个提醒的
        MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(targetId);
        memberSettingEntity.setMessageReminder(1);
        memberSettingDao.updateById(memberSettingEntity);
        return Result.ok("发送成功");
    }
 
    @Override
    public Result getChatBoxConnect(ConnectDto connectDto) {
 
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        Long memberId = member.getId();
//        Long memberId = 443L;
        long targetId = connectDto.getTargetId();
        if(ObjectUtil.isEmpty(targetId)){
            return Result.fail("请返回重试");
        }
        IPage<ChatBoxVo> chatBoxVos= new Page<>();
        List<OtcMsgUserListEntity> otcMsgUserListEntitys = otcMsgUserListDao.selectListByMemberIdAndTargetId(memberId,targetId);
        if(CollUtil.isNotEmpty(otcMsgUserListEntitys)){
            Page<ChatBoxVo> page = new Page<>(connectDto.getPageNum(), connectDto.getPageSize());
            OtcMsgHistoryEntity otcMsgHistoryEntity = new OtcMsgHistoryEntity();
            otcMsgHistoryEntity.setMemberId(memberId);
            otcMsgHistoryEntity.setTargetId(targetId);
            chatBoxVos = otcMsgHistoryDao.getChatBoxMsgList(page,otcMsgHistoryEntity);
            List<ChatBoxVo> records = chatBoxVos.getRecords();
            if(CollUtil.isNotEmpty(records)){
                for(ChatBoxVo chatBoxVo : records){
                    long memberIds = chatBoxVo.getMemberId();
                    if(memberIds == memberId){
                        chatBoxVo.setIsSelf(1);
                    }else{
                        chatBoxVo.setIsSelf(2);
                    }
                }
            }
        }
        return Result.ok(chatBoxVos);
    }
 
 
}