package com.zswl.dataservice.service.user.impl; import com.zswl.dataservice.dao.RoleDao; import com.zswl.dataservice.dataConfig.ResultMessage; import com.zswl.dataservice.domain.user.Role; import com.zswl.dataservice.model.user.RoleAddParam; import com.zswl.dataservice.model.user.RoleModel; import com.zswl.dataservice.model.user.RoleSearchParam; import com.zswl.dataservice.service.base.SuperService; import com.zswl.dataservice.type.DataState; import com.zswl.dataservice.type.RoleType; import com.zswl.dataservice.utils.bean.BeanUtils; import com.zswl.dataservice.utils.page.PageEntityUtil; import com.zswl.dataservice.utils.result.ResultContent; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * @author TRX * @date 2024/6/5 */ @Slf4j @Service public class RoleServiceImpl extends SuperService { @Autowired RoleDao roleDao; /** * 添加/编辑角色 * * @param param * @return */ public ResultContent addRole(RoleAddParam param) { Assert.hasText(param.getName(), "name不能为空"); Assert.hasText(param.getCode(), "code不能为空"); String oid = param.getOid(); if (StringUtils.isEmpty(oid)) { oid = getCurrentOid(); } param.setOid(oid); if (param.getSort() == null) { param.setSort(1L); } if (param.getState() == null) { param.setState(DataState.Enable); } Role nameRole = roleDao.findTopByName(param.getName()); Role codeRole = roleDao.findTopByCode(param.getCode()); initDefaultUser(param); if (StringUtils.isEmpty(param.getId())) { param.setId(null); // 添加 if (ObjectUtils.isNotEmpty(nameRole)) { return ResultContent.buildFail(String.format("角色名称已存在:%s", param.getName())); } if (ObjectUtils.isNotEmpty(codeRole)) { return ResultContent.buildFail(String.format("角色标识已存在:%s", param.getCode())); } Role role = new Role(); BeanUtils.copyProperties(param, role); role.setRoleType(RoleType.Custom); role.setCreateUserId(getCurrentUserId()); role.setIsAdmin(Boolean.FALSE); roleDao.save(role); } else { // 编辑 Role role = roleDao.findTopById(param.getId()); if (ObjectUtils.isEmpty(role)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId())); } if (ObjectUtils.isNotEmpty(nameRole) && !nameRole.getId().equals(role.getId())) { return ResultContent.buildFail(String.format("角色名称已存在:%s", param.getName())); } if (ObjectUtils.isNotEmpty(codeRole) && !codeRole.getId().equals(role.getId())) { return ResultContent.buildFail(String.format("角色标识已存在:%s", param.getCode())); } // 编辑本地角色 BeanUtils.copyProperties(param, role); roleDao.save(role); } return ResultContent.buildSuccess(); } /** * 删除角色 * * @param id * @return */ public ResultContent deleteRole(String id) { Role role = roleDao.findTopById(id); if (ObjectUtils.isEmpty(role)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id)); } if (role.getIsAdmin() != null && role.getIsAdmin()) { return ResultContent.buildFail(String.format("管理员角色不能删除")); } if (role.getRoleType() == RoleType.BuildIn) { return ResultContent.buildFail(String.format("内置角色不能删除")); } // 删除本地角色 roleDao.delete(role); return ResultContent.buildSuccess(); } /** * 角色详情 * * @param id * @return */ public ResultContent getRole(String id) { Role role = roleDao.findTopById(id); if (ObjectUtils.isEmpty(role)) { return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id)); } RoleModel roleModel = toModel(role); return ResultContent.buildSuccess(roleModel); } /** * 角色列表 * * @param param * @param pageable * @return */ public ResultContent> page(RoleSearchParam param, Pageable pageable) { initSearchParam(param); param.setIsSortDesc(Boolean.FALSE); Page page = roleDao.page(pageable, param); return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel)); } /** * 得到机构所有的角色 * * @param oid * @return */ public ResultContent> getAllRoles(String oid) { if (StringUtils.isEmpty(oid)) { oid = getCurrentOid(); } List list = roleDao.findAll(Sort.by(Sort.Order.asc("sort"), Sort.Order.asc("createTime"))); List models = new ArrayList<>(); if (ObjectUtils.isNotEmpty(list)) { models = list.stream().map(this::toModel).collect(Collectors.toList()); } return ResultContent.buildSuccess(models); } public RoleModel toModel(Role role) { RoleModel roleModel = new RoleModel(); if (ObjectUtils.isNotEmpty(role)) { BeanUtils.copyProperties(role, roleModel); } return roleModel; } }