package com.judong.chuanyiserver.controller;

import com.judong.chuanyiserver.annotation.PermissionControl;
import com.judong.chuanyiserver.entity.Permission;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.service.PermissionService;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @Autowired
    private PermissionService permissionService;

    /**
     * 新增权限资源
     *
     * @param permission
     * @return
     */
    @PostMapping("/addPermission")
    @PermissionControl
    public Result addPermission(Permission permission) {
        if (Blank.isEmpty(permission)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return permissionService.addPermission(permission);
    }

    /**
     * 修改权限
     *
     * @param permission
     * @return
     */
    @PostMapping("/updatePermission")
    @PermissionControl
    public Result updatePermission(Permission permission) {
        if (Blank.isEmpty(permission)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return permissionService.updatePermission(permission);
    }

    /**
     * 通过id查询权限
     *
     * @param id
     * @return
     */
    @GetMapping("/getPermissionById")
    @PermissionControl
    public Result getPermissionById(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return permissionService.getPermissionById(id);
    }

    /**
     * 分页查询权限
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getPermissionListByPage")
    @PermissionControl
    public Result getPermissionListByPage(Integer page, Integer limit, String permissionUrl, String permissionName) {
        if (Blank.isEmpty(page, limit) || page < 1 || limit < 1) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return permissionService.getPermissionListByPage(page, limit, permissionUrl, permissionName);
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @PostMapping("/deletePermissionById")
    @PermissionControl
    public Result deletePermissionById(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return permissionService.deletePermissionById(id);
    }
}
