Skip to content

Commit

Permalink
Update: 상장폐지 기능 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobhboy committed Mar 27, 2024
1 parent e08b77a commit 0776de7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.time.LocalDateTime;

import org.springframework.data.annotation.CreatedDate;

import com.project.bumawiki.domain.coin.domain.type.TradeStatus;

import jakarta.persistence.Entity;
Expand Down Expand Up @@ -35,13 +37,10 @@ public Trade(Long coinPrice, Long coinCount, Long usedMoney, TradeStatus tradeSt
this.usedMoney = usedMoney;
this.tradeStatus = tradeStatus;
this.coinAccountId = coinAccountId;
this.tradedTime = LocalDateTime.now();
}

public void updateTradeStatus(TradeStatus tradeStatus) {
this.tradeStatus = tradeStatus;
}

public void updateTradedTimeNow() {
this.tradedTime = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ default CoinAccount getById(Long id) {
.orElseThrow(CoinAccountNotFoundException::new);
}

@Query(value = "select c from CoinAccount c where c.coin > 0")
List<CoinAccount> findAllByCoinGreaterThan0();

@Query(value = "select c from CoinAccount c order by c.money + c.coin * :price desc, c.gotMoney asc")
List<CoinAccount> getRanking(Pageable pageable, Long price);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public enum TradeStatus {
SELLING,
SOLD,
CANCELLED,
DELISTING,
NONE
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ private void sellLater(Trade trade) {
private void sellNow(Trade trade, CoinAccount coinAccount) {
coinAccount.sellCoin(trade.getCoinPrice(), trade.getCoinCount());
trade.updateTradeStatus(TradeStatus.SOLD);
trade.updateTradedTimeNow();
}

private void buyLater(Trade trade) {
Expand All @@ -106,7 +105,6 @@ private void buyLater(Trade trade) {
private void buyNow(Trade trade, CoinAccount coinAccount) {
coinAccount.buyCoin(trade.getCoinPrice(), trade.getCoinCount());
trade.updateTradeStatus(TradeStatus.BOUGHT);
trade.updateTradedTimeNow();
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,42 @@ void changePrice() {

Price recentPrice = priceRepository.getRecentPrice();
Long max = recentPrice.getPrice() + CHANGE_MONEY_RANGE;
Long min = Math.max(recentPrice.getPrice() - CHANGE_MONEY_RANGE, 20000L);
Long min = Math.max(recentPrice.getPrice() - CHANGE_MONEY_RANGE, 0L);

SecureRandom random = getRandomInstance();
Long randomPrice = random.nextLong(max - min + 1L) + min;
Price newPrice = new Price(randomPrice - randomPrice % 100);
// Long randomPrice = random.nextLong(max - min + 1L) + min;
Long randomPrice = 0L;
Price newPrice;
if (randomPrice == 0) {
restartCoin();
newPrice = new Price(100000L);
} else {
newPrice = new Price(randomPrice - randomPrice % 100);
}

priceRepository.save(newPrice);

processBuyingTrade(newPrice);
processSellingTrade(newPrice);
}

private void restartCoin() {
List<CoinAccount> coinAccounts = coinAccountRepository.findAllByCoinGreaterThan0();

for (CoinAccount coinAccount : coinAccounts) {
Trade trade = new Trade(
0L,
coinAccount.getCoin(),
0L,
TradeStatus.DELISTING,
coinAccount.getId()
);
coinAccount.sellCoin(0L, coinAccount.getCoin());

tradeRepository.save(trade);
coinAccountRepository.save(coinAccount);
}
}

private void processSellingTrade(Price newPrice) {
List<Trade> sellingTrades = tradeRepository.findAllByTradeStatus(TradeStatus.SELLING);

Expand Down

0 comments on commit 0776de7

Please sign in to comment.