package kr.wisestone.owl.service.impl;
|
|
import kr.wisestone.owl.constant.Constants;
|
import kr.wisestone.owl.constant.MsgConstants;
|
import kr.wisestone.owl.domain.Event;
|
import kr.wisestone.owl.exception.OwlRuntimeException;
|
import kr.wisestone.owl.mapper.EventMapper;
|
import kr.wisestone.owl.repository.EventRepository;
|
import kr.wisestone.owl.service.EventService;
|
import kr.wisestone.owl.util.ConvertUtil;
|
import kr.wisestone.owl.vo.EventVo;
|
import kr.wisestone.owl.vo.ResPage;
|
import kr.wisestone.owl.web.condition.EventCondition;
|
import kr.wisestone.owl.web.form.EventForm;
|
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.List;
|
import java.util.Map;
|
|
@Service
|
public class EventServiceImpl extends AbstractServiceImpl<Event, Long, JpaRepository<Event, Long>> implements EventService {
|
|
@Autowired
|
private EventRepository eventRepository;
|
|
@Autowired
|
private EventMapper eventMapper;
|
|
@Override
|
protected JpaRepository<Event, Long> getRepository() {
|
return this.eventRepository;
|
}
|
|
// Event 등록
|
@Override
|
@Transactional
|
public Event addEvent(EventForm eventForm) {
|
// Event 제목 및 내용 공백 체크
|
this.verifyTitleAndDescription(eventForm.getTitle(), eventForm.getDescription());
|
|
eventForm.setStatus(Event.INACTIVATION);
|
Event event = ConvertUtil.copyProperties(eventForm, Event.class);
|
|
return this.eventRepository.saveAndFlush(event);
|
}
|
|
// Event 제목 및 내용 공백 체크
|
private void verifyTitleAndDescription(String title, String description) {
|
if (StringUtils.isEmpty(title) || StringUtils.isEmpty(description)) {
|
throw new OwlRuntimeException(this.messageAccessor.getMessage(MsgConstants.EVENT_EMPTY_CONTENT));
|
}
|
}
|
|
// Event 조회
|
@Override
|
@Transactional(readOnly = true)
|
public List<EventVo> findEvent(Map<String, Object> resJsonData,
|
EventCondition eventCondition, Pageable pageable) {
|
|
eventCondition.setPage(pageable.getPageNumber() * pageable.getPageSize());
|
eventCondition.setPageSize(pageable.getPageSize());
|
eventCondition.setTitle(eventCondition.getTitle());
|
|
List<Map<String, Object>> results = this.eventMapper.find(eventCondition);
|
Long totalCount = this.eventMapper.count(eventCondition);
|
int totalPage = (int) Math.ceil((totalCount - 1) / pageable.getPageSize()) + 1;
|
List<EventVo> eventVos = ConvertUtil.convertListToListClass(results, EventVo.class);
|
|
for (EventVo eventVo : eventVos) {
|
Boolean bActivation = false;
|
|
if(eventVo.getStatus().equals(Event.ACTIVATION)) {
|
bActivation = true;
|
}
|
eventVo.activation = bActivation;
|
}
|
|
resJsonData.put(Constants.RES_KEY_CONTENTS, eventVos);
|
resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
|
totalPage, totalCount));
|
|
return eventVos;
|
}
|
|
// Event 수정
|
@Override
|
@Transactional
|
public Event modifyEvent(EventForm eventForm) {
|
// Event 제목 및 내용 공백 체크
|
this.verifyTitleAndDescription(eventForm.getTitle(), eventForm.getDescription());
|
|
Event event = this.getEvent(eventForm.getId());
|
ConvertUtil.copyProperties(eventForm, event, "id");
|
|
return this.eventRepository.saveAndFlush(event);
|
}
|
|
// Event 수정
|
@Override
|
@Transactional
|
public Event activeEvent(EventForm eventForm) {
|
|
boolean bActivation = eventForm.getActivation();
|
|
if(bActivation) {
|
eventForm.setStatus(Event.ACTIVATION);
|
} else {
|
eventForm.setStatus(Event.INACTIVATION);
|
}
|
|
Event event = this.getEvent(eventForm.getId());
|
ConvertUtil.copyProperties(eventForm, event, "id");
|
|
if(bActivation) {
|
this.eventRepository.updateInActivation(eventForm.getId());
|
}
|
|
return this.eventRepository.saveAndFlush(event);
|
}
|
|
// Event을 id 로 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public Event getEvent(Long id) {
|
if (id == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.EVENT_NOT_EXIST));
|
}
|
|
Event event = this.findOne(id);
|
|
if (event == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.EVENT_NOT_EXIST));
|
}
|
|
return event;
|
}
|
|
// Event 상세 정보를 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public void detailEvent(Map<String, Object> resJsonData, EventCondition eventCondition) {
|
EventVo eventVo = new EventVo();
|
|
if (eventCondition.getId() != null) {
|
Event event = this.getEvent(eventCondition.getId());
|
eventVo = ConvertUtil.copyProperties(event, EventVo.class);
|
}
|
|
resJsonData.put(Constants.RES_KEY_CONTENTS, eventVo);
|
}
|
}
|