package shop.alien.entity.store.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import shop.alien.entity.store.LifeSysMenu;

import java.util.List;

/**
 * 菜单权限VO（包含角色拥有状态）
 *
 * @author system
 * @since 2025-01-XX
 */
@Data
@EqualsAndHashCode(callSuper = false)
@JsonInclude
@ApiModel(value = "MenuWithRoleVo对象", description = "菜单权限VO（包含角色拥有状态）")
public class MenuWithRoleVo {

    @ApiModelProperty(value = "菜单ID")
    private Long menuId;

    @ApiModelProperty(value = "菜单名称")
    private String menuName;

    @ApiModelProperty(value = "父菜单ID（0表示根菜单）")
    private Long parentId;

    @ApiModelProperty(value = "菜单类型（M目录 C菜单 F按钮）")
    private String menuType;

    @ApiModelProperty(value = "显示顺序")
    private Integer menuSort;

    @ApiModelProperty(value = "路由地址")
    private String path;

    @ApiModelProperty(value = "组件路径")
    private String component;

    @ApiModelProperty(value = "权限标识（如：sys:user:list）")
    private String perms;

    @ApiModelProperty(value = "菜单图标")
    private String icon;

    @ApiModelProperty(value = "菜单状态（0正常 1停用）")
    private String status;

    @ApiModelProperty(value = "是否显示（0显示 1隐藏）")
    private String visible;

    @ApiModelProperty(value = "是否为外链（0是 1否）")
    private String isFrame;

    @ApiModelProperty(value = "是否缓存（0缓存 1不缓存）")
    private String isCache;

    @ApiModelProperty(value = "删除标志（0存在 2删除）")
    private String delFlag;

    @ApiModelProperty(value = "创建者")
    private String createBy;

    @ApiModelProperty(value = "创建时间")
    private java.util.Date createdTime;

    @ApiModelProperty(value = "更新者")
    private String updateBy;

    @ApiModelProperty(value = "更新时间")
    private java.util.Date updatedTime;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "是否拥有该权限（true:拥有 false:未拥有）")
    private Boolean hasPermission;

    @ApiModelProperty(value = "子菜单列表（用于树形结构）")
    private List<MenuWithRoleVo> children;

    /**
     * 从LifeSysMenu转换为MenuWithRoleVo
     *
     * @param menu 菜单实体
     * @param hasPermission 是否拥有权限
     * @return MenuWithRoleVo
     */
    public static MenuWithRoleVo fromEntity(LifeSysMenu menu, Boolean hasPermission) {
        if (menu == null) {
            return null;
        }
        MenuWithRoleVo vo = new MenuWithRoleVo();
        vo.setMenuId(menu.getMenuId());
        vo.setMenuName(menu.getMenuName());
        vo.setParentId(menu.getParentId());
        vo.setMenuType(menu.getMenuType());
        vo.setMenuSort(menu.getMenuSort());
        vo.setPath(menu.getPath());
        vo.setComponent(menu.getComponent());
        vo.setPerms(menu.getPerms());
        vo.setIcon(menu.getIcon());
        vo.setStatus(menu.getStatus());
        vo.setVisible(menu.getVisible());
        vo.setIsFrame(menu.getIsFrame());
        vo.setIsCache(menu.getIsCache());
        vo.setDelFlag(menu.getDelFlag());
        vo.setCreateBy(menu.getCreateBy());
        vo.setCreatedTime(menu.getCreatedTime());
        vo.setUpdateBy(menu.getUpdateBy());
        vo.setUpdatedTime(menu.getUpdatedTime());
        vo.setRemark(menu.getRemark());
        vo.setHasPermission(hasPermission != null ? hasPermission : false);
        vo.setChildren(null); // 子菜单会在构建树形结构时设置
        return vo;
    }
}

