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

import com.red.circle.console.app.convertor.UserAppConvertor;
import com.red.circle.console.app.dto.clienobject.admin.UserCO;
import com.red.circle.console.app.dto.cmd.admin.UserIdCmd;
import com.red.circle.console.infra.database.rds.entity.admin.User;
import com.red.circle.console.infra.database.rds.service.admin.UserRoleService;
import com.red.circle.console.infra.database.rds.service.admin.UserService;
import com.red.circle.console.inner.error.ConsoleErrorCode;
import com.red.circle.framework.core.asserts.ResponseAssert;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 查询单个用户.
 *
 * @author pengliang on 2023/5/15
 */
@Component
@RequiredArgsConstructor
public class UserSingleQryExe {

  private final UserService userService;
  private final UserRoleService userRoleService;
  private final UserAppConvertor userAppConvertor;

  public UserCO execute(UserIdCmd cmd) {
    User user = userService.getById(cmd.getId());
    ResponseAssert.notNull(ConsoleErrorCode.USER_NOT_FOUND, user);
    UserCO co = userAppConvertor.toUserCO(user);
    List<Integer> roleIds = userRoleService.listRoleId(user.getId());
    co.setRoleIds(roleIds);
    return co;
  }

}
