package com.yami.shop.api.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.yami.shop.bean.app.dto.NoticeDto; import com.yami.shop.bean.enums.NoticeType; import com.yami.shop.bean.model.Notice; import com.yami.shop.bean.model.NoticeUserLog; import com.yami.shop.common.enums.StatusEnum; import com.yami.shop.common.response.ServerResponseEntity; import com.yami.shop.common.util.PageParam; import com.yami.shop.security.api.model.YamiUser; import com.yami.shop.security.api.util.SecurityUtils; import com.yami.shop.security.common.bo.UserInfoInTokenBO; import com.yami.shop.security.common.util.AuthUserContext; import com.yami.shop.service.NoticeService; import com.yami.shop.service.NoticeUserLogService; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Operation; import lombok.AllArgsConstructor; import ma.glasnost.orika.MapperFacade; import org.springframework.web.bind.annotation.*; import java.security.Security; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @RestController @RequestMapping("/p/notice") @Tag(name = "用户登录状态的公告管理接口") @AllArgsConstructor public class MyNoticeController { private final NoticeService noticeService; private final NoticeUserLogService noticeUserLogService; private final MapperFacade mapperFacade; @GetMapping("/topNoticeList/{shopId}") @Operation(summary = "置顶公告列表信息", description = "获取所有置顶公告列表信息") @Parameter(name = "shopId", description = "店铺id", required = true) public ServerResponseEntity> getTopNoticeListByShopId(@PathVariable("shopId") Long shopId) { List noticeList = noticeService.listTopNoticeByShopId(shopId); //筛选出立即发送和到时间定时发送的公告 List list = noticeList.stream().filter(noticeVO -> Objects.equals(noticeVO.getImmediatelySend(), 1) || (Objects.equals(noticeVO.getImmediatelySend(), 0) && noticeVO.getSendTime().before(new Date()))).collect(Collectors.toList()); List noticeDtoList = mapperFacade.mapAsList(list, NoticeDto.class); UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get(); List result = new ArrayList<>(); String accountId = ""; //登录状态显示 指定全部用户和指定用户可见的公告 if (Objects.nonNull(userInfoInTokenBO)) { accountId = userInfoInTokenBO.getUserId(); String userId = String.valueOf(accountId); result = noticeDtoList.stream().filter(notice -> Objects.isNull(notice.getUserIds()) || (Objects.nonNull(notice.getUserIds()) && notice.getUserIds().contains(userId))).collect(Collectors.toList()); } else { //未登录状态只显示 全部用户可见的公告 result = noticeDtoList.stream().filter(notice -> Objects.isNull(notice.getUserIds())).collect(Collectors.toList()); } return ServerResponseEntity.success(result); } @GetMapping("/info/{id}") @Operation(summary = "公告详情", description = "获取公告id公告详情") @Parameter(name = "id", description = "公告id", required = true) public ServerResponseEntity getNoticeById(@PathVariable("id") Long id) { Notice notice = noticeService.getNoticeById(id); NoticeDto noticeDto = mapperFacade.map(notice, NoticeDto.class); return ServerResponseEntity.success(noticeDto); } /** * 公告列表 */ @GetMapping("/noticeList/{shopId}") @Operation(summary = "公告列表信息", description = "获取所有公告列表信息") @Parameter(name = "shopId", description = "店铺id", required = true) public ServerResponseEntity> pageNotice(@PathVariable("shopId") Long shopId, PageParam page) { Notice notice = new Notice(); notice.setShopId(shopId); notice.setType(NoticeType.TO_USER.value()); UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get(); notice.setAccountId(Objects.nonNull(userInfoInTokenBO) ? userInfoInTokenBO.getUserId() : null); notice.setSendTime(new Date()); notice.setStatus(StatusEnum.ENABLE.value()); return ServerResponseEntity.success(noticeService.pageNotice(page, notice)); } /** * 用户已读公告记录 */ @PostMapping("/readNoticeLog") @Operation(summary = "用户已读公告记录", description = "用户已读公告记录") public ServerResponseEntity readNoticeLog(@RequestBody NoticeDto noticeDto) { String userId = SecurityUtils.getUser().getUserId(); NoticeUserLog noticeUserLog = noticeUserLogService.getOne(Wrappers.lambdaQuery(NoticeUserLog.class) .eq(NoticeUserLog::getNoticeId, noticeDto.getId()) .eq(NoticeUserLog::getUserId, userId)); if (noticeUserLog != null) { return ServerResponseEntity.success(); } noticeUserLog = new NoticeUserLog(); noticeUserLog.setNoticeId( noticeDto.getId()); noticeUserLog.setUserId(userId); noticeUserLogService.save(noticeUserLog); return ServerResponseEntity.success(); } /** * 查询最新的公告 */ @GetMapping("/newNotice") @Operation(summary = "查询最新的公告", description = "查询最新的公告") public ServerResponseEntity getNewNotice() { NoticeDto newNotice = noticeService.getNewNotice(); YamiUser user = SecurityUtils.getUser(); if (user != null) { NoticeUserLog noticeUserLog = noticeUserLogService.getOne(Wrappers.lambdaQuery(NoticeUserLog.class) .eq(NoticeUserLog::getNoticeId, newNotice.getId()) .eq(NoticeUserLog::getUserId, user.getUserId())); if (noticeUserLog != null) { return ServerResponseEntity.success(); } } return ServerResponseEntity.success(newNotice); } }