package com.elephant.framework.galaxy.aroad.module.system.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.elephant.framework.galaxy.aroad.module.common.annotation.SysLog;
import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController;
import com.elephant.framework.galaxy.aroad.module.system.entity.OauthClientDetails;
import com.elephant.framework.galaxy.aroad.module.system.service.OauthClientDetailsService;
import com.elephant.framework.galaxy.aroad.response.R;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 终端信息表 前端控制器
 * </p>
 *
 * @author wangjian
 * @since 2019-02-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/client")
public class OauthClientDetailsController extends BaseController {
    private final OauthClientDetailsService oauthClientDetailsService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysOauthClientDetails
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return new R(oauthClientDetailsService.getById(id));
    }


    /**
     * 简单分页查询
     *
     * @param page                  分页对象
     * @param oauthClientDetails 系统终端
     * @return
     */
    @GetMapping("/page")
    public R getOauthClientDetailsPage(Page page, OauthClientDetails oauthClientDetails) {
        return new R(oauthClientDetailsService.page(page, Wrappers.query(oauthClientDetails)));
    }

    /**
     * 添加
     *
     * @param oauthClientDetails 实体
     * @return success/false
     */
    @SysLog("添加终端")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_client_add')")
    public R add(@Valid @RequestBody OauthClientDetails oauthClientDetails) {
        return new R(oauthClientDetailsService.save(oauthClientDetails));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @SysLog("删除终端")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_client_del')")
    public R removeById(@PathVariable String id) {
        return new R(oauthClientDetailsService.removeClientDetailsById(id));
    }

    /**
     * 编辑
     *
     * @param oauthClientDetails 实体
     * @return success/false
     */
    @SysLog("编辑终端")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_client_edit')")
    public R update(@Valid @RequestBody OauthClientDetails oauthClientDetails) {
        return new R(oauthClientDetailsService.updateClientDetailsById(oauthClientDetails));
    }
}

