package com.example.jz.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.jz.auth.TokenJwtManager; import com.example.jz.dao.AnnouncementDao; import com.example.jz.dao.UserDao; import com.example.jz.modle.PageParam; import com.example.jz.modle.entity.Announcement; import com.example.jz.modle.entity.User; import com.example.jz.modle.vo.AnnouncementVo; import com.example.jz.service.AnnouncementService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * 公告表(Announcement)表服务实现类 * * @author makejava * @since 2022-07-11 16:35:56 */ @Service("announcementService") public class AnnouncementServiceImpl extends ServiceImpl implements AnnouncementService { @Resource AnnouncementDao announcementDao; @Resource UserDao userDao; @Autowired TokenJwtManager tokenJwtManager; @Override public PageParam getAnnouncements(Integer size, Integer current, String content, Integer status, Integer groupId) { PageParam announcementPageParam = new PageParam<>(); announcementPageParam.setSize(size); announcementPageParam.setCurrent(current); QueryWrapper announcementQueryWrapper = new QueryWrapper<>(); if (StringUtils.isNotBlank(content)) { announcementQueryWrapper.like("text", content); } if (status != null) { announcementQueryWrapper.eq("status", status); } announcementQueryWrapper.eq("group_id", groupId); announcementQueryWrapper.orderByDesc("ctime"); PageParam announcementPageParamList = announcementDao.selectPage(announcementPageParam, announcementQueryWrapper); List announcementVoList = announcementPageParamList.getRecords().stream().map( a -> { AnnouncementVo announcementVo = new AnnouncementVo(); BeanUtils.copyProperties(a, announcementVo); announcementVo.setCreateName(userDao.selectOne(new QueryWrapper().eq("id", a.getCreator())).getRealName()); return announcementVo; } ).collect(Collectors.toList()); PageParam announcementVoPageParam = new PageParam<>(); BeanUtils.copyProperties(announcementPageParamList, announcementVoPageParam); announcementVoPageParam.setRecords(announcementVoList); return announcementVoPageParam; } @Override public List getAnnouncements(Integer groupId) { List announcements = announcementDao.selectList(Wrappers.lambdaQuery().eq(Announcement::getGroupId, groupId).orderByDesc(Announcement::getPublishTime)); return announcements; } @Override public void updateStatus(Integer id,Integer status) { if (status==1){ announcementDao.update(new Announcement().setStatus(2), new UpdateWrapper().eq("id", id)); }else { Announcement announcement = new Announcement(); announcement.setStatus(1); announcement.setPublishTime(new Date()); announcementDao.update(announcement, new UpdateWrapper().eq("id", id)); } } @Override public Integer add(Integer groupId, Announcement announcement) { announcement.setCreator(userDao.selectOne(new QueryWrapper().eq("login_username", SecurityContextHolder.getContext().getAuthentication().getPrincipal())).getId()); if(announcement.getStatus()==1){ announcement.setPublishTime(new Date()); } announcement.setCtime(new Date()); announcement.setGroupId(groupId); return announcementDao.insert(announcement); } @Override public Integer vxAdd(Integer groupId, Announcement announcement) { // announcement.setCreator(userDao.selectOne(new QueryWrapper().eq("login_username", SecurityContextHolder.getContext().getAuthentication().getPrincipal())).getId()); announcement.setPublishTime(new Date()); announcement.setCtime(new Date()); announcement.setGroupId(groupId); return announcementDao.insert(announcement); } @Override public AnnouncementVo getAnnouncementsById(Integer id) { AnnouncementVo announcementVo = new AnnouncementVo(); Announcement announcement = announcementDao.selectById(id); User user = userDao.selectOne(new QueryWrapper().eq("id", announcement.getCreator())); BeanUtils.copyProperties(announcement,announcementVo); announcementVo.setCreateName(user.getRealName()); return announcementVo; } }