package com.njzscloud.supervisory.menu.pojo;

import cn.hutool.core.util.StrUtil;
import com.njzscloud.supervisory.menu.contant.MenuCategory;
import com.njzscloud.common.mvc.validator.Constrained;
import com.njzscloud.common.mvc.validator.Constraint;
import com.njzscloud.common.mvc.validator.ValidRule;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotBlank;

/**
 * 添加菜单
 */
@Getter
@Setter
@Constraint
public class MenuAddParam implements Constrained {
    /**
     * 上级 Id; 层级为 1 的节点值为 0
     */
    private Long pid;

    /**
     * 菜单名称
     */
    @NotBlank(message = "菜单名称不能为空")
    private String title;

    /**
     * 图标
     */
    private String icon;

    /**
     * 类型; 字典代码：menu_category
     */
    private MenuCategory menuCategory;

    /**
     * 排序
     */
    private Integer sort;

    /**
     * 路由名称
     */
    private String routeName;

    private String sn;

    @Override
    public ValidRule[] rules() {
        return new ValidRule[]{
                ValidRule.of(() -> pid == null || pid >= 0, "上级 Id 必须大于 0 或不指定"),
                ValidRule.of(() -> menuCategory != null, "类型不能为空"),
                ValidRule.of(() -> {
                    if ((menuCategory == MenuCategory.Catalog || menuCategory == MenuCategory.Btn)) {
                        return StrUtil.isBlank(routeName);
                    } else {
                        return true;
                    }
                }, "菜单目录和按钮不用指定路由名称"),
                ValidRule.of(() -> {
                    if (menuCategory == MenuCategory.Page || menuCategory == MenuCategory.SubPage) {
                        return StrUtil.isNotBlank(routeName);
                    } else {
                        return true;
                    }
                }, "路由名称不能为空"),
        };
    }
}
