package com.njzscloud.supervisory.menu.controller;

import cn.hutool.core.lang.Assert;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.support.MenuResource;
import com.njzscloud.supervisory.menu.pojo.MenuAddParam;
import com.njzscloud.supervisory.menu.pojo.MenuDetailResult;
import com.njzscloud.supervisory.menu.pojo.MenuModifyParam;
import com.njzscloud.supervisory.menu.pojo.MenuSearchParam;
import com.njzscloud.supervisory.menu.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单
 */
@Slf4j
@RestController
@RequestMapping("/sys_menu")
@RequiredArgsConstructor
public class SysMenuController {

    private final SysMenuService sysMenuService;

    /**
     * 新增<br/>
     *
     * @param menuAddParam 数据
     * @required
     */
    @PostMapping("/add")
    public R<Long> add(@RequestBody @Validated MenuAddParam menuAddParam) {
        return R.success(sysMenuService.add(menuAddParam));
    }

    /**
     * 修改
     *
     * @param menuModifyParam 数据
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody @Validated MenuModifyParam menuModifyParam) {
        sysMenuService.modify(menuModifyParam);
        return R.success();
    }

    /**
     * 删除
     *
     * @param ids Id
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        Assert.notEmpty(ids, "未指定要删除的数据");
        sysMenuService.del(ids);
        return R.success();
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysMenuVO 结果
     */
    @GetMapping("/detail")
    public R<MenuDetailResult> detail(@RequestParam Long id) {
        return R.success(sysMenuService.detail(id));
    }

    /**
     * 查询
     */
    @GetMapping("/list")
    public R<List<MenuDetailResult>> list(@RequestParam(required = false) Long pid, MenuSearchParam menuSearchParam) {
        return R.success(sysMenuService.list(pid, menuSearchParam));
    }

    /**
     * 查询
     */
    @GetMapping("/page_list")
    public R<PageResult<MenuDetailResult>> pageList(PageParam pageParam, MenuSearchParam menuSearchParam) {
        return R.success(sysMenuService.pageList(pageParam.toPage(), menuSearchParam));
    }

    @GetMapping("/user_menu")
    public R<List<MenuResource>> listUserMenu(@RequestParam Long userId) {
        return R.success(sysMenuService.listUserMenu(userId));
    }

}
