package com.qmth.teachcloud.report.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysPrivilege;
import com.qmth.teachcloud.common.entity.SysRole;
import com.qmth.teachcloud.common.entity.SysRolePrivilege;
import com.qmth.teachcloud.common.service.SysPrivilegeService;
import com.qmth.teachcloud.common.service.SysRoleService;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Api(tags = "菜单权限Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_SYS + "/privilege")
public class SysPrivilegeController {

    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 查询
     *
     * @return
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list() {
        return ResultUtil.ok(sysPrivilegeService.listPrivilegeTree());
    }

    /**
     * 查询需要授权的菜单
     *
     * @return
     */
    @ApiOperation(value = "查询需要授权的菜单")
    @RequestMapping(value = "/list_auth", method = RequestMethod.POST)
    public Result listAuth() {
        return ResultUtil.ok(sysPrivilegeService.listPrivilegeTreeAuth());
    }

    /**
     * 新增/修改
     *
     * @param privilege
     * @return
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody SysPrivilege privilege) {
        return ResultUtil.ok(sysPrivilegeService.savePrivilege(privilege));
    }

    /**
     * 删除
     *
     * @param privilege
     * @return
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestBody SysPrivilege privilege) {
        return ResultUtil.ok(sysPrivilegeService.remove(privilege.getId()));
    }

    /**
     * 角色已绑定权限列表
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "角色已绑定权限列表")
    @RequestMapping(value = "/get_role_privileges", method = RequestMethod.POST)
    public Result getRolePrivileges(@RequestParam(value = "roleId", required = true) Long roleId) {
        SysRole sysRole = sysRoleService.getById(roleId);
        Long schoolId = Objects.nonNull(ServletUtil.getRequestHeaderSchoolIdByNotVaild()) ? Long.valueOf(ServletUtil.getRequestHeaderSchoolIdByNotVaild().toString()) : null;
        List<SysRolePrivilege> sysRolePrivilegeList = sysPrivilegeService.getRolePrivileges(schoolId, sysRole);
        List<String> privilegeIds = sysRolePrivilegeList.stream().map(m -> String.valueOf(m.getPrivilegeId())).collect(Collectors.toList());
        return ResultUtil.ok(privilegeIds);
    }
}

