package com.gkhy.safePlatform.account.service.baseService.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gkhy.safePlatform.account.entity.user.RoleInfo; import com.gkhy.safePlatform.account.entity.user.RoleInfoDO; import com.gkhy.safePlatform.account.enums.RoleStatusEnum; import com.gkhy.safePlatform.commons.enums.E; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.exception.AusinessException; import com.gkhy.safePlatform.commons.exception.BusinessException; import com.gkhy.safePlatform.commons.utils.StringUtils; import com.gkhy.safePlatform.account.repository.RoleInfoRepository; import com.gkhy.safePlatform.account.service.baseService.RoleInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.List; @Service("roleInfoService") public class RoleInfoServiceImpl extends ServiceImpl implements RoleInfoService { @Autowired private RoleInfoRepository roleInfoRepository; @Override public List getRoleInfo(Collection idList) { if (idList.size() < 1) { throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL); } return this.listByIds(idList); } @Override public RoleInfo getRoleInfoByUserId(Long userId) { if (userId == null) { throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL); } return roleInfoRepository.selectRoleInfoByUserId(userId); } @Override public List getRoleInfoByCodes(List roleCodes) { List roleInfos = new ArrayList<>(); for (String code : roleCodes) { if (StringUtils.isBlank(code)) { throw new AusinessException(E.DATA_PARAM_NULL,"角色code传入为空"); } RoleInfo roleInfo = roleInfoRepository.getRoleInfoByCode(code); if (roleInfo == null) { // 角色code不存在 throw new BusinessException(ResultCodes.CLIENT_ROLE_CODE_NOT_EXIST); } else if (roleInfo.getStatus() != RoleStatusEnum.ENABLED.getCode()){ // 未启用 throw new BusinessException(ResultCodes.CLIENT_ROLE_CODE_NOT_ON); } roleInfos.add(roleInfo); } return roleInfos; } @Override public void saveRoleInfo(RoleInfo roleInfo) { int i = roleInfoRepository.insertRoleInfo(roleInfo); if (i != 1) { throw new BusinessException(ResultCodes.SERVER_ADD_ERROR); } } @Override public RoleInfo getRoleInfoById(Long id) { if (id == null) { throw new BusinessException(ResultCodes.SERVER_PARAM_NULL); } return roleInfoRepository.getRoleInfoById(id); } @Override public RoleInfoDO getRoleInfoDOById(Long id) { if (id == null) { throw new BusinessException(ResultCodes.SERVER_PARAM_NULL); } return roleInfoRepository.getRoleInfoDOById(id); } @Override public void updateRoleInfo(RoleInfo roleInfo) { if (roleInfo.getId() == null) { throw new BusinessException(ResultCodes.SERVER_PARAM_NULL); } int i = roleInfoRepository.updateRoleInfo(roleInfo); if (i == 0) { throw new BusinessException(ResultCodes.SERVER_UPDATE_DATA_NO_CHANGE); } if (i > 1) { throw new BusinessException(ResultCodes.SERVER_UPDATE_ERROR); } } @Override public List getRoleInfoByStatus(RoleStatusEnum status) { if (status == null) { throw new BusinessException(ResultCodes.SERVER_PARAM_NULL); } return roleInfoRepository.getRoleInfoByStatus(status.getCode()); } @Override public RoleInfo getRoleInfoByCode(String code) { if (StringUtils.isBlank(code)) { throw new BusinessException(ResultCodes.SERVER_PARAM_NULL); } List roleInfos = roleInfoRepository.selectList(new LambdaQueryWrapper() .eq(RoleInfo::getCode, code) .eq(RoleInfo::getStatus, RoleStatusEnum.ENABLED.getCode())); if (roleInfos.size() > 1) { throw new BusinessException(ResultCodes.SERVER_DATABASE_DATA_DUPLICATED); } if (roleInfos.size() == 0) { return null; } return roleInfos.get(0); } @Override public RoleInfo getRoleInfoByName(String name) { if (StringUtils.isBlank(name)) { throw new BusinessException(ResultCodes.SERVER_PARAM_NULL); } List roleInfos = roleInfoRepository.selectList(new LambdaQueryWrapper() .eq(RoleInfo::getName, name) .eq(RoleInfo::getStatus, RoleStatusEnum.ENABLED.getCode())); if (roleInfos.size() > 1) { throw new BusinessException(ResultCodes.SERVER_DATABASE_DATA_DUPLICATED); } if (roleInfos.size() == 0) { return null; } return roleInfos.get(0); } }