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 @@ -123,15 +123,15 @@ public void marketSell(Long marketId, OrderRequestDto orderRequestDto) {
throw new CustomException(UserMarketHoldingErrorCode.INSUFFICIENT_QUANTITY);
}

// 2. 보유 수량 감소
userMarketHolding.subtractQuantity(quantity);

// 3. 사용자 자산 변화
// 2. 사용자 자산 변화
BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(quantity));
user.addCashBalance(totalPrice);
user.subtractInvestmentBalance(totalPrice);
if (userMarketHolding.getQuantity() <= 0) userMarketHoldingRepository.delete(userMarketHolding);

// 3. 보유 수량 감소
userMarketHolding.subtractQuantity(quantity);

// 4. 체결 내역 DB에 저장
Transaction transaction =
Transaction.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public UserGroupDetailResponseDto getUserGroupDetails(Long userId, Long groupId)
List<UserGroupRankingDto> rankingAndPnLInGroup =
userGroupRankingService.getRankingAndPnLInGroup(groupId);
Map<String, BigDecimal> pnLAndPnLRate =
userGroupRankingService.getMyRankingAndPnLInGroup(userId, group);
userGroupRankingService.getMyRankingAndPnLInGroup(userGroup);

return UserGroupDetailResponseDto.builder()
.groupName(group.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.hackathon.tomolow.domain.userGroup.repository.UserGroupRepository;
import com.hackathon.tomolow.domain.userGroupStockHolding.dto.UserGroupMarketHoldingPnLDto;
import com.hackathon.tomolow.domain.userGroupStockHolding.service.UserGroupMarketHoldingService;
import com.hackathon.tomolow.domain.userGroupTransaction.entity.UserGroupTransaction;
import com.hackathon.tomolow.domain.userGroupTransaction.repository.UserGroupTransactionRepository;
import com.hackathon.tomolow.domain.userGroupTransaction.service.GroupOrderInfoService;
import com.hackathon.tomolow.global.exception.CustomException;

import lombok.RequiredArgsConstructor;
Expand All @@ -30,6 +33,8 @@ public class UserGroupRankingService {
private final UserGroupRepository userGroupRepository;
private final GroupRepository groupRepository;
private final UserGroupMarketHoldingService userGroupMarketHoldingService;
private final GroupOrderInfoService groupOrderInfoService;
private final UserGroupTransactionRepository userGroupTransactionRepository;

/** 그룹 내 랭킹과 손익금액 조회 */
public List<UserGroupRankingDto> getRankingAndPnLInGroup(Long groupId) {
Expand Down Expand Up @@ -145,21 +150,48 @@ public int getMyRanking(Long userId, Long groupId) {
return 0;
}

public Map<String, BigDecimal> getMyRankingAndPnLInGroup(Long userId, Group group) {
List<UserGroupRankingDto> rankingAndPnLInGroup = getRankingAndPnLInGroup(group.getId());
public Map<String, BigDecimal> getMyRankingAndPnLInGroup(UserGroup userGroup) {
List<UserGroupRankingDto> rankingAndPnLInGroup =
getRankingAndPnLInGroup(userGroup.getGroup().getId());
Map<String, BigDecimal> result = new HashMap<>();
BigDecimal pnL = null;
BigDecimal pnLRate = null;
BigDecimal pnLRate = BigDecimal.ZERO;

// PnL 조회
for (UserGroupRankingDto userRanking : rankingAndPnLInGroup) {
if (userRanking.getUserPnl().getUserId().equals(userId)) {
if (userRanking.getUserPnl().getUserId().equals(userGroup.getUser().getId())) {
pnL = userRanking.getUserPnl().getPnL().setScale(0, RoundingMode.DOWN);
pnLRate =
pnL.divide(group.getSeedMoney(), 6, RoundingMode.HALF_UP)
.multiply(new BigDecimal(100))
.setScale(1, RoundingMode.HALF_UP);
;
}
}

if (pnL == null) {
result.put("pnL", BigDecimal.ZERO);
result.put("pnLRate", BigDecimal.ZERO);
return result;
}

// 2. 유저의 전체 매수매도 트랜잭션 불러오기
List<UserGroupTransaction> allByUserGroupId =
userGroupTransactionRepository.findAllByUserGroup_Id(userGroup.getId());

BigDecimal totalBuy = BigDecimal.ZERO;
BigDecimal totalSell = BigDecimal.ZERO;

for (UserGroupTransaction tx : allByUserGroupId) {
BigDecimal amount = tx.getPrice().multiply(BigDecimal.valueOf(tx.getQuantity()));

if (tx.isBuy()) totalBuy = totalBuy.add(amount);
else totalSell = totalSell.add(amount);
}

if (totalBuy.compareTo(BigDecimal.ZERO) == 0) {
pnLRate = BigDecimal.ZERO;
} else {
pnLRate =
pnL.divide(totalBuy, 6, RoundingMode.HALF_UP)
.multiply(BigDecimal.valueOf(100))
.setScale(1, RoundingMode.HALF_UP);
}
result.put("pnL", pnL);
result.put("pnLRate", pnLRate);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ List<UserGroupTransaction> findAllByUserGroupAndCreatedAtBetweenOrderByCreatedAt

// 해당 UserGroup의 첫 거래 하나 (createdAt 오름차순)
Optional<UserGroupTransaction> findFirstByUserGroupOrderByCreatedAtAsc(UserGroup userGroup);

List<UserGroupTransaction> findAllByUserGroupId(Long userGroupId);

List<UserGroupTransaction> findAllByUserGroup_Id(Long userGroupId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ public void marketSell(

// 4. 사용자 자산 변화
BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(quantity));
BigDecimal investedAmount =
userGroupMarketHolding.getAvgPrice().multiply(BigDecimal.valueOf(quantity));
userGroup.addCash(totalPrice);
userGroup.subtractInvestment(totalPrice);
userGroup.subtractInvestment(investedAmount);
if (userGroupMarketHolding.getQuantity() <= 0)
userGroupMarketHoldingRepository.delete(userGroupMarketHolding);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ public void executeSell(
return;
}

// 2. 보유 수량 감소
holding.subtractQuantity(quantity);

// 3. 사용자 자산 변화
// 2. 사용자 자산 변화
BigDecimal totalPrice = tradePrice.multiply(BigDecimal.valueOf(quantity)); // 매도금액
userGroup.addCash(totalPrice);

BigDecimal costBasis = holding.getAvgPrice().multiply(BigDecimal.valueOf(quantity));
userGroup.subtractInvestment(costBasis);

// 3. 보유 수량 감소
holding.subtractQuantity(quantity);

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