package com.gkhy.labRiskManage.domain.account.service.impl; import cn.hutool.core.util.ObjectUtil; import com.gkhy.labRiskManage.commons.enums.ResultCode; import com.gkhy.labRiskManage.commons.exception.BusinessException; import com.gkhy.labRiskManage.domain.account.converter.RoleInfoConverter; import com.gkhy.labRiskManage.domain.account.entity.Role; import com.gkhy.labRiskManage.domain.account.model.dto.RoleInfoDoaminDTO; import com.gkhy.labRiskManage.domain.account.repository.jpa.RoleRepository; import com.gkhy.labRiskManage.domain.account.service.RoleDomainService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Optional; @Repository public class RoleDomainServiceImpl implements RoleDomainService { @Autowired private RoleRepository roleRepository; @Autowired private RoleInfoConverter roleInfoConverter; @Override @Transactional public RoleInfoDoaminDTO createNewRole(String roleName) { if(roleName == null || roleName.isEmpty()) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数缺失"); Role role = roleRepository.findRoleByName(roleName); if(role != null) throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(),"角色名已经被使用"); role = new Role(); role.setName(roleName); role.setDelFlag((byte)0); role.setGmtCreate(LocalDateTime.now()); role.setGmtModified(role.getGmtCreate()); role = roleRepository.save(role); if(role == null) return null; RoleInfoDoaminDTO roleInfoDoaminDTO = roleInfoConverter.roleRoleInfoDTO(role); return roleInfoDoaminDTO; } @Override public RoleInfoDoaminDTO findRoleByName(String roleName) { Role role = roleRepository.findRoleByName(roleName); if(role == null) return null; RoleInfoDoaminDTO roleInfoDoaminDTO = roleInfoConverter.roleRoleInfoDTO(role); return roleInfoDoaminDTO; } @Override public RoleInfoDoaminDTO findRoleById(Long roleId) { if(roleId == null || roleId < 1) return null; Optional roleOptional = roleRepository.findById(roleId); if(!roleOptional.isPresent()) return null; RoleInfoDoaminDTO roleInfoDoaminDTO = roleInfoConverter.roleRoleInfoDTO(roleOptional.get()); return roleInfoDoaminDTO; } @Override public List findAllActiveRoleList() { List roleList = roleRepository.findAllByDelFlag((byte)0); if(roleList == null || roleList.isEmpty()) return null; List dtoList = new ArrayList<>(); for (Role role : roleList){ RoleInfoDoaminDTO dto = roleInfoConverter.roleRoleInfoDTO(role); dtoList.add(dto); } return dtoList; } @Override public List findAllDeleteRoleList() { List roleList = roleRepository.findAllByDelFlag((byte)1); if(roleList == null || roleList.isEmpty()) return null; List dtoList = new ArrayList<>(); for (Role role : roleList){ RoleInfoDoaminDTO dto = roleInfoConverter.roleRoleInfoDTO(role); dtoList.add(dto); } return dtoList; } @Override @Transactional public boolean deleteRole(Long roleId) { if(roleId == null || roleId < 1) return false; if(roleRepository.deleteRole(roleId) == 1) return true; return false; } @Override @Transactional public boolean updateUserRole(Long uid, Long roleId) { return false; } @Override @Transactional public boolean updateRoleName(Long roleId, String name) { if(roleId == null || name == null || name.isEmpty()) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数缺失"); Optional roleOptional = roleRepository.findById(roleId); if(!roleOptional.isPresent()){ throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR_OBJECT_NOT_EXIST.getCode(),"角色不存在"); } Role oldRole = roleOptional.get(); if(oldRole.getName() != null && !oldRole.getName().isEmpty() && oldRole.getName().equals(name)){ throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(),"角色名称未发生变化"); } Integer count = roleRepository.updateRoleName(roleId,name); if(count == 1) return true; else throw new BusinessException(this.getClass(),ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"数据库出错"); } //根据ids获取角色 @Override public List findAllByIdIn(List idList){ if(ObjectUtil.isEmpty(idList)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数缺失"); } List roles = roleRepository.findAllByIdInAndDelFlag(idList); List roleInfoDoaminDTOS = new ArrayList<>(); for (Role role:roles){ RoleInfoDoaminDTO roleInfoDoaminDTO = new RoleInfoDoaminDTO(); roleInfoDoaminDTO.setId(role.getId()); roleInfoDoaminDTO.setName(role.getName()); roleInfoDoaminDTOS.add(roleInfoDoaminDTO); } return roleInfoDoaminDTOS; } }