package org.xyzh.api.system.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.xyzh.common.vo.BaseVO;

import java.util.List;
import org.xyzh.common.dto.sys.TbSysModuleDTO;
import org.xyzh.common.dto.sys.TbSysPermissionDTO;
import org.xyzh.common.dto.sys.TbSysViewDTO;

/**
 * @description    权限视图对象
 * @filename    PermissionVO.java
 * @author    yslg
 * @copyright yslg
 * @since 2025-11-04
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "权限视图对象")
public class PermissionVO extends BaseVO {
    // TbSysDeptDTO对应字段
    @Schema(description = "部门ID")
    private String deptId;
    
    @Schema(description = "部门名称")
    private String deptName;
    
    @Schema(description = "父级部门ID")
    private String deptParentId;
    
    @Schema(description = "部门描述")
    private String deptDescription;

    // TbSysRoleDTO对应字段
    @Schema(description = "角色ID")
    private String roleId;

    @Schema(description = "角色名称")
    private String roleName;

    @Schema(description = "角色描述")
    private String roleDescription;

    @Schema(description = "角色作用域")
    private String roleScope;

    @Schema(description = "所属部门ID")
    private String roleOwnerDeptId;

    @Schema(description = "角色状态")
    private Boolean roleStatus;

    // TbSysModuleDTO对应字段
    @Schema(description = "模块ID")
    private String moduleId;

    @Schema(description = "模块名称")
    private String moduleName;

    @Schema(description = "模块描述")
    private String moduleDescription;

    // TbSysPermissionDTO对应字段
    @Schema(description = "权限ID")
    private String permissionId;

    @Schema(description = "权限名称")
    private String permissionName;

    @Schema(description = "权限代码")
    private String permissionCode;

    @Schema(description = "权限描述")
    private String permissionDescription;

    @Schema(description = "权限状态")
    private Boolean permissionStatus;

    // TbSysViewDTO对应字段
    @Schema(description = "视图ID")
    private String viewId;

    @Schema(description = "视图名称")
    private String viewName;

    @Schema(description = "父视图ID")
    private String viewParentId;

    @Schema(description = "URL")
    private String viewUrl;

    @Schema(description = "组件")
    private String viewComponent;

    @Schema(description = "图标")
    private String viewIcon;

    @Schema(description = "类型")
    private Integer viewType;

    @Schema(description = "视图类型")
    private String viewViewType;

    @Schema(description = "iframe URL")
    private String viewIframeUrl;

    @Schema(description = "所属服务：platform=平台应用 bidding=招标应用 workcase=客服应用")
    private String viewService;

    @Schema(description = "布局")
    private String viewLayout;

    @Schema(description = "排序")
    private Integer viewOrderNum;
    
    @Schema(description = "视图描述")
    private String viewDescription;

    // 角色权限数组
    @Schema(description = "用户视图权限列表")
    private List<String> permissionIdList;

    public static TbSysModuleDTO toModuleDTO(PermissionVO vo) {
        if (vo == null) {
            return null;
        }
        TbSysModuleDTO dto = new TbSysModuleDTO();
        dto.setModuleId(vo.getModuleId());
        dto.setName(vo.getModuleName());
        dto.setDescription(vo.getModuleDescription());
        dto.setOptsn(vo.getOptsn());
        dto.setCreator(vo.getCreator());
        dto.setUpdater(vo.getUpdater());
        dto.setDeptPath(vo.getDeptPath());
        dto.setRemark(vo.getRemark());
        dto.setCreateTime(vo.getCreateTime());
        dto.setUpdateTime(vo.getUpdateTime());
        dto.setDeleteTime(vo.getDeleteTime());
        dto.setDeleted(vo.getDeleted());
        return dto;
    }

    public static TbSysPermissionDTO toPermissionDTO(PermissionVO vo) {
        if (vo == null) {
            return null;
        }
        TbSysPermissionDTO dto = new TbSysPermissionDTO();
        dto.setPermissionId(vo.getPermissionId());
        dto.setModuleId(vo.getModuleId());
        dto.setCode(vo.getPermissionCode());
        dto.setName(vo.getPermissionName());
        dto.setDescription(vo.getPermissionDescription());
        dto.setOptsn(vo.getOptsn());
        dto.setCreator(vo.getCreator());
        dto.setUpdater(vo.getUpdater());
        dto.setDeptPath(vo.getDeptPath());
        dto.setRemark(vo.getRemark());
        dto.setCreateTime(vo.getCreateTime());
        dto.setUpdateTime(vo.getUpdateTime());
        dto.setDeleteTime(vo.getDeleteTime());
        dto.setDeleted(vo.getDeleted());
        return dto;
    }

    public static TbSysViewDTO toViewDTO(PermissionVO vo) {
        if (vo == null) {
            return null;
        }
        TbSysViewDTO dto = new TbSysViewDTO();
        dto.setViewId(vo.getViewId());
        dto.setName(vo.getViewName());
        dto.setParentId(vo.getViewParentId());
        dto.setUrl(vo.getViewUrl());
        dto.setComponent(vo.getViewComponent());
        dto.setIcon(vo.getViewIcon());
        dto.setType(vo.getViewType());
        dto.setViewType(vo.getViewViewType());
        dto.setIframeUrl(vo.getViewIframeUrl());
        dto.setService(vo.getViewService());
        dto.setLayout(vo.getViewLayout());
        dto.setOrderNum(vo.getViewOrderNum());
        dto.setDescription(vo.getViewDescription());
        dto.setOptsn(vo.getOptsn());
        dto.setCreator(vo.getCreator());
        dto.setUpdater(vo.getUpdater());
        dto.setDeptPath(vo.getDeptPath());
        dto.setRemark(vo.getRemark());
        dto.setCreateTime(vo.getCreateTime());
        dto.setUpdateTime(vo.getUpdateTime());
        dto.setDeleteTime(vo.getDeleteTime());
        dto.setDeleted(vo.getDeleted());
        return dto;
    }

    public static PermissionVO fromViewDTO(TbSysViewDTO dto) {
        if (dto == null) {
            return null;
        }
        PermissionVO vo = new PermissionVO();
        vo.setViewId(dto.getViewId());
        vo.setViewName(dto.getName());
        vo.setViewParentId(dto.getParentId());
        vo.setViewUrl(dto.getUrl());
        vo.setViewComponent(dto.getComponent());
        vo.setViewIcon(dto.getIcon());
        vo.setViewType(dto.getType());
        vo.setViewViewType(dto.getViewType());
        vo.setViewIframeUrl(dto.getIframeUrl());
        vo.setViewService(dto.getService());
        vo.setViewLayout(dto.getLayout());
        vo.setViewOrderNum(dto.getOrderNum());
        vo.setViewDescription(dto.getDescription());
        vo.setOptsn(dto.getOptsn());
        vo.setCreator(dto.getCreator());
        vo.setUpdater(dto.getUpdater());
        vo.setDeptPath(dto.getDeptPath());
        vo.setRemark(dto.getRemark());
        vo.setCreateTime(dto.getCreateTime());
        vo.setUpdateTime(dto.getUpdateTime());
        vo.setDeleteTime(dto.getDeleteTime());
        vo.setDeleted(dto.getDeleted());
        return vo;
    }

    public static List<PermissionVO> fromViewDTOList(List<TbSysViewDTO> dtoList) {
        if (dtoList == null || dtoList.isEmpty()) {
            return java.util.Collections.emptyList();
        }
        return dtoList.stream()
            .map(PermissionVO::fromViewDTO)
            .toList();
    }
}
