package com.simple.controller; import com.github.pagehelper.PageInfo; import com.simple.common.Result; import com.simple.common.ResultData; import com.simple.domain.po.MallRole; import com.simple.domain.po.MallRolePermission; import com.simple.service.MallRolePermissionService; import com.simple.service.MallRoleService; import io.swagger.annotations.Api; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("role") @Api(description = "角色相关接口") public class MallRoleController extends BaseController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private MallRoleService sysRoleService; @Autowired private MallRolePermissionService sysRolePermissionService; @GetMapping("list") public ResultData list(MallRole sysRole, Integer pageNum, Integer pageSize) { String tenantId = getTenantId(); sysRole.setTenantId(tenantId); final PageInfo page = sysRoleService.listAsPage(sysRole, pageNum, pageSize); for (MallRole r : page.getList()) { MallRolePermission p = new MallRolePermission(); p.setRoleId(r.getId()); p.setTenantId(tenantId); PageInfo pers = sysRolePermissionService.listAsPage(p, 1, 1000); String menus = ""; for (MallRolePermission rp : pers.getList()) { menus += rp.getPermissionId() + ","; } if (menus.length() > 1) { menus = menus.substring(0, menus.length() - 1); } r.setMenus(menus); } return new ResultData(page); } @PostMapping("saveOrUpdate") public ResultData saveOrUpdate(@RequestBody MallRole sysRole) { String tenantId = getTenantId(); int count = sysRoleService.countByName(sysRole.getName(), sysRole.getId()); if (count > 0) { return new ResultData(ResultData.ERROR, "角色名已存在"); } sysRole.setTenantId(tenantId); sysRoleService.saveOrUpdate(sysRole); // SysRolePermissionService.deleteById(id); if (StringUtils.isNoneBlank(sysRole.getMenus())) { List mIds = new ArrayList<>(); for (String mId : sysRole.getMenus().split(",")) { mIds.add(Long.valueOf(mId)); } sysRolePermissionService.savePermissions(tenantId, sysRole.getId(), mIds.toArray(new Long[]{})); ; } return new ResultData(); } @PostMapping("add") public ResultData add(MallRole sysRole) { Assert.notNull(sysRole.getName(), "角色名不能为空"); Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); sysRoleService.saveOrUpdate(sysRole); return new ResultData(); } @PostMapping("update") public ResultData update(MallRole sysRole) { if (sysRole.getName() != null) { System.out.println(checkUnique(sysRole.getName(), sysRole.getId())); Assert.isTrue(!checkUnique(sysRole.getName(), sysRole.getId()), "角色名已存在"); } sysRoleService.saveOrUpdate(sysRole); // Assert.notNull(sysRole.getName(), "角色名不能为空"); return new ResultData(); } @PostMapping("/del") public ResultData delete(@RequestBody MallRole sysRole) { sysRoleService.deleteById(sysRole.getId()); sysRolePermissionService.deleteByRoleId(sysRole.getId()); return new ResultData(Result.SUCCESS, "删除成功", null); } @GetMapping("setPermission") public ResultData setPermission(Long roleId, String perId) { Assert.notNull(perId, "请选择权限"); /*String[] perIds = perId.split(","); SysRolePermissionService.savePermissions(roleId, perIds);*/ //TODO return new ResultData(Result.SUCCESS, "设置角色权限成功", null); } private boolean checkUnique(String name, Long id) { return sysRoleService.countByName(name, id) > 0 ? true : false; } }