| | |
| | | package cn.iocoder.yudao.module.system.service.notice; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeImportExcelVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeImportReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeImportRespVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeSaveReqVO; |
| | | import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; |
| | |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * 通知公告 Service 实现类 |
| | |
| | | return noticeMapper.selectById(id); |
| | | } |
| | | |
| | | @Override |
| | | public NoticeImportRespVO importNoticeList(List<NoticeImportExcelVO> importNotices, NoticeImportReqVO importReqVO) { |
| | | importNotices = filterList(importNotices, item -> StrUtil.isNotBlank(item.getTitle())); |
| | | if (CollUtil.isEmpty(importNotices)) { |
| | | throw exception(NOTICE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | NoticeImportRespVO respVO = NoticeImportRespVO.builder() |
| | | .createNoticeNames(new ArrayList<>()) |
| | | .updateNoticeNames(new ArrayList<>()) |
| | | .failureNoticeNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importNotices.forEach(importNotice -> { |
| | | try { |
| | | // 1. 公告类型必填 |
| | | if (importNotice.getType() == null) { |
| | | throw exception(NOTICE_TYPE_NOT_EXISTS); |
| | | } |
| | | // 2. 判断是否存在(按标题) |
| | | NoticeDO existNotice = noticeMapper.selectByTitle(importNotice.getTitle()); |
| | | if (existNotice == null) { |
| | | // 2.1 新增 |
| | | NoticeDO notice = BeanUtils.toBean(importNotice, NoticeDO.class) |
| | | .setStatus(ObjUtil.defaultIfNull(importNotice.getStatus(), CommonStatusEnum.ENABLE.getStatus())); |
| | | noticeMapper.insert(notice); |
| | | respVO.getCreateNoticeNames().add(importNotice.getTitle()); |
| | | return; |
| | | } |
| | | // 2.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureNoticeNames().put(importNotice.getTitle(), |
| | | StrUtil.format(NOTICE_TITLE_DUPLICATE.getMsg(), importNotice.getTitle())); |
| | | return; |
| | | } |
| | | NoticeDO updateNotice = BeanUtils.toBean(importNotice, NoticeDO.class) |
| | | .setId(existNotice.getId()); |
| | | noticeMapper.updateById(updateNotice); |
| | | respVO.getUpdateNoticeNames().add(importNotice.getTitle()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureNoticeNames().put(importNotice.getTitle(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | @VisibleForTesting |
| | | public void validateNoticeExists(Long id) { |
| | | if (id == null) { |