package com.red.circle.other.app.inner.service.user.user.impl;

import com.red.circle.other.infra.database.mongo.service.user.count.WeekCpValueCountService;
import com.red.circle.other.infra.database.rds.service.user.user.CpRelationshipService;
import com.red.circle.tool.core.parse.DataTypeUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.other.app.inner.convertor.user.UserCpInnerConvertor;
import com.red.circle.other.inner.model.dto.user.CpRelationshipDTO;
import com.red.circle.other.inner.model.dto.user.WeekCpValueCountDTO;
import com.red.circle.other.app.inner.service.user.user.UserCpWeekClientService;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.springframework.stereotype.Service;

/**
 * 用户每周cp统计.
 *
 * @author pengliang on 2023/6/6
 */
@Service
@RequiredArgsConstructor
public class UserCpWeekClientServiceImpl implements UserCpWeekClientService {

  private final UserCpInnerConvertor userCpInnerConvertor;
  private final CpRelationshipService cpRelationshipService;
  private final WeekCpValueCountService weekCpValueCountService;

  @Override
  public List<WeekCpValueCountDTO> listThisWeekTop(String sysOrigin, Integer size) {
    return userCpInnerConvertor.toListWeekCpValueCountDTO(
        weekCpValueCountService.listThisWeekTop(sysOrigin, size));
  }

  @Override
  public List<WeekCpValueCountDTO> listThisWeekTop(String sysOrigin, String userRegionCode,
      Integer size) {
    return userCpInnerConvertor.toListWeekCpValueCountDTO(
        weekCpValueCountService.listThisWeekTop(sysOrigin, userRegionCode, size));
  }

  @Override
  public List<WeekCpValueCountDTO> listLastWeekTop(String sysOrigin, Integer size) {
    return userCpInnerConvertor.toListWeekCpValueCountDTO(
        weekCpValueCountService.listLastWeekTop(sysOrigin, size));
  }

  @Override
  public List<WeekCpValueCountDTO> listLastWeekTop(String sysOrigin, String userRegionCode,
      Integer size) {
    return userCpInnerConvertor.toListWeekCpValueCountDTO(
        weekCpValueCountService.listLastWeekTop(sysOrigin, userRegionCode, size));
  }

  @Override
  public ImmutablePair<Long, Long> parseUserIdPair(String id) {
    if (StringUtils.isBlanks(id)) {
      return ImmutablePair.nullPair();
    }
    String[] ids = id.split("_");
    if (ids.length < 2) {
      return ImmutablePair.nullPair();
    }
    return ImmutablePair.of(DataTypeUtils.toLong(ids[0]), DataTypeUtils.toLong(ids[1]));
  }

  @Override
  public WeekCpValueCountDTO getUserThisWeekCount(String sysOrigin, Long userIdOne,
      Long userIdTwo) {
    return userCpInnerConvertor.toWeekCpValueCountDTO(
        weekCpValueCountService.getUserThisWeekCount(sysOrigin, userIdOne,
            userIdTwo));
  }

  @Override
  public List<Long> getCpUserId(Long userId) {
    return cpRelationshipService.getCpUserId(userId);
  }

  @Override
  public List<CpRelationshipDTO> listLatestCp(String sysOrigin, Integer size) {
    return userCpInnerConvertor.toListCpRelationshipDTO(
        cpRelationshipService.listLatestCp(sysOrigin, size));
  }

  @Override
  public List<CpRelationshipDTO> listLatestCp(String sysOrigin, Set<Long> excludeUserId,
      Integer size) {
    return userCpInnerConvertor.toListCpRelationshipDTO(
        cpRelationshipService.listLatestCp(sysOrigin, excludeUserId, size));
  }

  @Override
  public void incrThisWeekQuantity(String sysOrigin, String userRegionCode, Long userIdOne,
      Long userIdTwo, Long quantity) {
    weekCpValueCountService.incrThisWeekQuantity(sysOrigin, userRegionCode, userIdOne, userIdTwo,
        quantity);
  }
}
