package kr.wisestone.owl.service.impl; import kr.wisestone.owl.common.ExcelConditionCheck; import kr.wisestone.owl.config.kafka.KafkaSender; import kr.wisestone.owl.constant.Constants; import kr.wisestone.owl.constant.MsgConstants; import kr.wisestone.owl.domain.Qna; import kr.wisestone.owl.exception.OwlRuntimeException; import kr.wisestone.owl.mapper.QnaMapper; import kr.wisestone.owl.repository.QnaRepository; import kr.wisestone.owl.service.QnaService; import kr.wisestone.owl.service.UserService; import kr.wisestone.owl.service.WorkspaceService; import kr.wisestone.owl.util.ConvertUtil; import kr.wisestone.owl.vo.*; import kr.wisestone.owl.web.condition.NoticeCondition; import kr.wisestone.owl.web.condition.QnaCondition; import kr.wisestone.owl.web.form.NoticeForm; import kr.wisestone.owl.web.form.QnaForm; import kr.wisestone.owl.web.view.ExcelView; 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 org.springframework.ui.Model; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class QnaServiceImpl extends AbstractServiceImpl> implements QnaService { @Autowired private QnaRepository qnaRepository; @Autowired private UserService userService; @Autowired private KafkaSender kafkaSender; @Autowired private QnaMapper qnaMapper; @Autowired private WorkspaceService workspaceService; @Autowired private ExcelView excelView; @Autowired private ExcelConditionCheck excelConditionCheck; @Override protected JpaRepository getRepository() { return this.qnaRepository; } // 공지 사항 등록 @Override @Transactional public Qna addQna(QnaForm qnaForm) { // Qna 제목 및 내용 공백 체크 this.verifyTitleAndDescription(qnaForm.getTitle(), qnaForm.getDescription()); Qna qna = ConvertUtil.copyProperties(qnaForm, Qna.class); return this.qnaRepository.saveAndFlush(qna); } // Qna 제목 및 내용 공백 체크 private void verifyTitleAndDescription(String title, String description) { if (StringUtils.isEmpty(title) || StringUtils.isEmpty(description)) { throw new OwlRuntimeException(this.messageAccessor.getMessage(MsgConstants.QNA_EMPTY_CONTENT)); } } // Qna 조회 @Override @Transactional(readOnly = true) public List findQna(Map resJsonData, QnaCondition qnaCondition, Pageable pageable) { qnaCondition.setPage(pageable.getPageNumber() * pageable.getPageSize()); qnaCondition.setPageSize(pageable.getPageSize()); qnaCondition.setTitle(qnaCondition.getTitle()); List> results = this.qnaMapper.find(qnaCondition); Long totalCount = this.qnaMapper.count(qnaCondition); int totalPage = (int) Math.ceil((totalCount - 1) / pageable.getPageSize()) + 1; List qnaVos = ConvertUtil.convertListToListClass(results, QnaVo.class); resJsonData.put(Constants.RES_KEY_CONTENTS, qnaVos); resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(), totalPage, totalCount)); return qnaVos; } // Qna 수정 @Override @Transactional public Qna modifyQna(QnaForm qnaForm) { // 공지사항 제목 및 내용 공백 체크 this.verifyTitleAndDescription(qnaForm.getTitle(), qnaForm.getDescription()); Qna qna = this.getQna(qnaForm.getId()); ConvertUtil.copyProperties(qnaForm, qna, "id"); return this.qnaRepository.saveAndFlush(qna); } // Qna id 로 조회한다. @Override @Transactional(readOnly = true) public Qna getQna(Long id) { if (id == null) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.QNA_NOT_EXIST)); } Qna qna = this.findOne(id); if (qna == null) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.QNA_NOT_EXIST)); } return qna; } // Qna 상세 정보를 조회한다. @Override @Transactional(readOnly = true) public void detailQna(Map resJsonData, QnaCondition qnaCondition) { QnaVo qnaVo = new QnaVo(); if (qnaCondition.getId() != null) { Qna qna = this.getQna(qnaCondition.getId()); qnaVo = ConvertUtil.copyProperties(qna, QnaVo.class); } resJsonData.put(Constants.RES_KEY_CONTENTS, qnaVo); } @Override public void remove(QnaForm qnaForm) { if (qnaForm.getRemoveIds().size() < 1) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.COMPANY_REMOVE_NOT_SELECT)); } for (Long id : qnaForm.getRemoveIds()) { this.qnaRepository.deleteById(id); this.qnaRepository.flush(); } } @Override public ModelAndView downloadExcel(HttpServletRequest request, Model model) { ModelAndView modelAndView = this.workspaceService.checkUseExcelDownload(model); if (modelAndView != null) { return modelAndView; } Map conditions = new HashMap<>(); // 엑셀 다운로드에 필요한 검색 조건 정보를 추출하고 검색 조건 추출에 오류가 발생하면 경고를 표시해준다. modelAndView = this.excelConditionCheck.checkCondition(conditions, request, model); if (modelAndView != null) { return modelAndView; } QnaCondition qnaCondition = QnaCondition.make(conditions); List> results = this.qnaMapper.find(qnaCondition); List qnaVos = ConvertUtil.convertListToListClass(results, QnaVo.class); // code_ko_KR 에 code명 설정 ExportExcelVo excelInfo = new ExportExcelVo(); excelInfo.setFileName(this.messageAccessor.message("QNA 목록")); excelInfo.addAttrInfos(new ExportExcelAttrVo("title", this.messageAccessor.message("qna.title"), 6, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("registerDate", this.messageAccessor.message("qna.registerDate"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("writer", this.messageAccessor.message("qna.registerId"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.setDatas(qnaVos); model.addAttribute(Constants.EXCEL, excelInfo); return new ModelAndView(this.excelView); } }