cloudroam
3 天以前 3a819e4f668c15e8b77b188b322470da12bb7a43
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
package com.mzl.flower.service.system;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.system.CreateMessageDTO;
import com.mzl.flower.dto.request.system.SearchMessageDTO;
import com.mzl.flower.dto.response.system.UserMessageDTO;
import com.mzl.flower.entity.system.UserMessage;
import com.mzl.flower.mapper.system.UserMessageMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.utils.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
@Transactional
@Slf4j
public class UserMessageService extends BaseService {
 
    @Autowired
    private UserMessageMapper userMessageMapper;
 
    @Async
    public void saveUserMsg(List<String> users, CreateMessageDTO dto, String createdBy) {
        try {
            if (CollectionUtils.isNotEmpty(users)) {
                List<UserMessage> result = new ArrayList<>();
                for (String userId : users) {
                    UserMessage m = new UserMessage();
                    BeanUtils.copyProperties(dto, m);
                    m.setId(UUIDGenerator.getUUID());
                    m.setUserId(userId);
                    m.setRead(false);
                    m.create(createdBy);
                    result.add(m);
                }
                if (CollectionUtils.isNotEmpty(result)) {
                    userMessageMapper.batchInsert(result);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
 
    public String sendMsg(String userId, CreateMessageDTO dto, String createdBy){
        UserMessage m = new UserMessage();
        BeanUtils.copyProperties(dto, m);
        m.setId(UUIDGenerator.getUUID());
        m.setUserId(userId);
        m.setRead(false);
        m.create(createdBy);
        userMessageMapper.insert(m);
 
        return m.getId();
    }
 
    public Page<UserMessageDTO> selectUserMessage(Page page, SearchMessageDTO dto) {
        dto.setUserId(SecurityUtils.getUserId());
        List<UserMessageDTO> list = userMessageMapper.selectUserMessage(page, dto);
 
        page.setRecords(list);
        return page;
    }
 
    public UserMessageDTO detail(String id){
        UserMessageDTO dto = new UserMessageDTO();
        UserMessage userMsg = userMessageMapper.selectById(id);
        if(userMsg != null){
            BeanUtils.copyProperties(userMsg, dto);
            if(!userMsg.getRead()){
                userMsg.setRead(true);
                userMessageMapper.updateById(userMsg);
            }
        }
        return dto;
    }
 
    public int getUnReadCount() {
        String userId = SecurityUtils.getUserId();
        Integer count = userMessageMapper.selectCount(new QueryWrapper<UserMessage>()
                .eq("user_id", userId)
                .eq("`read`", 0));
        return count;
    }
 
    public void setMsgRead() {
        String userId = SecurityUtils.getUserId();
        userMessageMapper.setMessageRead(userId);
    }
 
    public void setMsgRead(String id) {
        String userId = SecurityUtils.getUserId();
        UserMessage uc = userMessageMapper.selectById(id);
        if (!userId.equals(uc.getUserId())) {
            throw new ValidationException("无权操作");
        }
 
        uc.setRead(true);
        uc.update(userId);
        userMessageMapper.updateById(uc);
    }
}