package com.red.circle.wallet.infra.database.rds.service.auth.impl;

import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.wallet.infra.database.rds.dao.PayAuthDAO;
import com.red.circle.wallet.infra.database.rds.entity.auth.PayAuth;
import com.red.circle.wallet.infra.database.rds.service.auth.PayAuthService;

import java.util.Optional;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 用户支付认证 服务实现类.
 * </p>
 *
 * @author zongpubin on 2023-11-30 16:12
 */
@Service
@AllArgsConstructor
public class PayAuthServiceImpl extends BaseServiceImpl<PayAuthDAO, PayAuth> implements
    PayAuthService {

  private final PasswordEncoder passwordEncoder;

  @Override
  public PayAuth getByUserId(Long userId) {
    return query().eq(PayAuth::getUserId, userId).last(PageConstant.LIMIT_ONE).getOne();
  }

  @Override
  public PayAuth getByPassword(String pwd, Long userId) {
    PayAuth payAuth = getByUserId(userId);
    return Optional.ofNullable(payAuth)
        .filter(accountAuth -> passwordEncoder.matches(pwd, accountAuth.getPwd())).orElse(null);
  }

  @Override
  public void deleteByUserId(Long userId) {
    delete().eq(PayAuth::getUserId, userId).execute();
  }

}
