package kr.wisestone.owl.service.impl; import kr.wisestone.owl.domain.*; import kr.wisestone.owl.repository.HostingFieldRepository; import kr.wisestone.owl.repository.IspFieldRepository; import kr.wisestone.owl.service.*; import kr.wisestone.owl.web.condition.CompanyFieldCondition; import kr.wisestone.owl.web.form.CompanyFieldForm; import org.apache.commons.lang3.StringUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.Model; import com.google.common.collect.Lists; import kr.wisestone.owl.common.ExcelConditionCheck; import kr.wisestone.owl.constant.Constants; import kr.wisestone.owl.constant.MsgConstants; import kr.wisestone.owl.exception.OwlRuntimeException; import kr.wisestone.owl.mapper.CompanyFieldMapper; import kr.wisestone.owl.repository.CompanyFieldRepository; import kr.wisestone.owl.util.ConvertUtil; import kr.wisestone.owl.vo.*; import kr.wisestone.owl.web.view.ExcelView; 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.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @Service public class CompanyFieldServiceImpl extends AbstractServiceImpl> implements CompanyFieldService { @Autowired private CompanyFieldRepository companyFieldRepository; @Autowired private CompanyFieldMapper companyFieldMapper; @Autowired private IspFieldRepository ispFieldRepository; @Autowired private HostingFieldRepository hostingFieldRepository; @Autowired private IspFieldService ispFieldService; @Autowired private HostingFieldService hostingFieldService; @Autowired private UserService userService; @Autowired private WorkspaceService workspaceService; @Autowired private ExcelView excelView; @Autowired private ExcelConditionCheck excelConditionCheck; @Override protected JpaRepository getRepository() { return this.companyFieldRepository; } // 업체 추가 @Override public CompanyField addCompany(CompanyFieldForm companyFieldForm) { // url 유효성 체크 this.verifyUrl(companyFieldForm.getUrl(), null); CompanyField companyField = ConvertUtil.copyProperties(companyFieldForm, CompanyField.class); companyFieldRepository.saveAndFlush(companyField); return companyField; } // url 유효성 체크 private void verifyUrl(String url, Long id) { if (StringUtils.isEmpty(url)) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.COMPANYFIELD_NOT_URL)); } CompanyField companyField; if(id == null){ companyField = this.companyFieldRepository.findByUrl(url); } else { companyField = this.companyFieldRepository.findByUrlAndIdNot(url,id); } if (companyField != null) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.COMPANYFIELD_USED_URL)); } } // 업체 목록을 가져온다. @Override public List findCompany(Map resJsonData, CompanyFieldCondition condition, Pageable pageable) { condition.setPage(pageable.getPageNumber() * pageable.getPageSize()); condition.setPageSize(pageable.getPageSize()); List> results = this.companyFieldMapper.find(condition); Long totalCompanyCount = this.companyFieldMapper.count(condition); return this.convertCompanyVoToMap(results, totalCompanyCount, pageable, resJsonData); } public List> find(CompanyFieldCondition condition) { return this.companyFieldMapper.find(condition); } // 업체 상세 조회한다. @Override @Transactional public void detailCompany(Map resJsonData, CompanyFieldCondition companyFieldCondition) { CompanyFieldVo companyFieldVo = new CompanyFieldVo(); IspFieldVo ispFieldVo = new IspFieldVo(); HostingFieldVo hostingFieldVo = new HostingFieldVo(); IspField ispField = new IspField(); HostingField hostingField = new HostingField(); Long companyId = companyFieldCondition.getId(); if (companyId != null) { CompanyField companyField = this.getCompany(companyId); if(companyField.getIspId() != null){ ispField = this.ispFieldRepository.getOne(companyField.getIspId()); } if(companyField.getHostingId() != null){ hostingField = this.hostingFieldRepository.getOne(companyField.getHostingId()); } companyFieldVo = ConvertUtil.copyProperties(companyField, CompanyFieldVo.class); ispFieldVo = ConvertUtil.copyProperties(ispField, IspFieldVo.class); hostingFieldVo = ConvertUtil.copyProperties(hostingField, HostingFieldVo.class); companyFieldVo.setIspFieldVo(ispFieldVo); companyFieldVo.setHostingFieldVo(hostingFieldVo); } resJsonData.put(Constants.REQ_KEY_CONTENT, companyFieldVo); } // 업체 정로를 수정한다. @Override public void modifyCompany(CompanyFieldForm companyFieldForm) { // url 유효성 체크 this.verifyUrl(companyFieldForm.getUrl(), companyFieldForm.getId()); CompanyField companyField = ConvertUtil.copyProperties(companyFieldForm, CompanyField.class); companyFieldRepository.saveAndFlush(companyField); } // 업체를 삭제한다. @Override public void removeCompany(CompanyFieldForm companyFieldForm) { if (companyFieldForm.getRemoveIds().size() < 1) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.COMPANY_REMOVE_NOT_SELECT)); } for (Long id : companyFieldForm.getRemoveIds()) { this.companyFieldRepository.deleteById(id); this.companyFieldRepository.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; } CompanyFieldCondition companyFieldCondition = CompanyFieldCondition.make(conditions); List> results = this.companyFieldMapper.find(companyFieldCondition); List companyFieldVos = ConvertUtil.convertListToListClass(results, CompanyFieldVo.class); // code_ko_KR 에 code명 설정 ExportExcelVo excelInfo = new ExportExcelVo(); excelInfo.setFileName(this.messageAccessor.message("업체 목록")); excelInfo.addAttrInfos(new ExportExcelAttrVo("name", this.messageAccessor.message("companyField.companyName"), 6, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("manager", this.messageAccessor.message("companyField.companyManager"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("tel", this.messageAccessor.message("companyField.companyTel"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("email", this.messageAccessor.message("companyField.companyEmail"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("url", this.messageAccessor.message("companyField.companyUrl"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("memo", this.messageAccessor.message("companyField.companyMemo"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.setDatas(companyFieldVos); model.addAttribute(Constants.EXCEL, excelInfo); return new ModelAndView(this.excelView); } // 검색 결과를 CompanyFieldVo 로 변환한다. private List convertCompanyVoToMap(List> results, Long totalCompanyCount, Pageable pageable, Map resJsonData) { List companyFieldVos = Lists.newArrayList(); for (Map result : results) { CompanyFieldVo companyFieldVo = ConvertUtil.convertMapToClass(result, CompanyFieldVo.class); if(companyFieldVo.getIspId() != null && companyFieldVo.getIspId() != -1){ IspField ispField = this.ispFieldService.getIsp(companyFieldVo.getIspId()); if(ispField != null){ IspFieldVo ispFieldVo = ConvertUtil.copyProperties(ispField, IspFieldVo.class); companyFieldVo.setIspFieldVo(ispFieldVo); } } if(companyFieldVo.getHostingId() != null && companyFieldVo.getHostingId() != -1){ HostingField hostingField = this.hostingFieldService.getHosting(companyFieldVo.getHostingId()); if(hostingField != null){ HostingFieldVo hostingFieldVo = ConvertUtil.copyProperties(hostingField, HostingFieldVo.class); companyFieldVo.setHostingFieldVo(hostingFieldVo); } } companyFieldVos.add(companyFieldVo); } int totalPage = (int) Math.ceil((totalCompanyCount - 1) / pageable.getPageSize()) + 1; resJsonData.put(Constants.RES_KEY_CONTENTS, companyFieldVos); resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(), totalPage, totalCompanyCount)); return companyFieldVos; } // 업체 ID 로 조회한다 @Override public CompanyField getCompany(Long id) { if (id == null) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.COMPANYFIELD_NOT_EXIST)); } CompanyField companyField = this.findOne(id); if (companyField == null) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.COMPANYFIELD_NOT_EXIST)); } return companyField; } }