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

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.red.circle.console.app.convertor.SourceAppConvertor;
import com.red.circle.console.app.dto.clienobject.admin.ResourceCO;
import com.red.circle.console.app.dto.cmd.admin.PageResourceQryCmd;
import com.red.circle.console.infra.database.rds.entity.admin.Resource;
import com.red.circle.console.infra.database.rds.service.admin.ResourceService;
import com.red.circle.framework.dto.PageResult;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取资源分页列表.
 *
 * @author pengliang on 2023/5/12
 */
@Component
@RequiredArgsConstructor
public class ResourcePageQryExe {

  private final ResourceService resourceService;
  private final SourceAppConvertor sourceAppConvertor;

  public PageResult<ResourceCO> execute(PageResourceQryCmd cmd) {
    return resourceService.query()
        .like(StringUtils.isNotEmpty(cmd.getResourceName()), Resource::getResourceName,
            cmd.getResourceName())
        .eq(StringUtils.isNotEmpty(cmd.getMethod()), Resource::getMethod, cmd.getMethod())
        .eq(Objects.nonNull(cmd.getAuthType()), Resource::getAuthType, cmd.getAuthType())
        .page(cmd.getPageQuery())
        .convert(sourceAppConvertor::toResourceCO);
  }

}
