package com.red.circle.other.app.service.sys;

import com.red.circle.other.app.command.sys.OauthLoginCmdExe;
import com.red.circle.other.app.command.sys.OauthResetIfAbsentCmdExe;
import com.red.circle.other.app.dto.clientobject.sys.OauthAccessTokenCO;
import com.red.circle.other.app.dto.cmd.sys.OauthCertificateCmd;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author pengliang on 2022/5/10
 */
@Service
@RequiredArgsConstructor
public class OauthServiceImpl implements OauthService {

  private final OauthLoginCmdExe oauthLoginCmdExe;
  private final OauthResetIfAbsentCmdExe oauthResetIfAbsentCmdExe;

  @Override
  public OauthAccessTokenCO login(OauthCertificateCmd cmd) {
    return oauthLoginCmdExe.execute(cmd);
  }

  @Override
  public OauthAccessTokenCO resetIfAbsent(OauthCertificateCmd cmd) {
    return Optional.ofNullable(oauthResetIfAbsentCmdExe.execute(cmd))
        .orElseGet((() -> oauthLoginCmdExe.execute(cmd)));
  }

}
