package kr.wisestone.owl.service.impl;
|
|
import kr.wisestone.owl.domain.Department;
|
import kr.wisestone.owl.mapper.DepartmentMapper;
|
import kr.wisestone.owl.service.UserService;
|
import kr.wisestone.owl.web.condition.DepartmentCondition;
|
import kr.wisestone.owl.web.condition.UserCondition;
|
import kr.wisestone.owl.web.form.DepartmentForm;
|
import kr.wisestone.owl.web.form.UserDepartmentForm;
|
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.repository.DepartmentRepository;
|
import kr.wisestone.owl.service.DepartmentService;
|
import kr.wisestone.owl.service.WorkspaceService;
|
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;
|
|
@Service
|
public class DepartmentServiceImpl extends AbstractServiceImpl<Department, Long, JpaRepository<Department, Long>> implements DepartmentService {
|
|
@Autowired
|
private DepartmentRepository departmentRepository;
|
|
@Autowired
|
private DepartmentMapper departmentMapper;
|
|
@Autowired
|
private UserService userService;
|
|
@Autowired
|
private WorkspaceService workspaceService;
|
|
@Autowired
|
private ExcelView excelView;
|
|
@Autowired
|
private ExcelConditionCheck excelConditionCheck;
|
|
@Override
|
protected JpaRepository<Department, Long> getRepository() {
|
return this.departmentRepository;
|
}
|
|
// 부서 추가
|
@Override
|
public Department add(DepartmentForm departmentForm) {
|
Department department = ConvertUtil.copyProperties(departmentForm, Department.class);
|
departmentRepository.saveAndFlush(department);
|
return department;
|
}
|
|
// 부서 목록을 가져온다.
|
@Override
|
public List<DepartmentVo>find(Map<String, Object> resJsonData,
|
DepartmentCondition condition, Pageable pageable) {
|
condition.setPage(pageable.getPageNumber() * pageable.getPageSize());
|
condition.setPageSize(pageable.getPageSize());
|
|
List<Map<String, Object>> results = this.departmentMapper.find(condition);
|
Long totalDepartmentCount = this.departmentMapper.count(condition);
|
|
return this.convertDepartmentVoToMap(results, totalDepartmentCount, pageable, resJsonData);
|
}
|
|
// 부서 상세 조회한다.
|
@Override
|
public void detail(Map<String, Object> resJsonData, DepartmentCondition departmentCondition) {
|
DepartmentVo departmentVo = new DepartmentVo();
|
|
Long departmentId = departmentCondition.getId();
|
if (departmentId != null) {
|
Department department = this.getDepartment(departmentId);
|
departmentVo = ConvertUtil.copyProperties(department, DepartmentVo.class);
|
}
|
resJsonData.put(Constants.RES_KEY_CONTENTS, departmentVo);
|
}
|
|
// 부서 정보를 수정한다.
|
@Override
|
public void modify(DepartmentForm departmentForm) {
|
Department department = ConvertUtil.copyProperties(departmentForm, Department.class);
|
departmentRepository.saveAndFlush(department);
|
}
|
|
// 부서를 삭제한다.
|
@Override
|
public void remove(DepartmentForm departmentForm) {
|
if (departmentForm.getRemoveIds().size() < 1) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.DEPARTMENT_REMOVE_NOT_SELECT));
|
}
|
|
for (Long id : departmentForm.getRemoveIds()) {
|
this.departmentRepository.deleteById(id);
|
this.departmentRepository.flush();
|
}
|
}
|
|
|
// 부서 목록을 엑셀로 다운로드 한다.
|
@Override
|
public ModelAndView downloadExcel(HttpServletRequest request, Model model) {
|
|
ModelAndView modelAndView = this.workspaceService.checkUseExcelDownload(model);
|
if (modelAndView != null) {
|
return modelAndView;
|
}
|
|
Map<String, Object> conditions = new HashMap<>();
|
// 엑셀 다운로드에 필요한 검색 조건 정보를 추출하고 검색 조건 추출에 오류가 발생하면 경고를 표시해준다.
|
modelAndView = this.excelConditionCheck.checkCondition(conditions, request, model);
|
if (modelAndView != null) {
|
return modelAndView;
|
}
|
|
DepartmentCondition departmentCondition = DepartmentCondition.make(conditions);
|
|
|
List<Map<String, Object>> results = this.departmentMapper.find(departmentCondition);
|
List<DepartmentVo> departmentVos = ConvertUtil.convertListToListClass(results, DepartmentVo.class);
|
// code_ko_KR 에 code명 설정
|
ExportExcelVo excelInfo = new ExportExcelVo();
|
excelInfo.setFileName(this.messageAccessor.message("부서 목록"));
|
excelInfo.addAttrInfos(new ExportExcelAttrVo("departmentName", this.messageAccessor.message("department.departmentName"), 6, ExportExcelAttrVo.ALIGN_CENTER));
|
excelInfo.addAttrInfos(new ExportExcelAttrVo("departmentDescription", this.messageAccessor.message("department.departmentDescription"), 20, ExportExcelAttrVo.ALIGN_CENTER));
|
excelInfo.addAttrInfos(new ExportExcelAttrVo("departmentCount", this.messageAccessor.message("department.departmentCount"), 3, ExportExcelAttrVo.ALIGN_CENTER));
|
|
excelInfo.setDatas(departmentVos);
|
|
model.addAttribute(Constants.EXCEL, excelInfo);
|
return new ModelAndView(this.excelView);
|
|
}
|
|
// 사용자 부서 ID로 조회한다.
|
@Override
|
public Department getDepartment(Long id) {
|
if (id == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.DEPARTMENT_NOT_EXIST));
|
}
|
|
Department department = this.findOne(id);
|
|
if (department == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.DEPARTMENT_NOT_EXIST));
|
}
|
|
return department;
|
}
|
|
// 삭제 할 부서 유저가 사용하고 있는지 확인
|
@Override
|
public boolean department(Long Id) {
|
return this.departmentMapper.findBydepartmentId(Id) > 0;
|
}
|
|
@Override
|
public List<Map<String, Object>> findByDepartmentIds(UserCondition condition) {
|
return this.departmentMapper.findByDepartmentIds(condition);
|
}
|
|
// 검색 결과를 DepartmentVo 로 변환한다.
|
private List<DepartmentVo> convertDepartmentVoToMap(List<Map<String, Object>> results, Long totalDepartmentsCount, Pageable pageable, Map<String, Object> resJsonData) {
|
List<DepartmentVo> departmentVos = Lists.newArrayList();
|
|
for (Map<String, Object> result : results) {
|
DepartmentVo departmentVo = ConvertUtil.convertMapToClass(result, DepartmentVo.class);
|
departmentVo.setByName(departmentVo.getDepartmentName());
|
departmentVos.add(departmentVo);
|
}
|
|
int totalPage = (int) Math.ceil((totalDepartmentsCount - 1) / pageable.getPageSize()) + 1;
|
|
resJsonData.put(Constants.RES_KEY_CONTENTS, departmentVos);
|
resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
|
totalPage, totalDepartmentsCount));
|
|
return departmentVos;
|
}
|
}
|