package com.ycl.jxkg.controller.admin; import com.ycl.jxkg.base.BaseApiController; import com.ycl.jxkg.base.Result; import com.ycl.jxkg.domain.entity.Message; import com.ycl.jxkg.domain.entity.MessageUser; import com.ycl.jxkg.domain.entity.User; import com.ycl.jxkg.service.MessageService; import com.ycl.jxkg.service.UserService; import com.ycl.jxkg.utils.DateTimeUtil; import com.ycl.jxkg.utils.PageInfoHelper; import com.ycl.jxkg.domain.vo.admin.message.MessagePageRequestVO; import com.ycl.jxkg.domain.vo.admin.message.MessageResponseVO; import com.ycl.jxkg.domain.vo.admin.message.MessageSendVO; import com.github.pagehelper.PageInfo; import lombok.RequiredArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @RequiredArgsConstructor @RestController("AdminMessageController") @RequestMapping(value = "/api/admin/message") public class MessageController extends BaseApiController { private final MessageService messageService; private final UserService userService; @RequestMapping(value = "/page", method = RequestMethod.POST) public Result> pageList(@RequestBody MessagePageRequestVO model) { PageInfo pageInfo = messageService.page(model); List ids = pageInfo.getList().stream().map(d -> d.getId()).collect(Collectors.toList()); List messageUsers = ids.size() == 0 ? null : messageService.selectByMessageIds(ids); PageInfo page = PageInfoHelper.copyMap(pageInfo, m -> { MessageResponseVO vo = new MessageResponseVO(); BeanUtils.copyProperties(m, vo); String receives = messageUsers.stream().filter(d -> d.getMessageId().equals(m.getId())).map(d -> d.getReceiveUserName()) .collect(Collectors.joining(",")); vo.setReceives(receives); vo.setCreateTime(DateTimeUtil.dateFormat(m.getCreateTime())); return vo; }); return Result.ok(page); } @RequestMapping(value = "/send", method = RequestMethod.POST) public Result send(@RequestBody @Valid MessageSendVO model) { User user = getCurrentUser(); List receiveUser = userService.selectByIds(model.getReceiveUserIds()); Date now = new Date(); Message message = new Message(); message.setTitle(model.getTitle()); message.setContent(model.getContent()); message.setCreateTime(now); message.setReadCount(0); message.setReceiveUserCount(receiveUser.size()); message.setSendUserId(user.getId()); message.setSendUserName(user.getUserName()); message.setSendRealName(user.getRealName()); List messageUsers = receiveUser.stream().map(d -> { MessageUser messageUser = new MessageUser(); messageUser.setCreateTime(now); messageUser.setReaded(false); messageUser.setReceiveRealName(d.getRealName()); messageUser.setReceiveUserId(d.getId()); messageUser.setReceiveUserName(d.getUserName()); return messageUser; }).collect(Collectors.toList()); messageService.sendMessage(message, messageUsers); return Result.ok(); } }