| 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); | 
|     } | 
| } |