package kr.wisestone.owl.service.impl;
|
|
import kr.wisestone.owl.constant.Constants;
|
import kr.wisestone.owl.constant.MsgConstants;
|
import kr.wisestone.owl.domain.Guide;
|
import kr.wisestone.owl.exception.OwlRuntimeException;
|
import kr.wisestone.owl.mapper.GuideMapper;
|
import kr.wisestone.owl.repository.GuideRepository;
|
import kr.wisestone.owl.service.GuideService;
|
import kr.wisestone.owl.util.CommonUtil;
|
import kr.wisestone.owl.util.ConvertUtil;
|
import kr.wisestone.owl.vo.GuideVo;
|
import kr.wisestone.owl.vo.ManageUserVo;
|
import kr.wisestone.owl.vo.ResPage;
|
import kr.wisestone.owl.web.condition.GuideCondition;
|
import kr.wisestone.owl.web.form.GuideForm;
|
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 GuideServiceImpl extends AbstractServiceImpl<Guide, Long, JpaRepository<Guide, Long>> implements GuideService {
|
|
@Autowired
|
private GuideRepository guideRepository;
|
|
@Autowired
|
private GuideMapper guideMapper;
|
|
@Override
|
protected JpaRepository<Guide, Long> getRepository() {
|
return this.guideRepository;
|
}
|
|
// guide 등록
|
@Override
|
@Transactional
|
public Guide addGuide(GuideForm guideForm) {
|
// guide 제목 및 내용 공백 체크
|
this.verifyTitleAndDescription(guideForm.getTitle(), guideForm.getDescription());
|
|
guideForm.setStatus(Guide.INACTIVATION);
|
Guide guide = ConvertUtil.copyProperties(guideForm, Guide.class);
|
|
return this.guideRepository.saveAndFlush(guide);
|
}
|
|
// guide 제목 및 내용 공백 체크
|
private void verifyTitleAndDescription(String title, String description) {
|
if (StringUtils.isEmpty(title) || StringUtils.isEmpty(description)) {
|
throw new OwlRuntimeException(this.messageAccessor.getMessage(MsgConstants.GUIDE_EMPTY_CONTENT));
|
}
|
}
|
|
// guide 조회
|
@Override
|
@Transactional(readOnly = true)
|
public List<GuideVo> findGuide(Map<String, Object> resJsonData,
|
GuideCondition guideCondition, Pageable pageable) {
|
|
guideCondition.setPage(pageable.getPageNumber() * pageable.getPageSize());
|
guideCondition.setPageSize(pageable.getPageSize());
|
guideCondition.setTitle(guideCondition.getTitle());
|
|
List<Map<String, Object>> results = this.guideMapper.find(guideCondition);
|
Long totalCount = this.guideMapper.count(guideCondition);
|
int totalPage = (int) Math.ceil((totalCount - 1) / pageable.getPageSize()) + 1;
|
List<GuideVo> guideVos = ConvertUtil.convertListToListClass(results, GuideVo.class);
|
|
for (GuideVo guideVo : guideVos) {
|
Boolean bActivation = false;
|
|
if(guideVo.getStatus().equals(Guide.ACTIVATION)) {
|
bActivation = true;
|
}
|
guideVo.activation = bActivation;
|
}
|
|
resJsonData.put(Constants.RES_KEY_CONTENTS, guideVos);
|
resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
|
totalPage, totalCount));
|
|
return guideVos;
|
}
|
|
// guide 수정
|
@Override
|
@Transactional
|
public Guide modifyGuide(GuideForm guideForm) {
|
// guide 제목 및 내용 공백 체크
|
this.verifyTitleAndDescription(guideForm.getTitle(), guideForm.getDescription());
|
|
Guide guide = this.getGuide(guideForm.getId());
|
ConvertUtil.copyProperties(guideForm, guide, "id");
|
|
return this.guideRepository.saveAndFlush(guide);
|
}
|
|
// guide 수정
|
@Override
|
@Transactional
|
public Guide activeGuide(GuideForm guideForm) {
|
|
boolean bActivation = guideForm.getActivation();
|
|
if(bActivation) {
|
guideForm.setStatus(Guide.ACTIVATION);
|
} else {
|
guideForm.setStatus(Guide.INACTIVATION);
|
}
|
|
Guide guide = this.getGuide(guideForm.getId());
|
ConvertUtil.copyProperties(guideForm, guide, "id");
|
|
if(bActivation) {
|
this.guideRepository.updateInActivation(guideForm.getId());
|
}
|
|
return this.guideRepository.saveAndFlush(guide);
|
}
|
|
// guide을 id 로 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public Guide getGuide(Long id) {
|
if (id == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.GUIDE_NOT_EXIST));
|
}
|
|
Guide guide = this.findOne(id);
|
|
if (guide == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.GUIDE_NOT_EXIST));
|
}
|
|
return guide;
|
}
|
|
// guide 상세 정보를 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public void detailGuide(Map<String, Object> resJsonData, GuideCondition guideCondition) {
|
GuideVo guideVo = new GuideVo();
|
|
if (guideCondition.getId() != null) {
|
Guide guide = this.getGuide(guideCondition.getId());
|
guideVo = ConvertUtil.copyProperties(guide, GuideVo.class);
|
}
|
|
resJsonData.put(Constants.RES_KEY_CONTENTS, guideVo);
|
}
|
}
|