package cn.sh.stc.sict.cloud.upms.controller.web;

import cn.sh.stc.sict.cloud.upms.service.HpRoleMenuService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.sh.stc.sict.cloud.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import cn.sh.stc.sict.cloud.upms.model.HpMenu;
import cn.sh.stc.sict.cloud.upms.service.HpMenuService;


/**
 * 系统菜单表
 *
 * @author F_xh。
 * @date 2021-01-07 09:26:00
 */
@RestController
@RequestMapping("/hpmenu")
@AllArgsConstructor
@Slf4j
@Api(value = "系统菜单表 controller", tags = "系统菜单表")
public class HpMenuController {

    private HpMenuService hpMenuService;
    private HpRoleMenuService hpRoleMenuService;

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param hpMenu 系统菜单表
     * @return
     */
    @ApiOperation("分页查询 系统菜单表")
    @GetMapping("/page")
    public R getHpMenuPage(Page page, HpMenu hpMenu) {
        return new R<>(hpMenuService.page(page, Wrappers.query(hpMenu)));
    }

    /**
     * 通过id查询系统菜单表
     *
     * @param id id
     * @return R
     */
    @ApiOperation("通过id查询 系统菜单表")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return new R<>(hpMenuService.getById(id));
    }

    /**
     * 新增系统菜单表
     *
     * @param hpMenu 系统菜单表
     * @return R
     */
    @ApiOperation("新增系统菜单表")
    @PostMapping
    public R save(@RequestBody HpMenu hpMenu) {
        hpMenuService.saveOrUpdate(hpMenu);
        return new R<>();
    }

    /**
     * 通过id删除系统菜单表
     *
     * @return R
     */
    @ApiOperation("删除系统菜单表")
    @DeleteMapping("/{menuId}")
    public R removeById(@PathVariable Long menuId) {
        if (hpRoleMenuService.exist(menuId)) {
            return new R().error("不可删除！");
        }
        hpMenuService.removeById(menuId);
        return new R<>();
    }
}
