content: package com.gm.wj.service;
  
  import com.gm.wj.dao.AdminRoleDAO;
  import com.gm.wj.entity.AdminMenu;
  import com.gm.wj.entity.AdminPermission;
  import com.gm.wj.entity.AdminRole;
  import com.gm.wj.entity.AdminUserRole;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.web.bind.annotation.RequestBody;
  
  import java.util.List;
  import java.util.stream.Collectors;
  
  /**
   * @author Evan
   * @date 2019/11
   */
  @Service
  public class AdminRoleService {
      @Autowired
      AdminRoleDAO adminRoleDAO;
      @Autowired
      UserService userService;
      @Autowired
      AdminUserRoleService adminUserRoleService;
      @Autowired
      AdminPermissionService adminPermissionService;
      @Autowired
      AdminRolePermissionService adminRolePermissionService;
      @Autowired
      AdminMenuService adminMenuService;
  
      public List<AdminRole> listWithPermsAndMenus() {
          List<AdminRole> roles = adminRoleDAO.findAll();
          List<AdminPermission> perms;
          List<AdminMenu> menus;
          for (AdminRole role : roles) {
              perms = adminPermissionService.listPermsByRoleId(role.getId());
              menus = adminMenuService.getMenusByRoleId(role.getId());
              role.setPerms(perms);
              role.setMenus(menus);
          }
          return roles;
      }
  
      public List<AdminRole> findAll() {
          return adminRoleDAO.findAll();
      }
  
  
      public void addOrUpdate(AdminRole adminRole) {
          adminRoleDAO.save(adminRole);
      }
  
      public List<AdminRole> listRolesByUser(String username) {
          int uid = userService.findByUsername(username).getId();
          List<Integer> rids = adminUserRoleService.listAllByUid(uid)
                  .stream().map(AdminUserRole::getRid).collect(Collectors.toList());
          return adminRoleDAO.findAllById(rids);
      }
  
      public AdminRole updateRoleStatus(AdminRole role) {
          AdminRole roleInDB = adminRoleDAO.findById(role.getId());
          roleInDB.setEnabled(role.isEnabled());
          return adminRoleDAO.save(roleInDB);
      }
  
      public void editRole(@RequestBody AdminRole role) {
          adminRoleDAO.save(role);
          adminRolePermissionService.savePermChanges(role.getId(), role.getPerms());
      }
  }
  