package com.ycwl.basic.controller.pc;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.model.pc.permission.resp.PermissionResp;
import com.ycwl.basic.model.pc.role.req.AddOrUpdateRoleReqVO;
import com.ycwl.basic.model.pc.role.req.RoleListReqVO;
import com.ycwl.basic.model.pc.role.resp.RoleListRespVO;
import com.ycwl.basic.model.pc.role.resp.RolePermissionResp;
import com.ycwl.basic.service.pc.RoleService;
import com.ycwl.basic.utils.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/role/v1")
// 系统角色管理
public class RoleController {

    @Autowired
    RoleService roleService;

    @PostMapping(value = "/page")
    // 角色列表分页查询
    public ApiResponse<PageInfo<RoleListRespVO>> page(@RequestBody RoleListReqVO roleListReqVO) {
        return roleService.pageQuery(roleListReqVO);
    }
    @PostMapping(value = "/list")
    // 角色列表
    public ApiResponse<List<RoleListRespVO>> list(@RequestBody RoleListReqVO roleListReqVO) {
        return roleService.list(roleListReqVO);
    }
    @GetMapping("/{roleId}/permission")
    // 角色权限列表
    public ApiResponse<PermissionResp> getPermissionByRoleId(@PathVariable("roleId") Long roleId) {
        RolePermissionResp permission = roleService.getPermissionByRoleId(roleId);
        if (permission == null) {
            return ApiResponse.fail("角色不存在");
        }
        return ApiResponse.success(new PermissionResp(Arrays.asList(StringUtils.split(permission.getPermStr(), ",")), Arrays.asList(StringUtils.split(permission.getMenuStr(), ","))));
    }

    @PostMapping(value = "/addOrUpdate")
    // 添加或更新角色
    public ApiResponse addOrUpdate(@RequestBody AddOrUpdateRoleReqVO addOrUpdateRoleReqVO) {
        return roleService.addOrUpdate(addOrUpdateRoleReqVO);
    }

    @GetMapping(value = "/delete/{id}")
    // 删除
    public ApiResponse delete(@PathVariable("id") String id) {
        return roleService.delete(id);
    }

    @GetMapping(value = "/updateReturnMenu/{id}")
    // 编辑回显该角色当前菜单
    public ApiResponse updateReturnMenu(@PathVariable("id") String id) {
        return roleService.updateReturnMenu(id);
    }



    @GetMapping(value = "/updateStatus/{id}")
    // 更改角色类型状态
    public ApiResponse updateStatus(@PathVariable("id") String id) {
        return roleService.updateStatus(id);
    }
}

