package kr.wisestone.owl.service.impl;
|
|
import kr.wisestone.owl.constant.Constants;
|
import kr.wisestone.owl.constant.MsgConstants;
|
import kr.wisestone.owl.domain.*;
|
import kr.wisestone.owl.exception.OwlRuntimeException;
|
import kr.wisestone.owl.mapper.UserWorkspaceMapper;
|
import kr.wisestone.owl.repository.UserWorkspaceRepository;
|
import kr.wisestone.owl.service.*;
|
import kr.wisestone.owl.util.CommonUtil;
|
import kr.wisestone.owl.util.ConvertUtil;
|
import kr.wisestone.owl.vo.DepartmentVo;
|
import kr.wisestone.owl.vo.ResPage;
|
import kr.wisestone.owl.vo.UserWorkspaceVo;
|
import kr.wisestone.owl.web.condition.UserCondition;
|
import kr.wisestone.owl.web.condition.UserWorkspaceCondition;
|
import kr.wisestone.owl.web.form.UserWorkspaceForm;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import java.util.List;
|
import java.util.Map;
|
|
@Service
|
public class UserWorkspaceServiceImpl extends AbstractServiceImpl<UserWorkspace, Long, JpaRepository<UserWorkspace, Long>>
|
implements UserWorkspaceService {
|
|
private static final Logger log = LoggerFactory.getLogger(UserWorkspaceServiceImpl.class);
|
|
@Autowired
|
private UserWorkspaceRepository userWorkspaceRepository;
|
|
@Autowired
|
private UserService userService;
|
|
@Autowired
|
private UserLevelService userLevelService;
|
|
@Autowired
|
private DepartmentService departmentService;
|
|
@Autowired
|
private UserWorkspaceMapper userWorkspaceMapper;
|
|
@Autowired
|
private WorkspaceService workspaceService;
|
|
@Autowired
|
private SimpMessagingTemplate simpMessagingTemplate;
|
|
@Override
|
protected JpaRepository<UserWorkspace, Long> getRepository() {
|
return this.userWorkspaceRepository;
|
}
|
|
// 업무 공간에 사용자가 가입된다
|
@Override
|
@Transactional
|
public UserWorkspace addUserWorkspace(User user, Workspace workspace, Boolean managerYn, Boolean useYn) {
|
Long disableCount = this.userWorkspaceRepository.maxDisablePosition(workspace.getId());
|
|
if (disableCount == null) {
|
disableCount = 1L;
|
}
|
|
UserWorkspace userWorkspace = new UserWorkspace(user, workspace, managerYn, useYn, disableCount);
|
|
return this.userWorkspaceRepository.saveAndFlush(userWorkspace);
|
}
|
|
// 업무 공간에 참여/참여대기 하는 전체 사용자 목록을 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public List<UserWorkspaceVo> findUserWorkspace(Map<String, Object> resJsonData,
|
UserWorkspaceCondition condition, Pageable pageable) {
|
|
UserWorkspace userWorkspace = this.findWorkspaceManager(this.webAppUtil.getLoginId());
|
Workspace myWorkspace = userWorkspace.getWorkspace();
|
condition.setPage(pageable.getPageNumber() * pageable.getPageSize());
|
condition.setPageSize(pageable.getPageSize());
|
condition.setWorkspaceId(myWorkspace.getId());
|
condition.setAccount(CommonUtil.encryptAES128(condition.getAccount())); //계정 암호화(필수 동작)
|
|
List<Map<String, Object>> results = this.userWorkspaceMapper.find(condition);
|
Long totalCount = this.userWorkspaceMapper.count(condition);
|
int totalPage = (int) Math.ceil((totalCount - 1) / pageable.getPageSize()) + 1;
|
List<UserWorkspaceVo> userWorkspaceVos = ConvertUtil.convertListToListClass(results, UserWorkspaceVo.class); //UserWorkspace의 데이터를 리스트 형식으로 복사해서 UserWorkspaceVo에 담기
|
|
for (UserWorkspaceVo userWorkspaceVo : userWorkspaceVos) {
|
userWorkspaceVo.setAccount(CommonUtil.decryptAES128(userWorkspaceVo.getAccount()));
|
|
// UserCondition 는 원래 userId 가 없었다.
|
UserCondition con = new UserCondition();
|
// 그래서 Condition에 추가 해주고 set 하는데 그건 userWorkspaceVo 에서 Id 를 가져온다
|
con.setId(userWorkspaceVo.getUserId());
|
// findByDepartmentIds 라는걸 mapper로 만들어서 쿼리 for문을 돌리고 us
|
List<Map<String, Object>> re = this.departmentService.findByDepartmentIds(con);
|
List<DepartmentVo> vos = ConvertUtil.convertListToListClass(re, DepartmentVo.class);
|
|
userWorkspaceVo.setDepartmentVos(vos);
|
}
|
|
resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(),
|
totalPage, totalCount));
|
resJsonData.put(Constants.RES_KEY_CONTENTS, userWorkspaceVos);
|
|
return userWorkspaceVos;
|
}
|
|
// 업무 공간에 연결된 사용자의 참여 상태를 변경한다.
|
@Override
|
@Transactional
|
public void modifyUserWorkspace(UserWorkspaceForm userWorkspaceForm) {
|
UserWorkspace userWorkspace = this.getUserWorkspace(userWorkspaceForm.getId());
|
|
User user = userWorkspace.getUser();
|
|
// 사용자 등급 변경
|
UserLevel currentUserlevel = user.getUserLevel();
|
if (currentUserlevel.getId() != userWorkspaceForm.getLevelId()) {
|
UserLevel userLevel = this.userLevelService.getUserLevel(userWorkspaceForm.getLevelId());
|
user.setUserLevel(userLevel);
|
userWorkspace.setUser(user);
|
|
// 등급 변경 된 유저 로그아웃 시키기
|
this.simpMessagingTemplate.convertAndSendToUser(user.getAccount(), "/notification/changeUserLevel", this.messageAccessor.getMessage(MsgConstants.USER_LEVEL_CHANGE));
|
|
// 세션 업데이트
|
//SecurityUtils.setUserToSession(user);
|
}
|
|
// 부서 변경
|
// DepartmentManage currentDepartment = user.getDepartmentManage();
|
// if (currentDepartment == null || (userWorkspaceForm.getDepartmentId() != null && currentDepartment.getId() != userWorkspaceForm.getDepartmentId())) {
|
// // 부서명 변경시
|
// DepartmentManage departmentManage = this.departmentService.getDepartment(userWorkspaceForm.getDepartmentId());
|
// user.setDepartmentManage(departmentManage);
|
// userWorkspace.setUser(user);
|
// }
|
|
// 참여로 상태를 변경하려고 할때
|
if (userWorkspace.getUseYn() != userWorkspaceForm.getUseYn()) {
|
if (!userWorkspace.getUseYn()) {
|
Integer maxUserCount = userWorkspace.getWorkspace().getMaxUser(); // 최대 사용자
|
Integer activeUserCount = this.countByWorkspaceIdAndUseYn(userWorkspace.getWorkspace().getId(), true);
|
|
// 최대 사용자 - 현재 참여 사용자가 0보다 크다면 참여 상태로 변경
|
if ((maxUserCount - activeUserCount) < 1) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.WORKSPACE_MAX_USER_EXCESS_NOT_INCLUDE));
|
}
|
} else {
|
user = userWorkspace.getUser();
|
// 참여 대기 사용자가 현재 해당 업무 공간을 사용하고 있을 경우 즉시 해당 업무 공간에서 튕기게 한다.
|
if (user.getLastWorkspaceId().equals(userWorkspace.getWorkspace().getId())) {
|
// 업무 공간에 참여하던 사용자에게 제외 알림 및 화면 새로고침
|
this.simpMessagingTemplate.convertAndSendToUser(user.getAccount(), "/notification/workspace-disabled", this.messageAccessor.getMessage(MsgConstants.WORKSPACE_OUT, userWorkspace.getWorkspace().getName()));
|
}
|
|
// 참여 대기 상태로 변경하면 해당 사용자의 마지막 접근 업무 공간는 자신이 관리하는 업무 공간로 변경한다.
|
this.userService.updateLastMyWorkspace(user);
|
}
|
userWorkspace.setUseYn(!userWorkspace.getUseYn());
|
}
|
|
this.userWorkspaceRepository.saveAndFlush(userWorkspace);
|
}
|
|
|
// 해당 사용자를 업무 공간에서 비활성화 한다.
|
@Override
|
@Transactional
|
public void disabledUserWorkspace(User user, Workspace workspace) {
|
UserWorkspace userWorkspace = this.userWorkspaceRepository.findByUserIdAndWorkspaceId(user.getId(), workspace.getId());
|
|
/*if (userWorkspace.getManagerYn()) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.WORKSPACE_MANAGER_NOT_CHANGE_USE_YN));
|
}*/
|
|
userWorkspace.setUseYn(false);
|
this.userWorkspaceRepository.saveAndFlush(userWorkspace);
|
}
|
// 해당 사용자가 업무 공간에 참여 정보가 있는지 확인한다.
|
@Override
|
@Transactional(readOnly = true)
|
public UserWorkspace findByUserIdAndWorkspaceId(Long userId, Long workspaceId) {
|
return this.userWorkspaceRepository.findByUserIdAndWorkspaceId(userId, workspaceId);
|
}
|
|
// 업무 공간에 참여/참여대기 사용자 수를 선택적으로 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public Integer countByWorkspaceIdAndUseYn(Long workspaceId, Boolean useYn) {
|
List<UserWorkspace> userWorkspaces = this.findByWorkspaceIdAndUseYn(workspaceId, useYn);
|
return userWorkspaces == null ? 0 : userWorkspaces.size();
|
}
|
|
// 업무 공간에 참여/참여대기 사용자 정보를 선택적으로 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public List<UserWorkspace> findByWorkspaceIdAndUseYn(Long workspaceId, Boolean useYn) {
|
return this.userWorkspaceRepository.findByWorkspaceIdAndUseYn(workspaceId, useYn);
|
}
|
|
// 업무 공간의 관리자/일반 사용자 정보를 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public List<UserWorkspace> findByWorkspaceIdAndManagerYn(Long workspaceId, Boolean managerYn) {
|
return this.userWorkspaceRepository.findByWorkspaceIdAndManagerYn(workspaceId, managerYn);
|
}
|
|
// 로그인한 사용자가 관리하는 업무 공간 정보를 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public UserWorkspace findMyWorkspace(Long userId) {
|
return this.userWorkspaceRepository.findByUserIdAndManagerYn(userId, true);
|
}
|
|
// 워크스페이스 관리자 조회
|
@Override
|
@Transactional(readOnly = true)
|
public UserWorkspace findWorkspaceManager(Long userId) {
|
return this.userWorkspaceRepository.findByUserId(userId);
|
}
|
|
// 업무 공간 사용자 연결 아이디로 업무 공간 사용자 연결 정보를 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public UserWorkspace getUserWorkspace(Long id) {
|
if (id == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.USER_WORKSPACE_NOT_EXIST));
|
}
|
|
UserWorkspace userWorkspace = this.findOne(id);
|
|
if (userWorkspace == null) {
|
throw new OwlRuntimeException(
|
this.messageAccessor.getMessage(MsgConstants.USER_WORKSPACE_NOT_EXIST));
|
}
|
|
return userWorkspace;
|
}
|
|
// 업무 공간 담당자 여부를 확인한다.
|
@Override
|
@Transactional(readOnly = true)
|
public boolean checkWorkspaceManager(User user) {
|
boolean bIsManager = false;
|
|
try
|
{
|
Workspace workspace = this.workspaceService.getWorkspace(user.getLastWorkspaceId()); // 현재 접속한 업무 공간
|
// 로그인한 사용자가 관리하는 업무 공간을 찾는다.
|
UserWorkspace userWorkspace = this.findMyWorkspace(user.getId());
|
|
bIsManager = workspace.getId().equals(userWorkspace.getWorkspace().getId());
|
}
|
catch (Exception e)
|
{
|
// exception.
|
}
|
|
// 관리하는 업무 공간이 다를 경우
|
return bIsManager;
|
}
|
|
// 업무 공간에 모든 사용자 정보를 조회한다.
|
@Override
|
@Transactional(readOnly = true)
|
public List<UserWorkspace> findByWorkspaceId(Long workspaceId) {
|
return this.userWorkspaceRepository.findByWorkspaceId(workspaceId);
|
}
|
|
// 사용기간이 만료된 업무 공간의 관리자가 아닌 사용자를 추출하여 마지막 접근한 업무 공간 정보를 변경한다.
|
@Override
|
@Transactional
|
public void limitExpireUserWorkspace(Workspace workspace) {
|
// 관리자가 아닌 사용자 추출
|
List<UserWorkspace> userWorkspaces = this.findByWorkspaceIdAndManagerYn(workspace.getId(), false);
|
// 사용 금지 설정 & 마지막 접근 업무 공간 정보 변경
|
for (UserWorkspace userWorkspace : userWorkspaces) {
|
User user = userWorkspace.getUser();
|
this.userService.updateLastDefaultWorkspace(workspace, user);
|
userWorkspace.setUseYn(false);
|
}
|
|
if (userWorkspaces.size() > 0) {
|
this.userWorkspaceRepository.saveAll(userWorkspaces);
|
}
|
}
|
|
}
|