package com.red.circle.console.app.command.admin.query;

import com.red.circle.console.app.convertor.MenuAppConvertor;
import com.red.circle.console.app.dto.clienobject.admin.MenuCO;
import com.red.circle.console.app.dto.cmd.admin.MenuIdCmd;
import com.red.circle.console.infra.database.rds.entity.admin.Menu;
import com.red.circle.console.infra.database.rds.entity.admin.MenuResource;
import com.red.circle.console.infra.database.rds.service.admin.MenuResourceService;
import com.red.circle.console.infra.database.rds.service.admin.MenuService;
import com.red.circle.console.inner.error.ConsoleErrorCode;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.tool.core.parse.DataTypeUtils;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取菜单详情.
 *
 * @author pengliang on 2023/5/12
 */
@Component
@RequiredArgsConstructor
public class MenuQryExe {

  private final MenuService menuService;
  private final MenuResourceService menuResourceService;
  private final MenuAppConvertor menuAppConvertor;

  public MenuCO execute(MenuIdCmd cmd) {
    Menu menu = menuService.getById(cmd.getId());
    ResponseAssert.notNull(ConsoleErrorCode.MENU_NOT_FOUND, menu);
    MenuCO menuCO = menuAppConvertor.toMenuCO(menu);
    List<String> resourceIds = menuResourceService.query()
        .select(MenuResource::getResourceId)
        .eq(MenuResource::getMenuId, cmd.getId())
        .listObjs(DataTypeUtils::toString);
    menuCO.setResourceIds(resourceIds);
    return menuCO;
  }

}
