package com.tuoheng.controller.op;

import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.entity.Permissions;
import com.tuoheng.model.query.RoleClientQuery;
import com.tuoheng.service.CurrentUser;
import com.tuoheng.service.op.PermissionsService;
import com.tuoheng.until.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenjiandong
 * @description: TODO
 * @date 2022/11/11 14:33
 */
@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionsService permissionService;

    /**
     * 根据 RoleId 获取用户权限
     *
     * @return
     */
    @GetMapping("/getRolePermission")
    public JsonResult getMenuAndPermissionByRoleId(RoleClientQuery query) {
        return permissionService.getRolePermissionByRoleId(query);
    }


    /**
     * 添加权限(按钮)
     *
     * @param permissions
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody Permissions permissions, @CurrentUser LoginUser loginUser) {
        return permissionService.edit(permissions,loginUser);
    }

    /**
     * 编辑权限(按钮)
     *
     * @param permissions
     * @return
     */
    @PostMapping("/edit")
    public JsonResult edit(@RequestBody Permissions permissions ,@CurrentUser LoginUser loginUser) {
        return permissionService.edit(permissions,loginUser);
    }

    /**
     * 删除权限按钮(更新)
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public JsonResult delete(@PathVariable("id") Integer id) {
        boolean flag = permissionService.update().eq("id",id).set("mark", 0).update();
        if (flag) {
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }
}
