Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void marketSell(Long marketId, OrderRequestDto orderRequestDto) {
BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(quantity));
user.addCashBalance(totalPrice);
user.subtractInvestmentBalance(totalPrice);
if (userMarketHolding.getQuantity() <= 0) userMarketHoldingRepository.delete(userMarketHolding);

// 4. 체결 내역 DB에 저장
Transaction transaction =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ private void executeBuy(String marketId, String orderId, BigDecimal tradePrice,
log.info("매수 체결 - orderId : " + orderId);
}

private void executeSell(String marketId, String orderId, BigDecimal tradePrice, int quantity) {
@Transactional
public void executeSell(String marketId, String orderId, BigDecimal tradePrice, int quantity) {
Market market =
marketRepository
.findById(Long.valueOf(marketId))
Expand Down Expand Up @@ -190,6 +191,7 @@ private void executeSell(String marketId, String orderId, BigDecimal tradePrice,

// 4. 잔여 수량 갱신
orderRedisService.updateOrRemove(orderId, marketId, TradeType.SELL, quantity);
if (holding.getQuantity() <= 0) userMarketHoldingRepository.delete(holding);

// 5. 체결 내역 DB에 저장
Transaction transaction =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hackathon.tomolow.domain.ticker.service.PriceQueryService;
import com.hackathon.tomolow.domain.userGroup.entity.UserGroup;
Expand All @@ -27,19 +28,24 @@ public class UserGroupMarketHoldingService {
private final GroupOrderInfoService groupOrderInfoService;

/** 마켓별로 사용자의 PnL 계산 */
@Transactional
public UserGroupMarketHoldingPnLDto getPnLByUserGroupAndMarket(UserGroup userGroup) {

// 1. 사용자가 해당 그룹 내에서 소유하고 있는 마켓 조회
List<UserGroupMarketHolding> userGroupMarketHoldings =
userGroupMarketHoldingRepository.findByUserGroup_Id(userGroup.getId());

if (userGroupMarketHoldings.isEmpty()) {
if (userGroupMarketHoldings.isEmpty())
return UserGroupMarketHoldingPnLDto.builder().pnLDtos(List.of()).build();
}

// 2. 마켓별로 손익금액과 손익률 계산
List<UserGroupMarketHoldingPnLDto.SinglePnLDto> pnLDtos = new ArrayList<>();
for (UserGroupMarketHolding holding : userGroupMarketHoldings) {
if (holding.getQuantity() <= 0) {
userGroupMarketHoldingRepository.delete(holding);
continue;
}

// 2-1. 해당 마켓의 평균 매수가 / 실시간 가격 조회
BigDecimal avgPrice = holding.getAvgPrice();
BigDecimal lastTradePrice = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public void marketSell(
BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(quantity));
userGroup.addCash(totalPrice);
userGroup.subtractInvestment(totalPrice);
if (userGroupMarketHolding.getQuantity() <= 0)
userGroupMarketHoldingRepository.delete(userGroupMarketHolding);

// 5. 체결 내역 DB에 저장
UserGroupTransaction transaction =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ private void executeBuy(
log.info("그룹 매수 체결 - orderId : " + orderId);
}

private void executeSell(
@Transactional
public void executeSell(
String marketId, String groupId, String orderId, BigDecimal tradePrice, int quantity) {
Market market =
marketRepository
Expand Down Expand Up @@ -167,6 +168,7 @@ private void executeSell(

// 4. 잔여 수량 갱신
groupOrderRedisService.updateOrRemove(orderId, marketId, TradeType.SELL, quantity, groupId);
if (holding.getQuantity() <= 0) userGroupMarketHoldingRepository.delete(holding);

// 5. 체결 내역 DB에 저장
UserGroupTransaction transaction =
Expand Down