长数字
This commit is contained in:
parent
29ee759b39
commit
485c15184b
@ -13,11 +13,12 @@ import com.red.circle.wallet.inner.model.dto.BankBalanceDTO;
|
||||
import com.red.circle.wallet.inner.model.dto.UserBankBalanceDTO;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bson.Document;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.FindAndModifyOptions;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
@ -80,12 +81,23 @@ public class UserBankBalanceServiceImpl implements UserBankBalanceService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public BankBalanceDTO getBalance(Long userId) {
|
||||
return BankBalanceDTO.of(mongoPrimaryService.find(UserBankBalance.class,
|
||||
Filters.eq("_id", userId),
|
||||
documents -> CollectionUtils.isEmpty(documents) ? 0L
|
||||
: documents.get(0).getLong("balance")));
|
||||
}
|
||||
public BankBalanceDTO getBalance(Long userId) {
|
||||
return BankBalanceDTO.of(mongoPrimaryService.find(UserBankBalance.class,
|
||||
Filters.eq("_id", userId),
|
||||
documents -> CollectionUtils.isEmpty(documents) ? 0L
|
||||
: getLongValue(documents.get(0), "balance")));
|
||||
}
|
||||
|
||||
static Long getLongValue(Document document, String key) {
|
||||
Object value = document.get(key);
|
||||
if (Objects.isNull(value)) {
|
||||
return 0L;
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return ((Number) value).longValue();
|
||||
}
|
||||
return Long.valueOf(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, BankBalanceDTO> mapBalance(Collection<Long> userIds) {
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
package com.red.circle.wallet.infra.database.mongo.service.bank.impl;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class UserBankBalanceServiceImplTest {
|
||||
|
||||
@Test
|
||||
void getLongValueAcceptsIntegerBalance() {
|
||||
Document document = new Document("balance", 0);
|
||||
|
||||
assertEquals(0L, UserBankBalanceServiceImpl.getLongValue(document, "balance"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLongValueAcceptsLongBalance() {
|
||||
Document document = new Document("balance", 123L);
|
||||
|
||||
assertEquals(123L, UserBankBalanceServiceImpl.getLongValue(document, "balance"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLongValueDefaultsMissingBalanceToZero() {
|
||||
Document document = new Document();
|
||||
|
||||
assertEquals(0L, UserBankBalanceServiceImpl.getLongValue(document, "balance"));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user