package com.om.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.po.Menu;
import com.om.service.IMenuService;
import com.om.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 何超
 * @since 2022-08-22
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "系统菜单模块")
@Slf4j
public class MenuController {

    @Autowired
    private IMenuService menuService;

    //新增或修改
    @ApiOperation("新增或修改接口")
    @PostMapping("/save")
    public Result<Object> save(@RequestBody Menu menu) {
        log.info("====={}====", menu);
        menuService.saveOrUpdate(menu);
        return Result.succ(null);
    }

    //根据ID删除
    @ApiOperation("根据ID删除接口")
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable Integer id) {
        menuService.removeById(id);
        return Result.succ(null);
    }

    @ApiOperation("根据ID批量删除接口")
    @PostMapping("/delete/batch")
    public Result<Object> deleteBatch(@RequestBody List<Integer> ids) {
        menuService.removeByIds(ids);
        return Result.succ(null);
    }

    @GetMapping("/ids")
    public Result<Object> findAllIds() {
        return Result.succ(menuService.list().stream().map(Menu::getId));
    }

    //查询全部
    @GetMapping
    @ApiOperation("查询全部接口")
    public Result<Object> findAll(@RequestParam(defaultValue = "") String name,
                          @RequestParam(defaultValue = "zhCn") String lang) {
        return Result.succ(menuService.findMenus(name, lang));
    }

    @GetMapping("/getMenus")
    @ApiOperation("根据角色语言查询菜单")
    public Result<Object> findMenusByRoleAndLang(@RequestParam(defaultValue = "") String roleFlag,
                                                 @RequestParam(defaultValue = "zhCn") String lang) {
        return Result.succ(menuService.getRoleMenu(roleFlag, lang));
    }

    //根据ID查询
    @ApiOperation("根据ID查询接口")
    @GetMapping("/{id}")
    public Result<Object> findOne(@PathVariable Integer id) {
        return Result.succ(menuService.getById(id));
    }

    @ApiOperation("分页查询接口")
    @GetMapping("/page")
    public Result<Object> findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        queryWrapper.orderByDesc("id");
        return Result.succ(menuService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

