package com.red.circle.other.infra.database.rds.service.game.impl;

import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.other.infra.database.rds.dao.game.BaishunProviderConfigDAO;
import com.red.circle.other.infra.database.rds.entity.game.BaishunProviderConfig;
import com.red.circle.other.infra.database.rds.service.game.BaishunProviderConfigService;
import com.red.circle.tool.core.text.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 百顺三方配置服务实现.
 */
@Service
@RequiredArgsConstructor
public class BaishunProviderConfigServiceImpl
    extends BaseServiceImpl<BaishunProviderConfigDAO, BaishunProviderConfig>
    implements BaishunProviderConfigService {

  @Override
  public BaishunProviderConfig getBySysOrigin(String sysOrigin) {
    if (StringUtils.isBlank(sysOrigin)) {
      return null;
    }
    return query()
        .eq(BaishunProviderConfig::getSysOrigin, sysOrigin)
        .eq(BaishunProviderConfig::getActive, true)
        .orderByDesc(BaishunProviderConfig::getUpdateTime)
        .last(PageConstant.LIMIT_ONE)
        .getOne();
  }

  @Override
  public BaishunProviderConfig getByAppId(Long appId) {
    if (appId == null || appId <= 0) {
      return null;
    }
    return query()
        .eq(BaishunProviderConfig::getAppId, appId)
        .orderByDesc(BaishunProviderConfig::getActive)
        .orderByDesc(BaishunProviderConfig::getUpdateTime)
        .last(PageConstant.LIMIT_ONE)
        .getOne();
  }

  @Override
  public BaishunProviderConfig getByApp(Long appId, String appChannel) {
    if (appId == null || appId <= 0) {
      return null;
    }
    if (StringUtils.isBlank(appChannel)) {
      return getByAppId(appId);
    }
    return query()
        .eq(BaishunProviderConfig::getAppId, appId)
        .eq(BaishunProviderConfig::getAppChannel, appChannel)
        .orderByDesc(BaishunProviderConfig::getActive)
        .orderByDesc(BaishunProviderConfig::getUpdateTime)
        .last(PageConstant.LIMIT_ONE)
        .getOne();
  }
}
