package com.ruoyi.predict.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author hlf
 * @date 2022/12/9 15:12
 * 文件说明：菜单管理
 */
@Api(value = "菜单管理Controller", tags = {"菜单管理操作接口"})
@Controller
@RequestMapping("/predict/api/mmc")
public class MenuManagementController extends BaseController {

    @Autowired
    private ISysMenuService menuService;

    @ApiOperation("获取菜单信息列表")
    @PostMapping("/getMenuList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuName", value = "菜单名称"),
    })
    public TableDataInfo getMenuList(SysMenu menu) {
        startPage();
        menu.setRemark("功率预测");
        List<SysMenu> list = menuService.selectSysMenuList(menu);
        return getDataTable(list);
    }

    @ApiOperation("新增菜单信息")
    @PostMapping("/addMenu")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuName", value = "菜单名称", required = true),
    })
    public AjaxResult addMenu(SysMenu menu) {
        if (UserConstants.MENU_NAME_NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
            return error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setOrderNum("0");
        menu.setUrl("#");
        menu.setMenuType("C");
        menu.setIcon("#");
        menu.setCreateBy("admin");
        menu.setRemark("功率预测");
        return toAjax(menuService.insertMenu(menu));
    }

    @ApiOperation("修改菜单信息")
    @PostMapping("/editMenu")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单编号", required = true),
            @ApiImplicitParam(name = "menuName", value = "菜单名称"),
    })
    public AjaxResult editMenu(SysMenu menu) {
        if (UserConstants.MENU_NAME_NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
            return error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setUpdateBy("admin");
        return toAjax(menuService.updateMenu(menu));
    }

    @ApiOperation("删除菜单信息")
    @PostMapping("/delMenu")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单编号", required = true),
    })
    public AjaxResult delMenu(@RequestParam(value = "menuId") String menuId) {
        if (menuService.selectCountMenuByParentId(Convert.toLong(menuId)) > 0) {
            return AjaxResult.warn("存在子菜单,不允许删除");
        }
        if (menuService.selectCountRoleMenuByMenuId(Convert.toLong(menuId)) > 0) {
            return AjaxResult.warn("菜单已分配,不允许删除");
        }
        return toAjax(menuService.deleteMenuById(Convert.toLong(menuId)));
    }
}
