package com.fjs.scenic.controller.system;

import com.fjs.scenic.dto.PermissionDto;
import com.fjs.scenic.entity.system.Permission;
import com.fjs.scenic.service.system.PermissionService;
import com.fjs.scenic.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/index")
    public ModelAndView sceniclist(HttpSession session) {
        return new ModelAndView("/system/permission");
    }

    @GetMapping("/tree")
    public ReturnResult tree() {
        return permissionService.getPermissionTree();
    }

    /**
     * 获取全部权限信息
     *
     * @return
     */
    @PostMapping("/getAll")
    public ReturnResult getAll() {
        List<Permission> permissionList = permissionService.getAll();
        return ReturnResult.ok(permissionList);
    }

    @GetMapping("/getParent")
    public ReturnResult getParent() {
        return permissionService.getParent();
    }

    @PostMapping("/add")
    public ReturnResult add(Permission permission) {
        return permissionService.add(permission);
    }

    @GetMapping("/get/{id}")
    public ReturnResult get(@PathVariable("id") Integer id) {
        return ReturnResult.ok(permissionService.get(id));
    }

    @PostMapping("/update")
    public ReturnResult update(Permission permission) {
        return ReturnResult.ok(permissionService.update(permission));
    }

    @PostMapping("/del")
    @ResponseBody
    public ReturnResult del(int id) {
        return ReturnResult.ok(permissionService.del(id));
    }

    @PostMapping("/getListByManageId")
    public ReturnResult getListByManageId(Principal principal) {

        List<PermissionDto> permissionList = permissionService.getListByManageId(11);
        return ReturnResult.ok(permissionList);
    }

    /**
     * 获取登录用户权限
     *
     * @param principal
     * @return
     */
    @PostMapping("/getListByManageAccount")
    public ReturnResult getListByManageAccount(Principal principal) {
        String account = "fjsadmin".equals(principal.getName()) ? null : principal.getName();
        List<PermissionDto> permissionList = permissionService.getListByManageAccount(account);
        return ReturnResult.ok(permissionList);
    }

}
