package com.judong.chuanyiserver.controller;

import com.judong.chuanyiserver.entity.Menu;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.service.MenuService;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("/addMenu")
    public Result addMenu(Menu menu) {
        if (Blank.isEmpty(menu)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return menuService.addMenu(menu);
    }

    /**
     * 修改菜单信息.这个接口，父id不允许被修改
     *
     * @param menu
     * @return
     */
    @PostMapping("/updateMenu")
    public Result updateMenu(Menu menu) {
        if (Blank.isEmpty(menu)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return menuService.updateMenu(menu);
    }

    /**
     * 分页查询所有的菜单信息
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getMenuListByPage")
    public Result getMenuListByPage(Integer page, Integer limit, String menuName, String menuUrl, Integer menuStatus) {
        if (Blank.isEmpty(page, limit) || page < 1 || limit < 1) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return menuService.getMenuListByPage(page, limit, menuName, menuUrl, menuStatus);
    }

    /**
     * 通过id查看菜单信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getMenuById")
    public Result getMenuById(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return menuService.getMenuById(id);
    }

    /**
     * 通过id删除菜单信息，同时删除菜单关联的信息
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteMenuById")
    public Result deleteMenuById(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return menuService.deleteMenuById(id);
    }

    /**
     * 菜单分配接口权限
     *
     * @param menu
     * @return
     */
    @PostMapping("/assignPermission")
    public Result assignPermission(@RequestBody Menu menu) {
        if (Blank.isEmpty(menu, menu.getId(), menu.getPermissionList())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return menuService.assignPermission(menu.getId(), menu.getPermissionList());
    }

    /**
     * 通过父id所有本身及下级的权限信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getPermissionByMenuId")
    public Result getPermissionByMenuId(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return menuService.getPermissionByMenuId(id);
    }
}
