OWL ITS + 탐지시스템(인터넷 진흥원)
jhjang
2021-10-14 d680ff9fa4298ad3c0cd12f5f9d87f6c51110480
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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.Faq;
import kr.wisestone.owl.domain.Guide;
import kr.wisestone.owl.domain.User;
import kr.wisestone.owl.exception.OwlRuntimeException;
import kr.wisestone.owl.mapper.FaqMapper;
import kr.wisestone.owl.repository.FaqRepository;
import kr.wisestone.owl.service.FaqService;
import kr.wisestone.owl.service.UserService;
import kr.wisestone.owl.util.ConvertUtil;
import kr.wisestone.owl.vo.FaqVo;
import kr.wisestone.owl.vo.ResPage;
import kr.wisestone.owl.web.condition.FaqCondition;
import kr.wisestone.owl.web.form.FaqForm;
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 FaqServiceImpl extends AbstractServiceImpl<Faq, Long, JpaRepository<Faq, Long>> implements FaqService {
 
    @Autowired
    private FaqRepository faqRepository;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private KafkaSender kafkaSender;
 
    @Autowired
    private FaqMapper faqMapper;
 
    @Override
    protected JpaRepository<Faq, Long> getRepository() {
        return this.faqRepository;
    }
 
    //  공지 사항 등록
    @Override
    @Transactional
    public Faq addFaq(FaqForm faqForm) {
        //  faq 제목 및 내용 공백 체크
        this.verifyTitleAndDescription(faqForm.getTitle(), faqForm.getDescription());
 
        faqForm.setStatus(Faq.INACTIVATION);
        Faq faq = ConvertUtil.copyProperties(faqForm, Faq.class);
 
        return this.faqRepository.saveAndFlush(faq);
    }
 
    //  faq 제목 및 내용 공백 체크
    private void verifyTitleAndDescription(String title, String description) {
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(description)) {
            throw new OwlRuntimeException(this.messageAccessor.getMessage(MsgConstants.FAQ_EMPTY_CONTENT));
        }
    }
 
    //  faq 조회
    @Override
    @Transactional(readOnly = true)
    public List<FaqVo> findFaq(Map<String, Object> resJsonData,
                               FaqCondition faqCondition, Pageable pageable) {
 
        faqCondition.setPage(pageable.getPageNumber() * pageable.getPageSize());
        faqCondition.setPageSize(pageable.getPageSize());
        faqCondition.setTitle(faqCondition.getTitle());
 
        List<Map<String, Object>> results = this.faqMapper.find(faqCondition);
        Long totalCount = this.faqMapper.count(faqCondition);
        int totalPage = (int) Math.ceil((totalCount - 1) / pageable.getPageSize()) + 1;
        List<FaqVo> faqVos = ConvertUtil.convertListToListClass(results, FaqVo.class);
 
        //  faq 아이디 1 은 관리자 - 관리자만 수정 가능
        for (FaqVo faqVo : faqVos) {
            Boolean bActivation = false;
 
            if(faqVo.getStatus().equals(Faq.ACTIVATION)) {
                bActivation = true;
            }
            faqVo.activation =  bActivation;
        }
 
        resJsonData.put(Constants.RES_KEY_CONTENTS, faqVos);
        resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
                totalPage, totalCount));
 
        return faqVos;
    }
 
    //  faq 수정
    @Override
    @Transactional
    public Faq modifyFaq(FaqForm faqForm) {
        //  공지사항 제목 및 내용 공백 체크
        this.verifyTitleAndDescription(faqForm.getTitle(), faqForm.getDescription());
 
        Faq faq = this.getFaq(faqForm.getId());
        ConvertUtil.copyProperties(faqForm, faq, "id");
 
        return this.faqRepository.saveAndFlush(faq);
    }
 
    //  faq 수정
    @Override
    @Transactional
    public Faq activeFaq(FaqForm faqForm) {
 
        boolean bActivation = faqForm.getActivation();
 
        if(bActivation) {
            faqForm.setStatus(Faq.ACTIVATION);
        } else {
            faqForm.setStatus(Faq.INACTIVATION);
        }
 
        Faq faq = this.getFaq(faqForm.getId());
        ConvertUtil.copyProperties(faqForm, faq, "id");
 
        return this.faqRepository.saveAndFlush(faq);
    }
 
    //  faq id 로 조회한다.
    @Override
    @Transactional(readOnly = true)
    public Faq getFaq(Long id) {
        if (id == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.FAQ_NOT_EXIST));
        }
 
        Faq faq = this.findOne(id);
 
        if (faq == null) {
            throw new OwlRuntimeException(
                    this.messageAccessor.getMessage(MsgConstants.FAQ_NOT_EXIST));
        }
 
        return faq;
    }
 
    //  faq 상세 정보를 조회한다.
    @Override
    @Transactional(readOnly = true)
    public void detailFaq(Map<String, Object> resJsonData, FaqCondition faqCondition) {
        FaqVo faqVo = new FaqVo();
 
        if (faqCondition.getId() != null) {
            Faq faq = this.getFaq(faqCondition.getId());
            faqVo = ConvertUtil.copyProperties(faq, FaqVo.class);
        }
 
        resJsonData.put(Constants.RES_KEY_CONTENTS, faqVo);
    }
}