package com.xunhong.erp.turbo.rbac.app.assembler;

import cn.hutool.core.collection.CollUtil;
import com.xunhong.erp.turbo.api.rbac.dto.vo.RoleVO;
import com.xunhong.erp.turbo.rbac.domain.entity.Role;
import com.xunhong.erp.turbo.rbac.domain.entity.RoleMenu;
import com.xunhong.erp.turbo.rbac.domain.entity.RolePermission;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueCheckStrategy;

import java.util.ArrayList;
import java.util.List;

@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
public interface RoleAssembler {

	@Mapping(target = "permissionId", expression = "java(toPermissionId(role))")
	@Mapping(target = "menuId", expression = "java(toMenuId(role))")
	RoleVO toRoleVO(Role role);

	@SuppressWarnings("unused")
	default List<Long> toPermissionId(Role role) {
		if (CollUtil.isEmpty(role.getRolePermissionList())) {
			return new ArrayList<>();
		}
		return role.getRolePermissionList().stream().map(RolePermission::getPermissionId).toList();
	}

	@SuppressWarnings("unused")
	default List<Long> toMenuId(Role role) {
		if (CollUtil.isEmpty(role.getRoleMenuList())) {
			return new ArrayList<>();
		}
		return role.getRoleMenuList().stream().map(RoleMenu::getMenuId).toList();
	}
}
