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

import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.infra.database.cache.service.user.UserCpValueCacheService;
import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship;
import com.red.circle.other.infra.database.rds.service.user.user.CpApplyService;
import com.red.circle.other.infra.database.rds.service.user.user.CpRelationshipService;
import com.red.circle.other.infra.database.rds.service.user.user.CpValueService;
import com.red.circle.other.app.inner.convertor.user.UserCpInnerConvertor;
import com.red.circle.other.inner.model.cmd.user.CpApplyQryCmd;
import com.red.circle.other.inner.model.dto.user.CpRelationshipDTO;
import com.red.circle.other.inner.model.dto.user.UserCpApplyDTO;
import com.red.circle.other.app.inner.service.user.user.UserCpClientService;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.red.circle.tool.core.collection.CollectionUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 用户svip服务.
 *
 * @author pengliang on 2023/6/6
 */
@Service
@RequiredArgsConstructor
public class UserCpClientServiceImpl implements UserCpClientService {

  private final CpApplyService cpApplyService;
  private final CpValueService cpValueService;
  private final UserCpInnerConvertor userCpInnerConvertor;
  private final CpRelationshipService cpRelationshipService;
  private final UserCpValueCacheService userCpValueCacheService;

  @Override
  public BigDecimal getCpVal(Long id) {
    return cpValueService.getCpVal(id);
  }

  @Override
  public Long getCpValByUserId(Long userId) {
    return cpRelationshipService.getCpValByUserId(userId);
  }

  @Override
  public CpRelationshipDTO getByUserId(Long userId) {
    List<CpRelationship> cpRelationships = cpRelationshipService.getByUserId(userId);
    return userCpInnerConvertor.toCpRelationshipDTO(cpRelationships.isEmpty() ? null : cpRelationships.get(0));
  }

  @Override
  public void incrValue(Long id, BigDecimal val) {
    cpValueService.incrValue(id, val);
  }

  @Override
  public void updateRelationshipAccountByUserId(Long userId, BigDecimal account) {
    cpRelationshipService.updateAccountByUserId(userId, account);
  }

  @Override
  public void incrTotal(Long userId1, Long userId2, Long quantity) {
    userCpValueCacheService.incr(userId1, userId2, quantity);
  }

  @Override
  public PageResult<UserCpApplyDTO> pageCpApply(CpApplyQryCmd query) {
    return cpApplyService.pageCpApply(query).convert(userCpInnerConvertor::toUserCpApplyDTO);
  }

  @Override
  public Map<Long, Long> getCpUserIdByUserIds(List<Long> userIds) {
    if (CollectionUtils.isEmpty(userIds)) {
      return new HashMap<>();
    }
    List<CpRelationship> list = cpRelationshipService.query()
            .select(CpRelationship::getUserId, CpRelationship::getCpUserId)
            .in(CpRelationship::getUserId, userIds)
            .list();
    if (CollectionUtils.isEmpty(list)) {
      return new HashMap<>();
    }
    return list.stream().collect(
        HashMap::new,
        (m, v) -> m.put(v.getUserId(), v.getCpUserId()),
        HashMap::putAll);
  }

}
