package kr.wisestone.owl.service.impl;
|
|
import kr.wisestone.owl.config.kafka.KafkaSender;
|
import kr.wisestone.owl.constant.Constants;
|
import kr.wisestone.owl.constant.MsgConstants;
|
import kr.wisestone.owl.domain.Notice;
|
import kr.wisestone.owl.domain.User;
|
import kr.wisestone.owl.exception.OwlRuntimeException;
|
import kr.wisestone.owl.mapper.NoticeMapper;
|
import kr.wisestone.owl.repository.NoticeRepository;
|
import kr.wisestone.owl.service.NoticeService;
|
import kr.wisestone.owl.service.UserService;
|
import kr.wisestone.owl.util.ConvertUtil;
|
import kr.wisestone.owl.vo.NoticeVo;
|
import kr.wisestone.owl.vo.ResPage;
|
import kr.wisestone.owl.web.condition.NoticeCondition;
|
import kr.wisestone.owl.web.form.NoticeForm;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@Service
|
public class NoticeServiceImpl extends AbstractServiceImpl<Notice, Long, JpaRepository<Notice, Long>> implements NoticeService {
|
|
@Autowired
|
private NoticeRepository noticeRepository;
|
|
@Autowired
|
private UserService userService;
|
|
@Autowired
|
private KafkaSender kafkaSender;
|
|
@Autowired
|
private NoticeMapper noticeMapper;
|
|
@Override
|
protected JpaRepository<Notice, Long> getRepository() {
|
return this.noticeRepository;
|
}
|
|
// 공지 사항 등록
|
@Override
|
@Transactional
|
public Notice addNotice(NoticeForm noticeForm) {
|
// 공지사항 제목 및 내용 공백 체크
|
this.verifyTitleAndDescription(noticeForm.getTitle(), noticeForm.getDescription());
|
|
Notice notice = ConvertUtil.copyProperties(noticeForm, Notice.class);
|
|
return this.noticeRepository.saveAndFlush(notice);
|
}
|
|
// 공지사항 제목 및 내용 공백 체크
|
private void verifyTitleAndDescription(String title, String description) {
|
if (StringUtils.isEmpty(title) || StringUtils.isEmpty(description)) {
|
throw new OwlRuntimeException(this.messageAccessor.getMessage(MsgConstants.NOTICE_EMPTY_CONTENT));
|
}
|
}
|
|
// 공지사항 조회
|
@Override
|
@Transactional(readOnly = true)
|
public List<NoticeVo> findNotice(Map<String, Object> resJsonData,
|
NoticeCondition noticeCondition, Pageable pageable) {
|
|
noticeCondition.setPage(pageable.getPageNumber() * pageable.getPageSize());
|
noticeCondition.setPageSize(pageable.getPageSize());
|
noticeCondition.setTitle(noticeCondition.getTitle());
|
|
List<Map<String, Object>> results = this.noticeMapper.find(noticeCondition);
|
Long totalCount = this.noticeMapper.count(noticeCondition);
|
int totalPage = (int) Math.ceil((totalCount - 1) / pageable.getPageSize()) + 1;
|
List<NoticeVo> noticeVos = ConvertUtil.convertListToListClass(results, NoticeVo.class);
|
|
// 로그인 아이디 1 은 관리자 - 관리자만 수정 가능
|
for (NoticeVo noticeVo : noticeVos) {
|
if (this.webAppUtil.getLoginId().equals(1L)) {
|
noticeVo.setModifyPermissionCheck(true);
|
}
|
}
|
|
resJsonData.put(Constants.RES_KEY_CONTENTS, noticeVos);
|
resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
|
totalPage, totalCount));
|
|
return noticeVos;
|
}
|
|
// 공지사항 수정
|
@Override
|
@Transactional
|
public Notice modifyNotice(NoticeForm noticeForm) {
|
// 공지사항 제목 및 내용 공백 체크
|
this.verifyTitleAndDescription(noticeForm.getTitle(), noticeForm.getDescription());
|
|
Notice notice = this.getNotice(noticeForm.getId());
|
ConvertUtil.copyProperties(noticeForm, notice, "id");
|
|
return this.noticeRepository.saveAndFlush(notice);
|
}
|
|
// 공지사항을 id 로 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public Notice getNotice(Long id) {
|
if (id == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.NOTICE_NOT_EXIST));
|
}
|
|
Notice notice = this.findOne(id);
|
|
if (notice == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.NOTICE_NOT_EXIST));
|
}
|
|
return notice;
|
}
|
|
// 공지사항 상세 정보를 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public void detailNotice(Map<String, Object> resJsonData, NoticeCondition noticeCondition) {
|
NoticeVo noticeVo = new NoticeVo();
|
|
if (noticeCondition.getId() != null) {
|
Notice notice = this.getNotice(noticeCondition.getId());
|
noticeVo = ConvertUtil.copyProperties(notice, NoticeVo.class);
|
}
|
|
resJsonData.put(Constants.RES_KEY_CONTENTS, noticeVo);
|
}
|
|
// 메세지 보내기
|
@Override
|
@Transactional
|
public void sendNotice(NoticeForm noticeForm) {
|
for (Long userId : noticeForm.getUserIds()) {
|
User user = this.userService.getUser(userId);
|
|
Map<String, Object> message = new HashMap<>();
|
message.put("url", "/notification/message");
|
message.put("message", noticeForm.getDescription());
|
message.put("account", user.getAccount());
|
this.kafkaSender.send("common-topic", message);
|
}
|
}
|
}
|